-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,18 @@ | ||
import { describe, expect, it } from 'bun:test' | ||
import { describe, expect, it, jest } from 'bun:test' | ||
import { Block } from '@tevm/block' | ||
import { optimism } from '@tevm/common' | ||
import { InvalidBlockError, UnknownBlockError } from '@tevm/errors' | ||
import { transports } from '@tevm/test-utils' | ||
import { createBaseChain } from '../createBaseChain.js' | ||
import { getBlockFromRpc } from './getBlockFromRpc.js' | ||
|
||
describe('getBlockFromRpc', () => { | ||
const baseChain = createBaseChain({ common: optimism.copy() }) | ||
it('should fetch the latest block', async () => { | ||
const transport = transports.optimism | ||
const common = optimism.copy() | ||
|
||
const block = await getBlockFromRpc({ transport, blockTag: 'latest' }, common) | ||
const block = await getBlockFromRpc(baseChain, { transport, blockTag: 'latest' }, common) | ||
expect(block).toBeInstanceOf(Block) | ||
expect(block.header.number).toBeGreaterThanOrEqual(0n) | ||
}) | ||
|
@@ -19,7 +22,7 @@ describe('getBlockFromRpc', () => { | |
const common = optimism.copy() | ||
const blockNumber = 122606365n | ||
|
||
const block = await getBlockFromRpc({ transport, blockTag: blockNumber }, common) | ||
const block = await getBlockFromRpc(baseChain, { transport, blockTag: blockNumber }, common) | ||
expect(block).toBeInstanceOf(Block) | ||
expect(block.header.number).toBe(blockNumber) | ||
}) | ||
|
@@ -29,51 +32,60 @@ describe('getBlockFromRpc', () => { | |
const common = optimism.copy() | ||
const blockHash = '0x6d4f1b3c89f9a26e7b1d8af7b093b8936d4d1af7989d0b1a7b1a2b0b0b6a6a6a' | ||
|
||
const block = await getBlockFromRpc({ transport, blockTag: blockHash }, common) | ||
const block = await getBlockFromRpc(baseChain, { transport, blockTag: blockHash }, common) | ||
expect(block).toBeInstanceOf(Block) | ||
expect(block.hash().toString('hex')).toBe(blockHash.slice(2)) | ||
expect(block.hash().toString()).toBe(blockHash.slice(2)) | ||
}) | ||
|
||
it('should handle invalid block tag', async () => { | ||
const transport = transports.optimism | ||
const common = optimism.copy() | ||
const invalidBlockTag = 'invalid-tag' | ||
|
||
await expect(getBlockFromRpc({ transport, blockTag: invalidBlockTag as any }, common)).rejects.toThrow( | ||
`Invalid blocktag ${invalidBlockTag}`, | ||
const err = await getBlockFromRpc(baseChain, { transport, blockTag: invalidBlockTag as any }, common).catch( | ||
(e) => e, | ||
) | ||
expect(err).toBeInstanceOf(InvalidBlockError) | ||
Check failure on line 48 in packages/blockchain/src/utils/getBlockFromRpc.spec.ts GitHub Actions / Nx Cloud - Main Joberror: expect(received).toBeInstanceOf(expected)
|
||
expect(err).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle non-existing block number', async () => { | ||
const transport = transports.optimism | ||
const common = optimism.copy() | ||
const nonExistingBlockNumber = 99999999999n | ||
|
||
await expect(getBlockFromRpc({ transport, blockTag: nonExistingBlockNumber }, common)).rejects.toThrow( | ||
'No block found', | ||
const err = await getBlockFromRpc(baseChain, { transport, blockTag: nonExistingBlockNumber }, common).catch( | ||
(e) => e, | ||
) | ||
|
||
expect(err).toBeInstanceOf(UnknownBlockError) | ||
Check failure on line 61 in packages/blockchain/src/utils/getBlockFromRpc.spec.ts GitHub Actions / Nx Cloud - Main Joberror: expect(received).toBeInstanceOf(expected)
|
||
expect(err).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle non-existing block hash', async () => { | ||
const transport = transports.optimism | ||
const common = optimism.copy() | ||
const nonExistingBlockHash = `0x${'0'.repeat(64)}` | ||
const nonExistingBlockHash = `0x${'0'.repeat(64)}` as const | ||
|
||
await expect(getBlockFromRpc({ transport, blockTag: nonExistingBlockHash }, common)).rejects.toThrow( | ||
'No block found', | ||
) | ||
const err = await getBlockFromRpc(baseChain, { transport, blockTag: nonExistingBlockHash }, common).catch((e) => e) | ||
expect(err).toBeInstanceOf(UnknownBlockError) | ||
Check failure on line 71 in packages/blockchain/src/utils/getBlockFromRpc.spec.ts GitHub Actions / Nx Cloud - Main Joberror: expect(received).toBeInstanceOf(expected)
|
||
expect(err).toMatchSnapshot() | ||
}) | ||
|
||
it('should handle Optimism deposit transactions filtering', async () => { | ||
const transport = transports.optimism | ||
const common = optimism.copy() | ||
|
||
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}) | ||
const baseChain = createBaseChain({ common }) | ||
const consoleWarnSpy = jest.fn() | ||
baseChain.logger.warn = consoleWarnSpy | ||
|
||
const block = await getBlockFromRpc({ transport, blockTag: 'latest' }, common) | ||
const block = await getBlockFromRpc(baseChain, { transport, blockTag: 'latest' }, common) | ||
await getBlockFromRpc(baseChain, { transport, blockTag: 'latest' }, common) | ||
await getBlockFromRpc(baseChain, { transport, blockTag: 'latest' }, common) | ||
expect(block).toBeInstanceOf(Block) | ||
expect(consoleWarnSpy).toHaveBeenCalled() | ||
|
||
expect(consoleWarnSpy).toHaveBeenCalledTimes(1) | ||
Check failure on line 87 in packages/blockchain/src/utils/getBlockFromRpc.spec.ts GitHub Actions / Nx Cloud - Main Joberror: expect(received).toHaveBeenCalledTimes(expected)
|
||
expect(consoleWarnSpy.mock.calls).toMatchSnapshot() | ||
consoleWarnSpy.mockRestore() | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
let i = 0 | ||
/** | ||
* Creates a function that logs a warning once | ||
* @param {import('../BaseChain.js').BaseChain} baseChain | ||
*/ | ||
export const warnOnce = (baseChain) => { | ||
/** | ||
* @param {import('viem').RpcBlock} tx | ||
*/ | ||
return (tx) => { | ||
if (i > 0) { | ||
return | ||
} | ||
i++ | ||
baseChain.logger.warn( | ||
`Warning: Optimism deposit transactions (type 0x7e) are currently not supported and will be filtered out of blocks until support is added | ||
filtering out tx ${/** @type {import('viem').RpcBlock}*/ (tx).hash}`, | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.