mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 23:33:37 +00:00
stackitem: change Bool() to TryBool(), prepare for its failures
This commit is contained in:
parent
790693fc6d
commit
a7670303e8
7 changed files with 65 additions and 45 deletions
|
@ -1487,7 +1487,11 @@ func (bc *Blockchain) verifyHashAgainstScript(hash util.Uint160, witness *transa
|
|||
}
|
||||
resEl := vm.Estack().Pop()
|
||||
if resEl != nil {
|
||||
if !resEl.Bool() {
|
||||
res, err := resEl.Item().TryBool()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: invalid return value", ErrVerificationFailed)
|
||||
}
|
||||
if !res {
|
||||
return fmt.Errorf("%w: invalid signature", ErrVerificationFailed)
|
||||
}
|
||||
if vm.Estack().Len() != 0 {
|
||||
|
|
|
@ -577,8 +577,12 @@ func compareContractStates(t *testing.T, expected *state.Contract, actual stacki
|
|||
require.Equal(t, 4, len(act))
|
||||
require.Equal(t, expected.Script, act[0].Value().([]byte))
|
||||
require.Equal(t, expectedManifest, act[1].Value().([]byte))
|
||||
require.Equal(t, expected.HasStorage(), act[2].Bool())
|
||||
require.Equal(t, expected.IsPayable(), act[3].Bool())
|
||||
hasstorage, err := act[2].TryBool()
|
||||
require.NoError(t, err)
|
||||
ispayable, err := act[3].TryBool()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected.HasStorage(), hasstorage)
|
||||
require.Equal(t, expected.IsPayable(), ispayable)
|
||||
}
|
||||
|
||||
func TestContractUpdate(t *testing.T) {
|
||||
|
|
|
@ -42,7 +42,10 @@ func (c *candidate) fromStackItem(item stackitem.Item) *candidate {
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Registered = arr[0].Bool()
|
||||
c.Registered, err = arr[0].TryBool()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Votes = *vs
|
||||
return c
|
||||
}
|
||||
|
|
|
@ -191,8 +191,8 @@ func (c *Context) Dup() stackitem.Item {
|
|||
return c
|
||||
}
|
||||
|
||||
// Bool implements stackitem.Item interface.
|
||||
func (c *Context) Bool() bool { panic("can't convert Context to Bool") }
|
||||
// TryBool implements stackitem.Item interface.
|
||||
func (c *Context) TryBool() (bool, error) { panic("can't convert Context to Bool") }
|
||||
|
||||
// TryBytes implements stackitem.Item interface.
|
||||
func (c *Context) TryBytes() ([]byte, error) {
|
||||
|
|
|
@ -54,8 +54,8 @@ func (c *exceptionHandlingContext) Dup() stackitem.Item {
|
|||
return c
|
||||
}
|
||||
|
||||
// Bool implements stackitem.Item interface.
|
||||
func (c *exceptionHandlingContext) Bool() bool {
|
||||
// TryBool implements stackitem.Item interface.
|
||||
func (c *exceptionHandlingContext) TryBool() (bool, error) {
|
||||
panic("can't convert exceptionHandlingContext to Bool")
|
||||
}
|
||||
|
||||
|
|
|
@ -80,9 +80,14 @@ func (e *Element) BigInt() *big.Int {
|
|||
return val
|
||||
}
|
||||
|
||||
// Bool converts an underlying value of the element to a boolean.
|
||||
// Bool converts an underlying value of the element to a boolean if it's
|
||||
// possible to do so, it will panic otherwise.
|
||||
func (e *Element) Bool() bool {
|
||||
return e.value.Bool()
|
||||
b, err := e.value.TryBool()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Bytes attempts to get the underlying value of the element as a byte array.
|
||||
|
|
|
@ -31,8 +31,8 @@ type Item interface {
|
|||
Value() interface{}
|
||||
// Dup duplicates current Item.
|
||||
Dup() Item
|
||||
// Bool converts Item to a boolean value.
|
||||
Bool() bool
|
||||
// TryBool converts Item to a boolean value.
|
||||
TryBool() (bool, error)
|
||||
// TryBytes converts Item to a byte slice.
|
||||
TryBytes() ([]byte, error)
|
||||
// TryInteger converts Item to an integer.
|
||||
|
@ -155,7 +155,11 @@ func convertPrimitive(item Item, typ Type) (Item, error) {
|
|||
}
|
||||
return NewByteArray(b), nil
|
||||
case BooleanT:
|
||||
return NewBool(item.Bool()), nil
|
||||
b, err := item.TryBool()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewBool(b), nil
|
||||
default:
|
||||
return nil, errInvalidConversion
|
||||
}
|
||||
|
@ -210,8 +214,8 @@ func (i *Struct) Dup() Item {
|
|||
return i
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *Struct) Bool() bool { return true }
|
||||
// TryBool implements Item interface.
|
||||
func (i *Struct) TryBool() (bool, error) { return true, nil }
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
func (i *Struct) TryBytes() ([]byte, error) {
|
||||
|
@ -255,7 +259,7 @@ func (i *Struct) Convert(typ Type) (Item, error) {
|
|||
copy(arr, i.value)
|
||||
return NewArray(arr), nil
|
||||
case BooleanT:
|
||||
return NewBool(i.Bool()), nil
|
||||
return NewBool(true), nil
|
||||
default:
|
||||
return nil, errInvalidConversion
|
||||
}
|
||||
|
@ -296,8 +300,8 @@ func (i Null) Dup() Item {
|
|||
return i
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i Null) Bool() bool { return false }
|
||||
// TryBool implements Item interface.
|
||||
func (i Null) TryBool() (bool, error) { return false, nil }
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
func (i Null) TryBytes() ([]byte, error) {
|
||||
|
@ -346,9 +350,9 @@ func (i *BigInteger) Bytes() []byte {
|
|||
return bigint.ToBytes(i.value)
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *BigInteger) Bool() bool {
|
||||
return i.value.Sign() != 0
|
||||
// TryBool implements Item interface.
|
||||
func (i *BigInteger) TryBool() (bool, error) {
|
||||
return i.value.Sign() != 0, nil
|
||||
}
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
|
@ -431,8 +435,8 @@ func (i *Bool) Dup() Item {
|
|||
return &Bool{i.value}
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *Bool) Bool() bool { return i.value }
|
||||
// TryBool implements Item interface.
|
||||
func (i *Bool) TryBool() (bool, error) { return i.value, nil }
|
||||
|
||||
// Bytes converts Bool to bytes.
|
||||
func (i *Bool) Bytes() []byte {
|
||||
|
@ -500,17 +504,17 @@ func (i *ByteArray) String() string {
|
|||
return "ByteString"
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *ByteArray) Bool() bool {
|
||||
// TryBool implements Item interface.
|
||||
func (i *ByteArray) TryBool() (bool, error) {
|
||||
if len(i.value) > MaxBigIntegerSizeBits/8 {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
for _, b := range i.value {
|
||||
if b != 0 {
|
||||
return true
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
|
@ -601,8 +605,8 @@ func (i *Array) String() string {
|
|||
return "Array"
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *Array) Bool() bool { return true }
|
||||
// TryBool implements Item interface.
|
||||
func (i *Array) TryBool() (bool, error) { return true, nil }
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
func (i *Array) TryBytes() ([]byte, error) {
|
||||
|
@ -638,7 +642,7 @@ func (i *Array) Convert(typ Type) (Item, error) {
|
|||
copy(arr, i.value)
|
||||
return NewStruct(arr), nil
|
||||
case BooleanT:
|
||||
return NewBool(i.Bool()), nil
|
||||
return NewBool(true), nil
|
||||
default:
|
||||
return nil, errInvalidConversion
|
||||
}
|
||||
|
@ -691,8 +695,8 @@ func (i *Map) Len() int {
|
|||
return len(i.value)
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *Map) Bool() bool { return true }
|
||||
// TryBool implements Item interface.
|
||||
func (i *Map) TryBool() (bool, error) { return true, nil }
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
func (i *Map) TryBytes() ([]byte, error) {
|
||||
|
@ -743,7 +747,7 @@ func (i *Map) Convert(typ Type) (Item, error) {
|
|||
case MapT:
|
||||
return i, nil
|
||||
case BooleanT:
|
||||
return NewBool(i.Bool()), nil
|
||||
return NewBool(true), nil
|
||||
default:
|
||||
return nil, errInvalidConversion
|
||||
}
|
||||
|
@ -807,8 +811,8 @@ func (i *Interop) Dup() Item {
|
|||
return i
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *Interop) Bool() bool { return true }
|
||||
// TryBool implements Item interface.
|
||||
func (i *Interop) TryBool() (bool, error) { return true, nil }
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
func (i *Interop) TryBytes() ([]byte, error) {
|
||||
|
@ -840,7 +844,7 @@ func (i *Interop) Convert(typ Type) (Item, error) {
|
|||
case InteropT:
|
||||
return i, nil
|
||||
case BooleanT:
|
||||
return NewBool(i.Bool()), nil
|
||||
return NewBool(true), nil
|
||||
default:
|
||||
return nil, errInvalidConversion
|
||||
}
|
||||
|
@ -886,9 +890,9 @@ func (p *Pointer) Dup() Item {
|
|||
}
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (p *Pointer) Bool() bool {
|
||||
return true
|
||||
// TryBool implements Item interface.
|
||||
func (p *Pointer) TryBool() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
|
@ -921,7 +925,7 @@ func (p *Pointer) Convert(typ Type) (Item, error) {
|
|||
case PointerT:
|
||||
return p, nil
|
||||
case BooleanT:
|
||||
return NewBool(p.Bool()), nil
|
||||
return NewBool(true), nil
|
||||
default:
|
||||
return nil, errInvalidConversion
|
||||
}
|
||||
|
@ -959,9 +963,9 @@ func (i *Buffer) String() string {
|
|||
return "Buffer"
|
||||
}
|
||||
|
||||
// Bool implements Item interface.
|
||||
func (i *Buffer) Bool() bool {
|
||||
return true
|
||||
// TryBool implements Item interface.
|
||||
func (i *Buffer) TryBool() (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// TryBytes implements Item interface.
|
||||
|
@ -998,7 +1002,7 @@ func (i *Buffer) Type() Type { return BufferT }
|
|||
func (i *Buffer) Convert(typ Type) (Item, error) {
|
||||
switch typ {
|
||||
case BooleanT:
|
||||
return NewBool(i.Bool()), nil
|
||||
return NewBool(true), nil
|
||||
case BufferT:
|
||||
return i, nil
|
||||
case ByteArrayT:
|
||||
|
|
Loading…
Reference in a new issue