mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
accounts/abi: add bool unpack test and add a panic to readBool function
Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
parent
ee4ea6cb3f
commit
7a861d281d
2 changed files with 17 additions and 3 deletions
|
|
@ -112,6 +112,13 @@ func TestSimpleMethodUnpack(t *testing.T) {
|
|||
outVar string // the output variable (e.g. uint32, *big.Int, etc)
|
||||
err string // empty or error if expected
|
||||
}{
|
||||
{
|
||||
`[ { "type": "bool" } ]`,
|
||||
pad([]byte{1}, 32, true),
|
||||
bool(true),
|
||||
"bool",
|
||||
"",
|
||||
},
|
||||
{
|
||||
`[ { "type": "uint32" } ]`,
|
||||
pad([]byte{1}, 32, true),
|
||||
|
|
@ -215,6 +222,10 @@ func TestSimpleMethodUnpack(t *testing.T) {
|
|||
|
||||
var outvar interface{}
|
||||
switch test.outVar {
|
||||
case "bool":
|
||||
var v bool
|
||||
err = abi.Unpack(&v, "method", test.marshalledOutput)
|
||||
outvar = v
|
||||
case "uint8":
|
||||
var v uint8
|
||||
err = abi.Unpack(&v, "method", test.marshalledOutput)
|
||||
|
|
|
|||
|
|
@ -118,7 +118,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) {
|
|||
case IntTy, UintTy:
|
||||
inter = readInteger(t.Type.Kind, returnOutput)
|
||||
case BoolTy:
|
||||
inter, err = getBoolValue(returnOutput)
|
||||
inter, err = readBool(returnOutput)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -160,7 +160,10 @@ func readInteger(kind reflect.Kind, b []byte) interface{} {
|
|||
}
|
||||
}
|
||||
|
||||
func getBoolValue(word []byte) (bool, error) {
|
||||
func readBool(word []byte) (bool, error) {
|
||||
if len(word) != 32 {
|
||||
panic("abi: fatal error. Incorrect word length.")
|
||||
}
|
||||
improperEncoding := "abi: improperly encoded boolean value"
|
||||
for i, b := range word {
|
||||
if b != 0 && i != 31 {
|
||||
|
|
@ -218,7 +221,7 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) {
|
|||
case IntTy, UintTy:
|
||||
return readInteger(t.Type.Kind, returnOutput), nil
|
||||
case BoolTy:
|
||||
return getBoolValue(returnOutput)
|
||||
return readBool(returnOutput)
|
||||
case AddressTy:
|
||||
return common.BytesToAddress(returnOutput), nil
|
||||
case HashTy:
|
||||
|
|
|
|||
Loading…
Reference in a new issue