Skip to content

Commit

Permalink
update index approach
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed May 10, 2024
1 parent 1f47bdf commit c556311
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 184 deletions.
53 changes: 25 additions & 28 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
const (
selectBatch = "select b.header, bb.content from batch b join batch_body bb on b.body=bb.id"

queryReceipts = "select exec_tx.receipt, tx.content, batch.full_hash, batch.height from exec_tx join tx on tx.id=exec_tx.tx join batch on batch.sequence=exec_tx.batch "
queryReceipts = "select exec_tx.receipt, tx.content, batch.hash, batch.height from exec_tx join tx on tx.id=exec_tx.tx join batch on batch.sequence=exec_tx.batch "
)

// WriteBatchAndTransactions - persists the batch and the transactions
Expand All @@ -45,8 +45,8 @@ func WriteBatchAndTransactions(ctx context.Context, dbtx *sql.Tx, batch *core.Ba

var isCanon bool
err = dbtx.QueryRowContext(ctx,
"select is_canonical from block where hash=? and full_hash=?",
truncTo4(batch.Header.L1Proof), batch.Header.L1Proof.Bytes(),
"select is_canonical from block where hash=? ",
batch.Header.L1Proof.Bytes(),
).Scan(&isCanon)
if err != nil {
// if the block is not found, we assume it is non-canonical
Expand All @@ -56,9 +56,8 @@ func WriteBatchAndTransactions(ctx context.Context, dbtx *sql.Tx, batch *core.Ba

args := []any{
batch.Header.SequencerOrderNo.Uint64(), // sequence
batch.Hash(), // full hash
convertedHash, // converted_hash
truncTo4(batch.Hash()), // index hash
batch.Hash(), // hash
batch.Header.Number.Uint64(), // height
isCanon, // is_canonical
header, // header blob
Expand All @@ -71,14 +70,14 @@ func WriteBatchAndTransactions(ctx context.Context, dbtx *sql.Tx, batch *core.Ba
args = append(args, blockId)
}
args = append(args, false) // executed
_, err = dbtx.ExecContext(ctx, "insert into batch values (?,?,?,?,?,?,?,?,?,?,?)", args...)
_, err = dbtx.ExecContext(ctx, "insert into batch values (?,?,?,?,?,?,?,?,?,?)", args...)
if err != nil {
return err
}

// creates a big insert statement for all transactions
if len(batch.Transactions) > 0 {
insert := "replace into tx (hash, full_hash, content, sender_address, nonce, idx, body) values " + repeat("(?,?,?,?,?,?,?)", ",", len(batch.Transactions))
insert := "replace into tx (hash, content, sender_address, nonce, idx, body) values " + repeat("(?,?,?,?,?,?)", ",", len(batch.Transactions))

args := make([]any, 0)
for i, transaction := range batch.Transactions {
Expand All @@ -92,13 +91,12 @@ func WriteBatchAndTransactions(ctx context.Context, dbtx *sql.Tx, batch *core.Ba
return fmt.Errorf("unable to convert tx to message - %w", err)
}

args = append(args, truncTo4(transaction.Hash())) // truncated tx_hash
args = append(args, transaction.Hash()) // full tx_hash
args = append(args, txBytes) // content
args = append(args, from.Bytes()) // sender_address
args = append(args, transaction.Nonce()) // nonce
args = append(args, i) // idx
args = append(args, batchBodyID) // the batch body which contained it
args = append(args, transaction.Hash()) // tx_hash
args = append(args, txBytes) // content
args = append(args, from.Bytes()) // sender_address
args = append(args, transaction.Nonce()) // nonce
args = append(args, i) // idx
args = append(args, batchBodyID) // the batch body which contained it
}
_, err = dbtx.ExecContext(ctx, insert, args...)
if err != nil {
Expand Down Expand Up @@ -127,9 +125,8 @@ func WriteBatchExecution(ctx context.Context, dbtx *sql.Tx, seqNo *big.Int, rece

// ignore the error because synthetic transactions will not be inserted
txId, _ := GetTxId(ctx, dbtx, storageReceipt.TxHash)
args = append(args, truncBTo4(receipt.ContractAddress.Bytes())) // created_contract_address
args = append(args, receipt.ContractAddress.Bytes()) // created_contract_address
args = append(args, receiptBytes) // the serialised receipt
args = append(args, receipt.ContractAddress.Bytes()) // created_contract_address
args = append(args, receiptBytes) // the serialised receipt
if txId == 0 {
args = append(args, nil) // tx id
} else {
Expand All @@ -138,7 +135,7 @@ func WriteBatchExecution(ctx context.Context, dbtx *sql.Tx, seqNo *big.Int, rece
args = append(args, seqNo.Uint64()) // batch_seq
}
if len(args) > 0 {
insert := "insert into exec_tx (created_contract_address,created_contract_address_full, receipt, tx, batch) values " + repeat("(?,?,?,?,?)", ",", len(receipts))
insert := "insert into exec_tx (created_contract_address, receipt, tx, batch) values " + repeat("(?,?,?,?)", ",", len(receipts))
_, err = dbtx.ExecContext(ctx, insert, args...)
if err != nil {
return err
Expand All @@ -149,7 +146,7 @@ func WriteBatchExecution(ctx context.Context, dbtx *sql.Tx, seqNo *big.Int, rece

func GetTxId(ctx context.Context, dbtx *sql.Tx, txHash gethcommon.Hash) (int64, error) {
var txId int64
err := dbtx.QueryRowContext(ctx, "select id from tx where hash=? and full_hash=?", truncTo4(txHash), txHash.Bytes()).Scan(&txId)
err := dbtx.QueryRowContext(ctx, "select id from tx where hash=? ", txHash.Bytes()).Scan(&txId)
return txId, err
}

Expand All @@ -158,7 +155,7 @@ func ReadBatchBySeqNo(ctx context.Context, db *sql.DB, seqNo uint64) (*core.Batc
}

func ReadBatchByHash(ctx context.Context, db *sql.DB, hash common.L2BatchHash) (*core.Batch, error) {
return fetchBatch(ctx, db, " where b.hash=? and b.full_hash=?", truncTo4(hash), hash.Bytes())
return fetchBatch(ctx, db, " where b.hash=? ", hash.Bytes())
}

func ReadCanonicalBatchByHeight(ctx context.Context, db *sql.DB, height uint64) (*core.Batch, error) {
Expand All @@ -175,7 +172,7 @@ func ReadCurrentHeadBatch(ctx context.Context, db *sql.DB) (*core.Batch, error)
}

func ReadBatchesByBlock(ctx context.Context, db *sql.DB, hash common.L1BlockHash) ([]*core.Batch, error) {
return fetchBatches(ctx, db, " join block l1b on b.l1_proof=l1b.id where l1b.hash=? and l1b.full_hash=? order by b.sequence", truncTo4(hash), hash.Bytes())
return fetchBatches(ctx, db, " join block l1b on b.l1_proof=l1b.id where l1b.hash=? order by b.sequence", hash.Bytes())
}

func ReadCurrentSequencerNo(ctx context.Context, db *sql.DB) (*big.Int, error) {
Expand Down Expand Up @@ -326,12 +323,12 @@ func selectReceipts(ctx context.Context, db *sql.DB, config *params.ChainConfig,
// corresponding block body, so if the block body is not found it will return nil even
// if the receipt itself is stored.
func ReadReceiptsByBatchHash(ctx context.Context, db *sql.DB, hash common.L2BatchHash, config *params.ChainConfig) (types.Receipts, error) {
return selectReceipts(ctx, db, config, "where batch.hash=? and batch.full_hash=?", truncTo4(hash), hash.Bytes())
return selectReceipts(ctx, db, config, "where batch.hash=? ", hash.Bytes())
}

func ReadReceipt(ctx context.Context, db *sql.DB, txHash common.L2TxHash, config *params.ChainConfig) (*types.Receipt, error) {
// todo - canonical?
row := db.QueryRowContext(ctx, queryReceipts+" where tx.hash=? and tx.full_hash=?", truncTo4(txHash), txHash.Bytes())
row := db.QueryRowContext(ctx, queryReceipts+" where tx.hash=? ", txHash.Bytes())
// receipt, tx, batch, height
var receiptData []byte
var txData []byte
Expand Down Expand Up @@ -368,8 +365,8 @@ func ReadReceipt(ctx context.Context, db *sql.DB, txHash common.L2TxHash, config

func ReadTransaction(ctx context.Context, db *sql.DB, txHash gethcommon.Hash) (*types.Transaction, common.L2BatchHash, uint64, uint64, error) {
row := db.QueryRowContext(ctx,
"select tx.content, batch.full_hash, batch.height, tx.idx from exec_tx join tx on tx.id=exec_tx.tx join batch on batch.sequence=exec_tx.batch where batch.is_canonical=true and tx.hash=? and tx.full_hash=?",
truncTo4(txHash), txHash.Bytes())
"select tx.content, batch.hash, batch.height, tx.idx from exec_tx join tx on tx.id=exec_tx.tx join batch on batch.sequence=exec_tx.batch where batch.is_canonical=true and tx.hash=?",
txHash.Bytes())

// tx, batch, height, idx
var txData []byte
Expand All @@ -394,7 +391,7 @@ func ReadTransaction(ctx context.Context, db *sql.DB, txHash gethcommon.Hash) (*
}

func GetContractCreationTx(ctx context.Context, db *sql.DB, address gethcommon.Address) (*gethcommon.Hash, error) {
row := db.QueryRowContext(ctx, "select tx.full_hash from exec_tx join tx on tx.id=exec_tx.tx where created_contract_address=? and created_contract_address_full=?", truncBTo4(address.Bytes()), address.Bytes())
row := db.QueryRowContext(ctx, "select tx.hash from exec_tx join tx on tx.id=exec_tx.tx where created_contract_address=? ", address.Bytes())

var txHashBytes []byte
err := row.Scan(&txHashBytes)
Expand Down Expand Up @@ -427,7 +424,7 @@ func ReadUnexecutedBatches(ctx context.Context, db *sql.DB, from *big.Int) ([]*c
}

func BatchWasExecuted(ctx context.Context, db *sql.DB, hash common.L2BatchHash) (bool, error) {
row := db.QueryRowContext(ctx, "select is_executed from batch where is_canonical=true and hash=? and full_hash=?", truncTo4(hash), hash.Bytes())
row := db.QueryRowContext(ctx, "select is_executed from batch where is_canonical=true and hash=? ", hash.Bytes())

var result bool
err := row.Scan(&result)
Expand Down Expand Up @@ -467,7 +464,7 @@ func GetPublicTransactionData(ctx context.Context, db *sql.DB, pagination *commo
func selectPublicTxsBySender(ctx context.Context, db *sql.DB, query string, args ...any) ([]common.PublicTransaction, error) {
var publicTxs []common.PublicTransaction

q := "select tx.full_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
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) {
Expand Down
34 changes: 16 additions & 18 deletions go/enclave/storage/enclavedb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ func WriteBlock(ctx context.Context, dbtx *sql.Tx, b *types.Header) error {
return fmt.Errorf("could not encode block header. Cause: %w", err)
}

_, err = dbtx.ExecContext(ctx, "insert into block (hash,full_hash,is_canonical,header,height) values (?,?,?,?,?)",
truncTo4(b.Hash()), // hash
b.Hash().Bytes(), // full_hash
true, // is_canonical
header, // header
b.Number.Uint64(), // height
_, err = dbtx.ExecContext(ctx, "insert into block (hash,is_canonical,header,height) values (?,?,?,?)",
b.Hash().Bytes(), // hash
true, // is_canonical
header, // header
b.Number.Uint64(), // height
)
return err
}
Expand All @@ -49,12 +48,12 @@ func UpdateCanonicalBlocks(ctx context.Context, dbtx *sql.Tx, canonical []common
}

func updateCanonicalValue(ctx context.Context, dbtx *sql.Tx, isCanonical bool, blocks []common.L1BlockHash, _ gethlog.Logger) error {
currentBlocks := repeat("(hash=? and full_hash=?)", "OR", len(blocks))
currentBlocks := repeat(" hash=? ", "OR", len(blocks))

args := make([]any, 0)
args = append(args, isCanonical)
for _, blockHash := range blocks {
args = append(args, truncTo4(blockHash), blockHash.Bytes())
args = append(args, blockHash.Bytes())
}

updateBlocks := "update block set is_canonical=? where " + currentBlocks
Expand All @@ -80,7 +79,7 @@ func SetMissingBlockId(ctx context.Context, dbtx *sql.Tx, blockId int64, blockHa

// todo - remove this. For now creates a "block" but without a body.
func FetchBlock(ctx context.Context, db *sql.DB, hash common.L1BlockHash) (*types.Block, error) {
return fetchBlock(ctx, db, " where hash=? and full_hash=?", truncTo4(hash), hash.Bytes())
return fetchBlock(ctx, db, " where hash=?", hash.Bytes())
}

func FetchHeadBlock(ctx context.Context, db *sql.DB) (*types.Block, error) {
Expand All @@ -93,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=? and full_hash=?", truncTo4(hash), hash).Scan(&id)
err := db.QueryRowContext(ctx, "select id from block where hash=? ", hash).Scan(&id)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -123,8 +122,8 @@ func WriteL1Messages[T any](ctx context.Context, db *sql.Tx, blockId int64, mess

func FetchL1Messages[T any](ctx context.Context, db *sql.DB, blockHash common.L1BlockHash, isTransfer bool) ([]T, error) {
var result []T
query := "select message from l1_msg m join block b on m.block=b.id where b.hash = ? and b.full_hash = ? and is_transfer = ?"
rows, err := db.QueryContext(ctx, query, truncTo4(blockHash), blockHash.Bytes(), isTransfer)
query := "select message from l1_msg m join block b on m.block=b.id where b.hash = ? and is_transfer = ?"
rows, err := db.QueryContext(ctx, query, blockHash.Bytes(), isTransfer)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// make sure the error is converted to obscuro-wide not found error
Expand Down Expand Up @@ -158,8 +157,7 @@ func WriteRollup(ctx context.Context, dbtx *sql.Tx, rollup *common.RollupHeader,
if err != nil {
return fmt.Errorf("could not encode batch header. Cause: %w", err)
}
_, err = dbtx.ExecContext(ctx, "replace into rollup (hash, full_hash, start_seq, end_seq, time_stamp, header, compression_block) values (?,?,?,?,?,?,?)",
truncTo4(rollup.Hash()),
_, err = dbtx.ExecContext(ctx, "replace into rollup (hash, start_seq, end_seq, time_stamp, header, compression_block) values (?,?,?,?,?,?)",
rollup.Hash().Bytes(),
internalHeader.FirstBatchSequence.Uint64(),
rollup.LastBatchSeqNo,
Expand All @@ -175,13 +173,13 @@ func WriteRollup(ctx context.Context, dbtx *sql.Tx, rollup *common.RollupHeader,
}

func FetchReorgedRollup(ctx context.Context, db *sql.DB, reorgedBlocks []common.L1BlockHash) (*common.L2BatchHash, error) {
whereClause := repeat("(b.hash=? and b.full_hash=?)", "OR", len(reorgedBlocks))
whereClause := repeat(" b.hash=? ", "OR", len(reorgedBlocks))

query := "select r.full_hash from rollup r join block b on r.compression_block=b.id where " + whereClause
query := "select r.hash from rollup r join block b on r.compression_block=b.id where " + whereClause

args := make([]any, 0)
for _, blockHash := range reorgedBlocks {
args = append(args, truncTo4(blockHash), blockHash.Bytes())
args = append(args, blockHash.Bytes())
}
rollup := new(common.L2BatchHash)
err := db.QueryRowContext(ctx, query, args...).Scan(&rollup)
Expand All @@ -201,7 +199,7 @@ func FetchRollupMetadata(ctx context.Context, db *sql.DB, hash common.L2RollupHa

rollup := new(common.PublicRollupMetadata)
err := db.QueryRowContext(ctx,
"select start_seq, time_stamp from rollup where hash = ? and full_hash=?", truncTo4(hash), hash.Bytes(),
"select start_seq, time_stamp from rollup where hash = ?", hash.Bytes(),
).Scan(&startSeq, &startTime)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down
Loading

0 comments on commit c556311

Please sign in to comment.