Skip to content

Commit

Permalink
fix negative bigint topics
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur committed Jan 10, 2024
1 parent e5a0517 commit 4326b62
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions accounts/abi/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
27 changes: 24 additions & 3 deletions accounts/abi/topics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package abi

import (
"math"
"math/big"
"reflect"
"testing"
Expand All @@ -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{}
Expand Down Expand Up @@ -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,
},
{
Expand Down

0 comments on commit 4326b62

Please sign in to comment.