Skip to content

Commit

Permalink
🧪 Test: Add more procedures tests (#1247)
Browse files Browse the repository at this point in the history
## Description

_Concise description of proposed changes_

## Testing

Explain the quality checks that have been done on the code changes

## Additional Information

- [ ] I read the [contributing docs](../docs/contributing.md) (if this
is your first contribution)

Your ENS/address:



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced `getBlockFromRpc` function in the blockchain package,
enabling retrieval of blocks from an RPC server.
- **Documentation**
- Updated blockchain package documentation to include details on the new
`getBlockFromRpc` function.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: William Cory <[email protected]>
  • Loading branch information
roninjin10 and William Cory authored Jul 2, 2024
1 parent cb2dd84 commit d1d20e8
Show file tree
Hide file tree
Showing 54 changed files with 3,294 additions and 3 deletions.
29 changes: 29 additions & 0 deletions packages/blockchain/docs/functions/getBlockFromRpc.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/docs/globals.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/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export {
setIteratorHead,
getCanonicalHeadBlock,
} from './actions/index.js'
export { getBlockFromRpc } from './utils/getBlockFromRpc.js'
export type { Chain } from './Chain.js'
export type { ChainOptions } from './ChainOptions.js'
3 changes: 3 additions & 0 deletions packages/procedures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@
"@tevm/actions": "workspace:^",
"@tevm/base-client": "workspace:^",
"@tevm/block": "workspace:^",
"@tevm/blockchain": "workspace:^",
"@tevm/common": "workspace:^",
"@tevm/contract": "workspace:^",
"@tevm/errors": "workspace:^",
"@tevm/evm": "workspace:^",
"@tevm/jsonrpc": "workspace:^",
"@tevm/state": "workspace:^",
"@tevm/test-utils": "workspace:^",
"@tevm/tx": "workspace:^",
"@tevm/utils": "workspace:^",
"@tevm/vm": "workspace:^"
Expand Down
137 changes: 137 additions & 0 deletions packages/procedures/src/__snapshots__/createHandlers.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`createHandlers should handle tevm_mine 1`] = `
{
"id": 1,
"jsonrpc": "2.0",
"method": "tevm_mine",
"result": {
"blockHashes": [
"0x3201277b6823bbcd45e1a1c4a334c31b083676c70cebb1f386139f6091ae8e49",
],
},
}
`;

exports[`createHandlers should handle eth_chainId 1`] = `
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_chainId",
"result": "0x384",
}
`;

exports[`createHandlers should handle eth_call 1`] = `
{
"error": {
"code": -32000,
"data": {
"errors": [
"sender doesn't have enough funds to send tx. The upfront cost is: 210000000 and the sender's account (0x6969696969696969696969696969696969696969) only has: 0 -> block number=0 hash=0x63b5bacdb65c8c9d10e77d77d40bda5b4dc010a4f7e02a10bdea21a2b1b0a454 hf=cancun baseFeePerGas=7 txs=0 uncles=0 -> tx type=2 hash=0x96171020921a6736a4e62d65c40b749609224fe43187d437cb37d19b3c011005 nonce=0 value=0 signed=true hf=cancun maxFeePerGas=7 maxPriorityFeePerGas=0)
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Details: /reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73"
,
],
},
"message":
"sender doesn't have enough funds to send tx. The upfront cost is: 210000000 and the sender's account (0x6969696969696969696969696969696969696969) only has: 0 -> block number=0 hash=0x63b5bacdb65c8c9d10e77d77d40bda5b4dc010a4f7e02a10bdea21a2b1b0a454 hf=cancun baseFeePerGas=7 txs=0 uncles=0 -> tx type=2 hash=0x96171020921a6736a4e62d65c40b749609224fe43187d437cb37d19b3c011005 nonce=0 value=0 signed=true hf=cancun maxFeePerGas=7 maxPriorityFeePerGas=0)
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Details: /reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73"
,
},
"id": 1,
"jsonrpc": "2.0",
"method": "eth_call",
}
`;

exports[`createHandlers should handle eth_getBalance 1`] = `
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_getBalance",
"result": "0x0",
}
`;

exports[`createHandlers should handle eth_estimateGas 1`] = `
{
"error": {
"code": -32000,
"data": {
"errors": [
"sender doesn't have enough funds to send tx. The upfront cost is: 210000000 and the sender's account (0x6969696969696969696969696969696969696969) only has: 0 -> block number=0 hash=0x63b5bacdb65c8c9d10e77d77d40bda5b4dc010a4f7e02a10bdea21a2b1b0a454 hf=cancun baseFeePerGas=7 txs=0 uncles=0 -> tx type=2 hash=0x96171020921a6736a4e62d65c40b749609224fe43187d437cb37d19b3c011005 nonce=0 value=0 signed=true hf=cancun maxFeePerGas=7 maxPriorityFeePerGas=0)
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Details: /reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73"
,
],
},
"message":
"sender doesn't have enough funds to send tx. The upfront cost is: 210000000 and the sender's account (0x6969696969696969696969696969696969696969) only has: 0 -> block number=0 hash=0x63b5bacdb65c8c9d10e77d77d40bda5b4dc010a4f7e02a10bdea21a2b1b0a454 hf=cancun baseFeePerGas=7 txs=0 uncles=0 -> tx type=2 hash=0x96171020921a6736a4e62d65c40b749609224fe43187d437cb37d19b3c011005 nonce=0 value=0 signed=true hf=cancun maxFeePerGas=7 maxPriorityFeePerGas=0)
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Details: /reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73"
,
},
"id": 1,
"jsonrpc": "2.0",
"method": "eth_estimateGas",
}
`;

exports[`createHandlers should handle tevm_contract 1`] = `
{
"error": {
"code": "MethodNotSupported",
"message":
"UnsupportedMethodError: tevm_contract is not supported. Encode the contract arguments and use tevm_call instead.
Docs: https://tevm.sh/reference/tevm/errors/classes/methodnotsupportederror/
Version: 1.1.0.next-73"
,
},
"id": 1,
"jsonrpc": "2.0",
"method": "tevm_contract",
}
`;

exports[`createHandlers should handle eth_mining 1`] = `
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_mining",
"result": false,
}
`;

exports[`createHandlers should handle eth_syncing 1`] = `
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_syncing",
"result": false,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Bun Snapshot v1, https://goo.gl/fbAQLP

exports[`callProcedure should handle a basic call 1`] = `
{
"amountSpent": "0x296a6",
"createdAddresses": [],
"executionGasUsed": "0xc62",
"gas": "0x1c964d6",
"logs": [],
"rawData": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
"selfdestruct": [],
"totalGasSpent": "0x5eaa",
}
`;

exports[`callProcedure should handle a call with state override 1`] = `
{
"amountSpent": "0x23e38",
"executionGasUsed": "0x0",
"rawData": "0x",
"totalGasSpent": "0x5208",
}
`;

exports[`callProcedure should handle errors from callHandler 1`] = `
{
"code": -32000,
"data": {
"errors": [
"sender doesn't have enough funds to send tx. The upfront cost is: 210000500 and the sender's account (0x4242424242424242424242424242424242424242) only has: 0 -> block number=0 hash=0x63b5bacdb65c8c9d10e77d77d40bda5b4dc010a4f7e02a10bdea21a2b1b0a454 hf=cancun baseFeePerGas=7 txs=0 uncles=0 -> tx type=2 hash=0x66575cf759d7c97e42f103bfc8de6f88f70ea97a785d0c1f558496abf977cae1 nonce=0 value=500 signed=true hf=cancun maxFeePerGas=7 maxPriorityFeePerGas=0)
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Details: /reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73"
,
],
},
"message":
"sender doesn't have enough funds to send tx. The upfront cost is: 210000500 and the sender's account (0x4242424242424242424242424242424242424242) only has: 0 -> block number=0 hash=0x63b5bacdb65c8c9d10e77d77d40bda5b4dc010a4f7e02a10bdea21a2b1b0a454 hf=cancun baseFeePerGas=7 txs=0 uncles=0 -> tx type=2 hash=0x66575cf759d7c97e42f103bfc8de6f88f70ea97a785d0c1f558496abf977cae1 nonce=0 value=500 signed=true hf=cancun maxFeePerGas=7 maxPriorityFeePerGas=0)
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73
Docs: https://tevm.sh/reference/tevm/errors/classes/insufficientfundserror/
Details: /reference/tevm/errors/classes/insufficientfundserror/
Version: 1.1.0.next-73"
,
}
`;
105 changes: 105 additions & 0 deletions packages/procedures/src/call/callProcedure.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { beforeEach, describe, expect, it } from 'bun:test'
import { setAccountHandler } from '@tevm/actions'
import { type BaseClient, createBaseClient } from '@tevm/base-client'
import { ERC20 } from '@tevm/contract'
import { encodeFunctionData, numberToHex, parseEther } from '@tevm/utils'
import type { CallJsonRpcRequest } from './CallJsonRpcRequest.js'
import { callProcedure } from './callProcedure.js'

let client: BaseClient

beforeEach(() => {
client = createBaseClient()
})

describe('callProcedure', () => {
it('should handle a basic call', async () => {
const address = `0x${'69'.repeat(20)}` as const

await setAccountHandler(client)({
address,
balance: 420n,
nonce: 69n,
deployedBytecode: ERC20.deployedBytecode,
state: {
'0x0': '0x01',
},
})

const request: CallJsonRpcRequest = {
jsonrpc: '2.0',
method: 'tevm_call',
id: 1,
params: [
{
to: address,
data: encodeFunctionData(ERC20.read.name()),
gas: numberToHex(21000n),
gasPrice: numberToHex(1n),
},
],
}

const response = await callProcedure(client)(request)
expect(response.error).toBeUndefined()
expect(response.result).toBeDefined()
expect(response.method).toBe('tevm_call')
expect(response.id).toBe(request.id as any)
expect(response.result).toMatchSnapshot()
})

it('should handle a call with state override', async () => {
const to = `0x${'69'.repeat(20)}` as const
const from = `0x${'42'.repeat(20)}` as const
const request: CallJsonRpcRequest = {
jsonrpc: '2.0',
method: 'tevm_call',
id: 1,
params: [
{
to,
from,
value: numberToHex(parseEther('.9')),
},
{
[from]: {
balance: numberToHex(parseEther('1')),
},
},
],
}

const response = await callProcedure(client)(request)
expect(response.error).toBeUndefined()
expect(response.result).toBeDefined()
expect(response.method).toBe('tevm_call')
expect(response.id).toBe(request.id as any)
expect(response.result).toMatchSnapshot()
})

it.todo('should handle a call with block override', async () => {})

it('should handle errors from callHandler', async () => {
const request: CallJsonRpcRequest = {
jsonrpc: '2.0',
method: 'tevm_call',
id: 1,
params: [
{
to: `0x${'00'.repeat(20)}` as const, // Invalid address
from: `0x${'42'.repeat(20)}` as const,
data: '0x0',
value: numberToHex(500n),
gas: numberToHex(21000n),
gasPrice: numberToHex(1n),
},
],
}

const response = await callProcedure(client)(request)
expect(response.error).toBeDefined()
expect(response.error).toMatchSnapshot()
expect(response.method).toBe('tevm_call')
expect(response.id).toBe(request.id as any)
})
})
Loading

0 comments on commit d1d20e8

Please sign in to comment.