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
|
|
|
)
|
|
|
|
|
|
|
|
// Stack implementation for the neo-go virtual machine. The stack implements
|
|
|
|
// a double linked list where its semantics are first in first out.
|
|
|
|
// To simplify the implementation, internally a Stack s is implemented as a
|
|
|
|
// ring, such that &s.top is both the next element of the last element s.Back()
|
|
|
|
// and the previous element of the first element s.Top().
|
|
|
|
//
|
|
|
|
// s.Push(0)
|
|
|
|
// s.Push(1)
|
|
|
|
// s.Push(2)
|
|
|
|
//
|
|
|
|
// [ 2 ] > top
|
|
|
|
// [ 1 ]
|
|
|
|
// [ 0 ] > back
|
|
|
|
//
|
|
|
|
// s.Pop() > 2
|
|
|
|
//
|
|
|
|
// [ 1 ]
|
|
|
|
// [ 0 ]
|
|
|
|
|
|
|
|
// Element represents an element in the double linked list (the stack),
|
2020-06-03 12:55:06 +00:00
|
|
|
// which will hold the underlying stackitem.Item.
|
2018-03-30 16:15:06 +00:00
|
|
|
type Element struct {
|
2020-06-03 12:55:06 +00:00
|
|
|
value stackitem.Item
|
2018-03-30 16:15:06 +00:00
|
|
|
next, prev *Element
|
|
|
|
stack *Stack
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func NewElement(v interface{}) *Element {
|
|
|
|
return &Element{
|
2020-06-03 12:55:06 +00:00
|
|
|
value: stackitem.Make(v),
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next element in the stack.
|
|
|
|
func (e *Element) Next() *Element {
|
|
|
|
if elem := e.next; e.stack != nil && elem != &e.stack.top {
|
|
|
|
return elem
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prev returns the previous element in the stack.
|
|
|
|
func (e *Element) Prev() *Element {
|
|
|
|
if elem := e.prev; e.stack != nil && elem != &e.stack.top {
|
|
|
|
return elem
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Item returns Item contained in the element.
|
|
|
|
func (e *Element) Item() stackitem.Item {
|
2019-11-13 13:55:20 +00:00
|
|
|
return e.value
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
// Value returns value of the Item contained in the element.
|
2019-10-03 13:12:24 +00:00
|
|
|
func (e *Element) Value() interface{} {
|
|
|
|
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.
|
2019-02-13 18:01:10 +00:00
|
|
|
// Will panic if the assertion failed which will be caught by the VM.
|
2018-03-30 16:15:06 +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
|
|
|
}
|
|
|
|
|
2020-04-28 08:24:02 +00:00
|
|
|
// Bool converts an underlying value of the element to a boolean.
|
2019-10-03 13:26:32 +00:00
|
|
|
func (e *Element) Bool() bool {
|
2020-04-28 08:23:58 +00:00
|
|
|
return e.value.Bool()
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes attempts to get the underlying value of the element as a byte array.
|
2019-02-13 18:01:10 +00:00
|
|
|
// Will panic if the assertion failed which will be caught by the VM.
|
2018-03-30 16:15:06 +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-07-29 08:18:51 +00:00
|
|
|
// String attempts to get string from the element value.
|
|
|
|
// It is assumed to be use in interops and panics if string is not a valid UTF-8 byte sequence.
|
|
|
|
func (e *Element) String() string {
|
|
|
|
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
|
|
|
|
// other items. Will panic if the item type is different which will be caught
|
|
|
|
// by the VM.
|
2020-06-03 12:55:06 +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.
|
2020-06-03 12:55:06 +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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
// Stack represents a Stack backed by a double linked list.
|
|
|
|
type Stack struct {
|
|
|
|
top Element
|
|
|
|
name string
|
|
|
|
len int
|
2020-05-12 13:05:10 +00:00
|
|
|
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 {
|
|
|
|
s := &Stack{
|
|
|
|
name: n,
|
|
|
|
}
|
|
|
|
s.top.next = &s.top
|
|
|
|
s.top.prev = &s.top
|
|
|
|
s.len = 0
|
2020-05-12 13:05:10 +00:00
|
|
|
s.refs = newRefCounter()
|
2018-03-30 16:15:06 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
s.top.next = &s.top
|
|
|
|
s.top.prev = &s.top
|
|
|
|
s.len = 0
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return s.len
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// insert inserts the element after element (at) on the stack.
|
2018-03-30 16:15:06 +00:00
|
|
|
func (s *Stack) insert(e, at *Element) *Element {
|
|
|
|
// If we insert an element that is already popped from this stack,
|
|
|
|
// we need to clean it up, there are still pointers referencing to it.
|
|
|
|
if e.stack == s {
|
|
|
|
e = NewElement(e.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := at.next
|
|
|
|
at.next = e
|
|
|
|
e.prev = at
|
|
|
|
e.next = n
|
|
|
|
n.prev = e
|
|
|
|
e.stack = s
|
|
|
|
s.len++
|
2019-10-29 10:26:34 +00:00
|
|
|
|
2020-05-12 13:05:10 +00:00
|
|
|
s.refs.Add(e.value)
|
2019-10-29 10:26:34 +00:00
|
|
|
|
2018-03-30 16:15:06 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// InsertAt inserts the given item (n) deep on the stack.
|
2019-09-05 12:43:59 +00:00
|
|
|
// Be very careful using it and _always_ check both e and n before invocation
|
|
|
|
// as it will silently do wrong things otherwise.
|
2018-03-30 16:15:06 +00:00
|
|
|
func (s *Stack) InsertAt(e *Element, n int) *Element {
|
2019-09-05 13:34:35 +00:00
|
|
|
before := s.Peek(n - 1)
|
2018-03-30 16:15:06 +00:00
|
|
|
if before == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-05 13:34:35 +00:00
|
|
|
return s.insert(e, before)
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes the given element on the stack.
|
|
|
|
func (s *Stack) Push(e *Element) {
|
|
|
|
s.insert(e, &s.top)
|
|
|
|
}
|
|
|
|
|
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.
|
2018-03-30 16:15:06 +00:00
|
|
|
func (s *Stack) PushVal(v interface{}) {
|
|
|
|
s.Push(NewElement(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop removes and returns the element on top of the stack.
|
|
|
|
func (s *Stack) Pop() *Element {
|
|
|
|
return s.Remove(s.Top())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top returns the element on top of the stack. Nil if the stack
|
|
|
|
// is empty.
|
|
|
|
func (s *Stack) Top() *Element {
|
|
|
|
if s.len == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.top.next
|
|
|
|
}
|
|
|
|
|
|
|
|
// Back returns the element at the end of the stack. Nil if the stack
|
|
|
|
// is empty.
|
|
|
|
func (s *Stack) Back() *Element {
|
|
|
|
if s.len == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.top.prev
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peek returns the element (n) far in the stack beginning from
|
|
|
|
// the top of the stack.
|
|
|
|
// n = 0 => will return the element on top of the stack.
|
|
|
|
func (s *Stack) Peek(n int) *Element {
|
|
|
|
i := 0
|
|
|
|
for e := s.Top(); e != nil; e = e.Next() {
|
|
|
|
if n == i {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveAt removes the element (n) deep on the stack beginning
|
|
|
|
// from the top of the stack.
|
|
|
|
func (s *Stack) RemoveAt(n int) *Element {
|
|
|
|
return s.Remove(s.Peek(n))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove removes and returns the given element from the stack.
|
|
|
|
func (s *Stack) Remove(e *Element) *Element {
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
e.prev.next = e.next
|
|
|
|
e.next.prev = e.prev
|
|
|
|
e.next = nil // avoid memory leaks.
|
|
|
|
e.prev = nil // avoid memory leaks.
|
|
|
|
e.stack = nil
|
|
|
|
s.len--
|
2019-10-29 10:26:34 +00:00
|
|
|
|
2020-05-12 13:05:10 +00:00
|
|
|
s.refs.Remove(e.value)
|
2019-10-29 10:26:34 +00:00
|
|
|
|
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.
|
2018-03-30 16:15:06 +00:00
|
|
|
// Dup is used for copying elements on to the top of its own stack.
|
|
|
|
// s.Push(s.Peek(0)) // will result in unexpected behaviour.
|
|
|
|
// s.Push(s.Dup(0)) // is the correct approach.
|
|
|
|
func (s *Stack) Dup(n int) *Element {
|
|
|
|
e := s.Peek(n)
|
|
|
|
if e == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Element{
|
2019-12-17 13:38:42 +00:00
|
|
|
value: e.value.Dup(),
|
2018-03-30 16:15:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Iter iterates over all the elements int the stack, starting from the top
|
2018-03-30 16:15:06 +00:00
|
|
|
// of the stack.
|
|
|
|
// s.Iter(func(elem *Element) {
|
|
|
|
// // do something with the element.
|
|
|
|
// })
|
|
|
|
func (s *Stack) Iter(f func(*Element)) {
|
|
|
|
for e := s.Top(); e != nil; e = e.Next() {
|
|
|
|
f(e)
|
2019-11-26 16:53:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterBack iterates over all the elements of the stack, starting from the bottom
|
|
|
|
// of the stack.
|
|
|
|
// s.IterBack(func(elem *Element) {
|
|
|
|
// // do something with the element.
|
|
|
|
// })
|
|
|
|
func (s *Stack) IterBack(f func(*Element)) {
|
|
|
|
for e := s.Back(); e != nil; e = e.Prev() {
|
|
|
|
f(e)
|
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")
|
|
|
|
}
|
|
|
|
if n1 >= s.len || n2 >= s.len {
|
|
|
|
return errors.New("too big index")
|
|
|
|
}
|
|
|
|
if n1 == n2 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-06 09:12:29 +00:00
|
|
|
s.swap(n1, n2)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Stack) swap(n1, n2 int) {
|
2019-12-16 16:02:40 +00:00
|
|
|
a := s.Peek(n1)
|
|
|
|
b := s.Peek(n2)
|
|
|
|
a.value, b.value = b.value, a.value
|
2020-05-06 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReverseTop reverses top n items of the stack.
|
|
|
|
func (s *Stack) ReverseTop(n int) error {
|
|
|
|
if n < 0 {
|
|
|
|
return errors.New("negative index")
|
|
|
|
} else if n > s.len {
|
|
|
|
return errors.New("too big index")
|
|
|
|
} else if n <= 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, j := 0, n-1; i < j; {
|
|
|
|
s.swap(i, j)
|
|
|
|
i++
|
|
|
|
j--
|
|
|
|
}
|
2019-12-16 16:02:40 +00:00
|
|
|
return nil
|
2019-12-16 16:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Roll brings an item with the given index to the top of the stack, moving all
|
|
|
|
// the other elements down accordingly. It does all of that without popping and
|
|
|
|
// pushing elements.
|
|
|
|
func (s *Stack) Roll(n int) error {
|
|
|
|
if n < 0 {
|
|
|
|
return errors.New("negative index")
|
|
|
|
}
|
|
|
|
if n >= s.len {
|
|
|
|
return errors.New("too big index")
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
top := s.Peek(0)
|
|
|
|
e := s.Peek(n)
|
|
|
|
|
|
|
|
e.prev.next = e.next
|
|
|
|
e.next.prev = e.prev
|
|
|
|
|
|
|
|
top.prev = e
|
|
|
|
e.next = top
|
|
|
|
|
|
|
|
e.prev = &s.top
|
|
|
|
s.top.next = e
|
|
|
|
|
|
|
|
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
|
|
|
|
item := s.Pop()
|
|
|
|
if item == nil {
|
|
|
|
return nil, fmt.Errorf("nothing on the stack")
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2020-07-29 09:59:19 +00:00
|
|
|
// ToArray converts stack to an array of stackitems with top item being the last.
|
|
|
|
func (s *Stack) ToArray() []stackitem.Item {
|
|
|
|
items := make([]stackitem.Item, 0, s.len)
|
2020-03-03 10:05:57 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:26:59 +00:00
|
|
|
// MarshalJSON implements JSON marshalling interface.
|
|
|
|
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
|
|
|
}
|