From 48413900ca000b28469599ec64d7b98a8195e801 Mon Sep 17 00:00:00 2001 From: BlockChainDev Date: Sat, 16 Mar 2019 21:44:03 +0000 Subject: [PATCH] remove error on NewBoolean Expose underlying with Getter on Boolean StackItem Add Equals method for ByteArray --- pkg/vm/stack/Int.go | 2 +- pkg/vm/stack/boolean.go | 9 +++++++-- pkg/vm/stack/bytearray.go | 12 +++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/vm/stack/Int.go b/pkg/vm/stack/Int.go index 2483a78ea..ac9a4cdba 100644 --- a/pkg/vm/stack/Int.go +++ b/pkg/vm/stack/Int.go @@ -85,7 +85,7 @@ func (i *Int) ByteArray() (*ByteArray, error) { // to convert an Integer into a Boolean StackItem func (i *Int) Boolean() (*Boolean, error) { boolean := (i.val.Int64() != 0) - return NewBoolean(boolean) + return NewBoolean(boolean), nil } //Value returns the underlying big.Int diff --git a/pkg/vm/stack/boolean.go b/pkg/vm/stack/boolean.go index 2441a303c..66e3647e5 100644 --- a/pkg/vm/stack/boolean.go +++ b/pkg/vm/stack/boolean.go @@ -7,11 +7,11 @@ type Boolean struct { } //NewBoolean returns a new boolean stack item -func NewBoolean(val bool) (*Boolean, error) { +func NewBoolean(val bool) *Boolean { return &Boolean{ &abstractItem{}, val, - }, nil + } } // Boolean overrides the default implementation @@ -19,3 +19,8 @@ func NewBoolean(val bool) (*Boolean, error) { func (b *Boolean) Boolean() (*Boolean, error) { return b, nil } + +// Value returns the underlying boolean value +func (b *Boolean) Value() bool { + return b.val +} diff --git a/pkg/vm/stack/bytearray.go b/pkg/vm/stack/bytearray.go index 6b1de0d79..7d1c3c818 100644 --- a/pkg/vm/stack/bytearray.go +++ b/pkg/vm/stack/bytearray.go @@ -1,6 +1,7 @@ package stack import ( + "bytes" "errors" "math/big" "strconv" @@ -26,6 +27,15 @@ func (ba *ByteArray) ByteArray() (*ByteArray, error) { return ba, nil } +//Equals returns true, if two bytearrays are equal +func (ba *ByteArray) Equals(other *ByteArray) *Boolean { + // If either are nil, return false + if ba == nil || other == nil { + return NewBoolean(false) + } + return NewBoolean(bytes.Equal(ba.val, other.val)) +} + //Integer overrides the default Integer method to convert an // ByteArray Into an integer func (ba *ByteArray) Integer() (*Int, error) { @@ -41,7 +51,7 @@ func (ba *ByteArray) Boolean() (*Boolean, error) { if err != nil { return nil, errors.New("cannot convert byte array to a boolean") } - return NewBoolean(boolean) + return NewBoolean(boolean), nil } // XXX: move this into a pkg/util/slice folder