Skip to content

Commit

Permalink
feat: optimize transaction validation for wallet (#3537)
Browse files Browse the repository at this point in the history
Description
---
Optimized the transaction validation memory footprint by restricting all queries to the database to only return the information that is needed. This holds two considerable advantages for databases with many or large transactions:
- The memory footprint is reduced.
- Overall performance is increased.


Motivation and Context
---
See above.

How Has This Been Tested?
---
Unit tests
Cucumber tests
System-level tests with a large wallet
  • Loading branch information
hansieodendaal authored Nov 9, 2021
1 parent d8e0ced commit 9064b83
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 92 deletions.
2 changes: 1 addition & 1 deletion base_layer/core/src/chain_storage/blockchain_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ pub fn calculate_mmr_roots<T: BlockchainBackend>(db: &T, block: &Block) -> Resul

output_mmr.compress();

// TODO: #testnetreset clean up this code
// TODO: #testnet_reset clean up this code
let input_mr = if header.version == 1 {
MutableMmr::<HashDigest, _>::new(input_mmr.get_pruned_hash_set()?, Bitmap::create())?.get_merkle_root()?
} else {
Expand Down
2 changes: 1 addition & 1 deletion base_layer/core/src/transactions/aggregated_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl AggregateBody {
}
self.inputs.sort();
self.outputs.sort();
// TODO: #testnetreset clean up this code
// TODO: #testnet_reset clean up this code
if version <= 1 {
self.kernels.sort_by(|a, b| a.deprecated_cmp(b));
} else {
Expand Down
4 changes: 2 additions & 2 deletions base_layer/core/src/transactions/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ impl TransactionOutput {
Challenge::new()
.chain(public_commitment_nonce.as_bytes())
.chain(script.as_bytes())
// TODO: Use consensus encoded bytes #testnet reset
// TODO: Use consensus encoded bytes #testnet_reset
.chain(features.to_v1_bytes())
.chain(sender_offset_public_key.as_bytes())
.chain(commitment.as_bytes())
Expand Down Expand Up @@ -887,7 +887,7 @@ impl TransactionOutput {
impl Hashable for TransactionOutput {
fn hash(&self) -> Vec<u8> {
HashDigest::new()
// TODO: use consensus encoding #testnetreset
// TODO: use consensus encoding #testnet_reset
.chain(self.features.to_v1_bytes())
.chain(self.commitment.as_bytes())
// .chain(range proof) // See docs as to why we exclude this
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
PRAGMA foreign_keys=OFF;
ALTER TABLE completed_transactions
RENAME TO completed_transactions_old;
CREATE TABLE completed_transactions (
tx_id INTEGER PRIMARY KEY NOT NULL,
source_public_key BLOB NOT NULL,
destination_public_key BLOB NOT NULL,
amount INTEGER NOT NULL,
fee INTEGER NOT NULL,
transaction_protocol TEXT NOT NULL,
status INTEGER NOT NULL,
message TEXT NOT NULL,
timestamp DATETIME NOT NULL,
cancelled INTEGER NOT NULL DEFAULT 0,
direction INTEGER NULL DEFAULT NULL,
coinbase_block_height INTEGER NULL DEFAULT NULL,
send_count INTEGER NOT NULL DEFAULT 0,
last_send_timestamp DATETIME NULL DEFAULT NULL,
valid INTEGER NOT NULL DEFAULT 0,
confirmations INTEGER NULL DEFAULT NULL,
mined_height BIGINT NULL DEFAULT NULL,
mined_in_block BLOB NULL DEFAULT NULL

);
INSERT INTO completed_transactions (tx_id, source_public_key, destination_public_key, amount, fee, transaction_protocol,
status, message, timestamp, cancelled, direction, coinbase_block_height, send_count,
last_send_timestamp, valid, confirmations)
SELECT tx_id,
source_public_key,
destination_public_key,
amount,
fee,
transaction_protocol,
status,
message,
timestamp,
cancelled,
direction,
coinbase_block_height,
send_count,
last_send_timestamp,
valid,
confirmations,
mined_height,
mined_in_block
FROM completed_transactions_old;
DROP TABLE completed_transactions_old;
PRAGMA foreign_keys=ON;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE completed_transactions
ADD transaction_signature_nonce BLOB NOT NULL DEFAULT 0;

ALTER TABLE completed_transactions
ADD transaction_signature_key BLOB NOT NULL DEFAULT 0;
2 changes: 2 additions & 0 deletions base_layer/wallet/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ table! {
confirmations -> Nullable<BigInt>,
mined_height -> Nullable<BigInt>,
mined_in_block -> Nullable<Binary>,
transaction_signature_nonce -> Binary,
transaction_signature_key -> Binary,
}
}

Expand Down
4 changes: 4 additions & 0 deletions base_layer/wallet/src/transaction_service/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ pub enum TransactionKeyError {
Source(ByteArrayError),
#[error("Invalid destination PublicKey")]
Destination(ByteArrayError),
#[error("Invalid transaction signature nonce")]
SignatureNonce(ByteArrayError),
#[error("Invalid transaction signature key")]
SignatureKey(ByteArrayError),
}

#[derive(Debug, Error)]
Expand Down
Loading

0 comments on commit 9064b83

Please sign in to comment.