-
Notifications
You must be signed in to change notification settings - Fork 26
/
stack.go
413 lines (356 loc) · 9.8 KB
/
stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// Copyright (c) 2013-2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package interpreter
import (
"encoding/hex"
"github.com/libsv/go-bt/v2/bscript/interpreter/errs"
)
// asBool gets the boolean value of the byte array.
func asBool(t []byte) bool {
for i := range t {
if t[i] != 0 {
// Negative 0 is also considered false.
if i == len(t)-1 && t[i] == 0x80 {
return false
}
return true
}
}
return false
}
// fromBool converts a boolean into the appropriate byte array.
func fromBool(v bool) []byte {
if v {
return []byte{1}
}
return nil
}
// stack represents a stack of immutable objects to be used with bitcoin
// scripts. Objects may be shared, therefore in usage if a value is to be
// changed it *must* be deep-copied first to avoid changing other values on the
// stack.
type stack struct {
stk [][]byte
maxNumLength int
afterGenesis bool
verifyMinimalData bool
debug Debugger
sh StateHandler
}
func newStack(cfg config, verifyMinimalData bool) stack {
return stack{
maxNumLength: cfg.MaxScriptNumberLength(),
afterGenesis: cfg.AfterGenesis(),
verifyMinimalData: verifyMinimalData,
debug: &nopDebugger{},
sh: &nopStateHandler{},
}
}
// Depth returns the number of items on the stack.
func (s *stack) Depth() int32 {
return int32(len(s.stk))
}
// PushByteArray adds the given back array to the top of the stack.
//
// Stack transformation: [... x1 x2] -> [... x1 x2 data]
func (s *stack) PushByteArray(so []byte) {
defer s.afterStackPush(so)
s.beforeStackPush(so)
s.stk = append(s.stk, so)
}
// PushInt converts the provided scriptNumber to a suitable byte array then pushes
// it onto the top of the stack.
//
// Stack transformation: [... x1 x2] -> [... x1 x2 int]
func (s *stack) PushInt(n *scriptNumber) {
s.PushByteArray(n.Bytes())
}
// PushBool converts the provided boolean to a suitable byte array then pushes
// it onto the top of the stack.
//
// Stack transformation: [... x1 x2] -> [... x1 x2 bool]
func (s *stack) PushBool(val bool) {
s.PushByteArray(fromBool(val))
}
// PopByteArray pops the value off the top of the stack and returns it.
//
// Stack transformation: [... x1 x2 x3] -> [... x1 x2]
func (s *stack) PopByteArray() ([]byte, error) {
s.beforeStackPop()
data, err := s.nipN(0)
if err != nil {
return nil, err
}
s.afterStackPop(data)
return data, nil
}
// PopInt pops the value off the top of the stack, converts it into a scriptNumber,
// and returns it. The act of converting to a script num enforces the
// consensus rules imposed on data interpreted as numbers.
//
// Stack transformation: [... x1 x2 x3] -> [... x1 x2]
func (s *stack) PopInt() (*scriptNumber, error) {
so, err := s.PopByteArray()
if err != nil {
return nil, err
}
return makeScriptNumber(so, s.maxNumLength, s.verifyMinimalData, s.afterGenesis)
}
// PopBool pops the value off the top of the stack, converts it into a bool, and
// returns it.
//
// Stack transformation: [... x1 x2 x3] -> [... x1 x2]
func (s *stack) PopBool() (bool, error) {
so, err := s.PopByteArray()
if err != nil {
return false, err
}
return asBool(so), nil
}
// PeekByteArray returns the Nth item on the stack without removing it.
func (s *stack) PeekByteArray(idx int32) ([]byte, error) {
sz := int32(len(s.stk))
if idx < 0 || idx >= sz {
return nil, errs.NewError(errs.ErrInvalidStackOperation, "index %d is invalid for stack size %d", idx, sz)
}
return s.stk[sz-idx-1], nil
}
// PeekInt returns the Nth item on the stack as a script num without removing
// it. The act of converting to a script num enforces the consensus rules
// imposed on data interpreted as numbers.
func (s *stack) PeekInt(idx int32) (*scriptNumber, error) {
so, err := s.PeekByteArray(idx)
if err != nil {
return nil, err
}
return makeScriptNumber(so, s.maxNumLength, s.verifyMinimalData, s.afterGenesis)
}
// PeekBool returns the Nth item on the stack as a bool without removing it.
func (s *stack) PeekBool(idx int32) (bool, error) {
so, err := s.PeekByteArray(idx)
if err != nil {
return false, err
}
return asBool(so), nil
}
// nipN is an internal function that removes the nth item on the stack and
// returns it.
//
// Stack transformation:
// nipN(0): [... x1 x2 x3] -> [... x1 x2]
// nipN(1): [... x1 x2 x3] -> [... x1 x3]
// nipN(2): [... x1 x2 x3] -> [... x2 x3]
func (s *stack) nipN(idx int32) ([]byte, error) {
sz := int32(len(s.stk))
if idx < 0 || idx > sz-1 {
return nil, errs.NewError(errs.ErrInvalidStackOperation, "index %d is invalid for stack size %d", idx, sz)
}
so := s.stk[sz-idx-1]
if idx == 0 {
s.stk = s.stk[:sz-1]
} else if idx == sz-1 {
s1 := make([][]byte, sz-1)
copy(s1, s.stk[1:])
s.stk = s1
} else {
s1 := s.stk[sz-idx : sz]
s.stk = s.stk[:sz-idx-1]
s.stk = append(s.stk, s1...)
}
return so, nil
}
// NipN removes the Nth object on the stack
//
// Stack transformation:
// NipN(0): [... x1 x2 x3] -> [... x1 x2]
// NipN(1): [... x1 x2 x3] -> [... x1 x3]
// NipN(2): [... x1 x2 x3] -> [... x2 x3]
func (s *stack) NipN(idx int32) error {
_, err := s.nipN(idx)
return err
}
// Tuck copies the item at the top of the stack and inserts it before the 2nd
// to top item.
//
// Stack transformation: [... x1 x2] -> [... x2 x1 x2]
func (s *stack) Tuck() error {
so2, err := s.PopByteArray()
if err != nil {
return err
}
so1, err := s.PopByteArray()
if err != nil {
return err
}
s.PushByteArray(so2) // stack [... x2]
s.PushByteArray(so1) // stack [... x2 x1]
s.PushByteArray(so2) // stack [... x2 x1 x2]
return nil
}
// DropN removes the top N items from the stack.
//
// Stack transformation:
// DropN(1): [... x1 x2] -> [... x1]
// DropN(2): [... x1 x2] -> [...]
func (s *stack) DropN(n int32) error {
if n < 1 {
return errs.NewError(errs.ErrInvalidStackOperation, "attempt to drop %d items from stack", n)
}
for ; n > 0; n-- {
_, err := s.PopByteArray()
if err != nil {
return err
}
}
return nil
}
// DupN duplicates the top N items on the stack.
//
// Stack transformation:
// DupN(1): [... x1 x2] -> [... x1 x2 x2]
// DupN(2): [... x1 x2] -> [... x1 x2 x1 x2]
func (s *stack) DupN(n int32) error {
if n < 1 {
return errs.NewError(errs.ErrInvalidStackOperation, "attempt to dup %d stack items", n)
}
// Iteratively duplicate the value n-1 down the stack n times.
// This leaves an in-order duplicate of the top n items on the stack.
for i := n; i > 0; i-- {
so, err := s.PeekByteArray(n - 1)
if err != nil {
return err
}
s.PushByteArray(so)
}
return nil
}
// RotN rotates the top 3N items on the stack to the left N times.
//
// Stack transformation:
// RotN(1): [... x1 x2 x3] -> [... x2 x3 x1]
// RotN(2): [... x1 x2 x3 x4 x5 x6] -> [... x3 x4 x5 x6 x1 x2]
func (s *stack) RotN(n int32) error {
if n < 1 {
return errs.NewError(errs.ErrInvalidStackOperation, "attempt to rotate %d stack items", n)
}
// Nip the 3n-1th item from the stack to the top n times to rotate
// them up to the head of the stack.
entry := 3*n - 1
for i := n; i > 0; i-- {
so, err := s.nipN(entry)
if err != nil {
return err
}
s.PushByteArray(so)
}
return nil
}
// SwapN swaps the top N items on the stack with those below them.
//
// Stack transformation:
// SwapN(1): [... x1 x2] -> [... x2 x1]
// SwapN(2): [... x1 x2 x3 x4] -> [... x3 x4 x1 x2]
func (s *stack) SwapN(n int32) error {
if n < 1 {
return errs.NewError(errs.ErrInvalidStackOperation, "attempt to swap %d stack items", n)
}
entry := 2*n - 1
for i := n; i > 0; i-- {
// Swap 2n-1th entry to top.
so, err := s.nipN(entry)
if err != nil {
return err
}
s.PushByteArray(so)
}
return nil
}
// OverN copies N items N items back to the top of the stack.
//
// Stack transformation:
// OverN(1): [... x1 x2 x3] -> [... x1 x2 x3 x2]
// OverN(2): [... x1 x2 x3 x4] -> [... x1 x2 x3 x4 x1 x2]
func (s *stack) OverN(n int32) error {
if n < 1 {
return errs.NewError(errs.ErrInvalidStackOperation, "attempt to perform over on %d stack items", n)
}
// Copy 2n-1th entry to top of the stack.
entry := 2*n - 1
for ; n > 0; n-- {
so, err := s.PeekByteArray(entry)
if err != nil {
return err
}
s.PushByteArray(so)
}
return nil
}
// PickN copies the item N items back in the stack to the top.
//
// Stack transformation:
// PickN(0): [x1 x2 x3] -> [x1 x2 x3 x3]
// PickN(1): [x1 x2 x3] -> [x1 x2 x3 x2]
// PickN(2): [x1 x2 x3] -> [x1 x2 x3 x1]
func (s *stack) PickN(n int32) error {
so, err := s.PeekByteArray(n)
if err != nil {
return err
}
s.PushByteArray(so)
return nil
}
// RollN moves the item N items back in the stack to the top.
//
// Stack transformation:
// RollN(0): [x1 x2 x3] -> [x1 x2 x3]
// RollN(1): [x1 x2 x3] -> [x1 x3 x2]
// RollN(2): [x1 x2 x3] -> [x2 x3 x1]
func (s *stack) RollN(n int32) error {
so, err := s.nipN(n)
if err != nil {
return err
}
s.PushByteArray(so)
return nil
}
// String returns the stack in a readable format.
func (s *stack) String() string {
var result string
for _, stack := range s.stk {
if len(stack) == 0 {
result += "00000000 <empty>\n"
}
result += hex.Dump(stack)
}
return result
}
func (s *stack) beforeStackPush(bb []byte) {
s.debug.BeforeStackPush(s.sh.State(), bb)
}
func (s *stack) afterStackPush(bb []byte) {
s.debug.AfterStackPush(s.sh.State(), bb)
}
func (s *stack) beforeStackPop() {
s.debug.BeforeStackPop(s.sh.State())
}
func (s *stack) afterStackPop(bb []byte) {
s.debug.AfterStackPop(s.sh.State(), bb)
}
type boolStack interface {
PushBool(b bool)
PopBool() (bool, error)
PeekBool(int32) (bool, error)
Depth() int32
}
type nopBoolStack struct{}
func (n *nopBoolStack) PushBool(bool) {}
func (n *nopBoolStack) PopBool() (bool, error) {
return false, nil
}
func (n *nopBoolStack) PeekBool(int32) (bool, error) {
return false, nil
}
func (n *nopBoolStack) Depth() int32 {
return 0
}