From 04c56b514c745f1a4d5f64b4c84b4e158fb8e6ab Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Fri, 15 Mar 2019 22:30:25 +0000 Subject: [PATCH] Refactor Int, Boolean, ByteArray conversion --- pkg/vm/stack/Int.go | 21 +++++++++++---------- pkg/vm/stack/boolean.go | 8 ++++++++ pkg/vm/stack/bytearray.go | 36 ++++++++++++++++++++---------------- pkg/vm/stack/int_test.go | 15 +++++++++++++++ 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/pkg/vm/stack/Int.go b/pkg/vm/stack/Int.go index 4f1d070a5..08ee41629 100644 --- a/pkg/vm/stack/Int.go +++ b/pkg/vm/stack/Int.go @@ -11,6 +11,8 @@ type Int struct { // NewInt will convert a big integer into // a StackInteger func NewInt(val *big.Int) (*Int, error) { + // TOODO: check it is 32 bytes + return &Int{ abstractItem: &abstractItem{}, val: val, @@ -76,20 +78,19 @@ func (i *Int) Integer() (*Int, error) { // ByteArray override the default ByteArray method // to convert a Integer into a byte Array func (i *Int) ByteArray() (*ByteArray, error) { - return &ByteArray{ - i.abstractItem, - i.val.Bytes(), - }, nil + b := i.val.Bytes() + dest := reverse(b) + return NewByteArray(dest), nil } //Boolean override the default Boolean method // to convert an Integer into a Boolean StackItem func (i *Int) Boolean() (*Boolean, error) { - boolean := (i.val.Int64() != 0) - return &Boolean{ - i.abstractItem, - boolean, - }, nil - + return NewBoolean(boolean) +} + +//Value returns the underlying big.Int +func (i *Int) Value() *big.Int { + return i.val } diff --git a/pkg/vm/stack/boolean.go b/pkg/vm/stack/boolean.go index d6a2c12ac..2441a303c 100644 --- a/pkg/vm/stack/boolean.go +++ b/pkg/vm/stack/boolean.go @@ -6,6 +6,14 @@ type Boolean struct { val bool } +//NewBoolean returns a new boolean stack item +func NewBoolean(val bool) (*Boolean, error) { + return &Boolean{ + &abstractItem{}, + val, + }, nil +} + // Boolean overrides the default implementation // by the abstractItem, returning a Boolean struct func (b *Boolean) Boolean() (*Boolean, error) { diff --git a/pkg/vm/stack/bytearray.go b/pkg/vm/stack/bytearray.go index 7d57b12f0..6b1de0d79 100644 --- a/pkg/vm/stack/bytearray.go +++ b/pkg/vm/stack/bytearray.go @@ -29,29 +29,33 @@ func (ba *ByteArray) ByteArray() (*ByteArray, error) { //Integer overrides the default Integer method to convert an // ByteArray Into an integer func (ba *ByteArray) Integer() (*Int, error) { - - dest := make([]byte, 0) - - for i, j := 0, len(ba.val)-1; i < j+1; i, j = i+1, j-1 { - dest[i], dest[j] = ba.val[j], ba.val[i] - } - + dest := reverse(ba.val) integerVal := new(big.Int).SetBytes(dest) + return NewInt(integerVal) - return &Int{ - ba.abstractItem, - integerVal, - }, nil } -// Boolean will convert +// Boolean will convert a byte array into a boolean stack item func (ba *ByteArray) Boolean() (*Boolean, error) { boolean, err := strconv.ParseBool(string(ba.val)) if err != nil { return nil, errors.New("cannot convert byte array to a boolean") } - return &Boolean{ - ba.abstractItem, - boolean, - }, nil + return NewBoolean(boolean) +} + +// XXX: move this into a pkg/util/slice folder +// Go mod not working +func reverse(b []byte) []byte { + if len(b) < 2 { + return b + } + + dest := make([]byte, len(b)) + + for i, j := 0, len(b)-1; i < j+1; i, j = i+1, j-1 { + dest[i], dest[j] = b[j], b[i] + } + + return dest } diff --git a/pkg/vm/stack/int_test.go b/pkg/vm/stack/int_test.go index 40ef3c8b2..25d360183 100644 --- a/pkg/vm/stack/int_test.go +++ b/pkg/vm/stack/int_test.go @@ -55,3 +55,18 @@ func TestRsh(t *testing.T) { assert.Nil(t, err) assert.Equal(t, true, expected.Equal(c)) } + +func TestByteArrConversion(t *testing.T) { + + var num int64 = 100000 + + a := testMakeStackInt(t, num) + ba, err := a.ByteArray() + assert.Nil(t, err) + + have, err := ba.Integer() + assert.Nil(t, err) + + assert.Equal(t, num, have.val.Int64()) + +}