From 42dc7f8a81e3b919e7968093fbd784a08437538a Mon Sep 17 00:00:00 2001 From: Tudor Malene Date: Tue, 14 May 2024 13:30:35 +0100 Subject: [PATCH] tweak performance (#1912) --- go/enclave/storage/enclavedb/block.go | 8 +++---- .../storage/init/edgelessdb/001_init.sql | 3 ++- go/enclave/storage/init/sqlite/001_init.sql | 3 ++- go/enclave/storage/storage.go | 21 +++++++------------ 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/go/enclave/storage/enclavedb/block.go b/go/enclave/storage/enclavedb/block.go index 155b09fd8b..c88722a010 100644 --- a/go/enclave/storage/enclavedb/block.go +++ b/go/enclave/storage/enclavedb/block.go @@ -71,9 +71,9 @@ func updateCanonicalValue(ctx context.Context, dbtx *sql.Tx, isCanonical bool, b return nil } -func SetMissingBlockId(ctx context.Context, dbtx *sql.Tx, blockId int64, blockHash common.L1BlockHash) error { - // handle the corner case where the block wasn't available - _, err := dbtx.ExecContext(ctx, "update batch set l1_proof=? where (l1_proof is null) and l1_proof_hash=?", blockId, blockHash.Bytes()) +// HandleBlockArrivedAfterBatches- handle the corner case where the block wasn't available when the batch was received +func HandleBlockArrivedAfterBatches(ctx context.Context, dbtx *sql.Tx, blockId int64, blockHash common.L1BlockHash) error { + _, err := dbtx.ExecContext(ctx, "update batch set l1_proof=?, is_canonical=true where l1_proof_hash=?", blockId, blockHash.Bytes()) return err } @@ -92,7 +92,7 @@ func FetchBlockHeaderByHeight(ctx context.Context, db *sql.DB, height *big.Int) func GetBlockId(ctx context.Context, db *sql.Tx, hash common.L1BlockHash) (int64, error) { var id int64 - err := db.QueryRowContext(ctx, "select id from block where hash=? ", hash).Scan(&id) + err := db.QueryRowContext(ctx, "select id from block where hash=? ", hash.Bytes()).Scan(&id) if err != nil { return 0, err } diff --git a/go/enclave/storage/init/edgelessdb/001_init.sql b/go/enclave/storage/init/edgelessdb/001_init.sql index 8dc56204a6..9cc538b0db 100644 --- a/go/enclave/storage/init/edgelessdb/001_init.sql +++ b/go/enclave/storage/init/edgelessdb/001_init.sql @@ -92,7 +92,8 @@ create table if not exists obsdb.batch primary key (sequence), INDEX USING HASH (hash(8)), INDEX USING HASH (l1_proof_hash(8)), - INDEX (body, l1_proof), + INDEX (body), + INDEX (l1_proof), INDEX (height) ); GRANT ALL ON obsdb.batch TO obscuro; diff --git a/go/enclave/storage/init/sqlite/001_init.sql b/go/enclave/storage/init/sqlite/001_init.sql index 6f07aaa91a..d84ad9bebb 100644 --- a/go/enclave/storage/init/sqlite/001_init.sql +++ b/go/enclave/storage/init/sqlite/001_init.sql @@ -80,7 +80,8 @@ create table if not exists batch ); create index IDX_BATCH_HASH on batch (hash); create index IDX_BATCH_BLOCK on batch (l1_proof_hash); -create index IDX_BATCH_BODY on batch (body, l1_proof); +create index IDX_BATCH_BODY on batch (body); +create index IDX_BATCH_L1 on batch (l1_proof); create index IDX_BATCH_HEIGHT on batch (height); create table if not exists tx diff --git a/go/enclave/storage/storage.go b/go/enclave/storage/storage.go index c02adbbb48..38aa1068db 100644 --- a/go/enclave/storage/storage.go +++ b/go/enclave/storage/storage.go @@ -200,7 +200,7 @@ func (s *storageImpl) FetchNonCanonicalBatchesBetween(ctx context.Context, start return enclavedb.ReadNonCanonicalBatches(ctx, s.db.GetSQLDB(), startSeq, endSeq) } -func (s *storageImpl) StoreBlock(ctx context.Context, b *types.Block, chainFork *common.ChainFork) error { +func (s *storageImpl) StoreBlock(ctx context.Context, block *types.Block, chainFork *common.ChainFork) error { defer s.logDuration("StoreBlock", measure.NewStopwatch()) dbTx, err := s.db.NewDBTransaction(ctx) if err != nil { @@ -208,17 +208,17 @@ func (s *storageImpl) StoreBlock(ctx context.Context, b *types.Block, chainFork } defer dbTx.Rollback() - if err := enclavedb.WriteBlock(ctx, dbTx, b.Header()); err != nil { - return fmt.Errorf("2. could not store block %s. Cause: %w", b.Hash(), err) + if err := enclavedb.WriteBlock(ctx, dbTx, block.Header()); err != nil { + return fmt.Errorf("2. could not store block %s. Cause: %w", block.Hash(), err) } - blockId, err := enclavedb.GetBlockId(ctx, dbTx, b.Hash()) + blockId, err := enclavedb.GetBlockId(ctx, dbTx, block.Hash()) if err != nil { - return fmt.Errorf("could not get block id - %w", err) + return fmt.Errorf("3. could not get block id - %w", err) } // In case there were any batches inserted before this block was received - err = enclavedb.SetMissingBlockId(ctx, dbTx, blockId, b.Hash()) + err = enclavedb.HandleBlockArrivedAfterBatches(ctx, dbTx, blockId, block.Hash()) if err != nil { return err } @@ -231,16 +231,11 @@ func (s *storageImpl) StoreBlock(ctx context.Context, b *types.Block, chainFork } } - err = enclavedb.UpdateCanonicalBlocks(ctx, dbTx, []common.L1BlockHash{b.Hash()}, nil, s.logger) - if err != nil { - return err - } - if err := dbTx.Commit(); err != nil { - return fmt.Errorf("3. could not store block %s. Cause: %w", b.Hash(), err) + return fmt.Errorf("4. could not store block %s. Cause: %w", block.Hash(), err) } - common.CacheValue(ctx, s.blockCache, s.logger, b.Hash(), b) + common.CacheValue(ctx, s.blockCache, s.logger, block.Hash(), block) return nil }