-
Notifications
You must be signed in to change notification settings - Fork 190
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
feat: add support L2 block range lookup for Orbit chains #317
Merged
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
546d211
get block ranges for l1 block
brtkx ca9419c
better way to lower the range
brtkx d6b0153
fix
brtkx 9036c19
fix
brtkx 7cb3a2a
comments and small fix
brtkx c999016
var name
brtkx 5fa5dbd
extra checks, logic
brtkx 2f6e895
clean up
brtkx ab4eaa7
better fallback
brtkx 918f087
clean up
brtkx febc53c
unit tests
brtkx f0c70da
fix
brtkx c1144f2
errors
brtkx cb57b41
remove type cast
brtkx 6a2a2fc
remove duplicate
brtkx 8fe5b12
add nova to the list
brtkx ef3bda4
clean up
brtkx bd5197a
clean up and address review
brtkx ae85a70
fix
brtkx 62c2e62
add a check
brtkx 705c13d
lesser search
brtkx 0e134e7
comment
brtkx 7b1bda4
fixes
brtkx 79669c8
comment
brtkx 4b4983f
clean up
brtkx 051fd7d
fix jsdoc
brtkx 7af1c08
remove lowering the range logic
brtkx 13d5ddb
fix comment
brtkx 56465ed
fix comment
brtkx 2f2123d
fix
brtkx 9b268aa
clean up and unit tests
brtkx 89cc18f
remove comment
brtkx 97d286e
Merge branch 'main' of ssh://github.com/OffchainLabs/arbitrum-sdk int…
brtkx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { BigNumber } from 'ethers' | ||
import { expect } from 'chai' | ||
import { JsonRpcProvider } from '@ethersproject/providers' | ||
import { | ||
getBlockRangesForL1Block, | ||
getFirstBlockForL1Block, | ||
} from '../../src/lib/utils/lib' | ||
import { ArbitrumProvider } from '../../src/lib/utils/arbProvider' | ||
import { ArbBlock } from '../../src/lib/dataEntities/rpc' | ||
|
||
describe('L2 blocks lookup for an L1 block', () => { | ||
const provider = new JsonRpcProvider('https://arb1.arbitrum.io/rpc') | ||
const arbProvider = new ArbitrumProvider(provider) | ||
|
||
async function validateL2Blocks({ | ||
l2Blocks, | ||
l2BlocksCount, | ||
type = 'number', | ||
}: { | ||
l2Blocks: (number | undefined)[] | ||
l2BlocksCount: number | ||
type?: 'number' | 'undefined' | ||
}) { | ||
if (l2Blocks.length !== l2BlocksCount) { | ||
throw new Error( | ||
`Expected L2 block range to have the array length of ${l2BlocksCount}, got ${l2Blocks.length}.` | ||
) | ||
} | ||
|
||
if (l2Blocks.some(block => typeof block !== type)) { | ||
throw new Error(`Expected all blocks to be ${type}.`) | ||
} | ||
|
||
if (type === 'undefined') { | ||
return | ||
} | ||
|
||
const promises: Promise<ArbBlock>[] = [] | ||
|
||
l2Blocks.forEach((l2Block, index) => { | ||
if (!l2Block) { | ||
throw new Error('L2 block is undefined.') | ||
} | ||
const isStartBlock = index === 0 | ||
promises.push(arbProvider.getBlock(l2Block)) | ||
// Search for previous or next block. | ||
promises.push(arbProvider.getBlock(l2Block + (isStartBlock ? -1 : 1))) | ||
}) | ||
|
||
const [startBlock, blockBeforeStartBlock, endBlock, blockAfterEndBlock] = | ||
await Promise.all(promises).then(result => | ||
result.map(block => BigNumber.from(block.l1BlockNumber)) | ||
) | ||
|
||
if (startBlock && blockBeforeStartBlock) { | ||
const startBlockCondition = startBlock.gt(blockBeforeStartBlock) | ||
|
||
// Check if Arbitrum start block is the first block for this L1 block. | ||
expect( | ||
startBlockCondition, | ||
`L2 block is not the first block in range for L1 block.` | ||
).to.be.true | ||
} | ||
|
||
if (endBlock && blockAfterEndBlock) { | ||
const endBlockCondition = endBlock.lt(blockAfterEndBlock) | ||
|
||
// Check if Arbitrum end block is the last block for this L1 block. | ||
expect( | ||
endBlockCondition, | ||
`L2 block is not the last block in range for L1 block.` | ||
).to.be.true | ||
} | ||
} | ||
|
||
it('successfully searches for an L2 block range', async function () { | ||
const l2Blocks = await getBlockRangesForL1Block({ | ||
provider: arbProvider, | ||
forL1Block: 17926532, | ||
// Expected result: 121907680. Narrows down the range to speed up the search. | ||
minL2Block: 121800000, | ||
maxL2Block: 122000000, | ||
}) | ||
await validateL2Blocks({ l2Blocks, l2BlocksCount: 2 }) | ||
}) | ||
|
||
it('fails to search for an L2 block range', async function () { | ||
const l2Blocks = await getBlockRangesForL1Block({ | ||
provider: arbProvider, | ||
forL1Block: 17926533, | ||
minL2Block: 121800000, | ||
maxL2Block: 122000000, | ||
}) | ||
await validateL2Blocks({ l2Blocks, l2BlocksCount: 2, type: 'undefined' }) | ||
}) | ||
|
||
it('successfully searches for the first L2 block', async function () { | ||
const l2Blocks = [ | ||
await getFirstBlockForL1Block({ | ||
provider: arbProvider, | ||
forL1Block: 17926532, | ||
// Expected result: 121907680. Narrows down the range to speed up the search. | ||
minL2Block: 121800000, | ||
maxL2Block: 122000000, | ||
}), | ||
] | ||
await validateL2Blocks({ l2Blocks, l2BlocksCount: 1 }) | ||
}) | ||
|
||
it('fails to search for the first L2 block, while not using `allowGreater` flag', async function () { | ||
const l2Blocks = [ | ||
await getFirstBlockForL1Block({ | ||
provider: arbProvider, | ||
forL1Block: 17926533, | ||
allowGreater: false, | ||
minL2Block: 121800000, | ||
maxL2Block: 122000000, | ||
}), | ||
] | ||
await validateL2Blocks({ l2Blocks, l2BlocksCount: 1, type: 'undefined' }) | ||
}) | ||
|
||
it('successfully searches for the first L2 block, while using `allowGreater` flag', async function () { | ||
const l2Blocks = [ | ||
await getFirstBlockForL1Block({ | ||
provider: arbProvider, | ||
forL1Block: 17926533, | ||
allowGreater: true, | ||
// Expected result: 121907740. Narrows down the range to speed up the search. | ||
minL2Block: 121800000, | ||
maxL2Block: 122000000, | ||
}), | ||
] | ||
await validateL2Blocks({ l2Blocks, l2BlocksCount: 1 }) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to do some caching here or in the network object, can do later