diff --git a/pkg/vm/serialization.go b/pkg/vm/serialization.go index 5918745ef..ffeaf05a5 100644 --- a/pkg/vm/serialization.go +++ b/pkg/vm/serialization.go @@ -2,8 +2,10 @@ package vm import ( "errors" + "math/big" "github.com/CityOfZion/neo-go/pkg/io" + "github.com/CityOfZion/neo-go/pkg/util" ) type stackItemType byte @@ -35,7 +37,8 @@ func serializeItemTo(item StackItem, w *io.BinWriter) { w.WriteLE(byte(booleanT)) w.WriteLE(t.value) case *BigIntegerItem: - w.Err = errors.New("not implemented") + w.WriteLE(byte(integerT)) + w.WriteBytes(t.Bytes()) case *InteropItem: w.Err = errors.New("not supported") case *ArrayItem: @@ -72,8 +75,11 @@ func deserializeItemFrom(r *io.BinReader) StackItem { r.ReadLE(&b) return NewBoolItem(b) case integerT: - r.Err = errors.New("not implemented") - return nil + data := r.ReadBytes() + num := new(big.Int).SetBytes(util.ArrayReverse(data)) + return &BigIntegerItem{ + value: num, + } case arrayT: r.Err = errors.New("not implemented") return nil diff --git a/pkg/vm/stack.go b/pkg/vm/stack.go index 13a6c0f75..1a230b299 100644 --- a/pkg/vm/stack.go +++ b/pkg/vm/stack.go @@ -122,7 +122,7 @@ func (e *Element) Bytes() []byte { case *ByteArrayItem: return t.value case *BigIntegerItem: - return util.ArrayReverse(t.value.Bytes()) // neoVM returns in LE + return t.Bytes() // neoVM returns in LE case *BoolItem: if t.value { return []byte{1} diff --git a/pkg/vm/stack_item.go b/pkg/vm/stack_item.go index 925676a67..c2faabf0f 100644 --- a/pkg/vm/stack_item.go +++ b/pkg/vm/stack_item.go @@ -6,6 +6,8 @@ import ( "fmt" "math/big" "reflect" + + "github.com/CityOfZion/neo-go/pkg/util" ) // A StackItem represents the "real" value that is pushed on the stack. @@ -131,6 +133,11 @@ func NewBigIntegerItem(value int) *BigIntegerItem { } } +// Bytes converts i to a slice of bytes. +func (i *BigIntegerItem) Bytes() []byte { + return util.ArrayReverse(i.value.Bytes()) +} + // Value implements StackItem interface. func (i *BigIntegerItem) Value() interface{} { return i.value diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index a8d94f1e1..2e116b5b7 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -245,6 +245,17 @@ func TestSerializeByteArray(t *testing.T) { require.Equal(t, value, vm.estack.Top().Bytes()) } +func TestSerializeInteger(t *testing.T) { + vm := load(getSerializeProg()) + value := int64(123) + vm.estack.PushVal(value) + + testSerialize(t, vm) + + require.IsType(t, (*BigIntegerItem)(nil), vm.estack.Top().value) + require.Equal(t, value, vm.estack.Top().BigInt().Int64()) +} + func callNTimes(n uint16) []byte { return makeProgram( PUSHBYTES2, Instruction(n), Instruction(n>>8), // little-endian