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

Temp fix lastL1BlockNum & small off by 1 #357

Merged
merged 3 commits into from
Nov 5, 2020
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
40 changes: 31 additions & 9 deletions packages/batch-submitter/src/batch-submitter/tx-batch-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
protected chainContract: CanonicalTransactionChainContract
protected l2ChainId: number
protected syncing: boolean
protected lastL1BlockNumber: number

/*****************************
* Batch Submitter Overrides *
Expand Down Expand Up @@ -77,6 +78,8 @@ export class TransactionBatchSubmitter extends BatchSubmitter {

public async _onSync(): Promise<TransactionReceipt> {
const pendingQueueElements = await this.chainContract.getNumPendingQueueElements()
this._updateLastL1BlockNumber(pendingQueueElements)

if (pendingQueueElements !== 0) {
this.log.info(
`Syncing mode enabled! Skipping batch submission and clearing ${pendingQueueElements} queue elements`
Expand All @@ -91,13 +94,36 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
return
}

// TODO: Remove this function and use geth for lastL1BlockNumber!
private async _updateLastL1BlockNumber(pendingQueueElements: number) {
if (pendingQueueElements !== 0) {
const nextQueueIndex = await this.chainContract.getNextQueueIndex()
this.lastL1BlockNumber = await this.chainContract.getQueueElement(
nextQueueIndex
).blockNumber
} else {
const curBlockNum = await this.chainContract.provider.getBlockNumber()
if (!this.lastL1BlockNumber) {
// Set the block number to the current l1BlockNumber
this.lastL1BlockNumber = curBlockNum
} else {
if (curBlockNum - this.lastL1BlockNumber > 30) {
// If the lastL1BlockNumber is too old, then set it to a recent
// block number. (10 blocks ago to prevent reorgs)
this.lastL1BlockNumber = curBlockNum - 10
}
}
}
}

public async _getBatchStartAndEnd(): Promise<Range> {
const startBlock =
parseInt(await this.chainContract.getTotalElements(), 16) + 1 // +1 to skip L2 genesis block
const endBlock = Math.min(
startBlock + this.maxBatchSize,
await this.l2Provider.getBlockNumber()
)
const endBlock =
Math.min(
startBlock + this.maxBatchSize,
await this.l2Provider.getBlockNumber()
) + 1 // +1 because the `endBlock` is *exclusive*
if (startBlock >= endBlock) {
if (startBlock > endBlock) {
this.log
Expand Down Expand Up @@ -261,12 +287,8 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
block.transactions[0].queueOrigin =
queueOriginPlainText[block.transactions[0].queueOrigin]
// For now just set the l1BlockNumber based on the current l1 block number
const _getMockedL1BlockNumber = async (): Promise<number> => {
const curBlockNum = await this.chainContract.signer.provider.getBlockNumber()
return curBlockNum - 1
}
if (!block.transactions[0].l1BlockNumber) {
block.transactions[0].l1BlockNumber = await _getMockedL1BlockNumber()
block.transactions[0].l1BlockNumber = this.lastL1BlockNumber
}
return block
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ describe('TransactionBatchSubmitter', () => {
let logData = remove0x(receipt.logs[0].data)
expect(parseInt(logData.slice(64 * 0, 64 * 1), 16)).to.equal(0) // _startingQueueIndex
expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(0) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(5) // _totalElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(6) // _totalElements
receipt = await batchSubmitter.submitNextBatch()
logData = remove0x(receipt.logs[0].data)
expect(parseInt(logData.slice(64 * 0, 64 * 1), 16)).to.equal(0) // _startingQueueIndex
expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(0) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(10) // _totalElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(11) // _totalElements
})

it('should submit a queue batch correctly', async () => {
Expand All @@ -193,13 +193,13 @@ describe('TransactionBatchSubmitter', () => {
let receipt = await batchSubmitter.submitNextBatch()
let logData = remove0x(receipt.logs[0].data)
expect(parseInt(logData.slice(64 * 0, 64 * 1), 16)).to.equal(0) // _startingQueueIndex
expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(5) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(5) // _totalElements
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to update the tests here because I wasn't actually submitting all the blocks that I should have been cuz of the off-by-1

expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(6) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(6) // _totalElements
receipt = await batchSubmitter.submitNextBatch()
logData = remove0x(receipt.logs[0].data)
expect(parseInt(logData.slice(64 * 0, 64 * 1), 16)).to.equal(5) // _startingQueueIndex
expect(parseInt(logData.slice(64 * 0, 64 * 1), 16)).to.equal(6) // _startingQueueIndex
expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(5) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(10) // _totalElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(11) // _totalElements
})

it('should submit a batch with both queue and sequencer chain elements', async () => {
Expand Down Expand Up @@ -231,8 +231,8 @@ describe('TransactionBatchSubmitter', () => {
const receipt = await batchSubmitter.submitNextBatch()
const logData = remove0x(receipt.logs[0].data)
expect(parseInt(logData.slice(64 * 0, 64 * 1), 16)).to.equal(0) // _startingQueueIndex
expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(7) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(10) // _totalElements
expect(parseInt(logData.slice(64 * 1, 64 * 2), 16)).to.equal(8) // _numQueueElements
expect(parseInt(logData.slice(64 * 2, 64 * 3), 16)).to.equal(11) // _totalElements
})
})
})