From da0a56f9222f06198a867c14247c6bbff00baa33 Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Wed, 27 Feb 2019 22:41:46 +0000 Subject: [PATCH] Add conversions for bytearray and Int stack items --- pkg/vm/stack/Int.go | 21 +++++++++++++++++ pkg/vm/stack/bytearray.go | 47 +++++++++++++++++++++++++++++++++++++++ pkg/vm/stack/stack.go | 2 -- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/pkg/vm/stack/Int.go b/pkg/vm/stack/Int.go index ae81a2120..4f1d070a5 100644 --- a/pkg/vm/stack/Int.go +++ b/pkg/vm/stack/Int.go @@ -72,3 +72,24 @@ func (i *Int) Lsh(n *Int) (*Int, error) { func (i *Int) Integer() (*Int, error) { return i, nil } + +// 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 +} + +//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 + +} diff --git a/pkg/vm/stack/bytearray.go b/pkg/vm/stack/bytearray.go index 23b7fc805..4ff318e2b 100644 --- a/pkg/vm/stack/bytearray.go +++ b/pkg/vm/stack/bytearray.go @@ -1,12 +1,59 @@ package stack +import ( + "errors" + "math/big" + "strconv" +) + // ByteArray represents a slice of bytes on the stack type ByteArray struct { *abstractItem val []byte } +//NewByteArray returns a ByteArray stack item +// given a byte slice +func NewByteArray(val []byte) *ByteArray { + return &ByteArray{ + &abstractItem{}, + val, + } +} + //ByteArray overrides the default abstractItem Bytes array method func (ba *ByteArray) ByteArray() (*ByteArray, error) { return ba, nil } + +//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] + } + + integerVal := new(big.Int).SetBytes(dest) + + return &Int{ + ba.abstractItem, + integerVal, + }, nil + + // return ba, nil +} + +// Boolean will convert +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 +} diff --git a/pkg/vm/stack/stack.go b/pkg/vm/stack/stack.go index 0136f3f03..3c01ad4e7 100644 --- a/pkg/vm/stack/stack.go +++ b/pkg/vm/stack/stack.go @@ -70,7 +70,6 @@ func (ras *RandomAccess) Push(item Item) *RandomAccess { // Insert will push a stackItem onto the stack at position `n` // Note; index 0 is the top of the stack, which is the end of slice -// REDO: func (ras *RandomAccess) Insert(n uint16, item Item) (*RandomAccess, error) { if n == 0 { @@ -112,7 +111,6 @@ func (ras *RandomAccess) Peek(n uint16) (Item, error) { } // Check that we are not peeking out of the bounds - if n > stackSize-1 { return nil, fmt.Errorf("Tried to peek at index %d when length of stack is %d", n, len(ras.vals)) }