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

Optimize handle_transaction #93

Merged
merged 3 commits into from
Oct 25, 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
17 changes: 9 additions & 8 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,16 @@ pub fn handle_transaction(
(note, IncrementalWitness::read(wit).unwrap())
})
.collect::<Vec<_>>();
let nullifiers = handle_transaction_internal(&mut tree, tx, &key, true, &mut comp_note)
let nullifiers = handle_transaction_internal(&mut tree, tx, key, true, &mut comp_note)
.map_err(|_| "Cannot decode tx")?;
let mut ser_comp_note: Vec<(Note, String)> = vec![];
let mut ser_nullifiers: Vec<String> = vec![];
for (note, witness) in comp_note.iter() {
for (note, witness) in comp_note {
let mut buff = Vec::new();
witness
.write(&mut buff)
.map_err(|_| "Cannot write witness to buffer")?;
ser_comp_note.push((note.clone(), hex::encode(&buff)));
ser_comp_note.push((note, hex::encode(&buff)));
}

for nullif in nullifiers.iter() {
Expand All @@ -188,7 +188,7 @@ pub fn handle_transaction(
pub fn handle_transaction_internal(
tree: &mut CommitmentTree<Node>,
tx: &str,
key: &UnifiedFullViewingKey,
key: UnifiedFullViewingKey,
is_testnet: bool,
witnesses: &mut Vec<(Note, IncrementalWitness<Node>)>,
) -> Result<Vec<Nullifier>, Box<dyn Error>> {
Expand All @@ -197,8 +197,8 @@ pub fn handle_transaction_internal(
pivx_primitives::consensus::BranchId::Sapling,
)?;
let mut hash = HashMap::new();
hash.insert(AccountId::default(), key.clone());
let decrypted_tx = if is_testnet {
hash.insert(AccountId::default(), key);
let mut decrypted_tx = if is_testnet {
decrypt_transaction(&TEST_NETWORK, BlockHeight::from_u32(320), &tx, &hash)
} else {
decrypt_transaction(&MAIN_NETWORK, BlockHeight::from_u32(320), &tx, &hash)
Expand All @@ -218,11 +218,12 @@ pub fn handle_transaction_internal(
.append(Node::from_cmu(out.cmu()))
.map_err(|_| "Failed to add cmu to witness")?;
}
for note in &decrypted_tx {
for (index, note) in decrypted_tx.iter().enumerate() {
if note.index == i {
// Save witness
let witness = IncrementalWitness::from_tree(tree);
witnesses.push((note.note.clone(), witness));
witnesses.push((decrypted_tx.swap_remove(index).note, witness));
break;
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/transaction/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ fn check_tx_decryption() {
let key = UnifiedFullViewingKey::new(Some(skey.to_diversifiable_full_viewing_key()), None)
.expect("Failed to create key");
let mut comp_note = vec![];
let nullifiers =
handle_transaction_internal(&mut tree, tx, &key, true, &mut comp_note).unwrap();
let nullifiers = handle_transaction_internal(&mut tree, tx, key, true, &mut comp_note).unwrap();
//This was a t-s tx
assert_eq!(nullifiers.len(), 0);
//Successfully decrypt exactly 1 note
Expand Down Expand Up @@ -86,7 +85,7 @@ pub async fn test_create_transaction() -> Result<(), Box<dyn Error>> {

let mut notes = vec![];
let _nullifiers =
handle_transaction_internal(&mut commitment_tree, input_tx, &key, true, &mut notes)?;
handle_transaction_internal(&mut commitment_tree, input_tx, key, true, &mut notes)?;
assert_eq!(notes.len(), 1);
let (note, path) = &notes[0];
let mut path_vec = vec![];
Expand Down
Loading