Skip to content

Commit

Permalink
tweak performance (#1912)
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene authored May 14, 2024
1 parent 596ddf0 commit 42dc7f8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
8 changes: 4 additions & 4 deletions go/enclave/storage/enclavedb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion go/enclave/storage/init/edgelessdb/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion go/enclave/storage/init/sqlite/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 8 additions & 13 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,25 +200,25 @@ 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 {
return fmt.Errorf("could not create DB transaction - %w", err)
}
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
}
Expand All @@ -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
}
Expand Down

0 comments on commit 42dc7f8

Please sign in to comment.