Skip to content

Commit

Permalink
🚧 Compiles but wip
Browse files Browse the repository at this point in the history
  • Loading branch information
William Cory authored and William Cory committed Jul 14, 2024
1 parent 49c164b commit 9c67b80
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 36 deletions.
1 change: 1 addition & 0 deletions packages/blockchain/src/actions/getBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const getBlock = (baseChain) => async (blockId) => {
baseChain.logger.debug('Fetching block from remote rpc...')

const fetchedBlock = await getBlockFromRpc(
baseChain,
{
transport: baseChain.options.fork?.transport,
blockTag: blockId instanceof Uint8Array ? bytesToHex(blockId) : BigInt(blockId),
Expand Down
1 change: 1 addition & 0 deletions packages/blockchain/src/actions/getBlockByTag.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const getBlockByTag = (baseChain) => async (blockId) => {
const block = baseChain.blocksByTag.get(blockId)
if (!block && baseChain.options.fork?.transport) {
const block = await getBlockFromRpc(
baseChain,
{
transport: baseChain.options.fork.transport,
blockTag: blockId,
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/src/createBaseChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const createBaseChain = (options) => {
// Add genesis block and forked block to chain
const genesisBlockPromise = (async () => {
if (options.fork?.transport) {
const block = await getBlockFromRpc(options.fork, options.common)
const block = await getBlockFromRpc(chain, options.fork, options.common)
await putBlock(chain)(block)
chain.blocksByTag.set('forked', block)
} else {
Expand Down
22 changes: 4 additions & 18 deletions packages/blockchain/src/utils/getBlockFromRpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Block, blockFromRpc } from '@tevm/block'
import { createJsonRpcFetcher } from '@tevm/jsonrpc'
import { numberToHex } from '@tevm/utils'
import { withRetry } from 'viem'
import { warnOnce } from './warnOnce.js'

/**
* Determines if an unknown type is a valid block tag
Expand All @@ -12,29 +13,14 @@ const isBlockTag = (blockTag) => {
return typeof blockTag === 'string' && ['latest', 'earliest', 'pending', 'safe', 'finalized'].includes(blockTag)
}

let i = 0

/**
* @param {import('viem').RpcBlock} tx
*/
const warnOnce = (tx) => {
if (i > 0) {
return
}
i++
console.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}`,
)
}

/**
* @param {import('../BaseChain.js').BaseChain} baseChain
* @param {object} params
* @param {{request: import('viem').EIP1193RequestFn}} params.transport
* @param {bigint | import('viem').BlockTag | import('viem').Hex} [params.blockTag]
* @param {import('@tevm/common').Common} common
*/
export const getBlockFromRpc = async ({ transport, blockTag = 'latest' }, common) => {
export const getBlockFromRpc = async (baseChain, { transport, blockTag = 'latest' }, common) => {
const fetcher = createJsonRpcFetcher(transport)
/**
* @param {import('viem').RpcBlock<'latest', true>} rpcBlock
Expand All @@ -50,7 +36,7 @@ export const getBlockFromRpc = async ({ transport, blockTag = 'latest' }, common
// Optimism type is currently not in viem types
// @ts-expect-error
if (tx.type === '0x7e') {
warnOnce(tx)
warnOnce(baseChain)(tx)
return false
}
return true
Expand Down
46 changes: 29 additions & 17 deletions packages/blockchain/src/utils/getBlockFromRpc.spec.ts
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)
})
Expand All @@ -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)
})
Expand All @@ -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)
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)
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)
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)
expect(consoleWarnSpy.mock.calls).toMatchSnapshot()
consoleWarnSpy.mockRestore()
})
})
20 changes: 20 additions & 0 deletions packages/blockchain/src/utils/warnOnce.js
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}`,
)
}
}

0 comments on commit 9c67b80

Please sign in to comment.