Skip to content
Merged
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
50 changes: 32 additions & 18 deletions packages/rollup-full-node/src/app/web3-rpc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ const latestBlock: string = 'latest'

export class DefaultWeb3Handler
implements Web3Handler, FullnodeHandler, L1ToL2TransactionListener {
private readonly ovmHashToOvmTransactionCache: Object = {}
protected blockTimestamps: Object = {}

/**
* Creates a local node, deploys the L2ExecutionManager to it, and returns a
* Web3Handler that handles Web3 requests to it.
Expand Down Expand Up @@ -540,7 +542,8 @@ export class DefaultWeb3Handler
}

public async sendRawTransaction(rawOvmTx: string): Promise<string> {
const timestamp = this.getTimestamp()
const debugTime = new Date().getTime()
const blockTimestamp = this.getTimestamp()
log.debug('Sending raw transaction with params:', rawOvmTx)

// Decode the OVM transaction -- this will be used to construct our internal transaction
Expand Down Expand Up @@ -609,10 +612,13 @@ export class DefaultWeb3Handler
log.debug(`Transaction mined successfully: ${rawOvmTx}`)
await this.processTransactionEvents(receipt)
}
this.blockTimestamps[receipt.blockNumber] = timestamp
this.blockTimestamps[receipt.blockNumber] = blockTimestamp
})

log.debug(`Completed send raw tx [${rawOvmTx}]. Response: [${ovmTxHash}]`)
log.debug(
`Completed send raw tx [${rawOvmTx}]. Response: [${ovmTxHash}]. Total time: ${new Date().getTime() -
debugTime}ms`
)
// Return the *OVM* tx hash. We can do this because we store a mapping to the ovmTxHashs in the EM contract.
return ovmTxHash
}
Expand Down Expand Up @@ -755,21 +761,26 @@ export class DefaultWeb3Handler
)

const res = await this.context.provider.sendTransaction(signedTx)
try {
const receipt = await this.context.provider.waitForTransaction(res.hash)
log.debug(
`Got receipt mapping ${ovmTxHash} to ${internalTxHash}: ${JSON.stringify(
receipt
)}`
)
} catch (e) {
logError(
log,
`Error mapping ovmTxHash: ${ovmTxHash} to internal tx hash: ${internalTxHash}`,
e
)
throw e
}
this.ovmHashToOvmTransactionCache[ovmTxHash] = signedOvmTransaction

this.context.provider
.waitForTransaction(res.hash)
.then((receipt) => {
log.debug(
`Got receipt mapping ${ovmTxHash} to ${internalTxHash}: ${JSON.stringify(
receipt
)}`
)
delete this.ovmHashToOvmTransactionCache[ovmTxHash]
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah nice I was wondering if we'd run into any problems with the cache being too large but this looks good!

})
.catch((e) => {
logError(
log,
`Error mapping ovmTxHash: ${ovmTxHash} to internal tx hash: ${internalTxHash}. This should never happen!`,
e
)
throw e
})
}

/**
Expand Down Expand Up @@ -801,6 +812,9 @@ export class DefaultWeb3Handler
* @returns The signed OVM transaction if one exists, else undefined.
*/
private async getOvmTransactionByHash(ovmTxHash: string): Promise<string> {
if (ovmTxHash in this.ovmHashToOvmTransactionCache) {
return this.ovmHashToOvmTransactionCache[ovmTxHash]
}
return this.context.executionManager.getOvmTransaction(add0x(ovmTxHash))
}

Expand Down