Skip to content

Commit d0edc5a

Browse files
authored
accounts/abi: fix bigInt topic encoding (#28764)
1 parent 1010a79 commit d0edc5a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

accounts/abi/topics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"reflect"
2525

2626
"github.com/ethereum/go-ethereum/common"
27+
"github.com/ethereum/go-ethereum/common/math"
2728
"github.com/ethereum/go-ethereum/crypto"
2829
)
2930

@@ -41,8 +42,7 @@ func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) {
4142
case common.Address:
4243
copy(topic[common.HashLength-common.AddressLength:], rule[:])
4344
case *big.Int:
44-
blob := rule.Bytes()
45-
copy(topic[common.HashLength-len(blob):], blob)
45+
copy(topic[:], math.U256Bytes(rule))
4646
case bool:
4747
if rule {
4848
topic[common.HashLength-1] = 1

accounts/abi/topics_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package abi
1818

1919
import (
20+
"math"
2021
"math/big"
2122
"reflect"
2223
"testing"
@@ -55,9 +56,27 @@ func TestMakeTopics(t *testing.T) {
5556
false,
5657
},
5758
{
58-
"support *big.Int types in topics",
59-
args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}},
60-
[][]common.Hash{{common.Hash{128}}},
59+
"support positive *big.Int types in topics",
60+
args{[][]interface{}{
61+
{big.NewInt(1)},
62+
{big.NewInt(1).Lsh(big.NewInt(2), 254)},
63+
}},
64+
[][]common.Hash{
65+
{common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001")},
66+
{common.Hash{128}},
67+
},
68+
false,
69+
},
70+
{
71+
"support negative *big.Int types in topics",
72+
args{[][]interface{}{
73+
{big.NewInt(-1)},
74+
{big.NewInt(math.MinInt64)},
75+
}},
76+
[][]common.Hash{
77+
{common.MaxHash},
78+
{common.HexToHash("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
79+
},
6180
false,
6281
},
6382
{

0 commit comments

Comments
 (0)