forked from TrueCloudLab/neoneo-go
Merge pull request #746 from nspcc-dev/fix/equal
vm: implement EQUAL opcode properly Fixes #745, #749.
This commit is contained in:
commit
1b5dd53e07
5 changed files with 157 additions and 27 deletions
|
@ -173,6 +173,16 @@ func (c *Context) Dup() StackItem {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (c *Context) TryBytes() ([]byte, error) {
|
||||||
|
return nil, errors.New("can't convert Context to ByteArray")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (c *Context) Equals(s StackItem) bool {
|
||||||
|
return c == s
|
||||||
|
}
|
||||||
|
|
||||||
// ToContractParameter implements StackItem interface.
|
// ToContractParameter implements StackItem interface.
|
||||||
func (c *Context) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
func (c *Context) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
||||||
return smartcontract.Parameter{
|
return smartcontract.Parameter{
|
||||||
|
|
|
@ -125,21 +125,11 @@ func (e *Element) Bool() bool {
|
||||||
// Bytes attempts to get the underlying value of the element as a byte array.
|
// Bytes attempts to get the underlying value of the element as a byte array.
|
||||||
// Will panic if the assertion failed which will be caught by the VM.
|
// Will panic if the assertion failed which will be caught by the VM.
|
||||||
func (e *Element) Bytes() []byte {
|
func (e *Element) Bytes() []byte {
|
||||||
switch t := e.value.(type) {
|
bs, err := e.value.TryBytes()
|
||||||
case *ByteArrayItem:
|
if err != nil {
|
||||||
return t.value
|
panic(err)
|
||||||
case *BigIntegerItem:
|
|
||||||
return t.Bytes() // neoVM returns in LE
|
|
||||||
case *BoolItem:
|
|
||||||
if t.value {
|
|
||||||
return []byte{1}
|
|
||||||
}
|
|
||||||
// return []byte{0}
|
|
||||||
// FIXME revert when NEO 3.0 https://github.com/nspcc-dev/neo-go/issues/477
|
|
||||||
return []byte{}
|
|
||||||
default:
|
|
||||||
panic("can't convert to []byte: " + t.String())
|
|
||||||
}
|
}
|
||||||
|
return bs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array attempts to get the underlying value of the element as an array of
|
// Array attempts to get the underlying value of the element as an array of
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package vm
|
package vm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -18,6 +20,10 @@ type StackItem interface {
|
||||||
Value() interface{}
|
Value() interface{}
|
||||||
// Dup duplicates current StackItem.
|
// Dup duplicates current StackItem.
|
||||||
Dup() StackItem
|
Dup() StackItem
|
||||||
|
// TryBytes converts StackItem to a byte slice.
|
||||||
|
TryBytes() ([]byte, error)
|
||||||
|
// Equals checks if 2 StackItems are equal.
|
||||||
|
Equals(s StackItem) bool
|
||||||
// ToContractParameter converts StackItem to smartcontract.Parameter
|
// ToContractParameter converts StackItem to smartcontract.Parameter
|
||||||
ToContractParameter(map[StackItem]bool) smartcontract.Parameter
|
ToContractParameter(map[StackItem]bool) smartcontract.Parameter
|
||||||
}
|
}
|
||||||
|
@ -118,6 +124,30 @@ func (i *StructItem) Dup() StackItem {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *StructItem) TryBytes() ([]byte, error) {
|
||||||
|
return nil, errors.New("can't convert Struct to ByteArray")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *StructItem) Equals(s StackItem) bool {
|
||||||
|
if i == s {
|
||||||
|
return true
|
||||||
|
} else if s == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val, ok := s.(*StructItem)
|
||||||
|
if !ok || len(i.value) != len(val.value) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for j := range i.value {
|
||||||
|
if !i.value[j].Equals(val.value[j]) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ToContractParameter implements StackItem interface.
|
// ToContractParameter implements StackItem interface.
|
||||||
func (i *StructItem) ToContractParameter(seen map[StackItem]bool) smartcontract.Parameter {
|
func (i *StructItem) ToContractParameter(seen map[StackItem]bool) smartcontract.Parameter {
|
||||||
var value []smartcontract.Parameter
|
var value []smartcontract.Parameter
|
||||||
|
@ -167,6 +197,26 @@ func (i *BigIntegerItem) Bytes() []byte {
|
||||||
return emit.IntToBytes(i.value)
|
return emit.IntToBytes(i.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *BigIntegerItem) TryBytes() ([]byte, error) {
|
||||||
|
return i.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *BigIntegerItem) Equals(s StackItem) bool {
|
||||||
|
if i == s {
|
||||||
|
return true
|
||||||
|
} else if s == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val, ok := s.(*BigIntegerItem)
|
||||||
|
if ok {
|
||||||
|
return i.value.Cmp(val.value) == 0
|
||||||
|
}
|
||||||
|
bs, err := s.TryBytes()
|
||||||
|
return err == nil && bytes.Equal(i.Bytes(), bs)
|
||||||
|
}
|
||||||
|
|
||||||
// Value implements StackItem interface.
|
// Value implements StackItem interface.
|
||||||
func (i *BigIntegerItem) Value() interface{} {
|
func (i *BigIntegerItem) Value() interface{} {
|
||||||
return i.value
|
return i.value
|
||||||
|
@ -226,6 +276,36 @@ func (i *BoolItem) Dup() StackItem {
|
||||||
return &BoolItem{i.value}
|
return &BoolItem{i.value}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes converts BoolItem to bytes.
|
||||||
|
func (i *BoolItem) Bytes() []byte {
|
||||||
|
if i.value {
|
||||||
|
return []byte{1}
|
||||||
|
}
|
||||||
|
// return []byte{0}
|
||||||
|
// FIXME revert when NEO 3.0 https://github.com/nspcc-dev/neo-go/issues/477
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *BoolItem) TryBytes() ([]byte, error) {
|
||||||
|
return i.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *BoolItem) Equals(s StackItem) bool {
|
||||||
|
if i == s {
|
||||||
|
return true
|
||||||
|
} else if s == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val, ok := s.(*BoolItem)
|
||||||
|
if ok {
|
||||||
|
return i.value == val.value
|
||||||
|
}
|
||||||
|
bs, err := s.TryBytes()
|
||||||
|
return err == nil && bytes.Equal(i.Bytes(), bs)
|
||||||
|
}
|
||||||
|
|
||||||
// ToContractParameter implements StackItem interface.
|
// ToContractParameter implements StackItem interface.
|
||||||
func (i *BoolItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
func (i *BoolItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
||||||
return smartcontract.Parameter{
|
return smartcontract.Parameter{
|
||||||
|
@ -260,6 +340,22 @@ func (i *ByteArrayItem) String() string {
|
||||||
return "ByteArray"
|
return "ByteArray"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *ByteArrayItem) TryBytes() ([]byte, error) {
|
||||||
|
return i.value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *ByteArrayItem) Equals(s StackItem) bool {
|
||||||
|
if i == s {
|
||||||
|
return true
|
||||||
|
} else if s == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
bs, err := s.TryBytes()
|
||||||
|
return err == nil && bytes.Equal(i.value, bs)
|
||||||
|
}
|
||||||
|
|
||||||
// Dup implements StackItem interface.
|
// Dup implements StackItem interface.
|
||||||
func (i *ByteArrayItem) Dup() StackItem {
|
func (i *ByteArrayItem) Dup() StackItem {
|
||||||
a := make([]byte, len(i.value))
|
a := make([]byte, len(i.value))
|
||||||
|
@ -301,6 +397,16 @@ func (i *ArrayItem) String() string {
|
||||||
return "Array"
|
return "Array"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *ArrayItem) TryBytes() ([]byte, error) {
|
||||||
|
return nil, errors.New("can't convert Array to ByteArray")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *ArrayItem) Equals(s StackItem) bool {
|
||||||
|
return i == s
|
||||||
|
}
|
||||||
|
|
||||||
// Dup implements StackItem interface.
|
// Dup implements StackItem interface.
|
||||||
func (i *ArrayItem) Dup() StackItem {
|
func (i *ArrayItem) Dup() StackItem {
|
||||||
// reference type
|
// reference type
|
||||||
|
@ -341,6 +447,16 @@ func (i *MapItem) Value() interface{} {
|
||||||
return i.value
|
return i.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *MapItem) TryBytes() ([]byte, error) {
|
||||||
|
return nil, errors.New("can't convert Map to ByteArray")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *MapItem) Equals(s StackItem) bool {
|
||||||
|
return i == s
|
||||||
|
}
|
||||||
|
|
||||||
func (i *MapItem) String() string {
|
func (i *MapItem) String() string {
|
||||||
return "Map"
|
return "Map"
|
||||||
}
|
}
|
||||||
|
@ -438,6 +554,22 @@ func (i *InteropItem) Dup() StackItem {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TryBytes implements StackItem interface.
|
||||||
|
func (i *InteropItem) TryBytes() ([]byte, error) {
|
||||||
|
return nil, errors.New("can't convert Interop to ByteArray")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equals implements StackItem interface.
|
||||||
|
func (i *InteropItem) Equals(s StackItem) bool {
|
||||||
|
if i == s {
|
||||||
|
return true
|
||||||
|
} else if s == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val, ok := s.(*InteropItem)
|
||||||
|
return ok && i.value == val.value
|
||||||
|
}
|
||||||
|
|
||||||
// ToContractParameter implements StackItem interface.
|
// ToContractParameter implements StackItem interface.
|
||||||
func (i *InteropItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
func (i *InteropItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
|
||||||
return smartcontract.Parameter{
|
return smartcontract.Parameter{
|
||||||
|
|
14
pkg/vm/vm.go
14
pkg/vm/vm.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
@ -726,18 +725,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
|
||||||
if a == nil {
|
if a == nil {
|
||||||
panic("no second-to-the-top element found")
|
panic("no second-to-the-top element found")
|
||||||
}
|
}
|
||||||
if ta, ok := a.value.(*ArrayItem); ok {
|
v.estack.PushVal(a.value.Equals(b.value))
|
||||||
if tb, ok := b.value.(*ArrayItem); ok {
|
|
||||||
v.estack.PushVal(ta == tb)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} else if ma, ok := a.value.(*MapItem); ok {
|
|
||||||
if mb, ok := b.value.(*MapItem); ok {
|
|
||||||
v.estack.PushVal(ma == mb)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
v.estack.PushVal(reflect.DeepEqual(a, b))
|
|
||||||
|
|
||||||
// Bit operations.
|
// Bit operations.
|
||||||
case opcode.INVERT:
|
case opcode.INVERT:
|
||||||
|
|
|
@ -1006,6 +1006,16 @@ func TestEQUALGoodInteger(t *testing.T) {
|
||||||
assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value)
|
assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEQUALIntegerByteArray(t *testing.T) {
|
||||||
|
prog := makeProgram(opcode.EQUAL)
|
||||||
|
vm := load(prog)
|
||||||
|
vm.estack.PushVal([]byte{16})
|
||||||
|
vm.estack.PushVal(16)
|
||||||
|
runVM(t, vm)
|
||||||
|
assert.Equal(t, 1, vm.estack.Len())
|
||||||
|
assert.Equal(t, &BoolItem{true}, vm.estack.Pop().value)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEQUALArrayTrue(t *testing.T) {
|
func TestEQUALArrayTrue(t *testing.T) {
|
||||||
prog := makeProgram(opcode.DUP, opcode.EQUAL)
|
prog := makeProgram(opcode.DUP, opcode.EQUAL)
|
||||||
vm := load(prog)
|
vm := load(prog)
|
||||||
|
|
Loading…
Add table
Reference in a new issue