Skip to content

Commit

Permalink
✅ Test: Add 100% test coverage to vm handlers (#812) (#813)
Browse files Browse the repository at this point in the history
## Description

Add 100% test coverage to the procedures package. The procedures package
contains the vast amount of logic for tevm.

## Testing

bun test

## Additional Information

- [x] 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

- **Refactor**
	- Improved readability and formatting in JSON-RPC procedure handling.
- **Tests**
	- Enhanced testing suite with new scenarios for EVM script execution.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Will Cory <[email protected]>
  • Loading branch information
roninjin10 and Will Cory authored Jan 12, 2024
1 parent b07f2a3 commit 9973b1d
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 91 deletions.
40 changes: 20 additions & 20 deletions vm/procedures/src/jsonrpc/callProcedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,25 @@ export const callProcedure = (evm) => async (request) => {
: {}),
...(request.params.block
? {
...(request.params.block.gasLimit
? { gasLimit: hexToBigInt(request.params.block.gasLimit) }
: {}),
...(request.params.block.baseFeePerGas
? { baseFeePerGas: hexToBigInt(request.params.block.baseFeePerGas) }
: {}),
...(request.params.block.blobGasPrice
? { blobGasPrice: hexToBigInt(request.params.block.blobGasPrice) }
: {}),
...(request.params.block.difficulty
? { difficulty: hexToBigInt(request.params.block.difficulty) }
: {}),
...(request.params.block.number
? { number: hexToBigInt(request.params.block.number) }
: {}),
...(request.params.block.timestamp
? { timestamp: hexToBigInt(request.params.block.timestamp) }
: {}),
}
...(request.params.block.gasLimit
? { gasLimit: hexToBigInt(request.params.block.gasLimit) }
: {}),
...(request.params.block.baseFeePerGas
? { baseFeePerGas: hexToBigInt(request.params.block.baseFeePerGas) }
: {}),
...(request.params.block.blobGasPrice
? { blobGasPrice: hexToBigInt(request.params.block.blobGasPrice) }
: {}),
...(request.params.block.difficulty
? { difficulty: hexToBigInt(request.params.block.difficulty) }
: {}),
...(request.params.block.number
? { number: hexToBigInt(request.params.block.number) }
: {}),
...(request.params.block.timestamp
? { timestamp: hexToBigInt(request.params.block.timestamp) }
: {}),
}
: {}),
})
if (errors.length > 0) {
Expand All @@ -81,7 +81,7 @@ export const callProcedure = (evm) => async (request) => {
* @param {bigint} value
* @returns {import('viem').Hex}
*/
const toHex = (value) => /**@type {import('viem').Hex}*/(bigIntToHex(value))
const toHex = (value) => /**@type {import('viem').Hex}*/ (bigIntToHex(value))
return {
jsonrpc: '2.0',
result: {
Expand Down
48 changes: 24 additions & 24 deletions vm/procedures/src/jsonrpc/scriptProcedure.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@ export const scriptProcedure = (evm) => async (request) => {
: {}),
...(request.params.block
? {
...(request.params.block.gasLimit
? { gasLimit: hexToBigInt(request.params.block.gasLimit) }
: {}),
...(request.params.block.baseFeePerGas
? {
baseFeePerGas: hexToBigInt(
request.params.block.baseFeePerGas,
),
}
: {}),
...(request.params.block.blobGasPrice
? { blobGasPrice: hexToBigInt(request.params.block.blobGasPrice) }
: {}),
...(request.params.block.difficulty
? { difficulty: hexToBigInt(request.params.block.difficulty) }
: {}),
...(request.params.block.number
? { number: hexToBigInt(request.params.block.number) }
: {}),
...(request.params.block.timestamp
? { timestamp: hexToBigInt(request.params.block.timestamp) }
: {}),
}
...(request.params.block.gasLimit
? { gasLimit: hexToBigInt(request.params.block.gasLimit) }
: {}),
...(request.params.block.baseFeePerGas
? {
baseFeePerGas: hexToBigInt(
request.params.block.baseFeePerGas,
),
}
: {}),
...(request.params.block.blobGasPrice
? { blobGasPrice: hexToBigInt(request.params.block.blobGasPrice) }
: {}),
...(request.params.block.difficulty
? { difficulty: hexToBigInt(request.params.block.difficulty) }
: {}),
...(request.params.block.number
? { number: hexToBigInt(request.params.block.number) }
: {}),
...(request.params.block.timestamp
? { timestamp: hexToBigInt(request.params.block.timestamp) }
: {}),
}
: {}),
})
} catch (e) {
Expand Down Expand Up @@ -108,7 +108,7 @@ export const scriptProcedure = (evm) => async (request) => {
/**
* @param {bigint} value
*/
const toHex = (value) => /**@type {import('viem').Hex}*/(bigIntToHex(value))
const toHex = (value) => /**@type {import('viem').Hex}*/ (bigIntToHex(value))
return {
jsonrpc: '2.0',
result: {
Expand Down
159 changes: 112 additions & 47 deletions vm/procedures/src/requestProcedure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,21 +332,17 @@ describe('requestProcedure', () => {
})
expect(res).toEqual({
error: {
message: "UnexpectedError: unexpected error",
code: "UnexpectedError",
message: 'UnexpectedError: unexpected error',
code: 'UnexpectedError',
...{
data: {
errors: [
"UnexpectedError: unexpected error"
],
}
errors: ['UnexpectedError: unexpected error'],
},
},

},
id: 1,
jsonrpc: "2.0",
method: "tevm_account",

jsonrpc: '2.0',
method: 'tevm_account',
})
})
})
Expand Down Expand Up @@ -394,9 +390,8 @@ describe('requestProcedure', () => {
exceptionError: {
error: 'revert',
errorType: 'Contract was reverted',
} as any
}

} as any,
},
}
}
const to = `0x${'69'.repeat(20)}` as const
Expand All @@ -414,52 +409,122 @@ describe('requestProcedure', () => {
}),
).toEqual({
error: {
code: "revert",
code: 'revert',
...{
data: {
errors: [
"There was an error executing the evm"
],
errors: ['There was an error executing the evm'],
},
message: "There was an error executing the evm",
}
message: 'There was an error executing the evm',
},
},
id: 1,
jsonrpc: "2.0",
method: "tevm_call",
jsonrpc: '2.0',
method: 'tevm_call',
})
})
})

it('tevm_script', async () => {
expect(
await scriptProcedure(new EVM({}))({
jsonrpc: '2.0',
describe('tevm_script', async () => {
it('should work', async () => {
expect(
await scriptProcedure(new EVM({}))({
jsonrpc: '2.0',
method: 'tevm_script',
id: 1,
params: {
deployedBytecode: ERC20_BYTECODE,
data: encodeFunctionData({
abi: ERC20_ABI,
functionName: 'balanceOf',
args: [ERC20_ADDRESS],
}),
to: ERC20_ADDRESS,
},
}),
).toEqual({
method: 'tevm_script',
jsonrpc: '2.0',
id: 1,
params: {
deployedBytecode: ERC20_BYTECODE,
data: encodeFunctionData({
abi: ERC20_ABI,
functionName: 'balanceOf',
args: [ERC20_ADDRESS],
}),
to: ERC20_ADDRESS,
result: {
rawData:
'0x0000000000000000000000000000000000000000000000000000000000000000',
executionGasUsed: numberToHex(2447n),
selfdestruct: [],
gas: numberToHex(16774768n),
logs: [],
createdAddresses: [],
},
}),
).toEqual({
method: 'tevm_script',
jsonrpc: '2.0',
id: 1,
result: {
rawData:
'0x0000000000000000000000000000000000000000000000000000000000000000',
executionGasUsed: numberToHex(2447n),
selfdestruct: [],
gas: numberToHex(16774768n),
logs: [],
createdAddresses: [],
},
})
})

it('should handle the evm throwing an error', async () => {
const evm = new EVM({})
const caller = `0x${'69'.repeat(20)}` as const
expect(
await scriptProcedure(evm)({
jsonrpc: '2.0',
method: 'tevm_script',
id: 1,
params: {
deployedBytecode: ERC20_BYTECODE,
data: encodeFunctionData({
abi: ERC20_ABI,
functionName: 'transferFrom',
args: [caller, caller, 420n],
}),
to: ERC20_ADDRESS,
},
}),
).toEqual({
error: {
code: 'revert',
...{
data: {
errors: ['There was an error executing the evm'],
},
},
message: 'There was an error executing the evm',
},
id: 1,
jsonrpc: '2.0',
method: 'tevm_script',
})
})

it('should handle the handler function unexpectedly throwing', async () => {
const evm = new EVM({})
evm.runCall = async () => {
throw new Error('unexpected error')
}
expect(
await scriptProcedure(evm)({
jsonrpc: '2.0',
method: 'tevm_script',
id: 1,
params: {
deployedBytecode: ERC20_BYTECODE,
data: encodeFunctionData({
abi: ERC20_ABI,
functionName: 'balanceOf',
args: [ERC20_ADDRESS],
}),
to: ERC20_ADDRESS,
},
}),
).toEqual({
error: {
code: 'UnexpectedError',
...{
data: {
errors: ['unexpected error'],
},
},
message: 'unexpected error',
},
id: 1,
jsonrpc: '2.0',
method: 'tevm_script',
})
})
})
})

1 comment on commit 9973b1d

@vercel
Copy link

@vercel vercel bot commented on 9973b1d Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

evmts-docs – ./

evmts-docs-git-main-evmts.vercel.app
evmts-docs-evmts.vercel.app
evmts.dev

Please sign in to comment.