Skip to content

Commit

Permalink
bugfix: ProvenTransaction must be Send (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
hackaugusto authored Feb 28, 2024
1 parent 4b843cc commit a902288
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
60 changes: 30 additions & 30 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::cell::OnceCell;

use crate::{
accounts::AccountId,
assembly::{Assembler, AssemblyContext, ProgramAst},
Expand Down Expand Up @@ -82,8 +80,15 @@ pub struct Note {
serial_num: Word,
metadata: NoteMetadata,

id: OnceCell<NoteId>,
nullifier: OnceCell<Nullifier>,
id: NoteId,
recipient: Digest,
nullifier: Nullifier,
}

fn compute_recipient(serial_num: Word, script: &NoteScript, inputs: &NoteInputs) -> Digest {
let serial_num_hash = Hasher::merge(&[serial_num.into(), Digest::default()]);
let merge_script = Hasher::merge(&[serial_num_hash, script.hash()]);
Hasher::merge(&[merge_script, inputs.commitment()])
}

impl Note {
Expand All @@ -104,15 +109,11 @@ impl Note {
sender: AccountId,
tag: Felt,
) -> Result<Self, NoteError> {
Ok(Self {
script,
inputs: NoteInputs::new(inputs.to_vec())?,
assets: NoteAssets::new(assets)?,
serial_num,
metadata: NoteMetadata::new(sender, tag),
id: OnceCell::new(),
nullifier: OnceCell::new(),
})
let inputs = NoteInputs::new(inputs.to_vec())?;
let assets = NoteAssets::new(assets)?;
let metadata = NoteMetadata::new(sender, tag);

Ok(Self::from_parts(script, inputs, assets, serial_num, metadata))
}

/// Returns a note instance created from the provided parts.
Expand All @@ -123,14 +124,19 @@ impl Note {
serial_num: Word,
metadata: NoteMetadata,
) -> Self {
let recipient = compute_recipient(serial_num, &script, &inputs);
let id = NoteId::new(recipient, assets.commitment());
let nullifier =
Nullifier::new(script.hash(), inputs.commitment(), assets.commitment(), serial_num);
Self {
script,
inputs,
assets,
serial_num,
metadata,
id: OnceCell::new(),
nullifier: OnceCell::new(),
id,
recipient,
nullifier,
}
}

Expand Down Expand Up @@ -163,17 +169,18 @@ impl Note {
}

/// Returns the recipient of this note.
///
/// Recipient is defined and calculated as:
/// hash(hash(hash(serial_num, [0; 4]), script_hash), input_hash)
///
/// > hash(hash(hash(serial_num, [0; 4]), script_hash), input_hash)
///
pub fn recipient(&self) -> Digest {
let serial_num_hash = Hasher::merge(&[self.serial_num.into(), Digest::default()]);
let merge_script = Hasher::merge(&[serial_num_hash, self.script.hash()]);
Hasher::merge(&[merge_script, self.inputs.commitment()])
self.recipient
}

/// Returns a unique identifier of this note, which is simultaneously a commitment to the note.
pub fn id(&self) -> NoteId {
*self.id.get_or_init(|| self.into())
self.id
}

/// Returns the value used to authenticate a notes existence in the note tree. This is computed
Expand All @@ -184,7 +191,7 @@ impl Note {

/// Returns the nullifier for this note.
pub fn nullifier(&self) -> Nullifier {
*self.nullifier.get_or_init(|| self.into())
self.nullifier
}
}

Expand All @@ -201,6 +208,7 @@ impl Serializable for Note {
metadata,

id: _,
recipient: _,
nullifier: _,
} = self;

Expand All @@ -220,15 +228,7 @@ impl Deserializable for Note {
let serial_num = Word::read_from(source)?;
let metadata = NoteMetadata::read_from(source)?;

Ok(Self {
script,
inputs,
assets,
serial_num,
metadata,
id: OnceCell::new(),
nullifier: OnceCell::new(),
})
Ok(Self::from_parts(script, inputs, assets, serial_num, metadata))
}
}

Expand Down
12 changes: 12 additions & 0 deletions objects/src/transaction/proven_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,15 @@ impl Deserializable for ProvenTransaction {
})
}
}

#[cfg(test)]
mod tests {
use super::ProvenTransaction;

fn check_if_send<T: Send>() {}

#[test]
fn proven_transaction_is_send() {
check_if_send::<ProvenTransaction>();
}
}

0 comments on commit a902288

Please sign in to comment.