From dc7cd7e4b5a8bd3cf43cfecd153170d8338d08b6 Mon Sep 17 00:00:00 2001 From: Jeff Wentworth Date: Tue, 12 Nov 2019 20:41:41 +0900 Subject: [PATCH] fix parseTopics() and add tests --- accounts/abi/bind/topics.go | 8 ++++++ accounts/abi/bind/topics_test.go | 43 ++++++++++++++++++++++++++++++++ accounts/abi/unpack.go | 8 +++--- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/accounts/abi/bind/topics.go b/accounts/abi/bind/topics.go index e27fa54842..ea71a936f3 100644 --- a/accounts/abi/bind/topics.go +++ b/accounts/abi/bind/topics.go @@ -135,6 +135,7 @@ func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) er case reflect.Int8: num := new(big.Int).SetBytes(topics[0][:]) 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: num := new(big.Int).SetBytes(topics[0][:]) @@ -178,6 +179,13 @@ func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) er case reflectBigInt: 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)) default: diff --git a/accounts/abi/bind/topics_test.go b/accounts/abi/bind/topics_test.go index f18e2d1bd2..f178574381 100644 --- a/accounts/abi/bind/topics_test.go +++ b/accounts/abi/bind/topics_test.go @@ -17,6 +17,7 @@ package bind import ( + "math/big" "reflect" "testing" @@ -59,7 +60,15 @@ func TestParseTopics(t *testing.T) { type bytesStruct struct { StaticBytes [5]byte } + type int8Struct struct { + Int8Value int8 + } + type int256Struct struct { + Int256Value *big.Int + } bytesType, _ := abi.NewType("bytes5", "", nil) + int8Type, _ := abi.NewType("int8", "", nil) + int256Type, _ := abi.NewType("int256", "", nil) type args struct { createObj func() interface{} resultObj func() interface{} @@ -87,6 +96,40 @@ func TestParseTopics(t *testing.T) { }, 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 { t.Run(tt.name, func(t *testing.T) { diff --git a/accounts/abi/unpack.go b/accounts/abi/unpack.go index b2e61d06c4..918905c7fb 100644 --- a/accounts/abi/unpack.go +++ b/accounts/abi/unpack.go @@ -26,10 +26,10 @@ import ( ) var ( - maxUint256 = big.NewInt(0).Add( + MaxUint256 = big.NewInt(0).Add( big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil), 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(-1)) ) @@ -62,8 +62,8 @@ func readInteger(typ byte, kind reflect.Kind, b []byte) interface{} { return ret } - if ret.Cmp(maxInt256) > 0 { - ret.Add(maxUint256, big.NewInt(0).Neg(ret)) + if ret.Cmp(MaxInt256) > 0 { + ret.Add(MaxUint256, big.NewInt(0).Neg(ret)) ret.Add(ret, big.NewInt(1)) ret.Neg(ret) }