From 6c125b622f222384e7bccb36a99b8520b267f314 Mon Sep 17 00:00:00 2001 From: Will Hester Date: Wed, 8 May 2024 17:36:08 +0100 Subject: [PATCH 1/4] add missing batch index --- go/host/storage/hostdb/batch.go | 4 ++-- go/host/storage/init/postgres/001_init.sql | 3 +++ go/host/storage/init/sqlite/001_init.sql | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/go/host/storage/hostdb/batch.go b/go/host/storage/hostdb/batch.go index 5a83d562a1..ae908facc0 100644 --- a/go/host/storage/hostdb/batch.go +++ b/go/host/storage/hostdb/batch.go @@ -16,7 +16,7 @@ const ( selectBatch = "SELECT sequence, full_hash, hash, height, ext_batch FROM batch_host" selectExtBatch = "SELECT ext_batch FROM batch_host" selectLatestBatch = "SELECT sequence, full_hash, hash, height, ext_batch FROM batch_host ORDER BY sequence DESC LIMIT 1" - selectTxsAndBatch = "SELECT t.hash FROM transaction_host t JOIN batch_host b ON t.b_sequence = b.sequence WHERE b.full_hash = " + selectTxsAndBatch = "SELECT t.hash FROM transaction_host t JOIN batch_host b ON t.b_sequence = b.sequence WHERE b.hash = " selectBatchSeqByTx = "SELECT b_sequence FROM transaction_host WHERE hash = " selectTxBySeq = "SELECT full_hash FROM transaction_host WHERE b_sequence = " selectBatchTxs = "SELECT t.full_hash, b.sequence, b.height, b.ext_batch FROM transaction_host t JOIN batch_host b ON t.b_sequence = b.sequence" @@ -201,7 +201,7 @@ func GetBatchNumber(db HostDB, txHash gethcommon.Hash) (*big.Int, error) { // GetBatchTxHashes returns the transaction hashes of the batch with the given hash. func GetBatchTxHashes(db HostDB, batchHash gethcommon.Hash) ([]gethcommon.Hash, error) { query := selectTxsAndBatch + db.GetSQLStatement().Placeholder - rows, err := db.GetSQLDB().Query(query, batchHash) + rows, err := db.GetSQLDB().Query(query, truncTo16(batchHash)) if err != nil { return nil, fmt.Errorf("query execution failed: %w", err) } diff --git a/go/host/storage/init/postgres/001_init.sql b/go/host/storage/init/postgres/001_init.sql index 94174319af..c23ebe5985 100644 --- a/go/host/storage/init/postgres/001_init.sql +++ b/go/host/storage/init/postgres/001_init.sql @@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS batch_host ext_batch BYTEA NOT NULL ); +CREATE INDEX IF NOT EXISTS IDX_BATCH_HASH_HOST ON batch_host USING HASH (hash); CREATE INDEX IF NOT EXISTS IDX_BATCH_HEIGHT_HOST ON batch_host (height); CREATE TABLE IF NOT EXISTS transaction_host @@ -42,6 +43,8 @@ CREATE TABLE IF NOT EXISTS transaction_host FOREIGN KEY (b_sequence) REFERENCES batch_host(sequence) ); +CREATE INDEX IF NOT EXISTS IDX_TX_SEQ_HOST ON batch_host (b_sequence); + CREATE TABLE IF NOT EXISTS transaction_count ( id SERIAL PRIMARY KEY, diff --git a/go/host/storage/init/sqlite/001_init.sql b/go/host/storage/init/sqlite/001_init.sql index cb2e4d0869..c299176d58 100644 --- a/go/host/storage/init/sqlite/001_init.sql +++ b/go/host/storage/init/sqlite/001_init.sql @@ -31,6 +31,7 @@ create table if not exists batch_host height int NOT NULL, ext_batch mediumblob NOT NULL ); +create index IDX_BATCH_HASH_HOST on batch_host (hash); create index IDX_BATCH_HEIGHT_HOST on batch_host (height); create table if not exists transaction_host From 07850725d95aea0ccabbadd6b37485a32f074cf4 Mon Sep 17 00:00:00 2001 From: Will Hester Date: Wed, 8 May 2024 17:40:21 +0100 Subject: [PATCH 2/4] fix table name --- go/host/storage/init/postgres/001_init.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/host/storage/init/postgres/001_init.sql b/go/host/storage/init/postgres/001_init.sql index c23ebe5985..87eece1b15 100644 --- a/go/host/storage/init/postgres/001_init.sql +++ b/go/host/storage/init/postgres/001_init.sql @@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS transaction_host FOREIGN KEY (b_sequence) REFERENCES batch_host(sequence) ); -CREATE INDEX IF NOT EXISTS IDX_TX_SEQ_HOST ON batch_host (b_sequence); +CREATE INDEX IF NOT EXISTS IDX_TX_SEQ_HOST ON transaction_host (b_sequence); CREATE TABLE IF NOT EXISTS transaction_count ( From 44a309fdbdaa4d5ca4dde89a6e3e40bd1ca66fb3 Mon Sep 17 00:00:00 2001 From: Will Hester Date: Thu, 9 May 2024 10:05:09 +0100 Subject: [PATCH 3/4] add LogMethodDuration calls --- go/host/storage/init/sqlite/001_init.sql | 3 +++ go/host/storage/storage.go | 27 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/go/host/storage/init/sqlite/001_init.sql b/go/host/storage/init/sqlite/001_init.sql index c299176d58..7db851b9ed 100644 --- a/go/host/storage/init/sqlite/001_init.sql +++ b/go/host/storage/init/sqlite/001_init.sql @@ -34,6 +34,7 @@ create table if not exists batch_host create index IDX_BATCH_HASH_HOST on batch_host (hash); create index IDX_BATCH_HEIGHT_HOST on batch_host (height); + create table if not exists transaction_host ( hash binary(16) PRIMARY KEY, @@ -41,6 +42,8 @@ create table if not exists transaction_host b_sequence int REFERENCES batch_host ); +create index IDX_TX_SEQ_HOST ON transaction_host (b_sequence); + create table if not exists transaction_count ( id int NOT NULL PRIMARY KEY, diff --git a/go/host/storage/storage.go b/go/host/storage/storage.go index 9b6d1681f7..cff5eda994 100644 --- a/go/host/storage/storage.go +++ b/go/host/storage/storage.go @@ -2,6 +2,8 @@ package storage import ( "fmt" + "github.com/ten-protocol/go-ten/go/common/measure" + "github.com/ten-protocol/go-ten/go/enclave/core" "io" "math/big" @@ -22,6 +24,7 @@ type storageImpl struct { } func (s *storageImpl) AddBatch(batch *common.ExtBatch) error { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Add batch", log.BatchHashKey, batch.Hash()) // Check if the Batch is already stored _, err := hostdb.GetBatchHeader(s.db, batch.Hash()) if err == nil { @@ -47,6 +50,7 @@ func (s *storageImpl) AddBatch(batch *common.ExtBatch) error { } func (s *storageImpl) AddRollup(rollup *common.ExtRollup, metadata *common.PublicRollupMetadata, block *common.L1Block) error { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Add rollup", log.RollupHashKey, rollup.Hash()) // Check if the Header is already stored _, err := hostdb.GetRollupHeader(s.db, rollup.Header.Hash()) if err == nil { @@ -72,6 +76,7 @@ func (s *storageImpl) AddRollup(rollup *common.ExtRollup, metadata *common.Publi } func (s *storageImpl) AddBlock(b *types.Header, rollupHash common.L2RollupHash) error { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Add block", log.BlockHashKey, b.Hash()) dbtx, err := s.db.NewDBTransaction() if err != nil { return err @@ -91,90 +96,112 @@ func (s *storageImpl) AddBlock(b *types.Header, rollupHash common.L2RollupHash) } func (s *storageImpl) FetchBatchBySeqNo(seqNum uint64) (*common.ExtBatch, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by seq no", log.BatchSeqNoKey, seqNum) return hostdb.GetBatchBySequenceNumber(s.db, seqNum) } func (s *storageImpl) FetchBatchHashByHeight(number *big.Int) (*gethcommon.Hash, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by height", log.BatchHeightKey, number.Uint64()) return hostdb.GetBatchHashByNumber(s.db, number) } func (s *storageImpl) FetchBatchHeaderByHash(hash gethcommon.Hash) (*common.BatchHeader, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by hash", log.BatchHashKey, hash) return hostdb.GetBatchHeader(s.db, hash) } func (s *storageImpl) FetchHeadBatchHeader() (*common.BatchHeader, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch head batch header") return hostdb.GetHeadBatchHeader(s.db) } func (s *storageImpl) FetchPublicBatchByHash(batchHash common.L2BatchHash) (*common.PublicBatch, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch public batch by hash", log.BatchHashKey, batchHash) return hostdb.GetPublicBatch(s.db, batchHash) } func (s *storageImpl) FetchBatch(batchHash gethcommon.Hash) (*common.ExtBatch, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by hash", log.BatchHashKey, batchHash) return hostdb.GetBatchByHash(s.db, batchHash) } func (s *storageImpl) FetchBatchByTx(txHash gethcommon.Hash) (*common.ExtBatch, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by transaction", log.TransactionHashKey, txHash) return hostdb.GetBatchByTx(s.db, txHash) } func (s *storageImpl) FetchLatestBatch() (*common.BatchHeader, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch latest batch") return hostdb.GetLatestBatch(s.db) } func (s *storageImpl) FetchBatchHeaderByHeight(height *big.Int) (*common.BatchHeader, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch header by height", log.BatchHeightKey, height.Uint64()) return hostdb.GetBatchHeaderByHeight(s.db, height) } func (s *storageImpl) FetchBatchByHeight(height *big.Int) (*common.PublicBatch, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by height", log.BatchHeightKey, height.Uint64()) return hostdb.GetBatchByHeight(s.db, height) } func (s *storageImpl) FetchBatchListing(pagination *common.QueryPagination) (*common.BatchListingResponse, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch listing") return hostdb.GetBatchListing(s.db, pagination) } func (s *storageImpl) FetchBatchListingDeprecated(pagination *common.QueryPagination) (*common.BatchListingResponseDeprecated, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch listing (deprecated)") return hostdb.GetBatchListingDeprecated(s.db, pagination) } func (s *storageImpl) FetchLatestRollupHeader() (*common.RollupHeader, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch latest rollup header") return hostdb.GetLatestRollup(s.db) } func (s *storageImpl) FetchRollupListing(pagination *common.QueryPagination) (*common.RollupListingResponse, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch rollup listing") return hostdb.GetRollupListing(s.db, pagination) } func (s *storageImpl) FetchBlockListing(pagination *common.QueryPagination) (*common.BlockListingResponse, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch block listing") return hostdb.GetBlockListing(s.db, pagination) } func (s *storageImpl) FetchTotalTxCount() (*big.Int, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch total transaction count") return hostdb.GetTotalTxCount(s.db) } func (s *storageImpl) FetchTransaction(hash gethcommon.Hash) (*common.PublicTransaction, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch transaction by hash", log.TxKey, hash) return hostdb.GetTransaction(s.db, hash) } func (s *storageImpl) FetchRollupByHash(rollupHash gethcommon.Hash) (*common.PublicRollup, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch rollup by hash", log.RollupHashKey, rollupHash) return hostdb.GetRollupByHash(s.db, rollupHash) } func (s *storageImpl) FetchRollupBySeqNo(seqNo uint64) (*common.PublicRollup, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch rollup by sequence number", log.RollupHashKey, seqNo) return hostdb.GetRollupBySeqNo(s.db, seqNo) } func (s *storageImpl) FetchRollupBatches(rollupHash gethcommon.Hash) (*common.BatchListingResponse, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch rollup batches", log.RollupHashKey, rollupHash) return hostdb.GetRollupBatches(s.db, rollupHash) } func (s *storageImpl) FetchBatchTransactions(batchHash gethcommon.Hash) (*common.TransactionListingResponse, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch transactions", log.BatchHashKey, batchHash) return hostdb.GetBatchTransactions(s.db, batchHash) } func (s *storageImpl) FetchTransactionListing(pagination *common.QueryPagination) (*common.TransactionListingResponse, error) { + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch transaction listing") return hostdb.GetTransactionListing(s.db, pagination) } From 05083d9ca86fcb9f7bbaa461b97a82ac12112f77 Mon Sep 17 00:00:00 2001 From: Will Hester Date: Thu, 9 May 2024 10:27:20 +0100 Subject: [PATCH 4/4] fix key --- go/host/storage/storage.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/host/storage/storage.go b/go/host/storage/storage.go index cff5eda994..a7d4a5dbb5 100644 --- a/go/host/storage/storage.go +++ b/go/host/storage/storage.go @@ -126,7 +126,7 @@ func (s *storageImpl) FetchBatch(batchHash gethcommon.Hash) (*common.ExtBatch, e } func (s *storageImpl) FetchBatchByTx(txHash gethcommon.Hash) (*common.ExtBatch, error) { - defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by transaction", log.TransactionHashKey, txHash) + defer core.LogMethodDuration(s.logger, measure.NewStopwatch(), "Fetch batch by transaction", log.TxKey, txHash) return hostdb.GetBatchByTx(s.db, txHash) }