Skip to content

Commit 2663fcf

Browse files
committed
feat(evmreader): RetrieveOutputExecutionEvents in chunks
1 parent 698a47a commit 2663fcf

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

internal/evmreader/application_adapter.go

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
package evmreader
55

66
import (
7+
"math/big"
8+
79
appcontract "github.com/cartesi/rollups-node/pkg/contracts/iapplication"
10+
"github.com/cartesi/rollups-node/pkg/ethutil"
11+
"github.com/ethereum/go-ethereum"
12+
"github.com/ethereum/go-ethereum/accounts/abi"
813
"github.com/ethereum/go-ethereum/accounts/abi/bind"
914
"github.com/ethereum/go-ethereum/common"
1015
"github.com/ethereum/go-ethereum/ethclient"
@@ -13,6 +18,8 @@ import (
1318
// IConsensus Wrapper
1419
type ApplicationContractAdapter struct {
1520
application *appcontract.IApplication
21+
client *ethclient.Client
22+
applicationAddress common.Address
1623
}
1724

1825
func NewApplicationContractAdapter(
@@ -25,30 +32,65 @@ func NewApplicationContractAdapter(
2532
}
2633
return &ApplicationContractAdapter{
2734
application: applicationContract,
35+
applicationAddress: appAddress,
36+
client: client,
2837
}, nil
2938
}
3039

3140
func (a *ApplicationContractAdapter) GetConsensus(opts *bind.CallOpts) (common.Address, error) {
3241
return a.application.GetConsensus(opts)
3342
}
3443

44+
func buildOutputExecutedFilterQuery(
45+
opts *bind.FilterOpts,
46+
applicationAddress common.Address,
47+
) (q ethereum.FilterQuery, err error) {
48+
c, err := appcontract.IApplicationMetaData.GetAbi()
49+
if err != nil {
50+
return q, err
51+
}
52+
53+
topics, err := abi.MakeTopics(
54+
[]interface{}{c.Events["OutputExecuted"].ID},
55+
)
56+
if err != nil {
57+
return q, err
58+
}
59+
60+
q = ethereum.FilterQuery{
61+
Addresses: []common.Address{applicationAddress},
62+
FromBlock: new(big.Int).SetUint64(opts.Start),
63+
Topics: topics,
64+
}
65+
if opts.End != nil {
66+
q.ToBlock = new(big.Int).SetUint64(*opts.End)
67+
}
68+
return q, err
69+
}
70+
3571
func (a *ApplicationContractAdapter) RetrieveOutputExecutionEvents(
3672
opts *bind.FilterOpts,
3773
) ([]*appcontract.IApplicationOutputExecuted, error) {
74+
q, err := buildOutputExecutedFilterQuery(opts, a.applicationAddress)
75+
if err != nil {
76+
return nil, err
77+
}
3878

39-
itr, err := a.application.FilterOutputExecuted(opts)
79+
itr, err := ethutil.ChunkedFilterLogs(opts.Context, a.client, q)
4080
if err != nil {
4181
return nil, err
4282
}
43-
defer itr.Close()
4483

4584
var events []*appcontract.IApplicationOutputExecuted
46-
for itr.Next() {
47-
outputExecutedEvent := itr.Event
48-
events = append(events, outputExecutedEvent)
49-
}
50-
if err = itr.Error(); err != nil {
51-
return nil, err
85+
for log, err := range itr {
86+
if err != nil {
87+
return nil, err
88+
}
89+
ev, err := a.application.ParseOutputExecuted(*log)
90+
if err != nil {
91+
return nil, err
92+
}
93+
events = append(events, ev)
5294
}
5395
return events, nil
5496
}

internal/evmreader/output.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func (r *Service) readAndUpdateOutputs(
6464
contract := app.applicationContract
6565

6666
opts := &bind.FilterOpts{
67+
Context: ctx,
6768
Start: lastOutputCheck + 1,
6869
End: &mostRecentBlockNumber,
6970
}

0 commit comments

Comments
 (0)