Skip to content

Commit

Permalink
perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-malene committed Sep 19, 2023
1 parent 979e62c commit d8ab0dc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go/enclave/enclave.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ func (e *enclaveImpl) SubmitTx(tx common.EncryptedTx) (*responses.RawTx, common.
}

if err = e.service.SubmitTransaction(decryptedTx); err != nil {
e.logger.Warn("Could not submit transaction", log.TxKey, decryptedTx.Hash(), log.ErrKey, err)
e.logger.Debug("Could not submit transaction", log.TxKey, decryptedTx.Hash(), log.ErrKey, err)
return responses.AsEncryptedError(err, vkHandler), nil
}

Expand Down
20 changes: 11 additions & 9 deletions go/enclave/storage/enclavedb/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ import (
const (
bodyInsert = "replace into batch_body values (?,?)"
txInsert = "replace into tx values "
txInsertValue = "(?,?,?,?,?,?)"
txInsertValue = "(?,?,?,?,?,?,?)"

bInsert = "insert into batch values (?,?,?,?,?,?,?,?,?)"
bInsert = "insert into batch values (?,?,?,?,?,?,?,?,?,?)"
updateBatchExecuted = "update batch set is_executed=true where sequence=?"

selectBatch = "select b.header, bb.content from batch b join batch_body bb on b.body=bb.hash"
selectHeader = "select b.header from batch b"

txExecInsert = "insert into exec_tx values "
txExecInsertValue = "(?,?,?,?,?)"
queryReceipts = "select exec_tx.receipt, tx.content, exec_tx.batch, batch.height from exec_tx join tx on tx.hash=exec_tx.tx join batch on batch.hash=exec_tx.batch "
queryReceiptsCount = "select count(1) from exec_tx join tx on tx.hash=exec_tx.tx join batch on batch.hash=exec_tx.batch "
queryReceipts = "select exec_tx.receipt, tx.content, batch.full_hash, batch.height from exec_tx join tx on tx.hash=exec_tx.tx join batch on batch.sequence=exec_tx.batch "
queryReceiptsCount = "select count(1) from exec_tx join tx on tx.hash=exec_tx.tx join batch on batch.sequence=exec_tx.batch "

selectTxQuery = "select tx.content, exec_tx.batch, batch.height, tx.idx from exec_tx join tx on tx.hash=exec_tx.tx join batch on batch.hash=exec_tx.batch where batch.is_canonical=true and tx.hash=?"
selectTxQuery = "select tx.content, batch.full_hash, batch.height, tx.idx from exec_tx join tx on tx.hash=exec_tx.tx join batch on batch.sequence=exec_tx.batch where batch.is_canonical=true and tx.hash=?"

selectContractCreationTx = "select tx from exec_tx where created_contract_address=?"
selectTotalCreatedContracts = "select count( distinct created_contract_address) from exec_tx "
queryBatchWasExecuted = "select is_executed from batch where is_canonical=true and hash=?"

isCanonQuery = "select is_canonical from block where hash=?"

queryTxList = "select exec_tx.tx, batch.height from exec_tx join batch on batch.hash=exec_tx.batch"
queryTxCountList = "select count(1) from exec_tx join batch on batch.hash=exec_tx.batch"
queryTxList = "select tx.full_hash, batch.height from exec_tx join batch on batch.sequence=exec_tx.batch join tx on tx.hash=exec_tx.tx"
queryTxCountList = "select count(1) from exec_tx join batch on batch.sequence=exec_tx.batch"
)

// WriteBatchAndTransactions - persists the batch and the transactions
Expand Down Expand Up @@ -78,7 +78,8 @@ func WriteBatchAndTransactions(dbtx DBTransaction, batch *core.Batch) error {

dbtx.ExecuteSQL(bInsert,
batch.Header.SequencerOrderNo.Uint64(), // sequence
truncTo16(batch.Hash()), // hash
batch.Hash(), // full hash
truncTo16(batch.Hash()), // index hash
parentBytes, // parent
batch.Header.Number.Uint64(), // height
isCanon, // is_canonical
Expand All @@ -105,7 +106,8 @@ func WriteBatchAndTransactions(dbtx DBTransaction, batch *core.Batch) error {
return fmt.Errorf("unable to convert tx to message - %w", err)
}

args = append(args, truncTo16(transaction.Hash())) // tx_hash
args = append(args, truncTo16(transaction.Hash())) // indexed 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
Expand Down
9 changes: 5 additions & 4 deletions go/enclave/storage/enclavedb/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

const (
baseEventsQuerySelect = "select topic0, topic1, topic2, topic3, topic4, datablob, b.hash, b.height, tx.hash, tx.idx, log_idx, address"
baseDebugEventsQuerySelect = "select rel_address1, rel_address2, rel_address3, rel_address4, lifecycle_event, topic0, topic1, topic2, topic3, topic4, datablob, b.hash, b.height, tx.hash, tx.idx, log_idx, address"
baseEventsJoin = "from events e join exec_tx extx on e.exec_tx_id=extx.id join tx on extx.tx=tx.hash join batch b on extx.batch=b.hash where b.is_canonical=true "
baseEventsQuerySelect = "select full_topic0, full_topic1, full_topic2, full_topic3, full_topic4, datablob, b.full_hash, b.height, tx.full_hash, tx.idx, log_idx, address"
baseDebugEventsQuerySelect = "select rel_address1, rel_address2, rel_address3, rel_address4, lifecycle_event, topic0, topic1, topic2, topic3, topic4, datablob, b.full_hash, b.height, tx.full_hash, tx.idx, log_idx, address"
baseEventsJoin = "from events e join exec_tx extx on e.exec_tx_id=extx.id join tx on extx.tx=tx.hash join batch b on extx.batch=b.sequence where b.is_canonical=true "
insertEvent = "insert into events values "
insertEventValues = "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
insertEventValues = "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
orderBy = " order by b.height, tx.idx asc"
)

Expand Down Expand Up @@ -123,6 +123,7 @@ func writeLog(db *sql.DB, l *types.Log, receipt *types.Receipt, stateDB *state.S

return []any{
truncBTo16(t0), truncBTo16(t1), truncBTo16(t2), truncBTo16(t3), truncBTo16(t4),
t0, t1, t2, t3, t4,
data, l.Index, l.Address.Bytes(),
isLifecycle, a1, a2, a3, a4,
executedTransactionID(&receipt.BlockHash, &l.TxHash),
Expand Down
3 changes: 3 additions & 0 deletions go/enclave/storage/enclavedb/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ func truncTo16(hash gethcommon.Hash) []byte {
}

func truncBTo16(bytes []byte) []byte {
if len(bytes) == 0 {
return bytes
}
b := bytes[0:truncHash]
c := make([]byte, truncHash)
copy(c, b)
Expand Down
21 changes: 14 additions & 7 deletions go/enclave/storage/init/edgelessdb/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,33 @@ GRANT ALL ON obsdb.batch_body TO obscuro;

create table if not exists obsdb.batch
(
hash binary(16),
sequence int,
full_hash binary(32),
hash binary(16) NOT NULL,
parent binary(16),
sequence int NOT NULL,
height int NOT NULL,
is_canonical boolean NOT NULL,
header blob NOT NULL,
body binary(16) NOT NULL,
l1_proof binary(16) NOT NULL,
is_executed boolean NOT NULL,
INDEX (parent),
primary key (sequence),
# INDEX (parent),
INDEX (hash),
INDEX (body),
INDEX (l1_proof),
INDEX (height),
INDEX (sequence),
INDEX (is_canonical),
INDEX (is_executed),
INDEX (l1_proof)
# INDEX (is_canonical, is_executed),
# INDEX (is_canonical, is_executed, height),
primary key (hash)
);
GRANT ALL ON obsdb.batch TO obscuro;

create table if not exists obsdb.tx
(
hash binary(16),
full_hash binary(32) NOT NULL,
content mediumblob NOT NULL,
sender_address binary(20) NOT NULL,
nonce int NOT NULL,
Expand All @@ -115,7 +117,7 @@ create table if not exists obsdb.exec_tx
created_contract_address binary(20),
receipt mediumblob,
tx binary(16) NOT NULL,
batch binary(16) NOT NULL,
batch int NOT NULL,
INDEX (batch),
INDEX (tx),
primary key (id)
Expand All @@ -129,6 +131,11 @@ create table if not exists obsdb.events
topic2 binary(16),
topic3 binary(16),
topic4 binary(16),
full_topic0 binary(32) NOT NULL,
full_topic1 binary(32),
full_topic2 binary(32),
full_topic3 binary(32),
full_topic4 binary(32),
datablob mediumblob,
log_idx int NOT NULL,
address binary(20) NOT NULL,
Expand Down
7 changes: 7 additions & 0 deletions go/enclave/storage/init/sqlite/001_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ create table if not exists batch_body
create table if not exists batch
(
sequence int primary key,
full_hash binary(32),
hash binary(16) NOT NULL unique,
parent binary(16),
height int NOT NULL,
Expand All @@ -75,6 +76,7 @@ create index IDX_BATCH_Block on batch (l1_proof);
create table if not exists tx
(
hash binary(16) primary key,
full_hash binary(32) NOT NULL,
content mediumblob NOT NULL,
sender_address binary(20) NOT NULL,
nonce int NOT NULL,
Expand Down Expand Up @@ -102,6 +104,11 @@ create table if not exists events
topic2 binary(16),
topic3 binary(16),
topic4 binary(16),
full_topic0 binary(32) NOT NULL,
full_topic1 binary(32),
full_topic2 binary(32),
full_topic3 binary(32),
full_topic4 binary(32),
datablob mediumblob,
log_idx int NOT NULL,
address binary(20) NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion integration/simulation/transaction_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func (ti *TransactionInjector) issueInvalidL2Txs() {

err := ti.rpcHandles.ObscuroWalletRndClient(fromWallet).SendTransaction(ti.ctx, signedTx)
if err != nil {
ti.logger.Warn("Failed to issue withdrawal via RPC. ", log.ErrKey, err)
ti.logger.Info("Failed to issue withdrawal via RPC. ", log.ErrKey, err)
}
time.Sleep(testcommon.RndBtwTime(ti.avgBlockDuration/4, ti.avgBlockDuration))
}
Expand Down

0 comments on commit d8ab0dc

Please sign in to comment.