Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Jun 18, 2024
1 parent 3724ade commit fd0d92d
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 165 deletions.
5 changes: 3 additions & 2 deletions go/enclave/components/batch_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func (br *batchRegistry) OnBatchExecuted(batchHeader *common.BatchHeader, receip
defer br.callbackMutex.RUnlock()

txs, err := br.storage.FetchBatchTransactionsBySeq(context.Background(), batchHeader.SequencerOrderNo.Uint64())
if err != nil {
br.logger.Crit("cannot get transactions. ", log.ErrKey, err)
if err != nil && !errors.Is(err, errutil.ErrNotFound) {
// this function is called after a batch was successfully executed. This is a catastrophic failure
br.logger.Crit("should not happen. cannot get transactions. ", log.ErrKey, err)
}
br.headBatchSeq = batchHeader.SequencerOrderNo
if br.batchesCallback != nil {
Expand Down
31 changes: 21 additions & 10 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

const (
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 "
queryReceipts = "select receipt.content, tx.content, batch.hash, batch.height from receipt join tx on tx.id=receipt.tx join batch on batch.sequence=receipt.batch "
)

func WriteBatchHeader(ctx context.Context, dbtx *sql.Tx, batch *core.Batch, convertedHash gethcommon.Hash, blockId int64, isCanonical bool) error {
Expand All @@ -46,13 +46,25 @@ func WriteBatchHeader(ctx context.Context, dbtx *sql.Tx, batch *core.Batch, conv
return err
}

func UpdateCanonicalBatch(ctx context.Context, dbtx *sql.Tx, isCanonical bool, blocks []common.L1BlockHash) error {
args := make([]any, 0)
args = append(args, isCanonical)
for _, blockHash := range blocks {
args = append(args, blockHash.Bytes())
}

updateBatches := "update batch set is_canonical=? where " + repeat(" l1_proof_hash=? ", "OR", len(blocks))
_, err := dbtx.ExecContext(ctx, updateBatches, args...)
return err
}

func ExistsBatchAtHeight(ctx context.Context, dbTx *sql.Tx, height *big.Int) (bool, error) {
var count int
err := dbTx.QueryRowContext(ctx, "select count(*) from batch where height=?", height.Uint64()).Scan(&count)
var exists bool
err := dbTx.QueryRowContext(ctx, "select exists(select 1 from batch where height=?)", height.Uint64()).Scan(&exists)
if err != nil {
return false, err
}
return count > 0, nil
return exists, nil
}

// WriteTransactions - persists the batch and the transactions
Expand Down Expand Up @@ -111,8 +123,8 @@ func MarkBatchExecuted(ctx context.Context, dbtx *sql.Tx, seqNo *big.Int) error
return err
}

func WriteExecutedTransaction(ctx context.Context, dbtx *sql.Tx, batchSeqNo uint64, txId *uint64, createdContract *uint64, receipt []byte) (uint64, error) {
insert := "insert into exec_tx (created_contract_address, receipt, tx, batch) values " + "(?,?,?,?)"
func WriteReceipt(ctx context.Context, dbtx *sql.Tx, batchSeqNo uint64, txId *uint64, createdContract *uint64, receipt []byte) (uint64, error) {
insert := "insert into receipt (created_contract_address, content, tx, batch) values " + "(?,?,?,?)"
res, err := dbtx.ExecContext(ctx, insert, createdContract, receipt, txId, batchSeqNo)
if err != nil {
return 0, err
Expand Down Expand Up @@ -289,8 +301,7 @@ func selectReceipts(ctx context.Context, db *sql.DB, config *params.ChainConfig,
}

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=? ", txHash.Bytes())
row := db.QueryRowContext(ctx, queryReceipts+" where batch.is_canonical=true AND tx.hash=? ", txHash.Bytes())
// receipt, tx, batch, height
var receiptData []byte
var txData []byte
Expand Down Expand Up @@ -327,7 +338,7 @@ 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.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=?",
"select tx.content, batch.hash, batch.height, tx.idx from receipt join tx on tx.id=receipt.tx join batch on batch.sequence=receipt.batch where batch.is_canonical=true and tx.hash=?",
txHash.Bytes())

// tx, batch, height, idx
Expand Down Expand Up @@ -421,7 +432,7 @@ func GetTransactionsPerAddress(ctx context.Context, db *sql.DB, config *params.C
}

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())
row := db.QueryRowContext(ctx, "select count(1) from receipt join tx on tx.id=receipt.tx join batch on batch.sequence=receipt.batch "+" where tx.sender_address = ?", address.Bytes())

var count uint64
err := row.Scan(&count)
Expand Down
21 changes: 3 additions & 18 deletions go/enclave/storage/enclavedb/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (

gethcommon "github.com/ethereum/go-ethereum/common"

gethlog "github.com/ethereum/go-ethereum/log"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ten-protocol/go-ten/go/common"
Expand All @@ -33,29 +31,16 @@ func WriteBlock(ctx context.Context, dbtx *sql.Tx, b *types.Header) error {
return err
}

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

func UpdateCanonicalBlock(ctx context.Context, dbtx *sql.Tx, isCanonical bool, blocks []common.L1BlockHash) error {
args := make([]any, 0)
args = append(args, isCanonical)
for _, blockHash := range blocks {
args = append(args, blockHash.Bytes())
}

updateBlocks := "update block set is_canonical=? where " + currentBlocks
updateBlocks := "update block set is_canonical=? where " + repeat(" hash=? ", "OR", len(blocks))
_, err := dbtx.ExecContext(ctx, updateBlocks, args...)
if err != nil {
return err
}

updateBatches := "update batch set is_canonical=? where " + repeat(" l1_proof_hash=? ", "OR", len(blocks))
// updateBatches := "update batch set is_canonical=? where l1_proof in (select id from block where " + currentBlocks + ")"
_, err = dbtx.ExecContext(ctx, updateBatches, args...)
if err != nil {
return err
}

return nil
return err
}

func IsCanonicalBlock(ctx context.Context, dbtx *sql.Tx, hash *gethcommon.Hash) (bool, error) {
Expand Down
8 changes: 4 additions & 4 deletions go/enclave/storage/enclavedb/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

const (
baseEventsJoin = "from event_log e " +
"join exec_tx extx on e.exec_tx=extx.id" +
"join receipt extx on e.receipt=extx.id" +
" join tx on extx.tx=tx.id " +
" join batch b on extx.batch=b.sequence " +
"join event_type et on e.event_type=et.id " +
Expand Down Expand Up @@ -67,8 +67,8 @@ func WriteEventTopic(ctx context.Context, dbTX *sql.Tx, topic *gethcommon.Hash,
return uint64(id), nil
}

func UpdateEventTopic(ctx context.Context, dbTx *sql.Tx, etId uint64, eoaId uint64) error {
_, err := dbTx.ExecContext(ctx, "update event_topic set rel_address=? where id=?", eoaId, etId)
func UpdateEventTopicLifecycle(ctx context.Context, dbTx *sql.Tx, etId uint64, isLifecycle bool) error {
_, err := dbTx.ExecContext(ctx, "update event_topic set lifecycle_event=? where id=?", isLifecycle, etId)
return err
}

Expand All @@ -84,7 +84,7 @@ func ReadEventTopic(ctx context.Context, dbTX *sql.Tx, topic []byte) (uint64, *u
}

func WriteEventLog(ctx context.Context, dbTX *sql.Tx, eventTypeId uint64, userTopics []*uint64, data []byte, logIdx uint, execTx uint64) error {
_, err := dbTX.ExecContext(ctx, "insert into event_log (event_type, topic1, topic2, topic3, datablob, log_idx, exec_tx) values (?,?,?,?,?,?,?)",
_, err := dbTX.ExecContext(ctx, "insert into event_log (event_type, topic1, topic2, topic3, datablob, log_idx, receipt) values (?,?,?,?,?,?,?)",
eventTypeId, userTopics[0], userTopics[1], userTopics[2], data, logIdx, execTx)
return err
}
Expand Down
12 changes: 6 additions & 6 deletions go/enclave/storage/init/edgelessdb/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ create table if not exists obsdb.tx
id INTEGER AUTO_INCREMENT,
hash binary(32) NOT NULL,
content mediumblob NOT NULL,
sender_address int NOT NULL,
sender_address int NOT NULL,
idx int NOT NULL,
batch_height int NOT NULL,
INDEX USING HASH (hash),
Expand All @@ -102,18 +102,18 @@ create table if not exists obsdb.tx
);
GRANT ALL ON obsdb.tx TO obscuro;

create table if not exists obsdb.exec_tx
create table if not exists obsdb.receipt
(
id INTEGER AUTO_INCREMENT,
created_contract_address int,
receipt mediumblob,
content mediumblob,
tx int,
batch int NOT NULL,
INDEX (batch),
INDEX (tx, created_contract_address),
primary key (id)
);
GRANT ALL ON obsdb.exec_tx TO obscuro;
GRANT ALL ON obsdb.receipt TO obscuro;

create table if not exists obsdb.contract
(
Expand Down Expand Up @@ -165,8 +165,8 @@ create table if not exists obsdb.event_log
topic3 INTEGER,
datablob mediumblob,
log_idx INTEGER NOT NULL,
exec_tx INTEGER NOT NULL,
receipt INTEGER NOT NULL,
primary key (id),
INDEX (exec_tx, event_type, topic1, topic2, topic3)
INDEX (receipt, event_type, topic1, topic2, topic3)
);
GRANT ALL ON obsdb.event_log TO obscuro;
22 changes: 11 additions & 11 deletions go/enclave/storage/init/sqlite/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,17 @@ create index IDX_TX_HASH on tx (hash);
create index IDX_TX_SENDER_ADDRESS on tx (sender_address);
create index IDX_TX_BATCH_HEIGHT on tx (batch_height, idx);

create table if not exists exec_tx
create table if not exists receipt
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created_contract_address INTEGER REFERENCES contract,
receipt mediumblob,
content mediumblob,
-- commenting out the fk until synthetic transactions are also stored
tx INTEGER,
batch INTEGER NOT NULL REFERENCES batch
);
create index IDX_EX_TX_BATCH on exec_tx (batch);
create index IDX_EX_TX_CCA on exec_tx (created_contract_address, tx);
create index IDX_EX_TX_BATCH on receipt (batch);
create index IDX_EX_TX_CCA on receipt (created_contract_address, tx);

create table if not exists contract
(
Expand Down Expand Up @@ -147,13 +147,13 @@ create table if not exists event_log
topic3 INTEGER references event_topic,
datablob mediumblob,
log_idx INTEGER NOT NULL,
exec_tx INTEGER NOT NULL references exec_tx
receipt INTEGER NOT NULL references receipt
);
-- create index IDX_BATCH_TX on event_log (exec_tx);
create index IDX_EV on event_log (exec_tx, event_type, topic1, topic2, topic3);
-- create index IDX_BATCH_TX on event_log (receipt);
create index IDX_EV on event_log (receipt, event_type, topic1, topic2, topic3);

-- requester - address
-- exec_tx - range of batch heights or a single batch
-- receipt - range of batch heights or a single batch
-- address []list of contract addresses
-- topic0 - event sig []list
-- topic1 []list
Expand All @@ -162,8 +162,8 @@ create index IDX_EV on event_log (exec_tx, event_type, topic1, topic2, topic3);


-- select * from event_log
-- join exec_tx on exec_tx
-- join batch on exec_tx.batch -- to get the batch height range
-- join receipt on receipt
-- join batch on receipt.batch -- to get the batch height range
-- join event_type ec on event_type
-- join contract c on
-- left join event_topic t1 on topic1
Expand All @@ -173,7 +173,7 @@ create index IDX_EV on event_log (exec_tx, event_type, topic1, topic2, topic3);
-- left join event_topic t3 on topic3
-- left join externally_owned_account eoa3 on t3.rel_address
-- where
-- exec_tx.
-- receipt.
-- c.address in [address..] AND
-- ec.event_sig in [topic0..] AND
-- t1.topic in [topic1..] AND
Expand Down
Loading

0 comments on commit fd0d92d

Please sign in to comment.