Skip to content
This repository has been archived by the owner on Apr 11, 2021. It is now read-only.

Commit

Permalink
[Feat] Concurrent block fetching in state batch submitter (#66)
Browse files Browse the repository at this point in the history
* concurrently fetch state batch blocks, remove concurrency restraint

* bug fix and make async/await

* add state batch submitter unit test

Co-authored-by: Karl Floersch <[email protected]>
  • Loading branch information
annieke and karlfloersch authored Mar 31, 2021
1 parent 8411431 commit 1a4a532
Show file tree
Hide file tree
Showing 6 changed files with 571 additions and 454 deletions.
36 changes: 19 additions & 17 deletions src/batch-submitter/state-batch-submitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* External Imports */
import { Promise as bPromise } from 'bluebird'
import { Contract, Signer } from 'ethers'
import { TransactionReceipt } from '@ethersproject/abstract-provider'
import { getContractFactory } from '@eth-optimism/contracts'
Expand Down Expand Up @@ -108,14 +109,13 @@ export class StateBatchSubmitter extends BatchSubmitter {
const startBlock: number =
(await this.chainContract.getTotalElements()).toNumber() + BLOCK_OFFSET
// We will submit state roots for txs which have been in the tx chain for a while.
const callBlockNumber: number =
(await this.signer.provider.getBlockNumber()) - this.finalityConfirmations
const totalElements: number =
(await this.ctcContract.getTotalElements()).toNumber() + BLOCK_OFFSET
const endBlock: number = Math.min(
startBlock + this.maxBatchSize,
totalElements
)

if (startBlock >= endBlock) {
if (startBlock > endBlock) {
this.log.error(
Expand Down Expand Up @@ -172,21 +172,22 @@ export class StateBatchSubmitter extends BatchSubmitter {
startBlock: number,
endBlock: number
): Promise<Bytes32[]> {
const batch: Bytes32[] = []

for (let i = startBlock; i < endBlock; i++) {
const block = (await this.l2Provider.getBlockWithTransactions(
i
)) as L2Block
if (block.transactions[0].from === this.fraudSubmissionAddress) {
batch.push(
'0xbad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1'
)
this.fraudSubmissionAddress = 'no fraud'
} else {
batch.push(block.stateRoot)
}
}
const blockRange = endBlock - startBlock
const batch: Bytes32[] = await bPromise.map(
[...Array(blockRange).keys()],
async (i: number) => {
this.log.debug('Fetching L2BatchElement', { blockNo: startBlock + i })
const block = (await this.l2Provider.getBlockWithTransactions(
startBlock + i
)) as L2Block
if (block.transactions[0].from === this.fraudSubmissionAddress) {
this.fraudSubmissionAddress = 'no fraud'
return '0xbad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1'
}
return block.stateRoot
},
{ concurrency: 100 }
)

let tx = this.chainContract.interface.encodeFunctionData(
'appendStateBatch',
Expand All @@ -199,6 +200,7 @@ export class StateBatchSubmitter extends BatchSubmitter {
startBlock,
])
}

return batch
}
}
2 changes: 1 addition & 1 deletion src/batch-submitter/tx-batch-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
this.log.debug('Fetching L2BatchElement', { blockNo: startBlock + i })
return this._getL2BatchElement(startBlock + i)
},
{ concurrency: 50 }
{ concurrency: 100 }
)

// Fix our batches if we are configured to. TODO: Remove this.
Expand Down
Loading

0 comments on commit 1a4a532

Please sign in to comment.