Skip to content

Commit 62c846d

Browse files
authored
Merge pull request #46 from rsksmart/fix-toBlock-logic
Fixes toBlock logic to only setup this.toBlock when params.toBlock wa…
2 parents 33a57b7 + fde953e commit 62c846d

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ You can also specify a `toBlock` parameter.
152152
153153
If `toBlock` param is not provided, then the tool will continue to synch with new blocks.
154154

155+
If `toBlock` param is provided and the monitor reaches that block number, then the monitor will emit the `toBlockReached` event and stop.
156+
155157
### Samples with the exported `monitor` function
156158

157159
```js

tool/live-monitor/live-monitor-utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const MONITOR_EVENTS = {
88
stopped: 'stopped',
99
started: 'started',
1010
reset: 'reset',
11+
toBlockReached: 'toBlockReached',
1112
};
1213

1314
const defaultParamsValues = {

tool/live-monitor/live-monitor.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ class LiveMonitor extends EventEmitter {
5454
}
5555

5656
try {
57+
5758
attempts++;
59+
60+
this.latestBlockNumber = await this.rskClient.eth.getBlockNumber();
61+
5862
if(this.latestBlockNumber < this.currentBlockNumber) {
5963
if(!this.notified) {
6064
this.emit(MONITOR_EVENTS.latestBlockReached, 'Latest block reached');
@@ -123,40 +127,33 @@ class LiveMonitor extends EventEmitter {
123127
}
124128

125129
if(this.toBlock && this.toBlock !== -1 && this.currentBlockNumber >= this.toBlock) {
126-
this.emit(MONITOR_EVENTS.latestBlockReached, 'To block number reached. Exiting...');
127-
this.isStarted = false;
130+
this.emit(MONITOR_EVENTS.toBlockReached, 'To block number reached. Stopping monitor...');
131+
this.stop();
128132
return;
129133
}
130134

131135
this.currentBlockNumber++;
132136

133137
attempts = 0;
134138

135-
if(this.isStarted) {
136-
this.timer = setTimeout(() => {
137-
if(this.isStarted) {
138-
this.check();
139-
}
140-
}, this.checkEveryMilliseconds);
141-
}
142-
143139
} catch(error) {
144-
if(this.params.retryOnError && attempts < this.params.retryOnErrorAttempts) {
140+
const shouldRetryToProcessSameBlock = this.params.retryOnError && attempts < this.params.retryOnErrorAttempts;
141+
if(shouldRetryToProcessSameBlock) {
145142
console.error(`There was an error trying to get the tx data/events in block: ${this.currentBlockNumber}. Attempt ${attempts} of ${this.params.retryOnErrorAttempts}.`);
146-
if(this.isStarted) {
147-
this.timer = setTimeout(() => {
148-
if(this.isStarted) {
149-
this.check();
150-
}
151-
}, this.checkEveryMilliseconds);
152-
}
153143
} else {
154144
const errorMessages = `There was an error trying to get the tx data/events in block: ${this.currentBlockNumber}`;
155145
this.emit(MONITOR_EVENTS.error, `${errorMessages}: ${error.message}\nMoving forward with the next block ${this.currentBlockNumber + 1}...`);
156146
console.error(errorMessages, error);
157147
this.currentBlockNumber++;
158148
}
159-
149+
} finally {
150+
if(this.isStarted) {
151+
this.timer = setTimeout(() => {
152+
if(this.isStarted) {
153+
this.check();
154+
}
155+
}, this.params.checkEveryMilliseconds);
156+
}
160157
}
161158
}
162159

@@ -208,12 +205,14 @@ class LiveMonitor extends EventEmitter {
208205
this.currentBlockNumber = parseInt(this.currentBlockNumber);
209206
}
210207

211-
if(this.params.toBlock === 'latest') {
212-
this.toBlock = this.latestBlockNumber;
213-
} else {
214-
this.toBlock = Number(this.params.toBlock);
208+
if(this.params.toBlock) {
209+
if(this.params.toBlock === 'latest') {
210+
this.toBlock = this.latestBlockNumber;
211+
} else {
212+
this.toBlock = Number(this.params.toBlock);
213+
}
215214
}
216-
215+
217216
if(this.currentBlockNumber < 0) {
218217
// If the block number is negative, it will be interpreted as the number of blocks before the latest block
219218
this.currentBlockNumber = this.latestBlockNumber + this.currentBlockNumber;

0 commit comments

Comments
 (0)