Skip to content

Commit 6ae9dc1

Browse files
shoenseiwasogballet
authored andcommitted
[#20266] Fix bugs decoding integers and fixed bytes in indexed event fields (#20269)
* fix parseTopics() and add tests * remove printf * add ParseTopicsIntoMap() tests * fix FixedBytesTy * fix int and fixed bytes * golint topics_test.go
1 parent 49cf000 commit 6ae9dc1

3 files changed

Lines changed: 122 additions & 28 deletions

File tree

‎accounts/abi/bind/topics.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) er
178178

179179
case reflectBigInt:
180180
num := new(big.Int).SetBytes(topics[0][:])
181+
if arg.Type.T == abi.IntTy {
182+
if num.Cmp(abi.MaxInt256) > 0 {
183+
num.Add(abi.MaxUint256, big.NewInt(0).Neg(num))
184+
num.Add(num, big.NewInt(1))
185+
num.Neg(num)
186+
}
187+
}
181188
field.Set(reflect.ValueOf(num))
182189

183190
default:
@@ -212,16 +219,19 @@ func parseTopicsIntoMap(out map[string]interface{}, fields abi.Arguments, topics
212219
case abi.BoolTy:
213220
out[arg.Name] = topics[0][common.HashLength-1] == 1
214221
case abi.IntTy, abi.UintTy:
215-
num := new(big.Int).SetBytes(topics[0][:])
216-
out[arg.Name] = num
222+
out[arg.Name] = abi.ReadInteger(arg.Type.T, arg.Type.Kind, topics[0].Bytes())
217223
case abi.AddressTy:
218224
var addr common.Address
219225
copy(addr[:], topics[0][common.HashLength-common.AddressLength:])
220226
out[arg.Name] = addr
221227
case abi.HashTy:
222228
out[arg.Name] = topics[0]
223229
case abi.FixedBytesTy:
224-
out[arg.Name] = topics[0][:]
230+
array, err := abi.ReadFixedBytes(arg.Type, topics[0].Bytes())
231+
if err != nil {
232+
return err
233+
}
234+
out[arg.Name] = array
225235
case abi.StringTy, abi.BytesTy, abi.SliceTy, abi.ArrayTy:
226236
// Array types (including strings and bytes) have their keccak256 hashes stored in the topic- not a hash
227237
// whose bytes can be decoded to the actual value- so the best we can do is retrieve that hash

‎accounts/abi/bind/topics_test.go‎

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package bind
1818

1919
import (
20+
"math/big"
2021
"reflect"
2122
"testing"
2223

@@ -55,27 +56,44 @@ func TestMakeTopics(t *testing.T) {
5556
}
5657
}
5758

58-
func TestParseTopics(t *testing.T) {
59-
type bytesStruct struct {
60-
StaticBytes [5]byte
61-
}
59+
type args struct {
60+
createObj func() interface{}
61+
resultObj func() interface{}
62+
resultMap func() map[string]interface{}
63+
fields abi.Arguments
64+
topics []common.Hash
65+
}
66+
67+
type bytesStruct struct {
68+
StaticBytes [5]byte
69+
}
70+
type int8Struct struct {
71+
Int8Value int8
72+
}
73+
type int256Struct struct {
74+
Int256Value *big.Int
75+
}
76+
77+
type topicTest struct {
78+
name string
79+
args args
80+
wantErr bool
81+
}
82+
83+
func setupTopicsTests() []topicTest {
6284
bytesType, _ := abi.NewType("bytes5", "", nil)
63-
type args struct {
64-
createObj func() interface{}
65-
resultObj func() interface{}
66-
fields abi.Arguments
67-
topics []common.Hash
68-
}
69-
tests := []struct {
70-
name string
71-
args args
72-
wantErr bool
73-
}{
85+
int8Type, _ := abi.NewType("int8", "", nil)
86+
int256Type, _ := abi.NewType("int256", "", nil)
87+
88+
tests := []topicTest{
7489
{
7590
name: "support fixed byte types, right padded to 32 bytes",
7691
args: args{
7792
createObj: func() interface{} { return &bytesStruct{} },
7893
resultObj: func() interface{} { return &bytesStruct{StaticBytes: [5]byte{1, 2, 3, 4, 5}} },
94+
resultMap: func() map[string]interface{} {
95+
return map[string]interface{}{"staticBytes": [5]byte{1, 2, 3, 4, 5}}
96+
},
7997
fields: abi.Arguments{abi.Argument{
8098
Name: "staticBytes",
8199
Type: bytesType,
@@ -87,7 +105,54 @@ func TestParseTopics(t *testing.T) {
87105
},
88106
wantErr: false,
89107
},
108+
{
109+
name: "int8 with negative value",
110+
args: args{
111+
createObj: func() interface{} { return &int8Struct{} },
112+
resultObj: func() interface{} { return &int8Struct{Int8Value: -1} },
113+
resultMap: func() map[string]interface{} {
114+
return map[string]interface{}{"int8Value": int8(-1)}
115+
},
116+
fields: abi.Arguments{abi.Argument{
117+
Name: "int8Value",
118+
Type: int8Type,
119+
Indexed: true,
120+
}},
121+
topics: []common.Hash{
122+
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
123+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
124+
},
125+
},
126+
wantErr: false,
127+
},
128+
{
129+
name: "int256 with negative value",
130+
args: args{
131+
createObj: func() interface{} { return &int256Struct{} },
132+
resultObj: func() interface{} { return &int256Struct{Int256Value: big.NewInt(-1)} },
133+
resultMap: func() map[string]interface{} {
134+
return map[string]interface{}{"int256Value": big.NewInt(-1)}
135+
},
136+
fields: abi.Arguments{abi.Argument{
137+
Name: "int256Value",
138+
Type: int256Type,
139+
Indexed: true,
140+
}},
141+
topics: []common.Hash{
142+
{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
143+
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
144+
},
145+
},
146+
wantErr: false,
147+
},
90148
}
149+
150+
return tests
151+
}
152+
153+
func TestParseTopics(t *testing.T) {
154+
tests := setupTopicsTests()
155+
91156
for _, tt := range tests {
92157
t.Run(tt.name, func(t *testing.T) {
93158
createObj := tt.args.createObj()
@@ -101,3 +166,20 @@ func TestParseTopics(t *testing.T) {
101166
})
102167
}
103168
}
169+
170+
func TestParseTopicsIntoMap(t *testing.T) {
171+
tests := setupTopicsTests()
172+
173+
for _, tt := range tests {
174+
t.Run(tt.name, func(t *testing.T) {
175+
outMap := make(map[string]interface{})
176+
if err := parseTopicsIntoMap(outMap, tt.args.fields, tt.args.topics); (err != nil) != tt.wantErr {
177+
t.Errorf("parseTopicsIntoMap() error = %v, wantErr %v", err, tt.wantErr)
178+
}
179+
resultMap := tt.args.resultMap()
180+
if !reflect.DeepEqual(outMap, resultMap) {
181+
t.Errorf("parseTopicsIntoMap() = %v, want %v", outMap, resultMap)
182+
}
183+
})
184+
}
185+
}

‎accounts/abi/unpack.go‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ import (
2626
)
2727

2828
var (
29-
maxUint256 = big.NewInt(0).Add(
29+
// MaxUint256 is the maximum value that can be represented by a uint256
30+
MaxUint256 = big.NewInt(0).Add(
3031
big.NewInt(0).Exp(big.NewInt(2), big.NewInt(256), nil),
3132
big.NewInt(-1))
32-
maxInt256 = big.NewInt(0).Add(
33+
// MaxInt256 is the maximum value that can be represented by a int256
34+
MaxInt256 = big.NewInt(0).Add(
3335
big.NewInt(0).Exp(big.NewInt(2), big.NewInt(255), nil),
3436
big.NewInt(-1))
3537
)
3638

37-
// reads the integer based on its kind
38-
func readInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
39+
// ReadInteger reads the integer based on its kind and returns the appropriate value
40+
func ReadInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
3941
switch kind {
4042
case reflect.Uint8:
4143
return b[len(b)-1]
@@ -62,8 +64,8 @@ func readInteger(typ byte, kind reflect.Kind, b []byte) interface{} {
6264
return ret
6365
}
6466

65-
if ret.Cmp(maxInt256) > 0 {
66-
ret.Add(maxUint256, big.NewInt(0).Neg(ret))
67+
if ret.Cmp(MaxInt256) > 0 {
68+
ret.Add(MaxUint256, big.NewInt(0).Neg(ret))
6769
ret.Add(ret, big.NewInt(1))
6870
ret.Neg(ret)
6971
}
@@ -102,8 +104,8 @@ func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
102104
return
103105
}
104106

105-
// through reflection, creates a fixed array to be read from
106-
func readFixedBytes(t Type, word []byte) (interface{}, error) {
107+
// ReadFixedBytes uses reflection to create a fixed array to be read from
108+
func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
107109
if t.T != FixedBytesTy {
108110
return nil, fmt.Errorf("abi: invalid type in call to make fixed byte array")
109111
}
@@ -230,7 +232,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
230232
case StringTy: // variable arrays are written at the end of the return bytes
231233
return string(output[begin : begin+length]), nil
232234
case IntTy, UintTy:
233-
return readInteger(t.T, t.Kind, returnOutput), nil
235+
return ReadInteger(t.T, t.Kind, returnOutput), nil
234236
case BoolTy:
235237
return readBool(returnOutput)
236238
case AddressTy:
@@ -240,7 +242,7 @@ func toGoType(index int, t Type, output []byte) (interface{}, error) {
240242
case BytesTy:
241243
return output[begin : begin+length], nil
242244
case FixedBytesTy:
243-
return readFixedBytes(t, returnOutput)
245+
return ReadFixedBytes(t, returnOutput)
244246
case FunctionTy:
245247
return readFunctionType(t, returnOutput)
246248
default:

0 commit comments

Comments
 (0)