vm: use new big.Int (de-)serialization routines

Also fix a test with CAT.
This commit is contained in:
Evgenii Stratonikov 2020-01-17 15:31:37 +03:00
parent f26bdae2c5
commit f0083b94c5
5 changed files with 6 additions and 12 deletions

View file

@ -196,7 +196,7 @@ func compareItems(t *testing.T, a, b StackItem) {
case *BigIntegerItem: case *BigIntegerItem:
require.Equal(t, val, ac.value.Int64()) require.Equal(t, val, ac.value.Int64())
case *ByteArrayItem: 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: case *BoolItem:
if ac.value { if ac.value {
require.Equal(t, val, int64(1)) require.Equal(t, val, int64(1))

View file

@ -2,10 +2,8 @@ package vm
import ( import (
"errors" "errors"
"math/big"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
"github.com/CityOfZion/neo-go/pkg/util"
) )
type stackItemType byte type stackItemType byte
@ -50,7 +48,7 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
w.WriteBool(t.value) w.WriteBool(t.value)
case *BigIntegerItem: case *BigIntegerItem:
w.WriteBytes([]byte{byte(integerT)}) w.WriteBytes([]byte{byte(integerT)})
w.WriteVarBytes(t.Bytes()) w.WriteVarBytes(intToBytes(t.value))
case *InteropItem: case *InteropItem:
w.Err = errors.New("not supported") w.Err = errors.New("not supported")
case *ArrayItem, *StructItem: case *ArrayItem, *StructItem:
@ -108,7 +106,7 @@ func DecodeBinaryStackItem(r *io.BinReader) StackItem {
return NewBoolItem(b) return NewBoolItem(b)
case integerT: case integerT:
data := r.ReadVarBytes() data := r.ReadVarBytes()
num := new(big.Int).SetBytes(util.ArrayReverse(data)) num := bytesToInt(data)
return &BigIntegerItem{ return &BigIntegerItem{
value: num, value: num,
} }

View file

@ -5,8 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"github.com/CityOfZion/neo-go/pkg/util"
) )
// Stack implementation for the neo-go virtual machine. The stack implements // 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) return big.NewInt(0)
default: default:
b := t.Value().([]uint8) b := t.Value().([]uint8)
return new(big.Int).SetBytes(util.ArrayReverse(b)) return bytesToInt(b)
} }
} }

View file

@ -7,8 +7,6 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"reflect" "reflect"
"github.com/CityOfZion/neo-go/pkg/util"
) )
// A StackItem represents the "real" value that is pushed on the stack. // 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. // Bytes converts i to a slice of bytes.
func (i *BigIntegerItem) Bytes() []byte { func (i *BigIntegerItem) Bytes() []byte {
return util.ArrayReverse(i.value.Bytes()) return intToBytes(i.value)
} }
// Value implements StackItem interface. // Value implements StackItem interface.

View file

@ -2042,7 +2042,7 @@ func TestCATInt0ByteArray(t *testing.T) {
vm.estack.PushVal([]byte{}) vm.estack.PushVal([]byte{})
runVM(t, vm) runVM(t, vm)
assert.Equal(t, 1, vm.estack.Len()) 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) { func TestCATByteArrayInt1(t *testing.T) {