Skip to content

Commit

Permalink
wip: etrog: synchronize trusted batches (0xPolygonHermez#2871)
Browse files Browse the repository at this point in the history
* docker + genesis
* new prover image
* wip: isolate syncTrustedState code
* Feature/0xPolygonHermez#2840 l1 info root (0xPolygonHermez#2865)
* Keep siblings in memory + AddLeaf + tests
* l1infotree state and synchronizer
* synchronizer:moved common objects to common package
* pre-execution trs use ETROG
* fix generateReceipt. Use StoreL2Block from the state (0xPolygonHermez#2880)
* refactor init/get wip batch. Add SHA256 counter. Fixes (0xPolygonHermez#2882)
* process etrog batches with ExecuteBatchV2
* Fix final StateRoot for batch when it is closed (0xPolygonHermez#2887)
* removed timestamp comparation on virtualization of batches
* timestamp check
* use time.Now() as minTimestamp for open a trusted batch

---------

Co-authored-by: Alonso <[email protected]>
Co-authored-by: Alonso Rodriguez <[email protected]>
Co-authored-by: agnusmor <[email protected]>
Co-authored-by: tclemos <[email protected]>
Co-authored-by: Toni Ramírez <[email protected]>
Co-authored-by: agnusmor <[email protected]>
  • Loading branch information
7 people authored Dec 13, 2023
1 parent f77d582 commit 1a229c6
Show file tree
Hide file tree
Showing 72 changed files with 3,466 additions and 2,974 deletions.
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Outputs = ["stderr"]
MaxArithmetics = 236585
MaxBinaries = 473170
MaxSteps = 7570538
MaxSHA256Hashes = 1596
[Pool]
IntervalToRefreshBlockedAddresses = "5m"
Expand Down
3 changes: 2 additions & 1 deletion config/environments/local/local.genesis.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"transactions": "0xf9010380808401c9c38094aae872c70944d40001755c3eaae53bc4e1a78bd080b8e4f811bff7000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a40d5f56745a118d0906a34e69aec8c0db1cb8fa000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005ca1ab1e0000000000000000000000000000000000000000000000000000000005ca1ab1e1bff",
"globalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": 1700820478,
"sequencer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
"sequencer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
"ForcedBlockHash": "0x7874802e444cbae4df050fe75e2007e7a9180fa91f42df0438ef24717939ff91"
}
}
1 change: 1 addition & 0 deletions config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Outputs = ["stderr"]
MaxArithmetics = 236585
MaxBinaries = 473170
MaxSteps = 7570538
MaxSHA256Hashes = 1596

[Pool]
IntervalToRefreshBlockedAddresses = "5m"
Expand Down
1 change: 1 addition & 0 deletions config/environments/mainnet/node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Outputs = ["stderr"]
MaxArithmetics = 236585
MaxBinaries = 473170
MaxSteps = 7570538
MaxSHA256Hashes = 1596

[Pool]
MaxTxBytesSize=100132
Expand Down
1 change: 1 addition & 0 deletions config/environments/testnet/node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ AccountQueue = 64
MaxArithmetics = 236585
MaxBinaries = 473170
MaxSteps = 7570538
MaxSHA256Hashes = 1596

[Pool]
IntervalToRefreshBlockedAddresses = "5m"
Expand Down
6 changes: 3 additions & 3 deletions db/migrations/pool/0011_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ func (m migrationTest0011) InsertData(db *sql.DB) error {
return nil
}

var indexes = []string{
var indexesMigration11 = []string{
"idx_transaction_from_nonce",
"idx_transaction_status",
"idx_transaction_hash",
}

func (m migrationTest0011) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Check indexes adding
for _, idx := range indexes {
for _, idx := range indexesMigration11 {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
Expand All @@ -34,7 +34,7 @@ func (m migrationTest0011) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)

func (m migrationTest0011) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
// Check indexes removing
for _, idx := range indexes {
for _, idx := range indexesMigration11 {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
Expand Down
12 changes: 12 additions & 0 deletions db/migrations/pool/0012.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- +migrate Up
ALTER TABLE pool.transaction
ADD COLUMN l2_hash VARCHAR UNIQUE,
ADD COLUMN used_sha256_hashes INTEGER;
CREATE INDEX IF NOT EXISTS idx_transaction_l2_hash ON pool.transaction (l2_hash);

-- +migrate Down
DROP INDEX IF EXISTS pool.idx_transaction_l2_hash;
ALTER TABLE pool.transaction
DROP COLUMN l2_hash,
DROP COLUMN used_sha256_hashes;

63 changes: 63 additions & 0 deletions db/migrations/pool/0012_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package pool_migrations_test

import (
"database/sql"
"testing"

"github.com/stretchr/testify/assert"
)

// this migration changes length of the token name
type migrationTest0012 struct{}

func (m migrationTest0012) InsertData(db *sql.DB) error {
const insertTx = `
INSERT INTO pool.transaction (hash, ip, received_at, from_address)
VALUES ('0x0001', '127.0.0.1', '2023-12-07', '0x0011')`

_, err := db.Exec(insertTx)
if err != nil {
return err
}

return nil
}

var indexesMigration12 = []string{
"idx_transaction_l2_hash",
}

func (m migrationTest0012) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) {
// Check indexes adding
for _, idx := range indexesMigration12 {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)
}

const insertTx = `
INSERT INTO pool.transaction (hash, ip, received_at, from_address, used_sha256_hashes)
VALUES ('0x0002', '127.0.0.1', '2023-12-07', '0x0022', 222)`

_, err := db.Exec(insertTx)
assert.NoError(t, err)
}

func (m migrationTest0012) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) {
// Check indexes removing
for _, idx := range indexesMigration12 {
// getIndex
const getIndex = `SELECT count(*) FROM pg_indexes WHERE indexname = $1;`
row := db.QueryRow(getIndex, idx)
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}
}

func TestMigration0012(t *testing.T) {
runMigrationTest(t, 12, migrationTest0012{})
}
21 changes: 15 additions & 6 deletions db/migrations/state/0013.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ ALTER TABLE state.exit_root
ADD COLUMN prev_block_hash BYTEA DEFAULT NULL,
ADD COLUMN l1_info_root BYTEA DEFAULT NULL,
ADD COLUMN l1_info_tree_index BIGINT DEFAULT NULL UNIQUE;
CREATE INDEX IF NOT EXISTS exit_root_l1_info_tree_index ON state.exit_root (l1_info_tree_index);
CREATE INDEX IF NOT EXISTS idx_exit_root_l1_info_tree_index ON state.exit_root (l1_info_tree_index);

ALTER TABLE state.transaction
ADD COLUMN l2_hash VARCHAR UNIQUE;
ADD COLUMN l2_hash VARCHAR UNIQUE,
ADD COLUMN used_sha256_hashes INTEGER;

CREATE INDEX IF NOT EXISTS transaction_l2_hash ON state.transaction (l2_hash);
CREATE INDEX IF NOT EXISTS idx_transaction_l2_hash ON state.transaction (l2_hash);

ALTER TABLE state.batch
ADD COLUMN wip BOOLEAN NOT NULL;

-- +migrate Down
ALTER TABLE state.exit_root
DROP COLUMN prev_block_hash,
DROP COLUMN l1_info_root,
DROP COLUMN l1_info_tree_index;
DROP INDEX IF EXISTS state.exit_root_l1_info_tree_index;
DROP INDEX IF EXISTS state.idx_exit_root_l1_info_tree_index;

ALTER TABLE state.transaction
DROP COLUMN l2_hash;
DROP COLUMN l2_hash,
DROP COLUMN used_sha256_hashes;

DROP INDEX IF EXISTS state.idx_transaction_l2_hash;

ALTER TABLE state.batch
DROP COLUMN wip;

DROP INDEX IF EXISTS state.transaction_l2_hash;
32 changes: 28 additions & 4 deletions db/migrations/state/0013_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func (m migrationTest0013) InsertDataIntoTransactionsTable(db *sql.DB) error {
return err
}
const insertBatch = `
INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num)
VALUES (0,'0x0000', '0x0000', '0x0000', '0x0000', now(), '0x0000', null, null)`
INSERT INTO state.batch (batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num, wip)
VALUES (0,'0x0000', '0x0000', '0x0000', '0x0000', now(), '0x0000', null, null, true)`

// insert batch
_, err := db.Exec(insertBatch)
Expand All @@ -85,8 +85,8 @@ func (m migrationTest0013) InsertDataIntoTransactionsTable(db *sql.DB) error {
}

const insertTx = `
INSERT INTO state.transaction (hash, encoded, decoded, l2_block_num, effective_percentage, l2_hash)
VALUES ('0x0001', 'ABCDEF', '{}', 0, 255, '0x0002')`
INSERT INTO state.transaction (hash, encoded, decoded, l2_block_num, effective_percentage, l2_hash, used_sha256_hashes)
VALUES ('0x0001', 'ABCDEF', '{}', 0, 255, '0x0002', 1000)`

// insert tx
_, err = db.Exec(insertTx)
Expand Down Expand Up @@ -140,6 +140,18 @@ func (m migrationTest0013) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Check column used_sha256_hashes exists in state.transactions table
const getUsedSHA256HashesColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='transaction' and column_name='used_sha256_hashes'`
row = db.QueryRow(getUsedSHA256HashesColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Check column wip exists in state.batch table
const getWIPColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='batch' and column_name='wip'`
row = db.QueryRow(getWIPColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 1, result)

// Try to insert data into the transactions table
err = m.InsertDataIntoTransactionsTable(db)
assert.NoError(t, err)
Expand All @@ -158,6 +170,18 @@ func (m migrationTest0013) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB
var result int
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)

// Check column wip doesn't exists in state.batch table
const getUsedSHA256HashesColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='batch' and column_name='used_sha256_hashes'`
row = db.QueryRow(getUsedSHA256HashesColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)

// Check column wip doesn't exists in state.batch table
const getWIPColumn = `SELECT count(*) FROM information_schema.columns WHERE table_name='batch' and column_name='wip'`
row = db.QueryRow(getWIPColumn)
assert.NoError(t, row.Scan(&result))
assert.Equal(t, 0, result)
}

func TestMigration0013(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ services:
zkevm-prover:
container_name: zkevm-prover
restart: unless-stopped
image: hermeznetwork/zkevm-prover:v4.0.0-RC3
image: hermeznetwork/zkevm-prover:v4.0.0-RC5
depends_on:
zkevm-state-db:
condition: service_healthy
Expand Down
Loading

0 comments on commit 1a229c6

Please sign in to comment.