Skip to content

Commit fe1d82e

Browse files
committed
accounts/abi/bind: fixed erroneous filtering of negative ints (ethereum#20865)
* accounts/abi/bind: fixed erroneous packing of negative ints * accounts/abi/bind: added test cases for negative ints in topics * accounts/abi/bind: fixed genIntType for go 1.12 * accounts/abi: minor nitpick
1 parent 354f420 commit fe1d82e

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

accounts/abi/bind/topics.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,13 @@ func makeTopics(query ...[]interface{}) ([][]common.Hash, error) {
4949
topic[common.HashLength-1] = 1
5050
}
5151
case int8:
52-
blob := big.NewInt(int64(rule)).Bytes()
53-
copy(topic[common.HashLength-len(blob):], blob)
52+
copy(topic[:], genIntType(int64(rule), 1))
5453
case int16:
55-
blob := big.NewInt(int64(rule)).Bytes()
56-
copy(topic[common.HashLength-len(blob):], blob)
54+
copy(topic[:], genIntType(int64(rule), 2))
5755
case int32:
58-
blob := big.NewInt(int64(rule)).Bytes()
59-
copy(topic[common.HashLength-len(blob):], blob)
56+
copy(topic[:], genIntType(int64(rule), 4))
6057
case int64:
61-
blob := big.NewInt(rule).Bytes()
62-
copy(topic[common.HashLength-len(blob):], blob)
58+
copy(topic[:], genIntType(rule, 8))
6359
case uint8:
6460
blob := new(big.Int).SetUint64(uint64(rule)).Bytes()
6561
copy(topic[common.HashLength-len(blob):], blob)
@@ -103,6 +99,19 @@ func makeTopics(query ...[]interface{}) ([][]common.Hash, error) {
10399
return topics, nil
104100
}
105101

102+
func genIntType(rule int64, size uint) []byte {
103+
var topic [common.HashLength]byte
104+
if rule < 0 {
105+
// if a rule is negative, we need to put it into two's complement.
106+
// extended to common.Hashlength bytes.
107+
topic = [common.HashLength]byte{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}
108+
}
109+
for i := uint(0); i < size; i++ {
110+
topic[common.HashLength-i-1] = byte(rule >> (i * 8))
111+
}
112+
return topic[:]
113+
}
114+
106115
// parseTopics converts the indexed topic fields into actual log field values.
107116
func parseTopics(out interface{}, fields abi.Arguments, topics []common.Hash) error {
108117
return parseTopicWithSetter(fields, topics,

accounts/abi/bind/topics_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/XinFinOrg/XDPoSChain/accounts/abi"
2525
"github.com/XinFinOrg/XDPoSChain/common"
26+
"github.com/XinFinOrg/XDPoSChain/crypto"
2627
)
2728

2829
func TestMakeTopics(t *testing.T) {
@@ -41,6 +42,80 @@ func TestMakeTopics(t *testing.T) {
4142
[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
4243
false,
4344
},
45+
{
46+
"support common hash types in topics",
47+
args{[][]interface{}{{common.Hash{1, 2, 3, 4, 5}}}},
48+
[][]common.Hash{{common.Hash{1, 2, 3, 4, 5}}},
49+
false,
50+
},
51+
{
52+
"support address types in topics",
53+
args{[][]interface{}{{common.Address{1, 2, 3, 4, 5}}}},
54+
[][]common.Hash{{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5}}},
55+
false,
56+
},
57+
{
58+
"support *big.Int types in topics",
59+
args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}},
60+
[][]common.Hash{{common.Hash{128}}},
61+
false,
62+
},
63+
{
64+
"support boolean types in topics",
65+
args{[][]interface{}{
66+
{true},
67+
{false},
68+
}},
69+
[][]common.Hash{
70+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}},
71+
{common.Hash{0}},
72+
},
73+
false,
74+
},
75+
{
76+
"support int/uint(8/16/32/64) types in topics",
77+
args{[][]interface{}{
78+
{int8(-2)},
79+
{int16(-3)},
80+
{int32(-4)},
81+
{int64(-5)},
82+
{int8(1)},
83+
{int16(256)},
84+
{int32(65536)},
85+
{int64(4294967296)},
86+
{uint8(1)},
87+
{uint16(256)},
88+
{uint32(65536)},
89+
{uint64(4294967296)},
90+
}},
91+
[][]common.Hash{
92+
{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, 254}},
93+
{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, 253}},
94+
{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, 252}},
95+
{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, 251}},
96+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}},
97+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}},
98+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
99+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}},
100+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}},
101+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}},
102+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0}},
103+
{common.Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}},
104+
},
105+
false,
106+
},
107+
{
108+
"support string types in topics",
109+
args{[][]interface{}{{"hello world"}}},
110+
[][]common.Hash{{crypto.Keccak256Hash([]byte("hello world"))}},
111+
false,
112+
},
113+
{
114+
"support byte slice types in topics",
115+
args{[][]interface{}{{[]byte{1, 2, 3}}}},
116+
[][]common.Hash{{crypto.Keccak256Hash([]byte{1, 2, 3})}},
117+
false,
118+
},
44119
}
45120
for _, tt := range tests {
46121
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)