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 08c2b43
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/blockchain/docs/functions/createChain.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/blockchain/docs/functions/getBlockFromRpc.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions packages/blockchain/docs/type-aliases/Chain.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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)

Check failure on line 48 in packages/blockchain/src/utils/getBlockFromRpc.spec.ts

View workflow job for this annotation

GitHub Actions / Nx Cloud - Main Job

error: expect(received).toBeInstanceOf(expected)

Expected constructor: [class (anonymous)] Received value: 99 | if (!result) { 100 | throw new Error('No block found') 101 | } 102 | return asEthjsBlock(/** @type {any}*/ (result)) 103 | } 104 | throw new Error(`Invalid blocktag ${blockTag}`) ^ error: Invalid blocktag invalid-tag at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.js:104:10 at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.js:50:3 at /home/runner/work/tevm-monorepo/tevm-monorepo/node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]/node_modules/viem/_esm/utils/promise/withRetry.js:12:36 at attemptRetry (/home/runner/work/tevm-monorepo/tevm-monorepo/node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]/node_modules/viem/_esm/utils/promise/withRetry.js:4:39) at /home/runner/work/tevm-monorepo/tevm-monorepo/node_modules/.pnpm/[email protected][email protected][email protected][email protected][email protected]/node_modules/viem/_esm/utils/promise/withRetry.js:9:17 at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.spec.ts:48:15
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

View workflow job for this annotation

GitHub Actions / Nx Cloud - Main Job

error: expect(received).toBeInstanceOf(expected)

Expected constructor: [class (anonymous)] Received value: 61 | ) 62 | if (error) { 63 | throw error 64 | } 65 | if (!result) { 66 | throw new Error('No block found') ^ error: No block found at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.js:66:12 at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.spec.ts:61:15
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

View workflow job for this annotation

GitHub Actions / Nx Cloud - Main Job

error: expect(received).toBeInstanceOf(expected)

Expected constructor: [class (anonymous)] Received value: 78 | console.error(error) 79 | throw error 80 | } 81 | if (!result) { 82 | console.error(error) 83 | throw new Error('No block found') ^ error: No block found at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.js:83:12 at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.spec.ts:71:15
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

View workflow job for this annotation

GitHub Actions / Nx Cloud - Main Job

error: expect(received).toHaveBeenCalledTimes(expected)

Expected number of calls: 1 Received number of calls: 0 at /home/runner/work/tevm-monorepo/tevm-monorepo/packages/blockchain/src/utils/getBlockFromRpc.spec.ts:87:26
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}`,
)
}
}
6 changes: 5 additions & 1 deletion packages/procedures/src/utils/blockToJsonRpcBlock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { blockToJsonRpcBlock } from './blockToJsonRpcBlock.js'
describe('blockToJsonRpcBlock', async () => {
const client = createBaseClient({ common: optimism })
const vm = await client.getVm()
const block = await getBlockFromRpc({ blockTag: 121960766n, transport: transports.optimism }, vm.common)
const block = await getBlockFromRpc(
vm.blockchain,
{ blockTag: 121960766n, transport: transports.optimism },
vm.common,
)

it('should convert block to JSON-RPC block format with transactions', async () => {
expect(await blockToJsonRpcBlock(block, true)).toMatchSnapshot()
Expand Down
6 changes: 5 additions & 1 deletion packages/procedures/src/utils/txToJsonRpcTx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ describe(txToJsonRpcTx.name, () => {
common: optimism,
})
const vm = await client.getVm()
const block = await getBlockFromRpc({ blockTag: 121960766n, transport: transports.optimism }, vm.common)
const block = await getBlockFromRpc(
vm.blockchain,
{ blockTag: 121960766n, transport: transports.optimism },
vm.common,
)
expect(txToJsonRpcTx(tx, block, 0)).toMatchSnapshot()
})
})
4 changes: 3 additions & 1 deletion tevm/docs/blockchain/functions/getBlockFromRpc.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions tevm/docs/blockchain/type-aliases/Chain.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08c2b43

Please sign in to comment.