Skip to content

Commit

Permalink
Merge pull request #40 from 0xPolygonHermez/feature/fork-etrog
Browse files Browse the repository at this point in the history
Feature/fork etrog
  • Loading branch information
krlosMata authored Jan 26, 2024
2 parents d694c3f + b43d21f commit 487ac3d
Show file tree
Hide file tree
Showing 145 changed files with 23,011 additions and 513 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: Test executor inputs
on:
workflow_dispatch:
pull_request:
branches: [main, develop]
branches: [main, develop, develop-etrog, feature/fork-etrog, feature/l1-info-tree]

jobs:
build:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ tools/parallel-tests


.vscode/launch.json

.vscode/settings.json
39 changes: 23 additions & 16 deletions counters/counters-executor.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
/* eslint-disable import/no-extraneous-dependencies */
/* eslint-disable no-use-before-define */
/* eslint-disable no-restricted-syntax */
const path = require('path');
const fs = require('fs');
const smMain = require('@0xpolygonhermez/zkevm-proverjs/src/sm/sm_main/sm_main');

const fileCachePil = path.join(__dirname, '../node_modules/@0xpolygonhermez/zkevm-proverjs/cache-main-pil.json');
const empty_input = require('@0xpolygonhermez/zkevm-proverjs/test/inputs/empty_input.json')
const emptyInput = require('@0xpolygonhermez/zkevm-proverjs/test/inputs/empty_input.json');
const buildPoseidon = require('@0xpolygonhermez/zkevm-commonjs').getPoseidon;

const pathMainPil = path.join(__dirname, '../node_modules/@0xpolygonhermez/zkevm-proverjs/pil/main.pil');
const { newCommitPolsArray } = require('pilcom');
const { compile } = require('pilcom');
const zkasm = require("@0xpolygonhermez/zkasmcom");
const zkasm = require('@0xpolygonhermez/zkasmcom');

const testFilesDir = path.join(__dirname, './tests');
const { argv } = require('yargs');

async function main() {

// Compile pil
const cmPols = await compilePil();

// Get all zkasm files
const files = getTestFiles();

// Run all zkasm files
for (let file of files) {
await runTest(file, cmPols)
for (const file of files) {
await runTest(file, cmPols);
}
}

Expand All @@ -30,27 +35,28 @@ async function runTest(testName, cmPols) {
// Compile rom
const configZkasm = {
defines: [],
allowUndefinedLabels: true
allowUndefinedLabels: true,
};

const rom = await zkasm.compile(zkasmFile, null, configZkasm);
const config = {
debug: true,
stepsN: 8388608,
}
console.log(`Running ${testName}`)
};
console.log(`Running ${testName}`);
// Execute test
const res = await smMain.execute(cmPols.Main, empty_input, rom, config);
console.log(res.counters)
const res = await smMain.execute(cmPols.Main, emptyInput, rom, config);
console.log(res.counters);
}

// Get all zkasm counter test files
function getTestFiles() {
if(argv.test){
return [`${argv.test}.zkasm`]
if (argv.test) {
return [`${argv.test}.zkasm`];
}
const files = fs.readdirSync(testFilesDir).filter(name => name.endsWith('.zkasm'))
return files
const files = fs.readdirSync(testFilesDir).filter((name) => name.endsWith('.zkasm'));

return files;
}

async function compilePil() {
Expand All @@ -60,14 +66,15 @@ async function compilePil() {
const pilConfig = {
defines: { N: 4096 },
namespaces: ['Main', 'Global'],
disableUnusedError: true
disableUnusedError: true,
};
const p = await compile(F, pathMainPil, null, pilConfig);
fs.writeFileSync(fileCachePil, `${JSON.stringify(p, null, 1)}\n`, 'utf8');
}

const pil = JSON.parse(fs.readFileSync(fileCachePil));

return newCommitPolsArray(pil);
}

main()
main();
6 changes: 3 additions & 3 deletions counters/countersConstants.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ CONST %OPCODECOPY_CNT_PADDING_PG = 0
CONST %OPCODECOPY_CNT_POSEIDON_G = 0
; opEXTCODECOPY - COMPLEX - hardcoded values at test
CONST %OPEXTCODECOPY_STEP = 2000
CONST %OPEXTCODECOPY_CNT_BINARY = 105
CONST %OPEXTCODECOPY_CNT_BINARY = 104
CONST %OPEXTCODECOPY_CNT_ARITH = 4
CONST %OPEXTCODECOPY_CNT_KECCAK_F = 0
CONST %OPEXTCODECOPY_CNT_MEM_ALIGN = 43
Expand All @@ -248,8 +248,8 @@ CONST %OPCREATE_CNT_MEM_ALIGN = 0
CONST %OPCREATE_CNT_PADDING_PG = 0
CONST %OPCREATE_CNT_POSEIDON_G = 23
; opCREATE2 - COMPLEX - hardcoded values at test
CONST %OPCREATE2_STEP = 500
CONST %OPCREATE2_CNT_BINARY = 22
CONST %OPCREATE2_STEP = 400
CONST %OPCREATE2_CNT_BINARY = 21
CONST %OPCREATE2_CNT_ARITH = 2
CONST %OPCREATE2_CNT_KECCAK_F = 0
CONST %OPCREATE2_CNT_MEM_ALIGN = 0
Expand Down
5 changes: 3 additions & 2 deletions counters/endIncludes.zkasm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
INCLUDE "../main/load-tx-rlp-utils.zkasm"
INCLUDE "../main/2-exp.zkasm"
INCLUDE "../main/tables/2-exp.zkasm"
INCLUDE "../main/vars.zkasm"
INCLUDE "../main/utils.zkasm"
INCLUDE "../main/opcodes/calldata-returndata-code.zkasm"
Expand All @@ -14,4 +14,5 @@ INCLUDE "../main/opcodes/logs.zkasm"
INCLUDE "../main/opcodes/stack-operations.zkasm"
INCLUDE "../main/opcodes/storage-memory.zkasm"
INCLUDE "../main/touched.zkasm"
INCLUDE "../main/end.zkasm"
INCLUDE "../main/end.zkasm"
INCLUDE "../main/block-info.zkasm"
2 changes: 1 addition & 1 deletion counters/tests/opBLOCKHASH.zkasm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ start:

operation:
2 :HASHK1(0)
10 :MSTORE(txCount)
10 :MSTORE(blockNum)
1 :MSTORE(SP++)
:JMP(opBLOCKHASH)
checkCounters:
Expand Down
204 changes: 204 additions & 0 deletions main/block-info.zkasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
VAR GLOBAL tmpSR
VAR GLOBAL currentLogIndex

initBlockInfoTree:
0 :MSTORE(blockInfoSR), RETURN

; @info Save blockInfo root when a new context is created
checkpointBlockInfoTree:
$ => B :MLOAD(currentLogIndex)
B :MSTORE(initLogIndex)
$ => B :MLOAD(blockInfoSR)
B :MSTORE(initBlockInfoSR), RETURN

; @info Back to initial blockInfo root
revertBlockInfoTree:
$ => A :MLOAD(initLogIndex)
A :MSTORE(currentLogIndex)
$ => A :MLOAD(initBlockInfoSR)
A :MSTORE(blockInfoSR), RETURN

; @info Fill Block Info tree with initial block values
setupNewBlockInfoTree:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE*7 :JMPN(outOfCountersPoseidon)
%MAX_CNT_BINARY - CNT_BINARY - 7 :JMPN(outOfCountersBinary)

; save current state root & load block info root
SR :MSTORE(tmpSR)
$ => SR :MLOAD(blockInfoSR)

; Insert previous block hash
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: previousBlockHash
%INDEX_BLOCK_HEADER_PARAM_BLOCK_HASH => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(previousBlockHash)
$ => SR :SSTORE

; Insert coinbase address
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: sequencerAddr
%INDEX_BLOCK_HEADER_PARAM_COINBASE => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(sequencerAddr)
$ => SR :SSTORE

; Insert block number
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: blockNum
%INDEX_BLOCK_HEADER_PARAM_NUMBER => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(blockNum)
$ => SR :SSTORE

; Insert block gas limit
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: block gas limit
%INDEX_BLOCK_HEADER_PARAM_GAS_LIMIT => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
%BLOCK_GAS_LIMIT => D
$ => SR :SSTORE

; Insert block timestamp
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: timestamp
%INDEX_BLOCK_HEADER_PARAM_TIMESTAMP => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(timestamp)
$ => SR :SSTORE

; Insert block new ger
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: GER
%INDEX_BLOCK_HEADER_PARAM_GER => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(gerL1InfoTree)
$ => SR :SSTORE

; Insert block new blockHashL1
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: blockHashL1
%INDEX_BLOCK_HEADER_PARAM_BLOCK_HASH_L1 => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(blockHashL1InfoTree)
$ => SR :SSTORE

; Restore current SR
SR :MSTORE(blockInfoSR)
$ => SR :MLOAD(tmpSR), RETURN

; @info Fill Block Info tree with tx receipt values
fillBlockInfoTreeWithTxReceipt:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 50 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE*4 :JMPN(outOfCountersPoseidon)
%MAX_CNT_BINARY - CNT_BINARY - 4 :JMPN(outOfCountersBinary)
; save current state root & load block info root
SR :MSTORE(tmpSR)
$ => SR :MLOAD(blockInfoSR)
; Insert transaction hash
; key: H([txIndex[0:4], txIndex[4:8], txIndex[8:12], txIndex[12:16], txIndex[16:20], 0, SMT_KEY_BLOCK_HEADER_TRANSACTION_HASH, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: txHash
$ => A :MLOAD(txIndex)
%SMT_KEY_BLOCK_HEADER_TRANSACTION_HASH => B
0 => C
$ => D :MLOAD(l2TxHash)
$ => SR :SSTORE

; Insert transaction status
; key: H([txIndex[0:4], txIndex[4:8], txIndex[8:12], txIndex[12:16], txIndex[16:20], 0, SMT_KEY_BLOCK_HEADER_STATUS, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: txStatus
%SMT_KEY_BLOCK_HEADER_STATUS => B
0 => C
$ => D :MLOAD(txStatus)
$ => SR :SSTORE

; Insert transaction cumulativeGasUsed
; key: H([txIndex[0:4], txIndex[4:8], txIndex[8:12], txIndex[12:16], txIndex[16:20], 0, SMT_KEY_BLOCK_HEADER_CUMULATIVE_GAS_USED, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: txStatus
%SMT_KEY_BLOCK_HEADER_CUMULATIVE_GAS_USED => B
0 => C
$ => D :MLOAD(cumulativeGasUsed)
$ => SR :SSTORE

; Insert transaction effectivePercentage
; key: H([txIndex[0:4], txIndex[4:8], txIndex[8:12], txIndex[12:16], txIndex[16:20], 0, SMT_KEY_BLOCK_HEADER_CUMULATIVE_GAS_USED, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: effectivePercentage
%SMT_KEY_BLOCK_HEADER_EFFECTIVE_PERCENTAGE => B
0 => C
$ => D :MLOAD(effectivePercentageRLP)
$ => SR :SSTORE

; Restore current SR
SR :MSTORE(blockInfoSR)
$ => SR :MLOAD(tmpSR), RETURN

; @info Fill Block Info tree with block gas used at the end of block processing and Store block Info Root in storage
consolidateBlock:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE*2 :JMPN(outOfCountersPoseidon)
%MAX_CNT_BINARY - CNT_BINARY - 2 :JMPN(outOfCountersBinary)
; save current state root & load block info root
SR :MSTORE(tmpSR)
$ => SR :MLOAD(blockInfoSR)

; Insert transaction cumulativeGasUsed
; key: H([blockHeaderParams[0:4], blockHeaderParams[4:8], blockHeaderParams[8:12], blockHeaderParams[12:16], blockHeaderParams[16:20], 0, SMT_KEY_BLOCK_HEADER_PARAM, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; value: txStatus
%INDEX_BLOCK_HEADER_PARAM_GAS_USED => A
%SMT_KEY_BLOCK_HEADER_PARAM => B
0 => C
$ => D :MLOAD(cumulativeGasUsed)
$ => SR :SSTORE

; Restore current SR
SR :MSTORE(blockInfoSR)
$ => SR :MLOAD(tmpSR)

finalConsolidateBlockInfoTree:
; Store block Info Root in storage
%ADDRESS_SYSTEM => A
%SMT_KEY_SC_STORAGE => B
%BLOCK_INFO_ROOT_STORAGE_POS => C
writeBlockInfoRoot:
$ => D :MLOAD(blockInfoSR)
$ => SR :SSTORE
$${eventLog(onFinishBlock)}
:RETURN

; @info add new log hash to block info tree
; @in D => Value to store (linearPoseidon(log_data + log_topics))
fillBlockInfoTreeWithLog:
; checks zk-counters
%MAX_CNT_STEPS - STEP - 20 :JMPN(outOfCountersStep)
%MAX_CNT_POSEIDON_G - CNT_POSEIDON_G - %MAX_CNT_POSEIDON_SLOAD_SSTORE :JMPN(outOfCountersPoseidon)
%MAX_CNT_BINARY - CNT_BINARY - 1 :JMPN(outOfCountersBinary)
; save current state root & load block info root
SR :MSTORE(tmpSR)
$ => SR :MLOAD(blockInfoSR)

; Retrieve and update currentLogIndex
$ => C :MLOAD(currentLogIndex)
C + 1 :MSTORE(currentLogIndex)

; Insert new log to block info tree
; key: H([logIndexKey[0:4], logIndexKey[4:8], logIndexKey[8:12], logIndexKey[12:16], logIndexKey[16:20], 0, SMT_KEY_BLOCK_HEADER_LOGS, 0], [hk0[0], hk0[1], hk0[2], hk0[3]])
; hk0: H([logIndex[0:4], logIndex[4:8], logIndex[8:12], logIndex[12:16], logIndex[16:20], logIndex[20:24], logIndex[24:28], logIndex[28:32], [0, 0, 0, 0])
; value: linearPoseidon(log_data + log_topics)
$ => A :MLOAD(txIndex)
%SMT_KEY_BLOCK_HEADER_LOGS => B
$ => SR :SSTORE

; Restore current SR
SR :MSTORE(blockInfoSR)
$ => SR :MLOAD(tmpSR), RETURN
Loading

0 comments on commit 487ac3d

Please sign in to comment.