Skip to content

Commit

Permalink
feat: Add copy circuit stats by exec state (scroll-tech#1222)
Browse files Browse the repository at this point in the history
### Description

feat: Add copy circuit stats by exec state like the ones we had for evm
circuit and state circuit.

refactor: move duplicate code used to get circuit stats by exec state
into a new module, which also contains a helper struct to draw formatted
tables

feat: add the `warn-unimplemented` fature in `zkevm-circuits` so that it
can be enabled/disabled when using the zkevm-circuits crate.

### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Contents

Here are the stats for the Copy Circuit

| state          | opcode         | h    | g     | h/g    |
| -------------- | -------------- | ---- | ----- | ------ |
| CALLDATACOPY   | CALLDATACOPY   | 8192 | 803   | 10.202 |
| CODECOPY       | CODECOPY       | 8192 | 803   | 10.202 |
| EXTCODECOPY    | EXTCODECOPY    | 8192 | 900   | 9.102  |
| RETURNDATACOPY | RETURNDATACOPY | 8192 | 803   | 10.202 |
| LOG            | LOG0           | 8192 | 33559 | 0.244  |
| LOG            | LOG1           | 8192 | 33934 | 0.241  |
| LOG            | LOG2           | 8192 | 34309 | 0.239  |
| LOG            | LOG3           | 8192 | 34684 | 0.236  |
| LOG            | LOG4           | 8192 | 35059 | 0.234  |
| RETURN_REVERT  | RETURN         | 8192 | 416   | 19.692 |
| RETURN_REVERT  | REVERT         | 8192 | 416   | 19.692 |

<details><summary>Updated stats for EVM Circuit</summary>

| state          | opcode         | h  | g     | h/g   |
| -------------- | -------------- | -- | ----- | ----- |
| STOP           | STOP           | 2  | 0     | inf   |
| ADD_SUB        | ADD            | 5  | 3     | 1.667 |
| ADD_SUB        | SUB            | 5  | 3     | 1.667 |
| MUL_DIV_MOD    | MUL            | 8  | 5     | 1.600 |
| MUL_DIV_MOD    | DIV            | 8  | 5     | 1.600 |
| MUL_DIV_MOD    | MOD            | 8  | 5     | 1.600 |
| SDIV_SMOD      | SDIV           | 19 | 5     | 3.800 |
| SDIV_SMOD      | SMOD           | 19 | 5     | 3.800 |
| SHL_SHR        | SHL            | 10 | 3     | 3.333 |
| SHL_SHR        | SHR            | 10 | 3     | 3.333 |
| ADDMOD         | ADDMOD         | 17 | 8     | 2.125 |
| MULMOD         | MULMOD         | 20 | 8     | 2.500 |
| EXP            | EXP            | 7  | 60    | 0.117 |
| SIGNEXTEND     | SIGNEXTEND     | 3  | 5     | 0.600 |
| CMP            | LT             | 5  | 3     | 1.667 |
| CMP            | GT             | 5  | 3     | 1.667 |
| CMP            | EQ             | 5  | 3     | 1.667 |
| SCMP           | SLT            | 5  | 3     | 1.667 |
| SCMP           | SGT            | 5  | 3     | 1.667 |
| ISZERO         | ISZERO         | 2  | 3     | 0.667 |
| BITWISE        | AND            | 5  | 3     | 1.667 |
| BITWISE        | OR             | 5  | 3     | 1.667 |
| BITWISE        | XOR            | 5  | 3     | 1.667 |
| NOT            | NOT            | 5  | 3     | 1.667 |
| BYTE           | BYTE           | 3  | 3     | 1.000 |
| SAR            | SAR            | 10 | 3     | 3.333 |
| SHA3           | SHA3           | 4  | 57    | 0.070 |
| ADDRESS        | ADDRESS        | 2  | 2     | 1.000 |
| BALANCE        | BALANCE        | 2  | 2600  | 0.001 |
| ORIGIN         | ORIGIN         | 2  | 2     | 1.000 |
| CALLER         | CALLER         | 2  | 2     | 1.000 |
| CALLVALUE      | CALLVALUE      | 1  | 2     | 0.500 |
| CALLDATALOAD   | CALLDATALOAD   | 8  | 3     | 2.667 |
| CALLDATASIZE   | CALLDATASIZE   | 1  | 2     | 0.500 |
| CALLDATACOPY   | CALLDATACOPY   | 3  | 21    | 0.143 |
| CODESIZE       | CODESIZE       | 1  | 2     | 0.500 |
| CODECOPY       | CODECOPY       | 3  | 21    | 0.143 |
| GASPRICE       | GASPRICE       | 1  | 2     | 0.500 |
| EXTCODESIZE    | EXTCODESIZE    | 2  | 2600  | 0.001 |
| EXTCODECOPY    | EXTCODECOPY    | 4  | 2612  | 0.002 |
| RETURNDATASIZE | RETURNDATASIZE | 1  | 2     | 0.500 |
| RETURNDATACOPY | RETURNDATACOPY | 3  | 9     | 0.333 |
| EXTCODEHASH    | EXTCODEHASH    | 2  | 2600  | 0.001 |
| BLOCKHASH      | BLOCKHASH      | 3  | 20    | 0.150 |
| BLOCKCTXU64    | TIMESTAMP      | 1  | 2     | 0.500 |
| BLOCKCTXU64    | NUMBER         | 1  | 2     | 0.500 |
| BLOCKCTXU64    | GASLIMIT       | 1  | 2     | 0.500 |
| BLOCKCTXU160   | COINBASE       | 2  | 2     | 1.000 |
| BLOCKCTXU256   | DIFFICULTY     | 2  | 2     | 1.000 |
| BLOCKCTXU256   | BASEFEE        | 2  | 2     | 1.000 |
| CHAINID        | CHAINID        | 1  | 2     | 0.500 |
| SELFBALANCE    | SELFBALANCE    | 1  | 5     | 0.200 |
| POP            | POP            | 1  | 2     | 0.500 |
| MEMORY         | MLOAD          | 5  | 15    | 0.333 |
| MEMORY         | MSTORE         | 5  | 15    | 0.333 |
| MEMORY         | MSTORE8        | 5  | 12    | 0.417 |
| SLOAD          | SLOAD          | 2  | 2100  | 0.001 |
| SSTORE         | SSTORE         | 12 | 22100 | 0.001 |
| JUMP           | JUMP           | 1  | 8     | 0.125 |
| JUMPI          | JUMPI          | 3  | 10    | 0.300 |
| PC             | PC             | 1  | 2     | 0.500 |
| MSIZE          | MSIZE          | 1  | 2     | 0.500 |
| GAS            | GAS            | 1  | 2     | 0.500 |
| JUMPDEST       | JUMPDEST       | 1  | 1     | 1.000 |
| PUSH           | PUSH1          | 9  | 3     | 3.000 |
| PUSH           | PUSH2          | 9  | 3     | 3.000 |
| PUSH           | PUSH3          | 9  | 3     | 3.000 |
| PUSH           | PUSH4          | 9  | 3     | 3.000 |
| PUSH           | PUSH5          | 9  | 3     | 3.000 |
| PUSH           | PUSH6          | 9  | 3     | 3.000 |
| PUSH           | PUSH7          | 9  | 3     | 3.000 |
| PUSH           | PUSH8          | 9  | 3     | 3.000 |
| PUSH           | PUSH9          | 9  | 3     | 3.000 |
| PUSH           | PUSH10         | 9  | 3     | 3.000 |
| PUSH           | PUSH11         | 9  | 3     | 3.000 |
| PUSH           | PUSH12         | 9  | 3     | 3.000 |
| PUSH           | PUSH13         | 9  | 3     | 3.000 |
| PUSH           | PUSH14         | 9  | 3     | 3.000 |
| PUSH           | PUSH15         | 9  | 3     | 3.000 |
| PUSH           | PUSH16         | 9  | 3     | 3.000 |
| PUSH           | PUSH17         | 9  | 3     | 3.000 |
| PUSH           | PUSH18         | 9  | 3     | 3.000 |
| PUSH           | PUSH19         | 9  | 3     | 3.000 |
| PUSH           | PUSH20         | 9  | 3     | 3.000 |
| PUSH           | PUSH21         | 9  | 3     | 3.000 |
| PUSH           | PUSH22         | 9  | 3     | 3.000 |
| PUSH           | PUSH23         | 9  | 3     | 3.000 |
| PUSH           | PUSH24         | 9  | 3     | 3.000 |
| PUSH           | PUSH25         | 9  | 3     | 3.000 |
| PUSH           | PUSH26         | 9  | 3     | 3.000 |
| PUSH           | PUSH27         | 9  | 3     | 3.000 |
| PUSH           | PUSH28         | 9  | 3     | 3.000 |
| PUSH           | PUSH29         | 9  | 3     | 3.000 |
| PUSH           | PUSH30         | 9  | 3     | 3.000 |
| PUSH           | PUSH31         | 9  | 3     | 3.000 |
| PUSH           | PUSH32         | 9  | 3     | 3.000 |
| DUP            | DUP1           | 1  | 3     | 0.333 |
| DUP            | DUP2           | 1  | 3     | 0.333 |
| DUP            | DUP3           | 1  | 3     | 0.333 |
| DUP            | DUP4           | 1  | 3     | 0.333 |
| DUP            | DUP5           | 1  | 3     | 0.333 |
| DUP            | DUP6           | 1  | 3     | 0.333 |
| DUP            | DUP7           | 1  | 3     | 0.333 |
| DUP            | DUP8           | 1  | 3     | 0.333 |
| DUP            | DUP9           | 1  | 3     | 0.333 |
| DUP            | DUP10          | 1  | 3     | 0.333 |
| DUP            | DUP11          | 1  | 3     | 0.333 |
| DUP            | DUP12          | 1  | 3     | 0.333 |
| DUP            | DUP13          | 1  | 3     | 0.333 |
| DUP            | DUP14          | 1  | 3     | 0.333 |
| DUP            | DUP15          | 1  | 3     | 0.333 |
| DUP            | DUP16          | 1  | 3     | 0.333 |
| SWAP           | SWAP1          | 1  | 3     | 0.333 |
| SWAP           | SWAP2          | 1  | 3     | 0.333 |
| SWAP           | SWAP3          | 1  | 3     | 0.333 |
| SWAP           | SWAP4          | 1  | 3     | 0.333 |
| SWAP           | SWAP5          | 1  | 3     | 0.333 |
| SWAP           | SWAP6          | 1  | 3     | 0.333 |
| SWAP           | SWAP7          | 1  | 3     | 0.333 |
| SWAP           | SWAP8          | 1  | 3     | 0.333 |
| SWAP           | SWAP9          | 1  | 3     | 0.333 |
| SWAP           | SWAP10         | 1  | 3     | 0.333 |
| SWAP           | SWAP11         | 1  | 3     | 0.333 |
| SWAP           | SWAP12         | 1  | 3     | 0.333 |
| SWAP           | SWAP13         | 1  | 3     | 0.333 |
| SWAP           | SWAP14         | 1  | 3     | 0.333 |
| SWAP           | SWAP15         | 1  | 3     | 0.333 |
| SWAP           | SWAP16         | 1  | 3     | 0.333 |
| LOG            | LOG0           | 2  | 902   | 0.002 |
| LOG            | LOG1           | 2  | 1277  | 0.002 |
| LOG            | LOG2           | 2  | 1652  | 0.001 |
| LOG            | LOG3           | 2  | 2027  | 0.001 |
| LOG            | LOG4           | 2  | 2402  | 0.001 |
| CALL_OP        | CALL           | 21 | 36686 | 0.001 |
| CALL_OP        | CALLCODE       | 21 | 11686 | 0.002 |
| CALL_OP        | DELEGATECALL   | 21 | 2689  | 0.008 |
| CALL_OP        | STATICCALL     | 21 | 2689  | 0.008 |
| RETURN_REVERT  | RETURN         | 3  | 15    | 0.200 |
| RETURN_REVERT  | REVERT         | 3  | 15    | 0.200 |

</details>

<details><summary>Updated stats for State Circuit</summary>

| state          | opcode         | h    | g     | h/g    |
| -------------- | -------------- | ---- | ----- | ------ |
| STOP           | STOP           | 13   | 0     | inf    |
| ADD_SUB        | ADD            | 3    | 3     | 1.000  |
| ADD_SUB        | SUB            | 3    | 3     | 1.000  |
| MUL_DIV_MOD    | MUL            | 3    | 5     | 0.600  |
| MUL_DIV_MOD    | DIV            | 3    | 5     | 0.600  |
| MUL_DIV_MOD    | MOD            | 3    | 5     | 0.600  |
| SDIV_SMOD      | SDIV           | 3    | 5     | 0.600  |
| SDIV_SMOD      | SMOD           | 3    | 5     | 0.600  |
| SHL_SHR        | SHL            | 3    | 3     | 1.000  |
| SHL_SHR        | SHR            | 3    | 3     | 1.000  |
| ADDMOD         | ADDMOD         | 4    | 8     | 0.500  |
| MULMOD         | MULMOD         | 4    | 8     | 0.500  |
| EXP            | EXP            | 3    | 60    | 0.050  |
| SIGNEXTEND     | SIGNEXTEND     | 3    | 5     | 0.600  |
| CMP            | LT             | 3    | 3     | 1.000  |
| CMP            | GT             | 3    | 3     | 1.000  |
| CMP            | EQ             | 3    | 3     | 1.000  |
| SCMP           | SLT            | 3    | 3     | 1.000  |
| SCMP           | SGT            | 3    | 3     | 1.000  |
| ISZERO         | ISZERO         | 2    | 3     | 0.667  |
| BITWISE        | AND            | 3    | 3     | 1.000  |
| BITWISE        | OR             | 3    | 3     | 1.000  |
| BITWISE        | XOR            | 3    | 3     | 1.000  |
| NOT            | NOT            | 2    | 3     | 0.667  |
| BYTE           | BYTE           | 3    | 3     | 1.000  |
| SAR            | SAR            | 3    | 3     | 1.000  |
| SHA3           | SHA3           | 4099 | 1214  | 3.376  |
| ADDRESS        | ADDRESS        | 2    | 2     | 1.000  |
| BALANCE        | BALANCE        | 7    | 2600  | 0.003  |
| ORIGIN         | ORIGIN         | 2    | 2     | 1.000  |
| CALLER         | CALLER         | 2    | 2     | 1.000  |
| CALLVALUE      | CALLVALUE      | 2    | 2     | 1.000  |
| CALLDATALOAD   | CALLDATALOAD   | 5    | 3     | 1.667  |
| CALLDATASIZE   | CALLDATASIZE   | 2    | 2     | 1.000  |
| CALLDATACOPY   | CALLDATACOPY   | 4102 | 803   | 5.108  |
| CODESIZE       | CODESIZE       | 1    | 2     | 0.500  |
| CODECOPY       | CODECOPY       | 4099 | 803   | 5.105  |
| GASPRICE       | GASPRICE       | 2    | 2     | 1.000  |
| EXTCODESIZE    | EXTCODESIZE    | 7    | 2600  | 0.003  |
| EXTCODECOPY    | EXTCODECOPY    | 4105 | 900   | 4.561  |
| RETURNDATASIZE | RETURNDATASIZE | 2    | 2     | 1.000  |
| RETURNDATACOPY | RETURNDATACOPY | 8198 | 803   | 10.209 |
| EXTCODEHASH    | EXTCODEHASH    | 7    | 2600  | 0.003  |
| BLOCKHASH      | BLOCKHASH      | 2    | 20    | 0.100  |
| BLOCKCTXU64    | TIMESTAMP      | 1    | 2     | 0.500  |
| BLOCKCTXU64    | NUMBER         | 1    | 2     | 0.500  |
| BLOCKCTXU64    | GASLIMIT       | 1    | 2     | 0.500  |
| BLOCKCTXU160   | COINBASE       | 1    | 2     | 0.500  |
| BLOCKCTXU256   | DIFFICULTY     | 1    | 2     | 0.500  |
| BLOCKCTXU256   | BASEFEE        | 1    | 2     | 0.500  |
| CHAINID        | CHAINID        | 1    | 2     | 0.500  |
| SELFBALANCE    | SELFBALANCE    | 3    | 5     | 0.600  |
| POP            | POP            | 1    | 2     | 0.500  |
| MEMORY         | MLOAD          | 34   | 15    | 2.267  |
| MEMORY         | MSTORE         | 34   | 15    | 2.267  |
| MEMORY         | MSTORE8        | 3    | 12    | 0.250  |
| SLOAD          | SLOAD          | 8    | 2100  | 0.004  |
| SSTORE         | SSTORE         | 10   | 22100 | 0.000  |
| JUMP           | JUMP           | 16   | 8     | 2.000  |
| JUMPI          | JUMPI          | 17   | 10    | 1.700  |
| PC             | PC             | 1    | 2     | 0.500  |
| MSIZE          | MSIZE          | 1    | 2     | 0.500  |
| GAS            | GAS            | 1    | 2     | 0.500  |
| JUMPDEST       | JUMPDEST       | 0    | 1     | 0.000  |
| PUSH           | PUSH1          | 1    | 3     | 0.333  |
| PUSH           | PUSH2          | 1    | 3     | 0.333  |
| PUSH           | PUSH3          | 1    | 3     | 0.333  |
| PUSH           | PUSH4          | 1    | 3     | 0.333  |
| PUSH           | PUSH5          | 1    | 3     | 0.333  |
| PUSH           | PUSH6          | 1    | 3     | 0.333  |
| PUSH           | PUSH7          | 1    | 3     | 0.333  |
| PUSH           | PUSH8          | 1    | 3     | 0.333  |
| PUSH           | PUSH9          | 1    | 3     | 0.333  |
| PUSH           | PUSH10         | 1    | 3     | 0.333  |
| PUSH           | PUSH11         | 1    | 3     | 0.333  |
| PUSH           | PUSH12         | 1    | 3     | 0.333  |
| PUSH           | PUSH13         | 1    | 3     | 0.333  |
| PUSH           | PUSH14         | 1    | 3     | 0.333  |
| PUSH           | PUSH15         | 1    | 3     | 0.333  |
| PUSH           | PUSH16         | 1    | 3     | 0.333  |
| PUSH           | PUSH17         | 1    | 3     | 0.333  |
| PUSH           | PUSH18         | 1    | 3     | 0.333  |
| PUSH           | PUSH19         | 1    | 3     | 0.333  |
| PUSH           | PUSH20         | 1    | 3     | 0.333  |
| PUSH           | PUSH21         | 1    | 3     | 0.333  |
| PUSH           | PUSH22         | 1    | 3     | 0.333  |
| PUSH           | PUSH23         | 1    | 3     | 0.333  |
| PUSH           | PUSH24         | 1    | 3     | 0.333  |
| PUSH           | PUSH25         | 1    | 3     | 0.333  |
| PUSH           | PUSH26         | 1    | 3     | 0.333  |
| PUSH           | PUSH27         | 1    | 3     | 0.333  |
| PUSH           | PUSH28         | 1    | 3     | 0.333  |
| PUSH           | PUSH29         | 1    | 3     | 0.333  |
| PUSH           | PUSH30         | 1    | 3     | 0.333  |
| PUSH           | PUSH31         | 1    | 3     | 0.333  |
| PUSH           | PUSH32         | 1    | 3     | 0.333  |
| DUP            | DUP1           | 2    | 3     | 0.667  |
| DUP            | DUP2           | 2    | 3     | 0.667  |
| DUP            | DUP3           | 2    | 3     | 0.667  |
| DUP            | DUP4           | 2    | 3     | 0.667  |
| DUP            | DUP5           | 2    | 3     | 0.667  |
| DUP            | DUP6           | 2    | 3     | 0.667  |
| DUP            | DUP7           | 2    | 3     | 0.667  |
| DUP            | DUP8           | 2    | 3     | 0.667  |
| DUP            | DUP9           | 2    | 3     | 0.667  |
| DUP            | DUP10          | 2    | 3     | 0.667  |
| DUP            | DUP11          | 2    | 3     | 0.667  |
| DUP            | DUP12          | 2    | 3     | 0.667  |
| DUP            | DUP13          | 2    | 3     | 0.667  |
| DUP            | DUP14          | 2    | 3     | 0.667  |
| DUP            | DUP15          | 2    | 3     | 0.667  |
| DUP            | DUP16          | 2    | 3     | 0.667  |
| SWAP           | SWAP1          | 4    | 3     | 1.333  |
| SWAP           | SWAP2          | 4    | 3     | 1.333  |
| SWAP           | SWAP3          | 4    | 3     | 1.333  |
| SWAP           | SWAP4          | 4    | 3     | 1.333  |
| SWAP           | SWAP5          | 4    | 3     | 1.333  |
| SWAP           | SWAP6          | 4    | 3     | 1.333  |
| SWAP           | SWAP7          | 4    | 3     | 1.333  |
| SWAP           | SWAP8          | 4    | 3     | 1.333  |
| SWAP           | SWAP9          | 4    | 3     | 1.333  |
| SWAP           | SWAP10         | 4    | 3     | 1.333  |
| SWAP           | SWAP11         | 4    | 3     | 1.333  |
| SWAP           | SWAP12         | 4    | 3     | 1.333  |
| SWAP           | SWAP13         | 4    | 3     | 1.333  |
| SWAP           | SWAP14         | 4    | 3     | 1.333  |
| SWAP           | SWAP15         | 4    | 3     | 1.333  |
| SWAP           | SWAP16         | 4    | 3     | 1.333  |
| LOG            | LOG0           | 8199 | 33559 | 0.244  |
| LOG            | LOG1           | 8201 | 33934 | 0.242  |
| LOG            | LOG2           | 8203 | 34309 | 0.239  |
| LOG            | LOG3           | 8205 | 34684 | 0.237  |
| LOG            | LOG4           | 8207 | 35059 | 0.234  |
| CALL_OP        | CALL           | 24   | 36686 | 0.001  |
| CALL_OP        | CALLCODE       | 22   | 11686 | 0.002  |
| CALL_OP        | DELEGATECALL   | 23   | 2689  | 0.009  |
| CALL_OP        | STATICCALL     | 21   | 2689  | 0.008  |
| RETURN_REVERT  | RETURN         | 8212 | 416   | 19.740 |
| RETURN_REVERT  | REVERT         | 8213 | 416   | 19.743 |

</details>
  • Loading branch information
ed255 authored Feb 21, 2023
1 parent e9eda03 commit bfbedfd
Show file tree
Hide file tree
Showing 19 changed files with 441 additions and 240 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,14 @@ exp_bench: ## Run Exp Circuit benchmarks

circuit_benches: evm_bench state_bench ## Run All Circuit benchmarks

stats_state_circuit: # Print a table with State Circuit stats by ExecState/opcode
@cargo test -p zkevm-circuits --features=test,warn-unimplemented get_state_states_stats -- --nocapture --ignored

stats_evm_circuit: # Print a table with EVM Circuit stats by ExecState/opcode
@cargo test -p zkevm-circuits --features=test,warn-unimplemented get_evm_states_stats -- --nocapture --ignored

stats_copy_circuit: # Print a table with Copy Circuit stats by ExecState/opcode
@cargo test -p zkevm-circuits --features=test,warn-unimplemented get_copy_states_stats -- --nocapture --ignored


.PHONY: clippy doc fmt test test_benches test-all evm_bench state_bench circuit_benches help
98 changes: 56 additions & 42 deletions bus-mapping/src/circuit_input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,58 @@ pub struct BuilderClient<P: JsonRpcClient> {
circuits_params: CircuitsParams,
}

/// Get State Accesses from TxExecTraces
pub fn get_state_accesses(
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
) -> Result<AccessSet, Error> {
let mut block_access_trace = vec![Access::new(
None,
RW::WRITE,
AccessValue::Account {
address: eth_block
.author
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?,
},
)];
for (tx_index, tx) in eth_block.transactions.iter().enumerate() {
let geth_trace = &geth_traces[tx_index];
let tx_access_trace = gen_state_access_trace(eth_block, tx, geth_trace)?;
block_access_trace.extend(tx_access_trace);
}

Ok(AccessSet::from(block_access_trace))
}

/// Build a partial StateDB from step 3
pub fn build_state_code_db(
proofs: Vec<eth_types::EIP1186ProofResponse>,
codes: HashMap<Address, Vec<u8>>,
) -> (StateDB, CodeDB) {
let mut sdb = StateDB::new();
for proof in proofs {
let mut storage = HashMap::new();
for storage_proof in proof.storage_proof {
storage.insert(storage_proof.key, storage_proof.value);
}
sdb.set_account(
&proof.address,
state_db::Account {
nonce: proof.nonce,
balance: proof.balance,
storage,
code_hash: proof.code_hash,
},
)
}

let mut code_db = CodeDB::new();
for (_address, code) in codes {
code_db.insert(code.clone());
}
(sdb, code_db)
}

impl<P: JsonRpcClient> BuilderClient<P> {
/// Create a new BuilderClient
pub async fn new(
Expand Down Expand Up @@ -451,26 +503,10 @@ impl<P: JsonRpcClient> BuilderClient<P> {

/// Step 2. Get State Accesses from TxExecTraces
pub fn get_state_accesses(
&self,
eth_block: &EthBlock,
geth_traces: &[eth_types::GethExecTrace],
) -> Result<AccessSet, Error> {
let mut block_access_trace = vec![Access::new(
None,
RW::WRITE,
AccessValue::Account {
address: eth_block
.author
.ok_or(Error::EthTypeError(eth_types::Error::IncompleteBlock))?,
},
)];
for (tx_index, tx) in eth_block.transactions.iter().enumerate() {
let geth_trace = &geth_traces[tx_index];
let tx_access_trace = gen_state_access_trace(eth_block, tx, geth_trace)?;
block_access_trace.extend(tx_access_trace);
}

Ok(AccessSet::from(block_access_trace))
get_state_accesses(eth_block, geth_traces)
}

/// Step 3. Query geth for all accounts, storage keys, and codes from
Expand Down Expand Up @@ -511,32 +547,10 @@ impl<P: JsonRpcClient> BuilderClient<P> {

/// Step 4. Build a partial StateDB from step 3
pub fn build_state_code_db(
&self,
proofs: Vec<eth_types::EIP1186ProofResponse>,
codes: HashMap<Address, Vec<u8>>,
) -> (StateDB, CodeDB) {
let mut sdb = StateDB::new();
for proof in proofs {
let mut storage = HashMap::new();
for storage_proof in proof.storage_proof {
storage.insert(storage_proof.key, storage_proof.value);
}
sdb.set_account(
&proof.address,
state_db::Account {
nonce: proof.nonce,
balance: proof.balance,
storage,
code_hash: proof.code_hash,
},
)
}

let mut code_db = CodeDB::new();
for (_address, code) in codes {
code_db.insert(code.clone());
}
(sdb, code_db)
build_state_code_db(proofs, codes)
}

/// Step 5. For each step in TxExecTraces, gen the associated ops and state
Expand Down Expand Up @@ -575,9 +589,9 @@ impl<P: JsonRpcClient> BuilderClient<P> {
> {
let (eth_block, geth_traces, history_hashes, prev_state_root) =
self.get_block(block_num).await?;
let access_set = self.get_state_accesses(&eth_block, &geth_traces)?;
let access_set = Self::get_state_accesses(&eth_block, &geth_traces)?;
let (proofs, codes) = self.get_state(block_num, access_set).await?;
let (state_db, code_db) = self.build_state_code_db(proofs, codes);
let (state_db, code_db) = Self::build_state_code_db(proofs, codes);
let builder = self.gen_inputs_from_state(
state_db,
code_db,
Expand Down
3 changes: 1 addition & 2 deletions bus-mapping/src/circuit_input_builder/input_state_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,9 @@ impl<'a> CircuitInputStateRef<'a> {
field: AccountField,
value: Word,
value_prev: Word,
) -> Result<(), Error> {
) {
let op = AccountOp::new(address, field, value, value_prev);
self.push_op(step, RW::READ, op);
Ok(())
}

/// Push a write type [`AccountOp`] into the
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ pub fn gen_begin_tx_ops(state: &mut CircuitInputStateRef) -> Result<ExecStep, Er
AccountField::CodeHash,
callee_code_hash_word,
callee_code_hash_word,
)?;
);

// 3. Call to account with empty code.
if is_empty_code_hash {
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ impl Opcode for Balance {
AccountField::CodeHash,
code_hash.to_word(),
code_hash.to_word(),
)?;
);
if exists {
state.account_read(
&mut exec_step,
address,
AccountField::Balance,
balance,
balance,
)?;
);
}

// Write the BALANCE result to stack.
Expand Down
4 changes: 2 additions & 2 deletions bus-mapping/src/evm/opcodes/callop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl<const N_ARGS: usize> Opcode for CallOpcode<N_ARGS> {
AccountField::CodeHash,
callee_code_hash_word,
callee_code_hash_word,
)?;
);

let is_warm = state.sdb.check_account_in_access_list(&callee_address);
state.push_op_reversible(
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<const N_ARGS: usize> Opcode for CallOpcode<N_ARGS> {
AccountField::Balance,
caller_balance,
caller_balance,
)?;
);

// Transfer value only for CALL opcode, insufficient_balance = false
// and value > 0.
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/error_oog_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Opcode for OOGCall {
AccountField::CodeHash,
callee_code_hash_word,
callee_code_hash_word,
)?;
);

let is_warm = state.sdb.check_account_in_access_list(&call_address);
state.push_op(
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/extcodecopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn gen_extcodecopy_step(
AccountField::CodeHash,
code_hash.to_word(),
code_hash.to_word(),
)?;
);
Ok(exec_step)
}

Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/extcodehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Opcode for Extcodehash {
AccountField::CodeHash,
code_hash.to_word(),
code_hash.to_word(),
)?;
);

// Stack write of the result of EXTCODEHASH.
state.stack_write(&mut exec_step, stack_address, steps[1].stack.last()?)?;
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/extcodesize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Opcode for Extcodesize {
AccountField::CodeHash,
code_hash.to_word(),
code_hash.to_word(),
)?;
);
let code_size = if exists {
state.code(code_hash)?.len()
} else {
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/selfbalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Opcode for Selfbalance {
AccountField::Balance,
self_balance,
self_balance,
)?;
);

// Stack write of self_balance
state.stack_write(
Expand Down
16 changes: 6 additions & 10 deletions bus-mapping/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Mock types and functions to generate mock data useful for tests
use crate::{
circuit_input_builder::{Block, CircuitInputBuilder, CircuitsParams},
circuit_input_builder::{get_state_accesses, Block, CircuitInputBuilder, CircuitsParams},
state_db::{self, CodeDB, StateDB},
};
use eth_types::{geth_types::GethData, Word};
Expand Down Expand Up @@ -52,15 +52,11 @@ impl BlockData {
let mut sdb = StateDB::new();
let mut code_db = CodeDB::new();

sdb.set_account(
&geth_data.eth_block.author.expect("Block.author"),
state_db::Account::zero(),
);
for tx in geth_data.eth_block.transactions.iter() {
sdb.set_account(&tx.from, state_db::Account::zero());
if let Some(to) = tx.to.as_ref() {
sdb.set_account(to, state_db::Account::zero());
}
let access_set = get_state_accesses(&geth_data.eth_block, &geth_data.geth_traces)
.expect("state accesses");
// Initialize all accesses accounts to zero
for addr in access_set.state.keys() {
sdb.set_account(addr, state_db::Account::zero());
}

for account in geth_data.accounts {
Expand Down
8 changes: 5 additions & 3 deletions integration-tests/tests/circuit_input_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![cfg(feature = "circuit_input_builder")]

use bus_mapping::circuit_input_builder::{BuilderClient, CircuitsParams};
use bus_mapping::circuit_input_builder::{
build_state_code_db, get_state_accesses, BuilderClient, CircuitsParams,
};
use integration_tests::{get_client, log_init, GenDataOutput};
use lazy_static::lazy_static;
use log::trace;
Expand Down Expand Up @@ -32,14 +34,14 @@ async fn test_circuit_input_builder_block(block_num: u64) {
cli.get_block(block_num).await.unwrap();

// 2. Get State Accesses from TxExecTraces
let access_set = cli.get_state_accesses(&eth_block, &geth_trace).unwrap();
let access_set = get_state_accesses(&eth_block, &geth_trace).unwrap();
trace!("AccessSet: {:#?}", access_set);

// 3. Query geth for all accounts, storage keys, and codes from Accesses
let (proofs, codes) = cli.get_state(block_num, access_set).await.unwrap();

// 4. Build a partial StateDB from step 3
let (state_db, code_db) = cli.build_state_code_db(proofs, codes);
let (state_db, code_db) = build_state_code_db(proofs, codes);
trace!("StateDB: {:#?}", state_db);

// 5. For each step in TxExecTraces, gen the associated ops and state
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ pretty_assertions = "1.0.0"
default = []
test = ["ethers-signers", "mock"]
test-circuits = []
warn-unimplemented = ["eth-types/warn-unimplemented"]
40 changes: 40 additions & 0 deletions zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,43 @@ mod tests {
}
}
}

#[cfg(test)]
mod copy_circuit_stats {
use crate::evm_circuit::step::ExecutionState;
use crate::stats::{bytecode_prefix_op_big_rws, print_circuit_stats_by_states};

/// Prints the stats of Copy circuit per execution state. See
/// `print_circuit_stats_by_states` for more details.
///
/// Run with:
/// `cargo test -p zkevm-circuits --release --all-features
/// get_evm_states_stats -- --nocapture --ignored`
#[ignore]
#[test]
fn get_copy_states_stats() {
print_circuit_stats_by_states(
|state| {
// TODO: Enable CREATE/CREATE2 once they are supported
matches!(
state,
ExecutionState::RETURNDATACOPY
| ExecutionState::CODECOPY
| ExecutionState::LOG
| ExecutionState::CALLDATACOPY
| ExecutionState::EXTCODECOPY
| ExecutionState::RETURN_REVERT
)
},
bytecode_prefix_op_big_rws,
|block, _, _| {
assert!(block.copy_events.len() <= 1);
block
.copy_events
.iter()
.map(|c| c.bytes.len() * 2)
.sum::<usize>()
},
);
}
}
Loading

0 comments on commit bfbedfd

Please sign in to comment.