Skip to content

Commit dcd5e90

Browse files
committed
Support the --to-block flag
1 parent 524f9d9 commit dcd5e90

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

cmd/start.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ func startCommand() *cobra.Command {
1818
Run: startHandler,
1919
}
2020

21-
nodeURL, privateKey, timelockAddress, callProxyAddress string
22-
fromBlock, pollPeriod, eventListenerPollPeriod int64
23-
eventListenerPollSize uint64
24-
dryRun bool
21+
nodeURL, privateKey, timelockAddress, callProxyAddress string
22+
fromBlock, toBlock, pollPeriod, eventListenerPollPeriod int64
23+
eventListenerPollSize uint64
24+
dryRun bool
2525
)
2626

2727
// Initialize timelock-worker configuration.
@@ -41,6 +41,7 @@ func startCommand() *cobra.Command {
4141
startCmd.Flags().StringVarP(&callProxyAddress, "call-proxy-address", "f", timelockConf.CallProxyAddress, "Address of the target CallProxyAddress contract")
4242
startCmd.Flags().StringVarP(&privateKey, "private-key", "k", timelockConf.PrivateKey, "Private key used to execute transactions")
4343
startCmd.Flags().Int64Var(&fromBlock, "from-block", timelockConf.FromBlock, "Start watching from this block")
44+
startCmd.Flags().Int64Var(&toBlock, "to-block", -1, "Stop watching at this block (default: watch until the end of the chain)")
4445
startCmd.Flags().Int64Var(&pollPeriod, "poll-period", timelockConf.PollPeriod, "Poll period in seconds")
4546
startCmd.Flags().Int64Var(&eventListenerPollPeriod, "event-listener-poll-period", timelockConf.EventListenerPollPeriod, "Event Listener poll period in seconds")
4647
startCmd.Flags().Uint64Var(&eventListenerPollSize, "event-listener-poll-size", timelockConf.EventListenerPollSize, "Number of entries to fetch when polling logs")
@@ -83,6 +84,11 @@ func startTimelock(cmd *cobra.Command) {
8384
slog.Fatalf("value of from-block not set: %s", err.Error())
8485
}
8586

87+
toBlock, err := cmd.Flags().GetInt64("to-block")
88+
if err != nil {
89+
slog.Fatalf("value of to-block not set: %s", err.Error())
90+
}
91+
8692
pollPeriod, err := cmd.Flags().GetInt64("poll-period")
8793
if err != nil {
8894
slog.Fatalf("value of poll-period not set: %s", err.Error())
@@ -104,7 +110,7 @@ func startTimelock(cmd *cobra.Command) {
104110
}
105111

106112
tWorker, err := timelock.NewTimelockWorker(nodeURL, timelockAddress, callProxyAddress, privateKey,
107-
big.NewInt(fromBlock), pollPeriod, eventListenerPollPeriod, eventListenerPollSize, dryRun, slog)
113+
big.NewInt(fromBlock), big.NewInt((toBlock)), pollPeriod, eventListenerPollPeriod, eventListenerPollSize, dryRun, slog)
108114
if err != nil {
109115
slog.Fatalf("error creating the timelock-worker: %s", err.Error())
110116
}

pkg/timelock/timelock.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Worker struct {
3434
abi *abi.ABI
3535
address []common.Address
3636
fromBlock *big.Int
37+
toBlock *big.Int
3738
pollPeriod int64
3839
listenerPollPeriod int64
3940
pollSize uint64
@@ -50,7 +51,7 @@ var validNodeUrlSchemes = []string{"http", "https", "ws", "wss"}
5051
// NewTimelockWorker initializes and returns a timelockWorker.
5152
// It's a singleton, so further executions will retrieve the same timelockWorker.
5253
func NewTimelockWorker(
53-
nodeURL, timelockAddress, callProxyAddress, privateKey string, fromBlock *big.Int,
54+
nodeURL, timelockAddress, callProxyAddress, privateKey string, fromBlock *big.Int, toBlock *big.Int,
5455
pollPeriod int64, listenerPollPeriod int64, pollSize uint64, dryRun bool, logger *zap.SugaredLogger,
5556
) (*Worker, error) {
5657
// Sanity check on each provided variable before allocating more resources.
@@ -87,6 +88,10 @@ func NewTimelockWorker(
8788
return nil, fmt.Errorf("from block can't be a negative number (minimum value 0): got %d", fromBlock.Int64())
8889
}
8990

91+
if toBlock.Int64() < big.NewInt(0).Int64() {
92+
return nil, fmt.Errorf("to block can't be a negative number (minimum value 0): got %d", toBlock.Int64())
93+
}
94+
9095
if _, err := crypto.HexToECDSA(privateKey); err != nil {
9196
return nil, fmt.Errorf("the provided private key is not valid: got %s", privateKey)
9297
}
@@ -130,6 +135,7 @@ func NewTimelockWorker(
130135
abi: timelockABI,
131136
address: []common.Address{common.HexToAddress(timelockAddress)},
132137
fromBlock: fromBlock,
138+
toBlock: toBlock,
133139
pollPeriod: pollPeriod,
134140
listenerPollPeriod: listenerPollPeriod,
135141
pollSize: pollSize,
@@ -224,7 +230,7 @@ func (tw *Worker) retrieveNewLogs(ctx context.Context) (<-chan struct{}, <-chan
224230

225231
// subscribeNewLogs subscribes to a Timelock contract and emit logs through the channel it returns.
226232
func (tw *Worker) subscribeNewLogs(ctx context.Context) (<-chan struct{}, <-chan types.Log, error) {
227-
query := tw.setupFilterQuery(tw.fromBlock, nil)
233+
query := tw.setupFilterQuery(tw.fromBlock, tw.toBlock)
228234
logCh := make(chan types.Log)
229235
done := make(chan struct{})
230236

@@ -287,6 +293,7 @@ func (tw *Worker) subscribeNewLogs(ctx context.Context) (<-chan struct{}, <-chan
287293
// pollNewLogs periodically retrieves logs from the Timelock and emit them through the channel it returns.
288294
func (tw *Worker) pollNewLogs(ctx context.Context) (<-chan struct{}, <-chan types.Log, error) {
289295
lastBlock := tw.fromBlock
296+
toBlock := tw.toBlock
290297
logCh := make(chan types.Log)
291298
done := make(chan struct{})
292299

@@ -299,7 +306,7 @@ func (tw *Worker) pollNewLogs(ctx context.Context) (<-chan struct{}, <-chan type
299306
defer ticker.Stop()
300307

301308
for {
302-
lastBlock = tw.fetchAndDispatchLogs(ctx, logCh, lastBlock, nil)
309+
lastBlock = tw.fetchAndDispatchLogs(ctx, logCh, lastBlock, toBlock)
303310

304311
select {
305312
case <-ticker.C:
@@ -319,7 +326,7 @@ func (tw *Worker) pollNewLogs(ctx context.Context) (<-chan struct{}, <-chan type
319326
// retrieveHistoricalLogs returns a types.Log channel and retrieves all the historical events of a given contract.
320327
// Once all the logs have been sent into the channel the function returns and the channel is closed.
321328
func (tw *Worker) retrieveHistoricalLogs(ctx context.Context) (<-chan struct{}, <-chan types.Log, error) {
322-
query := tw.setupFilterQuery(tw.fromBlock, nil)
329+
query := tw.setupFilterQuery(tw.fromBlock, tw.toBlock)
323330
logCh := make(chan types.Log)
324331
done := make(chan struct{})
325332

@@ -585,6 +592,7 @@ func (tw *Worker) startLog() {
585592

586593
tw.logger.Infof("\tEOA address: %v", wallet)
587594
tw.logger.Infof("\tStarting from block: %v", tw.fromBlock)
595+
tw.logger.Infof("\tEnding at block: %v", tw.toBlock)
588596
tw.logger.Infof("\tPoll Period: %v", time.Duration(tw.pollPeriod*int64(time.Second)).String())
589597
tw.logger.Infof("\tEvent Listener Poll Period: %v", time.Duration(tw.listenerPollPeriod*int64(time.Second)).String())
590598
tw.logger.Infof("\tEvent Listener Poll # Logs%v", tw.pollSize)

0 commit comments

Comments
 (0)