Skip to content

Commit 698a47a

Browse files
committed
feat(evmreader): RetrieveInputs in chunks
1 parent 9cd2954 commit 698a47a

1 file changed

Lines changed: 61 additions & 8 deletions

File tree

internal/evmreader/inputsource_adapter.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"math/big"
88

99
"github.com/cartesi/rollups-node/pkg/contracts/iinputbox"
10+
"github.com/cartesi/rollups-node/pkg/ethutil"
11+
"github.com/ethereum/go-ethereum"
12+
"github.com/ethereum/go-ethereum/accounts/abi"
1013
"github.com/ethereum/go-ethereum/accounts/abi/bind"
1114
"github.com/ethereum/go-ethereum/common"
1215
"github.com/ethereum/go-ethereum/ethclient"
@@ -15,6 +18,8 @@ import (
1518
// InputBox Wrapper
1619
type InputSourceAdapter struct {
1720
inputbox *iinputbox.IInputBox
21+
client *ethclient.Client
22+
inputBoxAddress common.Address
1823
}
1924

2025
func NewInputSourceAdapter(
@@ -27,28 +32,76 @@ func NewInputSourceAdapter(
2732
}
2833
return &InputSourceAdapter{
2934
inputbox: inputbox,
35+
client: client,
36+
inputBoxAddress: inputBoxAddress,
3037
}, nil
3138
}
3239

40+
func buildInputAddedFilterQuery(
41+
opts *bind.FilterOpts,
42+
inputBoxAddress common.Address,
43+
appContract []common.Address,
44+
index []*big.Int,
45+
) (q ethereum.FilterQuery, err error) {
46+
c, err := iinputbox.IInputBoxMetaData.GetAbi()
47+
if err != nil {
48+
return q, err
49+
}
50+
51+
var appContractRule []interface{}
52+
for _, appContractItem := range appContract {
53+
appContractRule = append(appContractRule, appContractItem)
54+
}
55+
var indexRule []interface{}
56+
for _, indexItem := range index {
57+
indexRule = append(indexRule, indexItem)
58+
}
59+
60+
topics, err := abi.MakeTopics(
61+
[]interface{}{c.Events["InputAdded"].ID},
62+
appContractRule,
63+
indexRule,
64+
)
65+
if err != nil {
66+
return q, err
67+
}
68+
69+
q = ethereum.FilterQuery{
70+
Addresses: []common.Address{inputBoxAddress},
71+
FromBlock: new(big.Int).SetUint64(opts.Start),
72+
Topics: topics,
73+
}
74+
if opts.End != nil {
75+
q.ToBlock = new(big.Int).SetUint64(*opts.End)
76+
}
77+
return q, err
78+
}
79+
3380
func (i *InputSourceAdapter) RetrieveInputs(
3481
opts *bind.FilterOpts,
3582
appContract []common.Address,
3683
index []*big.Int,
3784
) ([]iinputbox.IInputBoxInputAdded, error) {
85+
q, err := buildInputAddedFilterQuery(opts, i.inputBoxAddress, appContract, index)
86+
if err != nil {
87+
return nil, err
88+
}
3889

39-
itr, err := i.inputbox.FilterInputAdded(opts, appContract, index)
90+
itr, err := ethutil.ChunkedFilterLogs(opts.Context, i.client, q)
4091
if err != nil {
4192
return nil, err
4293
}
43-
defer itr.Close()
4494

4595
var events []iinputbox.IInputBoxInputAdded
46-
for itr.Next() {
47-
inputAddedEvent := itr.Event
48-
events = append(events, *inputAddedEvent)
49-
}
50-
if err = itr.Error(); err != nil {
51-
return nil, err
96+
for log, err := range itr {
97+
if err != nil {
98+
return nil, err
99+
}
100+
ev, err := i.inputbox.ParseInputAdded(*log)
101+
if err != nil {
102+
return nil, err
103+
}
104+
events = append(events, *ev)
52105
}
53106
return events, nil
54107
}

0 commit comments

Comments
 (0)