Commit 43c4158f authored by Mark Tyneway's avatar Mark Tyneway

sdk: add tests for min gas util

parent c7ec576c
...@@ -26,7 +26,6 @@ import { ...@@ -26,7 +26,6 @@ import {
BedrockCrossChainMessageProof, BedrockCrossChainMessageProof,
decodeVersionedNonce, decodeVersionedNonce,
encodeVersionedNonce, encodeVersionedNonce,
calldataCost,
} from '@eth-optimism/core-utils' } from '@eth-optimism/core-utils'
import { getContractInterface, predeploys } from '@eth-optimism/contracts' import { getContractInterface, predeploys } from '@eth-optimism/contracts'
import * as rlp from 'rlp' import * as rlp from 'rlp'
...@@ -66,6 +65,7 @@ import { ...@@ -66,6 +65,7 @@ import {
makeMerkleTreeProof, makeMerkleTreeProof,
makeStateTrieProof, makeStateTrieProof,
hashLowLevelMessage, hashLowLevelMessage,
migratedWithdrawalGasLimit,
DEPOSIT_CONFIRMATION_BLOCKS, DEPOSIT_CONFIRMATION_BLOCKS,
CHAIN_BLOCK_TIMES, CHAIN_BLOCK_TIMES,
} from './utils' } from './utils'
...@@ -351,12 +351,7 @@ export class CrossChainMessenger { ...@@ -351,12 +351,7 @@ export class CrossChainMessenger {
} }
} }
// Compute the gas limit and cap at 25 million const minGasLimit = migratedWithdrawalGasLimit(resolved.message)
const dataCost = calldataCost(resolved.message)
let minGasLimit = dataCost.add(200_000)
if (minGasLimit.gt(25_000_000)) {
minGasLimit = BigNumber.from(25_000_000)
}
return { return {
...resolved, ...resolved,
......
import { hashWithdrawal } from '@eth-optimism/core-utils' import { hashWithdrawal, calldataCost } from '@eth-optimism/core-utils'
import { BigNumber } from 'ethers'
import { LowLevelMessage } from '../interfaces' import { LowLevelMessage } from '../interfaces'
...@@ -18,3 +19,16 @@ export const hashLowLevelMessage = (message: LowLevelMessage): string => { ...@@ -18,3 +19,16 @@ export const hashLowLevelMessage = (message: LowLevelMessage): string => {
message.message message.message
) )
} }
/**
* Compute the min gas limit for a migrated withdrawal.
*/
export const migratedWithdrawalGasLimit = (data: string): BigNumber => {
// Compute the gas limit and cap at 25 million
const dataCost = calldataCost(data)
let minGasLimit = dataCost.add(200_000)
if (minGasLimit.gt(25_000_000)) {
minGasLimit = BigNumber.from(25_000_000)
}
return minGasLimit
}
import { BigNumber } from 'ethers'
import { expect } from '../setup'
import { migratedWithdrawalGasLimit } from '../../src/utils/message-utils'
describe('Message Utils', () => {
describe('migratedWithdrawalGasLimit', () => {
it('should have a max of 25 million', () => {
const data = '0x' + 'ff'.repeat(15_000_000)
const result = migratedWithdrawalGasLimit(data)
expect(result).to.eq(BigNumber.from(25_000_000))
})
it('should work for mixes of zeros and ones', () => {
const tests = [
{ input: '0x', result: BigNumber.from(200_000) },
{ input: '0xff', result: BigNumber.from(200_000 + 16) },
{ input: '0xff00', result: BigNumber.from(200_000 + 16 + 4) },
{ input: '0x00', result: BigNumber.from(200_000 + 4) },
{ input: '0x000000', result: BigNumber.from(200_000 + 4 + 4 + 4) },
]
for (const test of tests) {
const result = migratedWithdrawalGasLimit(test.input)
expect(result).to.eq(test.result)
}
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment