accounts/abi: fix bigInt topic encoding (#28764)

This commit is contained in:
rjl493456442 2024-01-09 21:55:09 +08:00 committed by GitHub
parent 1010a79c7c
commit d0edc5af4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

@ -24,6 +24,7 @@ import (
"reflect" "reflect"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
) )
@ -41,8 +42,7 @@ func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) {
case common.Address: case common.Address:
copy(topic[common.HashLength-common.AddressLength:], rule[:]) copy(topic[common.HashLength-common.AddressLength:], rule[:])
case *big.Int: case *big.Int:
blob := rule.Bytes() copy(topic[:], math.U256Bytes(rule))
copy(topic[common.HashLength-len(blob):], blob)
case bool: case bool:
if rule { if rule {
topic[common.HashLength-1] = 1 topic[common.HashLength-1] = 1

@ -17,6 +17,7 @@
package abi package abi
import ( import (
"math"
"math/big" "math/big"
"reflect" "reflect"
"testing" "testing"
@ -55,9 +56,27 @@ func TestMakeTopics(t *testing.T) {
false, false,
}, },
{ {
"support *big.Int types in topics", "support positive *big.Int types in topics",
args{[][]interface{}{{big.NewInt(1).Lsh(big.NewInt(2), 254)}}}, args{[][]interface{}{
[][]common.Hash{{common.Hash{128}}}, {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{
{common.MaxHash},
{common.HexToHash("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
},
false, false,
}, },
{ {