diff --git a/wallet-core/src/ffi.rs b/wallet-core/src/ffi.rs index f5b54637bb..c09459d4c2 100644 --- a/wallet-core/src/ffi.rs +++ b/wallet-core/src/ffi.rs @@ -39,6 +39,10 @@ use zeroize::Zeroize; use rkyv::{from_bytes, to_bytes}; +/// The size of the scratch buffer used for parsing the notes. +/// It can roughly contains less than 128 serialized notes. +const NOTES_BUFFER_SIZE: usize = 96 * 1024; + /// The alignment of the memory allocated by the FFI. /// /// This is 1 because we're not allocating any complex data structures, and @@ -126,8 +130,8 @@ pub unsafe fn map_owned( keys.into_iter().for_each(|mut sk| sk.zeroize()); - let bytes = - to_bytes::<_, 4096>(&owned).or(Err(ErrorCode::ArchivingError))?; + let bytes = to_bytes::<_, NOTES_BUFFER_SIZE>(&owned) + .or(Err(ErrorCode::ArchivingError))?; let len = bytes.len().to_le_bytes(); diff --git a/wallet-core/src/lib.rs b/wallet-core/src/lib.rs index d89ff2ce9b..76fdf75cae 100644 --- a/wallet-core/src/lib.rs +++ b/wallet-core/src/lib.rs @@ -29,73 +29,4 @@ pub mod transaction; /// The seed used to generate the entropy for the keys pub type Seed = [u8; 64]; -// The maximum amount of input notes that can be spend in one -// phoenix-transaction -const MAX_INPUT_NOTES: usize = 4; - -use alloc::vec::Vec; - -use dusk_bytes::{DeserializableSlice, Serializable, Write}; - -use execution_core::transfer::phoenix::{Note, ViewKey as PhoenixViewKey}; - -pub use notes::map_owned; - -/// Calculate the sum for all the given [`Note`]s that belong to the given -/// [`PhoenixViewKey`]. -pub fn phoenix_balance( - phoenix_vk: &PhoenixViewKey, - notes: impl Iterator, -) -> BalanceInfo -where - T: AsRef, -{ - let mut values: Vec = notes - .filter_map(|note| note.as_ref().value(Some(phoenix_vk)).ok()) - .collect(); - - values.sort_by(|a, b| b.cmp(a)); - - let spendable = values.iter().take(MAX_INPUT_NOTES).sum(); - let value = spendable + values.iter().skip(MAX_INPUT_NOTES).sum::(); - - BalanceInfo { value, spendable } -} - -/// Information about the balance of a particular key. -#[derive(Debug, Default, Hash, Clone, Copy, PartialEq, Eq)] -pub struct BalanceInfo { - /// The total value of the balance. - pub value: u64, - /// The maximum _spendable_ value in a single transaction. This is - /// different from `value` since there is a maximum number of notes one can - /// spend. - pub spendable: u64, -} - -impl Serializable<{ 2 * u64::SIZE }> for BalanceInfo { - type Error = dusk_bytes::Error; - - fn from_bytes(buf: &[u8; Self::SIZE]) -> Result - where - Self: Sized, - { - let mut reader = &buf[..]; - - let value = u64::from_reader(&mut reader)?; - let spendable = u64::from_reader(&mut reader)?; - - Ok(Self { value, spendable }) - } - - #[allow(unused_must_use)] - fn to_bytes(&self) -> [u8; Self::SIZE] { - let mut buf = [0u8; Self::SIZE]; - let mut writer = &mut buf[..]; - - writer.write(&self.value.to_bytes()); - writer.write(&self.spendable.to_bytes()); - - buf - } -} +pub use notes::{map_owned, phoenix_balance, BalanceInfo}; diff --git a/wallet-core/src/notes.rs b/wallet-core/src/notes.rs index 713b17cf47..6b898b5d1d 100644 --- a/wallet-core/src/notes.rs +++ b/wallet-core/src/notes.rs @@ -8,6 +8,8 @@ use alloc::vec::Vec; use core::ops::Index; +use dusk_bytes::{DeserializableSlice, Serializable, Write}; +use execution_core::transfer::phoenix::{Note, ViewKey as PhoenixViewKey}; use execution_core::{ transfer::phoenix::{NoteLeaf, SecretKey as PhoenixSecretKey}, BlsScalar, @@ -15,6 +17,10 @@ use execution_core::{ use rkyv::{Archive, Deserialize, Serialize}; +// The maximum amount of input notes that can be spend in one +// phoenix-transaction +const MAX_INPUT_NOTES: usize = 4; + /// A collection of notes stored as key-value pairs. /// The key is a `BlsScalar` and the value is a `NoteLeaf`. /// Duplicates are allowed. @@ -77,8 +83,6 @@ pub fn map_owned( notes.as_ref().iter().fold( OwnedList::default(), |mut notes_map, note_leaf| { - eprintln!("Printing note..."); - dbg!(note_leaf); for sk in keys.as_ref() { if sk.owns(note_leaf.note.stealth_address()) { let nullifier = note_leaf.note.gen_nullifier(sk); @@ -90,3 +94,62 @@ pub fn map_owned( }, ) } + +/// Calculate the sum for all the given [`Note`]s that belong to the given +/// [`PhoenixViewKey`]. +pub fn phoenix_balance( + phoenix_vk: &PhoenixViewKey, + notes: impl Iterator, +) -> BalanceInfo +where + T: AsRef, +{ + let mut values: Vec = notes + .filter_map(|note| note.as_ref().value(Some(phoenix_vk)).ok()) + .collect(); + + values.sort_by(|a, b| b.cmp(a)); + + let spendable = values.iter().take(MAX_INPUT_NOTES).sum(); + let value = spendable + values.iter().skip(MAX_INPUT_NOTES).sum::(); + + BalanceInfo { value, spendable } +} + +/// Information about the balance of a particular key. +#[derive(Debug, Default, Hash, Clone, Copy, PartialEq, Eq)] +pub struct BalanceInfo { + /// The total value of the balance. + pub value: u64, + /// The maximum _spendable_ value in a single transaction. This is + /// different from `value` since there is a maximum number of notes one can + /// spend. + pub spendable: u64, +} + +impl Serializable<{ 2 * u64::SIZE }> for BalanceInfo { + type Error = dusk_bytes::Error; + + fn from_bytes(buf: &[u8; Self::SIZE]) -> Result + where + Self: Sized, + { + let mut reader = &buf[..]; + + let value = u64::from_reader(&mut reader)?; + let spendable = u64::from_reader(&mut reader)?; + + Ok(Self { value, spendable }) + } + + #[allow(unused_must_use)] + fn to_bytes(&self) -> [u8; Self::SIZE] { + let mut buf = [0u8; Self::SIZE]; + let mut writer = &mut buf[..]; + + writer.write(&self.value.to_bytes()); + writer.write(&self.spendable.to_bytes()); + + buf + } +}