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

Throttle block consumption #156

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 28 additions & 21 deletions src/services/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ export class ChainContext {
readonly dryRun: boolean;
readonly watchdogTimeout: number;
readonly addresses?: string[];
readonly processEveryNumBlocks: number;

private sync: ChainSync = ChainSync.SYNCING;
static chains: Chains = {};

Expand Down Expand Up @@ -106,6 +108,7 @@ export class ChainContext {
this.deploymentBlock = deploymentBlock;
this.pageSize = pageSize ?? PAGE_SIZE_DEFAULT;
this.dryRun = dryRun;
this.processEveryNumBlocks = options.processEveryNumBlocks ?? 1;
this.watchdogTimeout = watchdogTimeout ?? WATCHDOG_TIMEOUT_DEFAULT_SECS;
this.addresses = owners;

Expand Down Expand Up @@ -311,9 +314,8 @@ export class ChainContext {
let lastBlockReceived = lastProcessedBlock;
provider.on("block", async (blockNumber: number) => {
try {
const block = await provider.getBlock(blockNumber);

log.debug(`New block ${blockNumber}`);
const block = await provider.getBlock(blockNumber);

// Set the block time metric
const _blockTime = block.timestamp - lastBlockReceived.timestamp;
Expand Down Expand Up @@ -434,7 +436,7 @@ async function processBlock(
blockNumberOverride?: number,
blockTimestampOverride?: number
) {
const { provider, chainId } = context;
const { provider, chainId, processEveryNumBlocks } = context;
const timer = metrics.processBlockDurationSeconds
.labels(context.chainId.toString())
.startTimer();
Expand Down Expand Up @@ -463,24 +465,29 @@ async function processBlock(
}
}

// run action
const result = await checkForAndPlaceOrder(
context,
block,
blockNumberOverride,
blockTimestampOverride
)
.then(() => true)
.catch(() => {
hasErrors = true;
log.error(`Error running "checkForAndPlaceOrder" action`);
return false;
});
log.debug(
`Result of "checkForAndPlaceOrder" action for block ${
block.number
}: ${_formatResult(result)}`
);
// Decide if we should process this block
const shouldProcessBlock = block.number % processEveryNumBlocks === 0;

// Check programmatic orders and place orders if necessary
if (shouldProcessBlock) {
const result = await checkForAndPlaceOrder(
context,
block,
blockNumberOverride,
blockTimestampOverride
)
.then(() => true)
.catch(() => {
hasErrors = true;
log.error(`Error running "checkForAndPlaceOrder" action`);
return false;
});
log.debug(
`Result of "checkForAndPlaceOrder" action for block ${
block.number
}: ${_formatResult(result)}`
);
}

timer();
if (hasErrors) {
Expand Down
6 changes: 6 additions & 0 deletions src/types/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"watchdogTimeout": {
"type": "integer"
},
"processEveryNumBlocks": {
"type": "integer",
"minimum": 1,
"description": "Throttle block processing to only process blocks every N blocks. Set to 1 to process every block, 2 to process every other block, etc.",
"default": 1
},
"orderBookApi": {
"type": "string",
"format": "uri"
Expand Down
4 changes: 4 additions & 0 deletions src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface Config {
rpc: string;
deploymentBlock: number;
watchdogTimeout?: number;
/**
* Throttle block processing to only process blocks every N blocks. Set to 1 to process every block, 2 to process every other block, etc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel this comment is a bit redundant (it's documented in the shema above and we don't have redundant docs for any of the other parameters)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is automatically generated from the JSON schema

*/
anxolin marked this conversation as resolved.
Show resolved Hide resolved
processEveryNumBlocks?: number;
orderBookApi?: string;
pageSize?: number;
filterPolicy: {
Expand Down
Loading