Skip to content

Commit

Permalink
chore: update to latest master part 2 (#53)
Browse files Browse the repository at this point in the history
* feat(hardhat-ovm): allow ignoring libraries when compiling

ethereum-optimism/plugins#39

* feat(batch-submitter): add logs

ethereum-optimism/batch-submitter#75
ethereum-optimism/batch-submitter#73

* chore: build contracts sequentially

this worsens our compilation times but ensures that the runs are not flaky
  • Loading branch information
gakonst authored Apr 11, 2021
1 parent 0f8bac3 commit 41cfbd1
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export abstract class BatchSubmitter {
}
await this._updateChainInfo()
await this._checkBalance()
this.log.info('Readying to submit next batch...', {
l2ChainId: this.l2ChainId,
batchSubmitterAddress: await this.signer.getAddress(),
})

if (this.syncing === true) {
this.log.info(
Expand Down Expand Up @@ -225,7 +229,7 @@ export abstract class BatchSubmitter {
this.log
)

this.log.debug('Transaction receipt:', { receipt })
this.log.info('Received transaction receipt', { receipt })
this.log.info(successMessage)
return receipt
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Promise as bPromise } from 'bluebird'
import { Contract, Signer } from 'ethers'
import { TransactionReceipt } from '@ethersproject/abstract-provider'
import { getContractFactory } from '@eth-optimism/contracts'
import { Logger, Bytes32 } from '@eth-optimism/core-utils'
import { Logger, Bytes32, remove0x } from '@eth-optimism/core-utils'
import { OptimismProvider } from '@eth-optimism/provider'

/* Internal Imports */
Expand Down Expand Up @@ -82,6 +82,10 @@ export class StateBatchSubmitter extends BatchSubmitter {
sccAddress === this.chainContract.address &&
ctcAddress === this.ctcContract.address
) {
this.log.debug('Chain contract already initialized', {
sccAddress,
ctcAddress,
})
return
}

Expand All @@ -105,12 +109,21 @@ export class StateBatchSubmitter extends BatchSubmitter {
}

public async _getBatchStartAndEnd(): Promise<Range> {
this.log.info('Getting batch start and end for state batch submitter...')
// TODO: Remove BLOCK_OFFSET by adding a tx to Geth's genesis
const startBlock: number =
(await this.chainContract.getTotalElements()).toNumber() + BLOCK_OFFSET
this.log.info('Retrieved start block number from SCC', {
startBlock,
})

// We will submit state roots for txs which have been in the tx chain for a while.
const totalElements: number =
(await this.ctcContract.getTotalElements()).toNumber() + BLOCK_OFFSET
this.log.info('Retrieved total elements from CTC', {
totalElements,
})

const endBlock: number = Math.min(
startBlock + this.maxBatchSize,
totalElements
Expand Down Expand Up @@ -142,7 +155,13 @@ export class StateBatchSubmitter extends BatchSubmitter {
'appendStateBatch',
[batch, startBlock]
)
if (!this._shouldSubmitBatch(tx.length / 2)) {
const batchSizeInBytes = remove0x(tx).length / 2
this.log.debug('State batch generated', {
batchSizeInBytes,
tx,
})

if (!this._shouldSubmitBatch(batchSizeInBytes)) {
return
}

Expand All @@ -156,6 +175,13 @@ export class StateBatchSubmitter extends BatchSubmitter {
offsetStartsAtIndex,
{ nonce, gasPrice }
)
this.log.info('Submitted appendStateBatch transaction', {
nonce,
txHash: contractTx.hash,
contractAddr: this.chainContract.address,
from: contractTx.from,
data: contractTx.data,
})
return this.signer.provider.waitForTransaction(
contractTx.hash,
this.numConfirmations
Expand All @@ -180,7 +206,12 @@ export class StateBatchSubmitter extends BatchSubmitter {
const block = (await this.l2Provider.getBlockWithTransactions(
startBlock + i
)) as L2Block
if (block.transactions[0].from === this.fraudSubmissionAddress) {
const blockTx = block.transactions[0]
if (blockTx.from === this.fraudSubmissionAddress) {
this.log.warn('Found transaction from fraud submission address', {
txHash: blockTx.hash,
fraudSubmissionAddress: this.fraudSubmissionAddress,
})
this.fraudSubmissionAddress = 'no fraud'
return '0xbad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1bad1'
}
Expand All @@ -193,14 +224,20 @@ export class StateBatchSubmitter extends BatchSubmitter {
'appendStateBatch',
[batch, startBlock]
)
while (tx.length > this.maxTxSize) {
while (remove0x(tx).length / 2 > this.maxTxSize) {
batch.splice(Math.ceil((batch.length * 2) / 3)) // Delete 1/3rd of all of the batch elements
this.log.debug('Splicing batch...', {
batchSizeInBytes: tx.length / 2,
})
tx = this.chainContract.interface.encodeFunctionData('appendStateBatch', [
batch,
startBlock,
])
}

this.log.info('Generated state commitment batch', {
batch, // list of stateRoots
})
return batch
}
}
71 changes: 69 additions & 2 deletions packages/batch-submitter/src/batch-submitter/tx-batch-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
typeof this.chainContract !== 'undefined' &&
ctcAddress === this.chainContract.address
) {
this.log.debug('Chain contract already initialized', {
ctcAddress,
})
return
}

Expand All @@ -134,6 +137,9 @@ export class TransactionBatchSubmitter extends BatchSubmitter {

public async _onSync(): Promise<TransactionReceipt> {
const pendingQueueElements = await this.chainContract.getNumPendingQueueElements()
this.log.debug('Got number of pending queue elements', {
pendingQueueElements,
})

if (pendingQueueElements !== 0) {
this.log.info(
Expand All @@ -150,6 +156,13 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
nonce,
gasPrice,
})
this.log.info('Submitted appendQueueBatch transaction', {
nonce,
txHash: tx.hash,
contractAddr: this.chainContract.address,
from: tx.from,
data: tx.data,
})
return this.signer.provider.waitForTransaction(
tx.hash,
this.numConfirmations
Expand All @@ -166,6 +179,7 @@ export class TransactionBatchSubmitter extends BatchSubmitter {

// TODO: Remove this function and use geth for lastL1BlockNumber!
private async _updateLastL1BlockNumber() {
this.log.warn('Calling _updateLastL1BlockNumber...')
const pendingQueueElements = await this.chainContract.getNumPendingQueueElements()

if (pendingQueueElements !== 0) {
Expand All @@ -187,17 +201,32 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
}
}
}

this.log.debug('Set lastL1BlockNumber', {
lastL1BlockNumber: this.lastL1BlockNumber,
})
}

public async _getBatchStartAndEnd(): Promise<Range> {
this.log.info(
'Getting batch start and end for transaction batch submitter...'
)
// TODO: Remove BLOCK_OFFSET by adding a tx to Geth's genesis
const startBlock =
(await this.chainContract.getTotalElements()).toNumber() + BLOCK_OFFSET
this.log.info('Retrieved start block number from CTC', {
startBlock,
})

const endBlock =
Math.min(
startBlock + this.maxBatchSize,
await this.l2Provider.getBlockNumber()
) + 1 // +1 because the `endBlock` is *exclusive*
this.log.info('Retrieved end block number from L2 sequencer', {
endBlock,
})

if (startBlock >= endBlock) {
if (startBlock > endBlock) {
this.log
Expand All @@ -224,7 +253,7 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
)
if (gasPriceInGwei > this.gasThresholdInGwei) {
this.log.warn(
'Gas price is higher than gras price threshold; aborting batch submission',
'Gas price is higher than gas price threshold; aborting batch submission',
{
gasPriceInGwei,
gasThresholdInGwei: this.gasThresholdInGwei,
Expand All @@ -238,6 +267,9 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
wasBatchTruncated,
] = await this._generateSequencerBatchParams(startBlock, endBlock)
const batchSizeInBytes = encodeAppendSequencerBatch(batchParams).length / 2
this.log.debug('Sequencer batch generated', {
batchSizeInBytes,
})

// Only submit batch if one of the following is true:
// 1. it was truncated
Expand All @@ -256,6 +288,13 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
nonce,
gasPrice,
})
this.log.info('Submitted appendSequencerBatch transaction', {
nonce,
txHash: tx.hash,
contractAddr: this.chainContract.address,
from: tx.from,
data: tx.data,
})
return this.signer.provider.waitForTransaction(
tx.hash,
this.numConfirmations
Expand Down Expand Up @@ -299,6 +338,9 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
let wasBatchTruncated = false
let encoded = encodeAppendSequencerBatch(sequencerBatchParams)
while (encoded.length / 2 > this.maxTxSize) {
this.log.debug('Splicing batch...', {
batchSizeInBytes: encoded.length / 2,
})
batch.splice(Math.ceil((batch.length * 2) / 3)) // Delete 1/3rd of all of the batch elements
sequencerBatchParams = await this._getSequencerBatchParams(
startBlock,
Expand All @@ -310,6 +352,12 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
// In this case, we want to submit regardless of the batch's size.
wasBatchTruncated = true
}

this.log.info('Generated sequencer batch params', {
contexts: sequencerBatchParams.contexts,
transactions: sequencerBatchParams.transactions,
wasBatchTruncated,
})
return [sequencerBatchParams, wasBatchTruncated]
}

Expand Down Expand Up @@ -421,13 +469,18 @@ export class TransactionBatchSubmitter extends BatchSubmitter {

// TODO: Remove this super complex logic and rely on Geth to actually supply correct block data.
const fixMonotonicity = async (b: Batch): Promise<Batch> => {
this.log.debug('Fixing monotonicity...')
// The earliest allowed timestamp/blockNumber is the last timestamp submitted on chain.
const {
lastTimestamp,
lastBlockNumber,
} = await this._getLastTimestampAndBlockNumber()
let earliestTimestamp = lastTimestamp
let earliestBlockNumber = lastBlockNumber
this.log.debug('Determined earliest timestamp and blockNumber', {
earliestTimestamp,
earliestBlockNumber,
})

// The latest allowed timestamp/blockNumber is the next queue element!
let nextQueueIndex = await this.chainContract.getNextQueueIndex()
Expand Down Expand Up @@ -457,6 +510,10 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
}
// Actually update the latest timestamp and block number
await updateLatestTimestampAndBlockNumber()
this.log.debug('Determined latest timestamp and blockNumber', {
latestTimestamp,
latestBlockNumber,
})

// Now go through our batch and fix the timestamps and block numbers
// to automatically enforce monotonicity.
Expand Down Expand Up @@ -573,7 +630,13 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
blockNumber > queueElement.blockNumber
) {
this.log.warn(
'Double deposit detected!!! Fixing by skipping the deposit & replacing with a dummy tx.'
'Double deposit detected. Fixing by skipping the deposit & replacing with a dummy tx.',
{
timestamp,
blockNumber,
queueElementTimestamp: queueElement.timestamp,
queueElementBlockNumber: queueElement.blockNumber,
}
)
// This implies that we've double played a deposit.
// We can correct this by instead submitting a dummy sequencer tx
Expand Down Expand Up @@ -712,6 +775,9 @@ export class TransactionBatchSubmitter extends BatchSubmitter {

private async _getL2BatchElement(blockNumber: number): Promise<BatchElement> {
const block = await this._getBlock(blockNumber)
this.log.debug('Fetched L2 block', {
block,
})
const txType = block.transactions[0].txType

if (this._isSequencerTx(block)) {
Expand Down Expand Up @@ -744,6 +810,7 @@ export class TransactionBatchSubmitter extends BatchSubmitter {
if (!block.transactions[0].l1BlockNumber) {
block.transactions[0].l1BlockNumber = this.lastL1BlockNumber
}

return block
}

Expand Down
5 changes: 4 additions & 1 deletion packages/batch-submitter/src/exec/run-batch-submitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ export const run = async () => {
value: 0,
nonce: i,
})
log.info('Submitting empty transaction', {
log.info('Submitted empty transaction', {
nonce: i,
txHash: response.hash,
to: response.to,
from: response.from,
data: response.data,
})
await sequencerSigner.provider.waitForTransaction(
response.hash,
Expand Down
1 change: 0 additions & 1 deletion packages/batch-submitter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './batch-submitter'
export * from './utils'
export * from './transaction-chain-contract'
export * from './types'
2 changes: 1 addition & 1 deletion packages/batch-submitter/src/transaction-chain-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
TransactionRequest,
} from '@ethersproject/abstract-provider'
import { keccak256 } from 'ethers/lib/utils'
import { remove0x, encodeHex } from './utils'
import {
AppendSequencerBatchParams,
BatchContext,
encodeAppendSequencerBatch,
encodeHex,
} from '@eth-optimism/core-utils'

export { encodeAppendSequencerBatch, BatchContext, AppendSequencerBatchParams }
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
yarn run build:typescript & yarn run build:contracts & yarn run build:contracts:ovm
yarn run build:typescript & yarn run build:contracts
yarn run build:contracts:ovm
yarn run build:copy:artifacts & yarn run build:copy:artifacts:ovm & yarn run build:copy:contracts
Loading

0 comments on commit 41cfbd1

Please sign in to comment.