Merge pull request #746 from nspcc-dev/fix/equal

vm: implement EQUAL opcode properly

Fixes #745, #749.
This commit is contained in:
Roman Khimov 2020-03-12 11:46:51 +03:00 committed by GitHub
commit 1b5dd53e07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 157 additions and 27 deletions

View file

@ -173,6 +173,16 @@ func (c *Context) Dup() StackItem {
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.
func (c *Context) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
return smartcontract.Parameter{

View file

@ -125,21 +125,11 @@ func (e *Element) Bool() bool {
// 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.
func (e *Element) Bytes() []byte {
switch t := e.value.(type) {
case *ByteArrayItem:
return t.value
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())
bs, err := e.value.TryBytes()
if err != nil {
panic(err)
}
return bs
}
// Array attempts to get the underlying value of the element as an array of

View file

@ -1,9 +1,11 @@
package vm
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
@ -18,6 +20,10 @@ type StackItem interface {
Value() interface{}
// Dup duplicates current 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(map[StackItem]bool) smartcontract.Parameter
}
@ -118,6 +124,30 @@ func (i *StructItem) Dup() StackItem {
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.
func (i *StructItem) ToContractParameter(seen map[StackItem]bool) smartcontract.Parameter {
var value []smartcontract.Parameter
@ -167,6 +197,26 @@ func (i *BigIntegerItem) Bytes() []byte {
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.
func (i *BigIntegerItem) Value() interface{} {
return i.value
@ -226,6 +276,36 @@ func (i *BoolItem) Dup() StackItem {
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.
func (i *BoolItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
return smartcontract.Parameter{
@ -260,6 +340,22 @@ func (i *ByteArrayItem) String() string {
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.
func (i *ByteArrayItem) Dup() StackItem {
a := make([]byte, len(i.value))
@ -301,6 +397,16 @@ func (i *ArrayItem) String() string {
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.
func (i *ArrayItem) Dup() StackItem {
// reference type
@ -341,6 +447,16 @@ func (i *MapItem) Value() interface{} {
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 {
return "Map"
}
@ -438,6 +554,22 @@ func (i *InteropItem) Dup() StackItem {
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.
func (i *InteropItem) ToContractParameter(map[StackItem]bool) smartcontract.Parameter {
return smartcontract.Parameter{

View file

@ -8,7 +8,6 @@ import (
"io/ioutil"
"math/big"
"os"
"reflect"
"text/tabwriter"
"unicode/utf8"
@ -726,18 +725,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
if a == nil {
panic("no second-to-the-top element found")
}
if ta, ok := a.value.(*ArrayItem); ok {
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))
v.estack.PushVal(a.value.Equals(b.value))
// Bit operations.
case opcode.INVERT:

View file

@ -1006,6 +1006,16 @@ func TestEQUALGoodInteger(t *testing.T) {
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) {
prog := makeProgram(opcode.DUP, opcode.EQUAL)
vm := load(prog)