Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Create simple block watcher without blockstream #27

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ develop-eggs/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
Expand Down
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ethers = require("ethers");
const BlockWatcher = require("./lib/blockWatcher");
const RoundInitializer = require("./lib/roundInitializer");
const TxSigner = require("./lib/txSigner");
const SimpleBlockWatcher = require("./lib/simpleBlockWatcher");

const ROUNDS_MANAGER_ADDRESSES = {
1: ["0x3984fc4ceeef1739135476f625d36d6c35c40dc3"],
Expand All @@ -23,9 +24,9 @@ const ROUNDS_MANAGER_ADDRESSES = {

const argv = require("yargs")
.usage(
"Usage: $0 --rinkeby --provider [provider URL] --account [Ethereum account] --password [Ethereum account password] --datadir [data directory] --roundsManagerAddr [RoundsManager addresses]"
"Usage: $0 --rinkeby --provider [provider URL] --account [Ethereum account] --password [Ethereum account password] --datadir [data directory] --roundsManagerAddr [RoundsManager addresses] --pollingInterval [block polling interval in ms] --noBlockstream"
)
.boolean(["rinkeby"])
.boolean(["rinkeby", "noBlockstream"])
.string(["provider", "account", "roundsManagerAddr", "password"])
.default("roundsManagerAddr", "")
.demandOption(["account", "datadir"])
Expand Down Expand Up @@ -58,7 +59,9 @@ const run = async () => {
roundsManagerAddrs = ROUNDS_MANAGER_ADDRESSES[network.chainId];
}

const blockWatcher = new BlockWatcher(provider);
const blockWatcher = argv.noBlockstream
? new SimpleBlockWatcher(provider, argv.pollingInterval)
: new BlockWatcher(provider, argv.pollingInterval);
const roundInitializer = new RoundInitializer(
provider,
roundsManagerAddrs,
Expand Down
2 changes: 1 addition & 1 deletion lib/blockWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = class BlockWatcher {
pollingInterval
) {
this.provider = provider
this.pollingInterval = pollingInterval === undefined ? DEFAULT_POLLING_INTERVAL_MS : pollingInterval
this.pollingInterval = pollingInterval || DEFAULT_POLLING_INTERVAL_MS
this.blockAndLogStreamer = undefined
this.onBlockAddedSubscriptionToken = undefined
this.blockAndLogStreamerIntervalId = undefined
Expand Down
64 changes: 64 additions & 0 deletions lib/simpleBlockWatcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const DEFAULT_POLLING_INTERVAL_MS = 13000

/**
* Block watcher that just fetches the last block and triggers the callback if it changed. This is opposed to the
* blockstream implementation that fetches all blocks since the previous one. This is needed for some testnet providers
* that throttle their API response times, making the default blockstreamer not keep up with all blocks produced.
*/
module.exports = class SimpleBlockWatcher {
constructor(
provider,
pollingInterval
) {
this.provider = provider
this.pollingInterval = pollingInterval || DEFAULT_POLLING_INTERVAL_MS

this.lastBlockNumber = undefined
this.intervalId = undefined
}

subscribe(cb) {
this.intervalId = this.setIntervalAsyncFn(async () => {
const latestBlock = await this.provider.getBlock("latest")

if (!this.lastBlockNumber || latestBlock.number > this.lastBlockNumber) {
this.lastBlockNumber = latestBlock.number

await cb(latestBlock)
}
}, this.pollingInterval, this.onError.bind(this))
}

unsubscribe() {
this.clearIntervalAsyncFn(this.intervalId)
}

onError(err) {
console.warn("Block watcher error:", err)
}

// Utilities

setIntervalAsyncFn(fn, intervalMs, onError) {
let isRunning = false
const intervalId = setInterval(async () => {
if (isRunning) {
return
} else {
isRunning = true
try {
await fn()
} catch (err) {
onError(err)
}
isRunning = false
}
}, intervalMs)

return intervalId
}

clearIntervalAsyncFn(intervalId) {
clearInterval(intervalId)
}
}