From ae676ca3935151a8f536f26768bc428bc2c0d12e Mon Sep 17 00:00:00 2001 From: tclemos Date: Fri, 26 Jan 2024 10:22:50 -0300 Subject: [PATCH 1/3] fix tracer to work by block stateroot instead of by tx --- db/migrations/state/0015.sql | 6 ++++ db/migrations/state/0015_test.go | 49 +++++++++++++++++++++++++++++ docker-compose.yml | 2 +- state/pgstatestorage/transaction.go | 8 ++++- state/trace.go | 41 +++++++++++------------- test/docker-compose.yml | 4 +-- 6 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 db/migrations/state/0015.sql create mode 100644 db/migrations/state/0015_test.go diff --git a/db/migrations/state/0015.sql b/db/migrations/state/0015.sql new file mode 100644 index 0000000000..05657826cc --- /dev/null +++ b/db/migrations/state/0015.sql @@ -0,0 +1,6 @@ +-- +migrate Up +CREATE INDEX IF NOT EXISTS idx_receipt_tx_index ON state.receipt (block_num, tx_index); + +-- +migrate Down +DROP INDEX IF EXISTS state.idx_receipt_tx_index; + diff --git a/db/migrations/state/0015_test.go b/db/migrations/state/0015_test.go new file mode 100644 index 0000000000..20f34bdbf9 --- /dev/null +++ b/db/migrations/state/0015_test.go @@ -0,0 +1,49 @@ +package migrations_test + +import ( + "database/sql" + "testing" + + "github.com/stretchr/testify/assert" +) + +// this migration changes length of the token name +type migrationTest0015 struct{} + +func (m migrationTest0015) InsertData(db *sql.DB) error { + return nil +} + +func (m migrationTest0015) RunAssertsAfterMigrationUp(t *testing.T, db *sql.DB) { + indexes := []string{ + "idx_receipt_tx_index", + } + // Check indexes adding + for _, idx := range indexes { + // 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) + } +} + +func (m migrationTest0015) RunAssertsAfterMigrationDown(t *testing.T, db *sql.DB) { + indexes := []string{ + "idx_receipt_tx_index", + } + // Check indexes removing + for _, idx := range indexes { + // 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 TestMigration0015(t *testing.T) { + runMigrationTest(t, 15, migrationTest0015{}) +} diff --git a/docker-compose.yml b/docker-compose.yml index 77bcce0313..e592405fcd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -107,7 +107,7 @@ services: zkevm-prover: container_name: zkevm-prover restart: unless-stopped - image: hermeznetwork/zkevm-prover:v4.0.0-RC30 + image: hermeznetwork/zkevm-prover:v4.0.0-RC31 depends_on: zkevm-state-db: condition: service_healthy diff --git a/state/pgstatestorage/transaction.go b/state/pgstatestorage/transaction.go index 5ef4ad9485..9d8fe15efa 100644 --- a/state/pgstatestorage/transaction.go +++ b/state/pgstatestorage/transaction.go @@ -377,7 +377,13 @@ func scanLogs(rows pgx.Rows) ([]*types.Log, error) { // GetTxsByBlockNumber returns all the txs in a given block func (p *PostgresStorage) GetTxsByBlockNumber(ctx context.Context, blockNumber uint64, dbTx pgx.Tx) ([]*types.Transaction, error) { - const getTxsByBlockNumSQL = "SELECT encoded FROM state.transaction WHERE l2_block_num = $1" + const getTxsByBlockNumSQL = `SELECT t.encoded + FROM state.transaction t + JOIN state.receipt r + ON t.hash = r.tx_hash + WHERE t.l2_block_num = $1 + AND r.block_num = $1 + ORDER by r.tx_index ASC` q := p.getExecQuerier(dbTx) rows, err := q.Query(ctx, getTxsByBlockNumSQL, blockNumber) diff --git a/state/trace.go b/state/trace.go index 22cc5b5306..c567343c82 100644 --- a/state/trace.go +++ b/state/trace.go @@ -49,28 +49,23 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has return nil, err } - // if tx index is zero, we need to get the state root from the previous block - // else we need to get the state root from the previous tx + // the old state root is the previous block state root var oldStateRoot common.Hash - if receipt.TransactionIndex == 0 { - // get the previous L2 Block - previousL2BlockNumber := uint64(0) - if receipt.BlockNumber.Uint64() > 0 { - previousL2BlockNumber = receipt.BlockNumber.Uint64() - 1 - } - previousL2Block, err := s.GetL2BlockByNumber(ctx, previousL2BlockNumber, dbTx) - if err != nil { - return nil, err - } - oldStateRoot = previousL2Block.Root() - } else { - previousTx := l2Block.Transactions()[receipt.TransactionIndex-1] - // gets the tx receipt - previousReceipt, err := s.GetTransactionReceipt(ctx, previousTx.Hash(), dbTx) - if err != nil { - return nil, err - } - oldStateRoot = common.BytesToHash(previousReceipt.PostState) + previousL2BlockNumber := uint64(0) + if receipt.BlockNumber.Uint64() > 0 { + previousL2BlockNumber = receipt.BlockNumber.Uint64() - 1 + } + previousL2Block, err := s.GetL2BlockByNumber(ctx, previousL2BlockNumber, dbTx) + if err != nil { + return nil, err + } + oldStateRoot = previousL2Block.Root() + + // since the executor only stores the state roots by block, we need to + // execute all the txs in the block until the tx we want to trace + var txsToEncode []types.Transaction + for i := 0; i <= int(receipt.TransactionIndex); i++ { + txsToEncode = append(txsToEncode, *l2Block.Transactions()[i]) } // gets batch that including the l2 block @@ -111,7 +106,7 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has } } // generate batch l2 data for the transaction - batchL2Data, err := EncodeTransactions([]types.Transaction{*tx}, []uint8{MaxEffectivePercentage}, forkId) + batchL2Data, err := EncodeTransactions(txsToEncode, []uint8{MaxEffectivePercentage}, forkId) if err != nil { return nil, err } @@ -192,7 +187,7 @@ func (s *State) DebugTransaction(ctx context.Context, transactionHash common.Has deltaTimestamp := uint32(uint64(time.Now().Unix()) - l2Block.Time()) transactions := s.BuildChangeL2Block(deltaTimestamp, uint32(0)) - batchL2Data, err := EncodeTransactions([]types.Transaction{*tx}, []uint8{MaxEffectivePercentage}, forkId) + batchL2Data, err := EncodeTransactions(txsToEncode, []uint8{MaxEffectivePercentage}, forkId) if err != nil { log.Errorf("error encoding transaction ", err) return nil, err diff --git a/test/docker-compose.yml b/test/docker-compose.yml index 3390e8ed36..e9d1992c50 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -513,7 +513,7 @@ services: zkevm-prover: container_name: zkevm-prover - image: hermeznetwork/zkevm-prover:v4.0.0-RC30 + image: hermeznetwork/zkevm-prover:v4.0.0-RC31 ports: - 50061:50061 # MT - 50071:50071 # Executor @@ -602,7 +602,7 @@ services: zkevm-permissionless-prover: container_name: zkevm-permissionless-prover - image: hermeznetwork/zkevm-prover:v4.0.0-RC30 + image: hermeznetwork/zkevm-prover:v4.0.0-RC31 ports: # - 50058:50058 # Prover - 50059:50052 # Mock prover From fbe8dfbb4086ecf74db04bb0a1cc0593d5ccaff6 Mon Sep 17 00:00:00 2001 From: tclemos Date: Fri, 26 Jan 2024 10:29:53 -0300 Subject: [PATCH 2/3] change prover config useMainExecGenerated to true --- test/config/test.prover.config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/config/test.prover.config.json b/test/config/test.prover.config.json index fa4f4bd45e..91685ace2f 100644 --- a/test/config/test.prover.config.json +++ b/test/config/test.prover.config.json @@ -27,7 +27,7 @@ "runBlakeTest": false, "executeInParallel": true, - "useMainExecGenerated": false, + "useMainExecGenerated": true, "saveRequestToFile": false, "saveInputToFile": false, "saveDbReadsToFile": false, From f7119ed63b95de8c1cb51750696c0f59d2171d32 Mon Sep 17 00:00:00 2001 From: tclemos Date: Fri, 26 Jan 2024 10:57:04 -0300 Subject: [PATCH 3/3] revert prover upgrade changes --- docker-compose.yml | 2 +- test/config/test.prover.config.json | 2 +- test/docker-compose.yml | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index e592405fcd..77bcce0313 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -107,7 +107,7 @@ services: zkevm-prover: container_name: zkevm-prover restart: unless-stopped - image: hermeznetwork/zkevm-prover:v4.0.0-RC31 + image: hermeznetwork/zkevm-prover:v4.0.0-RC30 depends_on: zkevm-state-db: condition: service_healthy diff --git a/test/config/test.prover.config.json b/test/config/test.prover.config.json index 91685ace2f..fa4f4bd45e 100644 --- a/test/config/test.prover.config.json +++ b/test/config/test.prover.config.json @@ -27,7 +27,7 @@ "runBlakeTest": false, "executeInParallel": true, - "useMainExecGenerated": true, + "useMainExecGenerated": false, "saveRequestToFile": false, "saveInputToFile": false, "saveDbReadsToFile": false, diff --git a/test/docker-compose.yml b/test/docker-compose.yml index e9d1992c50..3390e8ed36 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -513,7 +513,7 @@ services: zkevm-prover: container_name: zkevm-prover - image: hermeznetwork/zkevm-prover:v4.0.0-RC31 + image: hermeznetwork/zkevm-prover:v4.0.0-RC30 ports: - 50061:50061 # MT - 50071:50071 # Executor @@ -602,7 +602,7 @@ services: zkevm-permissionless-prover: container_name: zkevm-permissionless-prover - image: hermeznetwork/zkevm-prover:v4.0.0-RC31 + image: hermeznetwork/zkevm-prover:v4.0.0-RC30 ports: # - 50058:50058 # Prover - 50059:50052 # Mock prover