fix parseTopics() and add tests

This commit is contained in:
Jeff Wentworth 2019-11-12 20:41:41 +09:00
parent dac98bc89d
commit dc7cd7e4b5
3 changed files with 55 additions and 4 deletions

View file

@ -135,6 +135,7 @@ func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) er
case reflect.Int8: case reflect.Int8:
num := new(big.Int).SetBytes(topics[0][:]) num := new(big.Int).SetBytes(topics[0][:])
field.Set(reflect.ValueOf(int8(num.Int64()))) field.Set(reflect.ValueOf(int8(num.Int64())))
fmt.Printf("parseTopics: num '%v', topics[0] '%v', field '%v'\n", num, topics[0], field)
case reflect.Int16: case reflect.Int16:
num := new(big.Int).SetBytes(topics[0][:]) num := new(big.Int).SetBytes(topics[0][:])
@ -178,6 +179,13 @@ func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) er
case reflectBigInt: case reflectBigInt:
num := new(big.Int).SetBytes(topics[0][:]) num := new(big.Int).SetBytes(topics[0][:])
if arg.Type.T == abi.IntTy {
if num.Cmp(abi.MaxInt256) > 0 {
num.Add(abi.MaxUint256, big.NewInt(0).Neg(num))
num.Add(num, big.NewInt(1))
num.Neg(num)
}
}
field.Set(reflect.ValueOf(num)) field.Set(reflect.ValueOf(num))
default: default:

View file

@ -17,6 +17,7 @@
package bind package bind
import ( import (
"math/big"
"reflect" "reflect"
"testing" "testing"
@ -59,7 +60,15 @@ func TestParseTopics(t *testing.T) {
type bytesStruct struct { type bytesStruct struct {
StaticBytes [5]byte StaticBytes [5]byte
} }
type int8Struct struct {
Int8Value int8
}
type int256Struct struct {
Int256Value *big.Int
}
bytesType, _ := abi.NewType("bytes5", "", nil) bytesType, _ := abi.NewType("bytes5", "", nil)
int8Type, _ := abi.NewType("int8", "", nil)
int256Type, _ := abi.NewType("int256", "", nil)
type args struct { type args struct {
createObj func() interface{} createObj func() interface{}
resultObj func() interface{} resultObj func() interface{}
@ -87,6 +96,40 @@ func TestParseTopics(t *testing.T) {
}, },
wantErr: false, wantErr: false,
}, },
{
name: "int8 with negative value",
args: args{
createObj: func() interface{} { return &int8Struct{} },
resultObj: func() interface{} { return &int8Struct{Int8Value: -1} },
fields: abi.Arguments{abi.Argument{
Name: "int8Value",
Type: int8Type,
Indexed: true,
}},
topics: []common.Hash{
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
},
},
wantErr: false,
},
{
name: "int256 with negative value",
args: args{
createObj: func() interface{} { return &int256Struct{} },
resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} },
fields: abi.Arguments{abi.Argument{
Name: "int256Value",
Type: int256Type,
Indexed: true,
}},
topics: []common.Hash{
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
},
},
wantErr: false,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View file

@ -26,10 +26,10 @@ import (
) )
var ( var (
maxUint256 = big.NewInt(0).Add( MaxUint256 = big.NewInt(0).Add(
big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil),
big.NewInt(-1)) big.NewInt(-1))
maxInt256 = big.NewInt(0).Add( MaxInt256 = big.NewInt(0).Add(
big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil),
big.NewInt(-1)) big.NewInt(-1))
) )
@ -62,8 +62,8 @@ func readInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
return ret return ret
} }
if ret.Cmp(maxInt256) > 0 { if ret.Cmp(MaxInt256) > 0 {
ret.Add(maxUint256, big.NewInt(0).Neg(ret)) ret.Add(MaxUint256, big.NewInt(0).Neg(ret))
ret.Add(ret, big.NewInt(1)) ret.Add(ret, big.NewInt(1))
ret.Neg(ret) ret.Neg(ret)
} }