2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2019-10-29 15:26:59 +00:00
|
|
|
"encoding/json"
|
2019-12-16 16:02:40 +00:00
|
|
|
"errors"
|
2019-09-23 16:54:06 +00:00
|
|
|
"fmt"
|
2018-03-30 16:15:06 +00:00
|
|
|
"math/big"
|
2020-02-03 15:05:13 +00:00
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2018-03-30 16:15:06 +00:00
|
|
|
)
|
|
|
|
|
2021-08-21 16:09:44 +00:00
|
|
|
// Stack implementation for the neo-go virtual machine. The stack with its LIFO
|
2022-04-20 18:30:09 +00:00
|
|
|
// semantics is emulated from a simple slice, where the top of the stack corresponds
|
2021-08-21 16:09:44 +00:00
|
|
|
// to the latest element of this slice. Pushes are appends to this slice, pops are
|
|
|
|
// slice resizes.
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Element represents an element on the stack. Technically, it's a wrapper around
|
|
|
|
// stackitem.Item interface to provide some API simplification for the VM.
|
2018-03-30 16:15:06 +00:00
|
|
|
type Element struct {
|
2021-08-21 16:09:44 +00:00
|
|
|
value stackitem.Item
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-09 15:53:58 +00:00
|
|
|
// NewElement returns a new Element object, with its underlying value inferred
|
2018-03-30 16:15:06 +00:00
|
|
|
// to the corresponding type.
|
2023-04-03 10:34:24 +00:00
|
|
|
func NewElement(v any) Element {
|
2021-08-21 16:09:44 +00:00
|
|
|
return Element{stackitem.Make(v)}
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Item returns the Item contained in the element.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) Item() stackitem.Item {
|
2019-11-13 13:55:20 +00:00
|
|
|
return e.value
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Value returns the value of the Item contained in the element.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (e Element) Value() any {
|
2019-10-03 13:12:24 +00:00
|
|
|
return e.value.Value()
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// BigInt attempts to get the underlying value of the element as a big integer.
|
2022-04-20 18:30:09 +00:00
|
|
|
// It will panic if the assertion has failed, which will be caught by the VM.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) BigInt() *big.Int {
|
2020-03-19 15:21:56 +00:00
|
|
|
val, err := e.value.TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2020-03-19 15:21:56 +00:00
|
|
|
return val
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Bool converts the underlying value of the element to a boolean if it's
|
|
|
|
// possible to do so. Otherwise, it will panic.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) Bool() bool {
|
2020-08-21 17:55:20 +00:00
|
|
|
b, err := e.value.TryBool()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes attempts to get the underlying value of the element as a byte array.
|
2022-04-20 18:30:09 +00:00
|
|
|
// It will panic if the assertion has failed, which will be caught by the VM.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) Bytes() []byte {
|
2020-03-11 13:04:28 +00:00
|
|
|
bs, err := e.value.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2019-09-10 11:38:25 +00:00
|
|
|
}
|
2020-03-11 13:04:28 +00:00
|
|
|
return bs
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:12:08 +00:00
|
|
|
// BytesOrNil attempts to get the underlying value of the element as a byte array or nil.
|
2022-04-20 18:30:09 +00:00
|
|
|
// It will panic if the assertion has failed, which will be caught by the VM.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) BytesOrNil() []byte {
|
2020-10-05 13:12:08 +00:00
|
|
|
if _, ok := e.value.(stackitem.Null); ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
bs, err := e.value.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bs
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// String attempts to get a string from the element value.
|
|
|
|
// It is assumed to be used in interops and panics if the string is not a valid UTF-8 byte sequence.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) String() string {
|
2020-07-29 08:18:51 +00:00
|
|
|
s, err := stackitem.ToString(e.value)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-09-06 07:33:43 +00:00
|
|
|
// Array attempts to get the underlying value of the element as an array of
|
2022-04-20 18:30:09 +00:00
|
|
|
// other items. It will panic if the item type is different, which will be caught
|
2019-09-06 07:33:43 +00:00
|
|
|
// by the VM.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) Array() []stackitem.Item {
|
2019-09-06 07:33:43 +00:00
|
|
|
switch t := e.value.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array:
|
|
|
|
return t.Value().([]stackitem.Item)
|
|
|
|
case *stackitem.Struct:
|
|
|
|
return t.Value().([]stackitem.Item)
|
2019-09-06 07:33:43 +00:00
|
|
|
default:
|
|
|
|
panic("element is not an array")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:34:03 +00:00
|
|
|
// Interop attempts to get the underlying value of the element
|
|
|
|
// as an interop item.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (e Element) Interop() *stackitem.Interop {
|
2019-11-13 11:34:03 +00:00
|
|
|
switch t := e.value.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Interop:
|
2019-11-13 11:34:03 +00:00
|
|
|
return t
|
|
|
|
default:
|
|
|
|
panic("element is not an interop")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:09:44 +00:00
|
|
|
// Stack represents a Stack backed by a slice of Elements.
|
2018-03-30 16:15:06 +00:00
|
|
|
type Stack struct {
|
2021-08-21 16:09:44 +00:00
|
|
|
elems []Element
|
|
|
|
name string
|
|
|
|
refs *refCounter
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStack returns a new stack name by the given name.
|
|
|
|
func NewStack(n string) *Stack {
|
2021-08-02 19:38:41 +00:00
|
|
|
return newStack(n, newRefCounter())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStack(n string, refc *refCounter) *Stack {
|
2021-08-11 11:40:41 +00:00
|
|
|
s := new(Stack)
|
2021-08-21 16:09:44 +00:00
|
|
|
s.elems = make([]Element, 0, 16) // Most of uses are expected to fit into 16 elements.
|
2021-08-11 11:40:41 +00:00
|
|
|
initStack(s, n, refc)
|
|
|
|
return s
|
|
|
|
}
|
2022-05-31 15:53:05 +00:00
|
|
|
|
|
|
|
func subStack(old *Stack) *Stack {
|
|
|
|
s := new(Stack)
|
|
|
|
*s = *old
|
|
|
|
s.elems = s.elems[len(s.elems):]
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2021-08-11 11:40:41 +00:00
|
|
|
func initStack(s *Stack, n string, refc *refCounter) {
|
|
|
|
s.name = n
|
|
|
|
s.refs = refc
|
2021-08-21 16:09:44 +00:00
|
|
|
s.Clear()
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Clear clears all elements on the stack and set its length to 0.
|
2018-04-02 15:04:42 +00:00
|
|
|
func (s *Stack) Clear() {
|
2021-08-21 16:09:44 +00:00
|
|
|
if s.elems != nil {
|
2021-08-25 08:20:43 +00:00
|
|
|
for _, el := range s.elems {
|
|
|
|
s.refs.Remove(el.value)
|
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
s.elems = s.elems[:0]
|
|
|
|
}
|
2018-04-02 15:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Len returns the number of elements that are on the stack.
|
2018-03-30 16:15:06 +00:00
|
|
|
func (s *Stack) Len() int {
|
2021-08-21 16:09:44 +00:00
|
|
|
return len(s.elems)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// InsertAt inserts the given item (n) deep on the stack.
|
2021-08-21 16:09:44 +00:00
|
|
|
// Be very careful using it and _always_ check n before invocation
|
|
|
|
// as it will panic otherwise.
|
|
|
|
func (s *Stack) InsertAt(e Element, n int) {
|
|
|
|
l := len(s.elems)
|
|
|
|
s.elems = append(s.elems, e)
|
|
|
|
copy(s.elems[l-n+1:], s.elems[l-n:l])
|
|
|
|
s.elems[l-n] = e
|
|
|
|
s.refs.Add(e.value)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes the given element on the stack.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) Push(e Element) {
|
|
|
|
s.elems = append(s.elems, e)
|
|
|
|
s.refs.Add(e.value)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// PushItem pushes an Item to the stack.
|
2021-08-28 19:35:43 +00:00
|
|
|
func (s *Stack) PushItem(i stackitem.Item) {
|
|
|
|
s.Push(Element{i})
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// PushVal pushes the given value on the stack. It will infer the
|
2020-06-03 12:55:06 +00:00
|
|
|
// underlying Item to its corresponding type.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (s *Stack) PushVal(v any) {
|
2018-03-30 16:15:06 +00:00
|
|
|
s.Push(NewElement(v))
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Pop removes and returns the element on top of the stack. It panics if the stack is
|
2021-08-21 16:09:44 +00:00
|
|
|
// empty.
|
|
|
|
func (s *Stack) Pop() Element {
|
|
|
|
l := len(s.elems)
|
|
|
|
e := s.elems[l-1]
|
|
|
|
s.elems = s.elems[:l-1]
|
|
|
|
s.refs.Remove(e.value)
|
|
|
|
return e
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Top returns the element on top of the stack. Nil if the stack
|
|
|
|
// is empty.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) Top() Element {
|
|
|
|
if len(s.elems) == 0 {
|
|
|
|
return Element{}
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
return s.elems[len(s.elems)-1]
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Back returns the element at the end of the stack. Nil if the stack
|
|
|
|
// is empty.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) Back() Element {
|
|
|
|
if len(s.elems) == 0 {
|
|
|
|
return Element{}
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
return s.elems[0]
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Peek returns the element (n) far in the stack beginning from
|
2022-04-20 18:30:09 +00:00
|
|
|
// the top of the stack. For n == 0 it's, effectively, the same as Top,
|
2021-08-21 16:09:44 +00:00
|
|
|
// but it'll panic if the stack is empty.
|
|
|
|
func (s *Stack) Peek(n int) Element {
|
|
|
|
n = len(s.elems) - n - 1
|
|
|
|
return s.elems[n]
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAt removes the element (n) deep on the stack beginning
|
2022-04-20 18:30:09 +00:00
|
|
|
// from the top of the stack. It panics if called with out of bounds n.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) RemoveAt(n int) Element {
|
|
|
|
l := len(s.elems)
|
|
|
|
e := s.elems[l-1-n]
|
|
|
|
s.elems = append(s.elems[:l-1-n], s.elems[l-n:]...)
|
2020-05-12 13:05:10 +00:00
|
|
|
s.refs.Remove(e.value)
|
2018-03-30 16:15:06 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Dup duplicates and returns the element at position n.
|
2022-04-20 18:30:09 +00:00
|
|
|
// Dup is used for copying elements on the top of its own stack.
|
2022-08-08 10:23:21 +00:00
|
|
|
//
|
|
|
|
// s.Push(s.Peek(0)) // will result in unexpected behavior.
|
|
|
|
// s.Push(s.Dup(0)) // is the correct approach.
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) Dup(n int) Element {
|
2018-03-30 16:15:06 +00:00
|
|
|
e := s.Peek(n)
|
2021-08-21 16:09:44 +00:00
|
|
|
return Element{e.value.Dup()}
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Iter iterates over all elements int the stack, starting from the top
|
2018-03-30 16:15:06 +00:00
|
|
|
// of the stack.
|
2022-08-08 10:23:21 +00:00
|
|
|
//
|
|
|
|
// s.Iter(func(elem *Element) {
|
2018-03-30 16:15:06 +00:00
|
|
|
// // do something with the element.
|
2022-08-08 10:23:21 +00:00
|
|
|
// })
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) Iter(f func(Element)) {
|
|
|
|
for i := len(s.elems) - 1; i >= 0; i-- {
|
|
|
|
f(s.elems[i])
|
2019-11-26 16:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// IterBack iterates over all elements of the stack, starting from the bottom
|
2019-11-26 16:53:30 +00:00
|
|
|
// of the stack.
|
2022-08-08 10:23:21 +00:00
|
|
|
//
|
|
|
|
// s.IterBack(func(elem *Element) {
|
2019-11-26 16:53:30 +00:00
|
|
|
// // do something with the element.
|
2022-08-08 10:23:21 +00:00
|
|
|
// })
|
2021-08-21 16:09:44 +00:00
|
|
|
func (s *Stack) IterBack(f func(Element)) {
|
|
|
|
for i := 0; i < len(s.elems); i++ {
|
|
|
|
f(s.elems[i])
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 16:54:06 +00:00
|
|
|
|
2019-12-16 16:02:40 +00:00
|
|
|
// Swap swaps two elements on the stack without popping and pushing them.
|
|
|
|
func (s *Stack) Swap(n1, n2 int) error {
|
|
|
|
if n1 < 0 || n2 < 0 {
|
|
|
|
return errors.New("negative index")
|
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
l := len(s.elems)
|
|
|
|
if n1 >= l || n2 >= l {
|
2019-12-16 16:02:40 +00:00
|
|
|
return errors.New("too big index")
|
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
s.elems[l-n1-1], s.elems[l-n2-1] = s.elems[l-n2-1], s.elems[l-n1-1]
|
2020-05-06 09:12:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReverseTop reverses top n items of the stack.
|
|
|
|
func (s *Stack) ReverseTop(n int) error {
|
2021-08-21 16:09:44 +00:00
|
|
|
l := len(s.elems)
|
2020-05-06 09:12:29 +00:00
|
|
|
if n < 0 {
|
|
|
|
return errors.New("negative index")
|
2021-08-21 16:09:44 +00:00
|
|
|
} else if n > l {
|
2020-05-06 09:12:29 +00:00
|
|
|
return errors.New("too big index")
|
|
|
|
} else if n <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:09:44 +00:00
|
|
|
for i, j := l-n, l-1; i <= j; i, j = i+1, j-1 {
|
|
|
|
s.elems[i], s.elems[j] = s.elems[j], s.elems[i]
|
2020-05-06 09:12:29 +00:00
|
|
|
}
|
2019-12-16 16:02:40 +00:00
|
|
|
return nil
|
2019-12-16 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Roll brings an item with the given index to the top of the stack moving all
|
|
|
|
// other elements down accordingly. It does all of that without popping and
|
2019-12-16 16:53:21 +00:00
|
|
|
// pushing elements.
|
|
|
|
func (s *Stack) Roll(n int) error {
|
|
|
|
if n < 0 {
|
|
|
|
return errors.New("negative index")
|
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
l := len(s.elems)
|
|
|
|
if n >= l {
|
2019-12-16 16:53:21 +00:00
|
|
|
return errors.New("too big index")
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
e := s.elems[l-1-n]
|
|
|
|
copy(s.elems[l-1-n:], s.elems[l-n:])
|
|
|
|
s.elems[l-1] = e
|
2019-12-16 16:53:21 +00:00
|
|
|
return nil
|
2019-12-16 16:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 11:04:52 +00:00
|
|
|
// PopSigElements pops keys or signatures from the stack as needed for
|
2019-09-23 16:54:06 +00:00
|
|
|
// CHECKMULTISIG.
|
2020-03-18 11:04:52 +00:00
|
|
|
func (s *Stack) PopSigElements() ([][]byte, error) {
|
2019-09-23 16:54:06 +00:00
|
|
|
var num int
|
|
|
|
var elems [][]byte
|
2021-08-21 16:09:44 +00:00
|
|
|
if s.Len() == 0 {
|
2019-09-23 16:54:06 +00:00
|
|
|
return nil, fmt.Errorf("nothing on the stack")
|
|
|
|
}
|
2021-08-21 16:09:44 +00:00
|
|
|
item := s.Pop()
|
2019-09-23 16:54:06 +00:00
|
|
|
switch item.value.(type) {
|
2020-06-03 12:55:06 +00:00
|
|
|
case *stackitem.Array:
|
2019-09-23 16:54:06 +00:00
|
|
|
num = len(item.Array())
|
|
|
|
if num < 1 {
|
|
|
|
return nil, fmt.Errorf("less than one element in the array")
|
|
|
|
}
|
|
|
|
elems = make([][]byte, num)
|
|
|
|
for k, v := range item.Array() {
|
|
|
|
b, ok := v.Value().([]byte)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("bad element %s", v.String())
|
|
|
|
}
|
|
|
|
elems[k] = b
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
num = int(item.BigInt().Int64())
|
|
|
|
if num < 1 || num > s.Len() {
|
|
|
|
return nil, fmt.Errorf("wrong number of elements: %d", num)
|
|
|
|
}
|
|
|
|
elems = make([][]byte, num)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
elems[i] = s.Pop().Bytes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return elems, nil
|
|
|
|
}
|
2019-10-29 15:26:59 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ToArray converts the stack to an array of stackitems with the top item being the last.
|
2020-07-29 09:59:19 +00:00
|
|
|
func (s *Stack) ToArray() []stackitem.Item {
|
2021-08-21 16:09:44 +00:00
|
|
|
items := make([]stackitem.Item, 0, len(s.elems))
|
|
|
|
s.IterBack(func(e Element) {
|
2020-07-29 09:59:19 +00:00
|
|
|
items = append(items, e.Item())
|
2020-03-03 10:05:57 +00:00
|
|
|
})
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the JSON marshalling interface.
|
2019-10-29 15:26:59 +00:00
|
|
|
func (s *Stack) MarshalJSON() ([]byte, error) {
|
2020-07-31 12:57:02 +00:00
|
|
|
items := s.ToArray()
|
|
|
|
arr := make([]json.RawMessage, len(items))
|
|
|
|
for i := range items {
|
|
|
|
data, err := stackitem.ToJSONWithTypes(items[i])
|
|
|
|
if err == nil {
|
|
|
|
arr[i] = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json.Marshal(arr)
|
2019-10-29 15:26:59 +00:00
|
|
|
}
|