From 111e5f44a6980f94f2cc868cb09582a7c1b3b721 Mon Sep 17 00:00:00 2001 From: Alonso Date: Mon, 23 Jan 2023 12:41:41 +0100 Subject: [PATCH 1/2] Db flag to identify trusted verified batches --- db/migrations/state/0005.sql | 6 ++++++ state/batch.go | 1 + state/pgstatestorage.go | 8 ++++---- state/pgstatestorage_test.go | 1 + synchronizer/synchronizer.go | 3 ++- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/db/migrations/state/0005.sql b/db/migrations/state/0005.sql index 4cae95933f..5741e9f5d5 100644 --- a/db/migrations/state/0005.sql +++ b/db/migrations/state/0005.sql @@ -1,6 +1,9 @@ -- +migrate Down DROP TABLE IF EXISTS state.monitored_txs; +ALTER TABLE state.verified_batch +DROP COLUMN IF EXISTS is_trusted; + -- +migrate Up CREATE TABLE state.monitored_txs ( @@ -20,3 +23,6 @@ CREATE TABLE state.monitored_txs updated_at TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY (owner, id) ); + +ALTER TABLE state.verified_batch +ADD COLUMN is_trusted BOOLEAN DEFAULT true; \ No newline at end of file diff --git a/state/batch.go b/state/batch.go index af45b7a089..839edfbf1a 100644 --- a/state/batch.go +++ b/state/batch.go @@ -47,6 +47,7 @@ type VerifiedBatch struct { Aggregator common.Address TxHash common.Hash StateRoot common.Hash + IsTrusted bool } // VirtualBatch represents a VirtualBatch diff --git a/state/pgstatestorage.go b/state/pgstatestorage.go index 5fe8791917..d5e8198504 100644 --- a/state/pgstatestorage.go +++ b/state/pgstatestorage.go @@ -24,8 +24,6 @@ const ( addBlockSQL = "INSERT INTO state.block (block_num, block_hash, parent_hash, received_at) VALUES ($1, $2, $3, $4)" getLastBlockSQL = "SELECT block_num, block_hash, parent_hash, received_at FROM state.block ORDER BY block_num DESC LIMIT 1" getPreviousBlockSQL = "SELECT block_num, block_hash, parent_hash, received_at FROM state.block ORDER BY block_num DESC LIMIT 1 OFFSET $1" - addVerifiedBatchSQL = "INSERT INTO state.verified_batch (block_num, batch_num, tx_hash, aggregator, state_root) VALUES ($1, $2, $3, $4, $5)" - getVerifiedBatchSQL = "SELECT block_num, batch_num, tx_hash, aggregator, state_root FROM state.verified_batch WHERE batch_num = $1" getLastBatchNumberSQL = "SELECT batch_num FROM state.batch ORDER BY batch_num DESC LIMIT 1" getLastNBatchesSQL = "SELECT batch_num, global_exit_root, local_exit_root, acc_input_hash, state_root, timestamp, coinbase, raw_txs_data, forced_batch_num from state.batch ORDER BY batch_num DESC LIMIT $1" getLastBatchTimeSQL = "SELECT timestamp FROM state.batch ORDER BY batch_num DESC LIMIT 1" @@ -349,7 +347,8 @@ func (p *PostgresStorage) GetForcedBatch(ctx context.Context, forcedBatchNumber // AddVerifiedBatch adds a new VerifiedBatch to the db func (p *PostgresStorage) AddVerifiedBatch(ctx context.Context, verifiedBatch *VerifiedBatch, dbTx pgx.Tx) error { e := p.getExecQuerier(dbTx) - _, err := e.Exec(ctx, addVerifiedBatchSQL, verifiedBatch.BlockNumber, verifiedBatch.BatchNumber, verifiedBatch.TxHash.String(), verifiedBatch.Aggregator.String(), verifiedBatch.StateRoot.String()) + const addVerifiedBatchSQL = "INSERT INTO state.verified_batch (block_num, batch_num, tx_hash, aggregator, state_root, is_trusted) VALUES ($1, $2, $3, $4, $5, $6)" + _, err := e.Exec(ctx, addVerifiedBatchSQL, verifiedBatch.BlockNumber, verifiedBatch.BatchNumber, verifiedBatch.TxHash.String(), verifiedBatch.Aggregator.String(), verifiedBatch.StateRoot.String(), verifiedBatch.IsTrusted) return err } @@ -362,7 +361,8 @@ func (p *PostgresStorage) GetVerifiedBatch(ctx context.Context, batchNumber uint sr string ) e := p.getExecQuerier(dbTx) - err := e.QueryRow(ctx, getVerifiedBatchSQL, batchNumber).Scan(&verifiedBatch.BlockNumber, &verifiedBatch.BatchNumber, &txHash, &agg, &sr) + const getVerifiedBatchSQL = "SELECT block_num, batch_num, tx_hash, aggregator, state_root, is_trusted FROM state.verified_batch WHERE batch_num = $1" + err := e.QueryRow(ctx, getVerifiedBatchSQL, batchNumber).Scan(&verifiedBatch.BlockNumber, &verifiedBatch.BatchNumber, &txHash, &agg, &sr, &verifiedBatch.IsTrusted) if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } else if err != nil { diff --git a/state/pgstatestorage_test.go b/state/pgstatestorage_test.go index 8935e284de..cfcb7e734d 100644 --- a/state/pgstatestorage_test.go +++ b/state/pgstatestorage_test.go @@ -235,6 +235,7 @@ func TestVerifiedBatch(t *testing.T) { StateRoot: common.HexToHash("0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f2"), Aggregator: common.HexToAddress("0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"), TxHash: common.HexToHash("0x29e885edaf8e4b51e1d2e05f9da28161d2fb4f6b1d53827d9b80a23cf2d7d9f1"), + IsTrusted: true, } err = testState.AddVerifiedBatch(ctx, &expectedVerifiedBatch, dbTx) require.NoError(t, err) diff --git a/synchronizer/synchronizer.go b/synchronizer/synchronizer.go index 9e2c9651ea..ab26195241 100644 --- a/synchronizer/synchronizer.go +++ b/synchronizer/synchronizer.go @@ -178,7 +178,7 @@ func (s *ClientSynchronizer) syncBlocks(lastEthBlockSynced *state.Block) (*state for { toBlock := fromBlock + s.cfg.SyncChunkSize - + log.Infof("Syncing block %d of %d", fromBlock, lastKnownBlock.Uint64()) log.Infof("Getting rollup info from block %d to block %d", fromBlock, toBlock) // This function returns the rollup information contained in the ethereum blocks and an extra param called order. // Order param is a map that contains the event order to allow the synchronizer store the info in the same order that is readed. @@ -921,6 +921,7 @@ func (s *ClientSynchronizer) processTrustedVerifyBatches(lastVerifiedBatch ether Aggregator: lastVerifiedBatch.Aggregator, StateRoot: lastVerifiedBatch.StateRoot, TxHash: lastVerifiedBatch.TxHash, + IsTrusted: true, } err = s.state.AddVerifiedBatch(s.ctx, &verifiedB, dbTx) if err != nil { From 6bf84476438b632c4ef5d859a7105486eb856919 Mon Sep 17 00:00:00 2001 From: Alonso Date: Mon, 23 Jan 2023 16:38:56 +0100 Subject: [PATCH 2/2] log --- state/state.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/state/state.go b/state/state.go index a7cc33b1d9..8c0b203c5b 100644 --- a/state/state.go +++ b/state/state.go @@ -567,7 +567,7 @@ func (s *State) processBatch( s.LogExecutorError(res.Error, processBatchRequest) } - log.Infof("It took %v for the executor to process the request", time.Since(now)) + log.Infof("Batch: %d. It took %v for the executor to process the request", batchNumber, time.Since(now)) return res, err }