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

add index on tx.sender, and clean up #1921

Merged
merged 1 commit into from
May 17, 2024
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
8 changes: 4 additions & 4 deletions go/enclave/rpc/GetCustomQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func GetCustomQueryExecute(builder *CallBuilder[common.PrivateCustomQueryListTra
return nil //nolint:nilerr
}

encryptReceipts, err := rpc.storage.GetReceiptsPerAddress(builder.ctx, &builder.Param.Address, &builder.Param.Pagination)
encryptReceipts, err := rpc.storage.GetTransactionsPerAddress(builder.ctx, &builder.Param.Address, &builder.Param.Pagination)
if err != nil {
return fmt.Errorf("GetReceiptsPerAddress - %w", err)
return fmt.Errorf("GetTransactionsPerAddress - %w", err)
}

receiptsCount, err := rpc.storage.GetReceiptsPerAddressCount(builder.ctx, &builder.Param.Address)
receiptsCount, err := rpc.storage.CountTransactionsPerAddress(builder.ctx, &builder.Param.Address)
if err != nil {
return fmt.Errorf("GetReceiptsPerAddressCount - %w", err)
return fmt.Errorf("CountTransactionsPerAddress - %w", err)
}

builder.ReturnValue = &common.PrivateQueryResponse{
Expand Down
63 changes: 2 additions & 61 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,11 @@ func BatchWasExecuted(ctx context.Context, db *sql.DB, hash common.L2BatchHash)
return result, nil
}

func GetReceiptsPerAddress(ctx context.Context, db *sql.DB, config *params.ChainConfig, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error) {
// todo - not indexed
func GetTransactionsPerAddress(ctx context.Context, db *sql.DB, config *params.ChainConfig, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error) {
return selectReceipts(ctx, db, config, "where tx.sender_address = ? ORDER BY height DESC LIMIT ? OFFSET ? ", address.Bytes(), pagination.Size, pagination.Offset)
}

func GetReceiptsPerAddressCount(ctx context.Context, db *sql.DB, address *gethcommon.Address) (uint64, error) {
// todo - this is not indexed and will do a full table scan!
func CountTransactionsPerAddress(ctx context.Context, db *sql.DB, address *gethcommon.Address) (uint64, error) {
row := db.QueryRowContext(ctx, "select count(1) from exec_tx join tx on tx.id=exec_tx.tx join batch on batch.sequence=exec_tx.batch "+" where tx.sender_address = ?", address.Bytes())

var count uint64
Expand All @@ -457,63 +455,6 @@ func GetReceiptsPerAddressCount(ctx context.Context, db *sql.DB, address *gethco
return count, nil
}

func GetPublicTransactionData(ctx context.Context, db *sql.DB, pagination *common.QueryPagination) ([]common.PublicTransaction, error) {
return selectPublicTxsBySender(ctx, db, " ORDER BY height DESC LIMIT ? OFFSET ? ", pagination.Size, pagination.Offset)
}

func selectPublicTxsBySender(ctx context.Context, db *sql.DB, query string, args ...any) ([]common.PublicTransaction, error) {
var publicTxs []common.PublicTransaction

q := "select tx.hash, batch.height, batch.header from exec_tx join batch on batch.sequence=exec_tx.batch join tx on tx.id=exec_tx.tx where batch.is_canonical=true " + query
rows, err := db.QueryContext(ctx, q, args...)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// make sure the error is converted to obscuro-wide not found error
return nil, errutil.ErrNotFound
}
return nil, err
}
defer rows.Close()
for rows.Next() {
var txHash []byte
var batchHeight uint64
var batchHeader string
err := rows.Scan(&txHash, &batchHeight, &batchHeader)
if err != nil {
return nil, err
}

h := new(common.BatchHeader)
if err := rlp.DecodeBytes([]byte(batchHeader), h); err != nil {
return nil, fmt.Errorf("could not decode batch header. Cause: %w", err)
}

publicTxs = append(publicTxs, common.PublicTransaction{
TransactionHash: gethcommon.BytesToHash(txHash),
BatchHeight: big.NewInt(0).SetUint64(batchHeight),
BatchTimestamp: h.Time,
Finality: common.BatchFinal,
})
}
if rows.Err() != nil {
return nil, rows.Err()
}

return publicTxs, nil
}

func GetPublicTransactionCount(ctx context.Context, db *sql.DB) (uint64, error) {
row := db.QueryRowContext(ctx, "select count(1) from exec_tx join batch on batch.sequence=exec_tx.batch where batch.is_canonical=true")

var count uint64
err := row.Scan(&count)
if err != nil {
return 0, err
}

return count, nil
}

func FetchConvertedBatchHash(ctx context.Context, db *sql.DB, seqNo uint64) (gethcommon.Hash, error) {
var hash []byte

Expand Down
1 change: 1 addition & 0 deletions go/enclave/storage/init/edgelessdb/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ create table if not exists obsdb.tx
idx int NOT NULL,
body int NOT NULL,
INDEX USING HASH (hash(8)),
INDEX USING HASH (sender_address),
primary key (id)
);
GRANT ALL ON obsdb.tx TO obscuro;
Expand Down
1 change: 1 addition & 0 deletions go/enclave/storage/init/sqlite/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ create table if not exists tx
body int NOT NULL REFERENCES batch_body
);
create index IDX_TX_HASH on tx (hash);
create index IDX_TX_SENDER_ADDRESS on tx (sender_address);

create table if not exists exec_tx
(
Expand Down
6 changes: 2 additions & 4 deletions go/enclave/storage/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ type Storage interface {

type ScanStorage interface {
GetContractCount(ctx context.Context) (*big.Int, error)
GetReceiptsPerAddress(ctx context.Context, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error)
GetPublicTransactionData(ctx context.Context, pagination *common.QueryPagination) ([]common.PublicTransaction, error)
GetPublicTransactionCount(ctx context.Context) (uint64, error)
GetTransactionsPerAddress(ctx context.Context, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error)

GetReceiptsPerAddressCount(ctx context.Context, addr *gethcommon.Address) (uint64, error)
CountTransactionsPerAddress(ctx context.Context, addr *gethcommon.Address) (uint64, error)
}
22 changes: 6 additions & 16 deletions go/enclave/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,24 +668,14 @@ func (s *storageImpl) BatchWasExecuted(ctx context.Context, hash common.L2BatchH
return enclavedb.BatchWasExecuted(ctx, s.db.GetSQLDB(), hash)
}

func (s *storageImpl) GetReceiptsPerAddress(ctx context.Context, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error) {
defer s.logDuration("GetReceiptsPerAddress", measure.NewStopwatch())
return enclavedb.GetReceiptsPerAddress(ctx, s.db.GetSQLDB(), s.chainConfig, address, pagination)
func (s *storageImpl) GetTransactionsPerAddress(ctx context.Context, address *gethcommon.Address, pagination *common.QueryPagination) (types.Receipts, error) {
defer s.logDuration("GetTransactionsPerAddress", measure.NewStopwatch())
return enclavedb.GetTransactionsPerAddress(ctx, s.db.GetSQLDB(), s.chainConfig, address, pagination)
}

func (s *storageImpl) GetReceiptsPerAddressCount(ctx context.Context, address *gethcommon.Address) (uint64, error) {
defer s.logDuration("GetReceiptsPerAddressCount", measure.NewStopwatch())
return enclavedb.GetReceiptsPerAddressCount(ctx, s.db.GetSQLDB(), address)
}

func (s *storageImpl) GetPublicTransactionData(ctx context.Context, pagination *common.QueryPagination) ([]common.PublicTransaction, error) {
defer s.logDuration("GetPublicTransactionData", measure.NewStopwatch())
return enclavedb.GetPublicTransactionData(ctx, s.db.GetSQLDB(), pagination)
}

func (s *storageImpl) GetPublicTransactionCount(ctx context.Context) (uint64, error) {
defer s.logDuration("GetPublicTransactionCount", measure.NewStopwatch())
return enclavedb.GetPublicTransactionCount(ctx, s.db.GetSQLDB())
func (s *storageImpl) CountTransactionsPerAddress(ctx context.Context, address *gethcommon.Address) (uint64, error) {
defer s.logDuration("CountTransactionsPerAddress", measure.NewStopwatch())
return enclavedb.CountTransactionsPerAddress(ctx, s.db.GetSQLDB(), address)
}

func (s *storageImpl) logDuration(method string, stopWatch *measure.Stopwatch) {
Expand Down
Loading