diff --git a/accounts/abi/topics.go b/accounts/abi/topics.go index ef99de0b9e..1c70fd3bf5 100644 --- a/accounts/abi/topics.go +++ b/accounts/abi/topics.go @@ -34,6 +34,7 @@ import ( "reflect" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/crypto" ) @@ -50,8 +51,7 @@ func packTopic(rule interface{}) (common.Hash, error) { case common.Address: copy(topic[common.HashLength-common.AddressLength:], rule[:]) case *big.Int: - blob := rule.Bytes() - copy(topic[common.HashLength-len(blob):], blob) + copy(topic[:], math.U256Bytes(rule)) case bool: if rule { topic[common.HashLength-1] = 1 diff --git a/accounts/abi/topics_test.go b/accounts/abi/topics_test.go index 77e9020f4e..6a7d3f04ac 100644 --- a/accounts/abi/topics_test.go +++ b/accounts/abi/topics_test.go @@ -27,6 +27,7 @@ package abi import ( + "math" "math/big" "reflect" "testing" @@ -35,6 +36,8 @@ import ( "github.com/ethereum/go-ethereum/crypto" ) +var MaxHash = common.HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") + func TestMakeTopics(t *testing.T) { type args struct { query [][]interface{} @@ -64,9 +67,27 @@ func TestMakeTopics(t *testing.T) { false, }, { - "support *big.Int types in topics", - args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}}, - [][]common.Hash{{common.Hash{128}}}, + "support positive *big.Int types in topics", + args{[][]interface{}{ + {big.NewInt(1)}, + {big.NewInt(1).Lsh(big.NewInt(2), 254)}, + }}, + [][]common.Hash{ + {common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001")}, + {common.Hash{128}}, + }, + false, + }, + { + "support negative *big.Int types in topics", + args{[][]interface{}{ + {big.NewInt(-1)}, + {big.NewInt(math.MinInt64)}, + }}, + [][]common.Hash{ + {MaxHash}, + {common.HexToHash("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, + }, false, }, {