Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db flag to identify trusted verified batches #1575

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions db/migrations/state/0005.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
-- +migrate Down
DROP TABLE IF EXISTS state.monitored_txs;

ALTER TABLE state.verified_batch
DROP COLUMN IF EXISTS is_trusted;

tclemos marked this conversation as resolved.
Show resolved Hide resolved
-- +migrate Up
CREATE TABLE state.monitored_txs
(
Expand All @@ -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;
1 change: 1 addition & 0 deletions state/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type VerifiedBatch struct {
Aggregator common.Address
TxHash common.Hash
StateRoot common.Hash
IsTrusted bool
}

// VirtualBatch represents a VirtualBatch
Expand Down
8 changes: 4 additions & 4 deletions state/pgstatestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions state/pgstatestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -921,6 +921,7 @@ func (s *ClientSynchronizer) processTrustedVerifyBatches(lastVerifiedBatch ether
Aggregator: lastVerifiedBatch.Aggregator,
StateRoot: lastVerifiedBatch.StateRoot,
TxHash: lastVerifiedBatch.TxHash,
IsTrusted: true,
tclemos marked this conversation as resolved.
Show resolved Hide resolved
}
err = s.state.AddVerifiedBatch(s.ctx, &verifiedB, dbTx)
if err != nil {
Expand Down