Skip to content

Commit

Permalink
merged labs/dev and TransactionRecord upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
fluidvanadium committed Apr 23, 2024
2 parents f057553 + 9c2154c commit 3382892
Show file tree
Hide file tree
Showing 13 changed files with 368 additions and 61 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions zingolib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ either = "1.8.1"
serde = { version = "1.0.188", features = ["derive"] }
sapling-crypto.workspace = true
getset = "0.1.2"
test-case = "3.3.1"

[dev-dependencies]
portpicker = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions zingolib/src/lightclient/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::cmp;
use crate::wallet::transaction_record::TransactionRecord;

use super::*;
use crate::wallet::notes::NoteInterface;
use crate::wallet::notes::ShieldedNoteInterface;
use zcash_note_encryption::Domain;

Expand Down
1 change: 0 additions & 1 deletion zingolib/src/lightclient/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use crate::{
},
keys::address_from_pubkeyhash,
notes::NoteInterface,
notes::ShieldedNoteInterface,
LightWallet, Pool,
},
};
Expand Down
4 changes: 2 additions & 2 deletions zingolib/src/test_framework/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ mod sapling_crypto_note {
let (_, _, address) = default_zaddr();
Self::new()
.recipient(address)
.value(NoteValue::from_raw(1000000))
.value(NoteValue::from_raw(200000))
.rseed(Rseed::AfterZip212([7; 32]))
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ pub mod orchard_note {
let fvk: FullViewingKey = (&sk).into();
let recipient = fvk.address_at(0u32, Scope::External);

let value = NoteValue::default(); // ZERO
let value = NoteValue::from_raw(800000);
let rho = {
loop {
let mut bytes = [0u8; 32];
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/wallet/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl LightWallet {
filtered_notes
.map(|notedata| {
if notedata.spent().is_none() && notedata.pending_spent().is_none() {
<D::WalletNote as ShieldedNoteInterface>::value(notedata)
<D::WalletNote as NoteInterface>::value(notedata)
} else {
0
}
Expand Down
28 changes: 14 additions & 14 deletions zingolib/src/wallet/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ pub mod tests {
use crate::{
test_framework::mocks::default_txid,
wallet::notes::{
query::NoteQuery, sapling::mocks::SaplingNoteBuilder,
query::OutputQuery, sapling::mocks::SaplingNoteBuilder,
transparent::mocks::TransparentOutputBuilder, NoteInterface,
},
};

use super::query::{NotePoolQuery, NoteSpendStatusQuery};
use super::query::{OutputPoolQuery, OutputSpendStatusQuery};

#[test]
fn note_queries() {
Expand All @@ -153,22 +153,22 @@ pub mod tests {
.build();
let sapling_spent_note = SaplingNoteBuilder::default().spent(spend).build();

let unspent_query = NoteSpendStatusQuery::new(true, false, false);
let pending_or_spent_query = NoteSpendStatusQuery::new(false, true, true);
let spent_query = NoteSpendStatusQuery::new(false, false, true);
let unspent_query = OutputSpendStatusQuery::new(true, false, false);
let pending_or_spent_query = OutputSpendStatusQuery::new(false, true, true);
let spent_query = OutputSpendStatusQuery::new(false, false, true);

let transparent_query = NotePoolQuery::new(true, false, false);
let shielded_query = NotePoolQuery::new(false, true, true);
let any_pool_query = NotePoolQuery::new(true, true, true);
let transparent_query = OutputPoolQuery::new(true, false, false);
let shielded_query = OutputPoolQuery::new(false, true, true);
let any_pool_query = OutputPoolQuery::new(true, true, true);

let unspent_transparent_query = NoteQuery::new(unspent_query, transparent_query);
let unspent_any_pool_query = NoteQuery::new(unspent_query, any_pool_query);
let unspent_transparent_query = OutputQuery::new(unspent_query, transparent_query);
let unspent_any_pool_query = OutputQuery::new(unspent_query, any_pool_query);
let pending_or_spent_transparent_query =
NoteQuery::new(pending_or_spent_query, transparent_query);
OutputQuery::new(pending_or_spent_query, transparent_query);
let pending_or_spent_shielded_query =
NoteQuery::new(pending_or_spent_query, shielded_query);
let spent_shielded_query = NoteQuery::new(spent_query, shielded_query);
let spent_any_pool_query = NoteQuery::new(spent_query, any_pool_query);
OutputQuery::new(pending_or_spent_query, shielded_query);
let spent_shielded_query = OutputQuery::new(spent_query, shielded_query);
let spent_any_pool_query = OutputQuery::new(spent_query, any_pool_query);

assert!(transparent_unspent_note.query(unspent_transparent_query));
assert!(transparent_unspent_note.query(unspent_any_pool_query));
Expand Down
16 changes: 7 additions & 9 deletions zingolib/src/wallet/notes/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ use super::{
traits::{FromBytes, FromCommitment, Nullifier, ReadableWriteable, ToBytes},
Pool,
},
query::{NotePoolQuery, NoteQuery, NoteSpendStatusQuery},
query::{OutputPoolQuery, OutputQuery, OutputSpendStatusQuery},
};

/// TODO: Add Doc Comment Here!
pub trait NoteInterface: Sized {
/// returns the zcash_client_backend PoolType enum (one of 3)
fn pool_type(&self) -> PoolType;

/// number of Zatoshis unlocked by the note
fn value(&self) -> u64;

/// TODO: Add Doc Comment Here!
fn spent(&self) -> &Option<(TxId, u32)>;

Expand Down Expand Up @@ -46,14 +49,14 @@ pub trait NoteInterface: Sized {
}

/// Returns true if the note has one of the spend statuses enumerated by the query
fn spend_status_query(&self, query: NoteSpendStatusQuery) -> bool {
fn spend_status_query(&self, query: OutputSpendStatusQuery) -> bool {
(*query.unspent() && !self.is_spent() && !self.is_pending_spent())
|| (*query.pending_spent() && self.is_pending_spent())
|| (*query.spent() && self.is_spent())
}

/// Returns true if the note is one of the pools enumerated by the query.
fn pool_query(&self, query: NotePoolQuery) -> bool {
fn pool_query(&self, query: OutputPoolQuery) -> bool {
(*query.transparent() && self.pool_type() == PoolType::Transparent)
|| (*query.sapling()
&& self.pool_type() == PoolType::Shielded(ShieldedProtocol::Sapling))
Expand All @@ -62,7 +65,7 @@ pub trait NoteInterface: Sized {
}

/// Returns true if the note is one of the spend statuses enumerated by the query AND one of the pools enumerated by the query.
fn query(&self, query: NoteQuery) -> bool {
fn query(&self, query: OutputQuery) -> bool {
self.spend_status_query(*query.spend_status()) && self.pool_query(*query.pools())
}
}
Expand Down Expand Up @@ -143,11 +146,6 @@ pub trait ShieldedNoteInterface: NoteInterface + Sized {
fn transaction_metadata_notes_mut(wallet_transaction: &mut TransactionRecord)
-> &mut Vec<Self>;

///Convenience function
fn value(&self) -> u64 {
Self::value_from_note(self.note())
}

/// TODO: Add Doc Comment Here!
fn value_from_note(note: &Self::Note) -> u64;

Expand Down
4 changes: 4 additions & 0 deletions zingolib/src/wallet/notes/orchard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ impl NoteInterface for OrchardNote {
PoolType::Shielded(ShieldedProtocol::Orchard)
}

fn value(&self) -> u64 {
self.orchard_crypto_note.value().inner()
}

fn spent(&self) -> &Option<(TxId, u32)> {
&self.spent
}
Expand Down
26 changes: 20 additions & 6 deletions zingolib/src/wallet/notes/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use getset::Getters;

/// Selects received notes by how they been spent
#[derive(Getters, Constructor, Clone, Copy)]
pub struct NoteSpendStatusQuery {
pub struct OutputSpendStatusQuery {
/// will the query include unspent notes?
#[getset(get = "pub")]
unspent: bool,
Expand All @@ -19,7 +19,7 @@ pub struct NoteSpendStatusQuery {

/// Selects received notes by pool
#[derive(Getters, Constructor, Clone, Copy)]
pub struct NotePoolQuery {
pub struct OutputPoolQuery {
/// will the query include transparent notes? (coins)
#[getset(get = "pub")]
transparent: bool,
Expand All @@ -33,16 +33,30 @@ pub struct NotePoolQuery {

/// Selects received notes by any properties
#[derive(Getters, Constructor, Clone, Copy)]
pub struct NoteQuery {
pub struct OutputQuery {
/// selects spend status properties
#[getset(get = "pub")]
spend_status: NoteSpendStatusQuery,
spend_status: OutputSpendStatusQuery,
/// selects pools
#[getset(get = "pub")]
pools: NotePoolQuery,
pools: OutputPoolQuery,
}

impl NoteQuery {
impl OutputQuery {
/// build a query, specifying each stipulation
pub fn stipulations(
unspent: bool,
pending_spent: bool,
spent: bool,
transparent: bool,
sapling: bool,
orchard: bool,
) -> Self {
Self::new(
OutputSpendStatusQuery::new(unspent, pending_spent, spent),
OutputPoolQuery::new(transparent, sapling, orchard),
)
}
/// will the query include unspent notes?
pub fn unspent(&self) -> &bool {
self.spend_status().unspent()
Expand Down
4 changes: 4 additions & 0 deletions zingolib/src/wallet/notes/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl NoteInterface for SaplingNote {
PoolType::Shielded(ShieldedProtocol::Sapling)
}

fn value(&self) -> u64 {
self.sapling_crypto_note.value().inner()
}

fn spent(&self) -> &Option<(TxId, u32)> {
&self.spent
}
Expand Down
6 changes: 5 additions & 1 deletion zingolib/src/wallet/notes/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ impl NoteInterface for TransparentOutput {
PoolType::Transparent
}

fn value(&self) -> u64 {
self.value
}

fn spent(&self) -> &Option<(TxId, u32)> {
&self.spent
}
Expand Down Expand Up @@ -248,7 +252,7 @@ pub mod mocks {
.txid(TxId::from_bytes([0u8; 32]))
.output_index(0)
.script(vec![])
.value(0)
.value(100000)
.spent(None)
.unconfirmed_spent(None)
}
Expand Down
Loading

0 comments on commit 3382892

Please sign in to comment.