From f0083b94c5840114ace6f182a30ca45597f86234 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 17 Jan 2020 15:31:37 +0300 Subject: [PATCH] vm: use new big.Int (de-)serialization routines Also fix a test with CAT. --- pkg/vm/json_test.go | 2 +- pkg/vm/serialization.go | 6 ++---- pkg/vm/stack.go | 4 +--- pkg/vm/stack_item.go | 4 +--- pkg/vm/vm_test.go | 2 +- 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/pkg/vm/json_test.go b/pkg/vm/json_test.go index 6ee5568ae..1cc33681c 100644 --- a/pkg/vm/json_test.go +++ b/pkg/vm/json_test.go @@ -196,7 +196,7 @@ func compareItems(t *testing.T, a, b StackItem) { case *BigIntegerItem: require.Equal(t, val, ac.value.Int64()) case *ByteArrayItem: - require.Equal(t, val, new(big.Int).SetBytes(util.ArrayReverse(ac.value)).Int64()) + require.Equal(t, val, bytesToInt(ac.value).Int64()) case *BoolItem: if ac.value { require.Equal(t, val, int64(1)) diff --git a/pkg/vm/serialization.go b/pkg/vm/serialization.go index e5a928c2d..c3adf299b 100644 --- a/pkg/vm/serialization.go +++ b/pkg/vm/serialization.go @@ -2,10 +2,8 @@ package vm import ( "errors" - "math/big" "github.com/CityOfZion/neo-go/pkg/io" - "github.com/CityOfZion/neo-go/pkg/util" ) type stackItemType byte @@ -50,7 +48,7 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) { w.WriteBool(t.value) case *BigIntegerItem: w.WriteBytes([]byte{byte(integerT)}) - w.WriteVarBytes(t.Bytes()) + w.WriteVarBytes(intToBytes(t.value)) case *InteropItem: w.Err = errors.New("not supported") case *ArrayItem, *StructItem: @@ -108,7 +106,7 @@ func DecodeBinaryStackItem(r *io.BinReader) StackItem { return NewBoolItem(b) case integerT: data := r.ReadVarBytes() - num := new(big.Int).SetBytes(util.ArrayReverse(data)) + num := bytesToInt(data) return &BigIntegerItem{ value: num, } diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index b0a2f72d9..9cdf551ac 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -5,8 +5,6 @@ import ( "errors" "fmt" "math/big" - - "github.com/CityOfZion/neo-go/pkg/util" ) // Stack implementation for the neo-go virtual machine. The stack implements @@ -83,7 +81,7 @@ func (e *Element) BigInt() *big.Int { return big.NewInt(0) default: b := t.Value().([]uint8) - return new(big.Int).SetBytes(util.ArrayReverse(b)) + return bytesToInt(b) } } diff --git a/pkg/vm/stack_item.go b/pkg/vm/stack_item.go index de277159a..d5ea56cc6 100644 --- a/pkg/vm/stack_item.go +++ b/pkg/vm/stack_item.go @@ -7,8 +7,6 @@ import ( "fmt" "math/big" "reflect" - - "github.com/CityOfZion/neo-go/pkg/util" ) // A StackItem represents the "real" value that is pushed on the stack. @@ -144,7 +142,7 @@ func NewBigIntegerItem(value int) *BigIntegerItem { // Bytes converts i to a slice of bytes. func (i *BigIntegerItem) Bytes() []byte { - return util.ArrayReverse(i.value.Bytes()) + return intToBytes(i.value) } // Value implements StackItem interface. diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index a8e02606d..2e9fb9ae2 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -2042,7 +2042,7 @@ func TestCATInt0ByteArray(t *testing.T) { vm.estack.PushVal([]byte{}) runVM(t, vm) assert.Equal(t, 1, vm.estack.Len()) - assert.Equal(t, &ByteArrayItem{[]byte{}}, vm.estack.Pop().value) + assert.Equal(t, &ByteArrayItem{[]byte{0}}, vm.estack.Pop().value) } func TestCATByteArrayInt1(t *testing.T) {