Refactor Int, Boolean, ByteArray conversion

This commit is contained in:
BlockChainDev 2019-03-15 22:30:25 +00:00
parent e2ef6bd2f4
commit 04c56b514c
4 changed files with 54 additions and 26 deletions

View file

@ -11,6 +11,8 @@ type Int struct {
// NewInt will convert a big integer into // NewInt will convert a big integer into
// a StackInteger // a StackInteger
func NewInt(val *big.Int) (*Int, error) { func NewInt(val *big.Int) (*Int, error) {
// TOODO: check it is 32 bytes
return &Int{ return &Int{
abstractItem: &abstractItem{}, abstractItem: &abstractItem{},
val: val, val: val,
@ -76,20 +78,19 @@ func (i *Int) Integer() (*Int, error) {
// ByteArray override the default ByteArray method // ByteArray override the default ByteArray method
// to convert a Integer into a byte Array // to convert a Integer into a byte Array
func (i *Int) ByteArray() (*ByteArray, error) { func (i *Int) ByteArray() (*ByteArray, error) {
return &ByteArray{ b := i.val.Bytes()
i.abstractItem, dest := reverse(b)
i.val.Bytes(), return NewByteArray(dest), nil
}, nil
} }
//Boolean override the default Boolean method //Boolean override the default Boolean method
// to convert an Integer into a Boolean StackItem // to convert an Integer into a Boolean StackItem
func (i *Int) Boolean() (*Boolean, error) { func (i *Int) Boolean() (*Boolean, error) {
boolean := (i.val.Int64() != 0) boolean := (i.val.Int64() != 0)
return &Boolean{ return NewBoolean(boolean)
i.abstractItem, }
boolean,
}, nil //Value returns the underlying big.Int
func (i *Int) Value() *big.Int {
return i.val
} }

View file

@ -6,6 +6,14 @@ type Boolean struct {
val bool 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 // Boolean overrides the default implementation
// by the abstractItem, returning a Boolean struct // by the abstractItem, returning a Boolean struct
func (b *Boolean) Boolean() (*Boolean, error) { func (b *Boolean) Boolean() (*Boolean, error) {

View file

@ -29,29 +29,33 @@ func (ba *ByteArray) ByteArray() (*ByteArray, error) {
//Integer overrides the default Integer method to convert an //Integer overrides the default Integer method to convert an
// ByteArray Into an integer // ByteArray Into an integer
func (ba *ByteArray) Integer() (*Int, error) { func (ba *ByteArray) Integer() (*Int, error) {
dest := reverse(ba.val)
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]
}
integerVal := new(big.Int).SetBytes(dest) 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) { func (ba *ByteArray) Boolean() (*Boolean, error) {
boolean, err := strconv.ParseBool(string(ba.val)) boolean, err := strconv.ParseBool(string(ba.val))
if err != nil { if err != nil {
return nil, errors.New("cannot convert byte array to a boolean") return nil, errors.New("cannot convert byte array to a boolean")
} }
return &Boolean{ return NewBoolean(boolean)
ba.abstractItem, }
boolean,
}, nil // 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
} }

View file

@ -55,3 +55,18 @@ func TestRsh(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, true, expected.Equal(c)) 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())
}