Add more fields to sapling note data

This commit is contained in:
Aditya Kulkarni 2019-09-06 15:25:08 -07:00
parent 4b4a308717
commit 8ff6d15e32
2 changed files with 61 additions and 23 deletions

View File

@ -232,22 +232,21 @@ impl LightClient {
}); });
}; };
// Print all the memos for fun. // // Print all the memos for fun.
let memos = self.wallet.txs.read().unwrap() // let memos = self.wallet.txs.read().unwrap()
.values().flat_map(|wtx| { // .values().flat_map(|wtx| {
wtx.notes.iter().map(|nd| nd.memo.clone() ).collect::<Vec<Option<Memo>>>() // wtx.notes.iter().map(|nd| nd.memo.clone() ).collect::<Vec<Option<Memo>>>()
}) // })
.map( |m| match m { // .map( |m| match m {
Some(memo) => { // Some(memo) => {
match memo.to_utf8() { // match memo.to_utf8() {
Some(Ok(memo_str)) => Some(memo_str), // Some(Ok(memo_str)) => Some(memo_str),
_ => None // _ => None
} // }
} // }
_ => None // _ => None
}) // })
.collect::<Vec<Option<String>>>(); // .collect::<Vec<Option<String>>>();
//println!("All Wallet Txns {:?}", memos); //println!("All Wallet Txns {:?}", memos);
} }

View File

@ -95,11 +95,12 @@ pub struct SaplingNoteData {
account: usize, account: usize,
extfvk: ExtendedFullViewingKey, // Technically, this should be recoverable from the account number, but we're going to refactor this in the future, so I'll write it again here. extfvk: ExtendedFullViewingKey, // Technically, this should be recoverable from the account number, but we're going to refactor this in the future, so I'll write it again here.
diversifier: Diversifier, diversifier: Diversifier,
note: Note<Bls12>, pub note: Note<Bls12>,
witnesses: Vec<IncrementalWitness<Node>>, witnesses: Vec<IncrementalWitness<Node>>,
nullifier: [u8; 32], nullifier: [u8; 32],
spent: Option<TxId>, pub spent: Option<TxId>,
pub memo: Option<Memo> pub memo: Option<Memo>,
pub is_change: bool,
} }
@ -160,7 +161,8 @@ impl SaplingNoteData {
witnesses: vec![witness], witnesses: vec![witness],
nullifier: nf, nullifier: nf,
spent: None, spent: None,
memo: None memo: None,
is_change: output.is_change,
} }
} }
@ -208,6 +210,8 @@ impl SaplingNoteData {
} }
})?; })?;
let is_change: bool = reader.read_u8()? > 0;
Ok(SaplingNoteData { Ok(SaplingNoteData {
account, account,
extfvk, extfvk,
@ -216,7 +220,8 @@ impl SaplingNoteData {
witnesses, witnesses,
nullifier, nullifier,
spent, spent,
memo memo,
is_change,
}) })
} }
@ -245,30 +250,54 @@ impl SaplingNoteData {
Optional::write(&mut writer, &self.memo, |w, m| w.write_all(m.as_bytes()))?; Optional::write(&mut writer, &self.memo, |w, m| w.write_all(m.as_bytes()))?;
writer.write_u8(if self.is_change {1} else {0})?;
Ok(()) Ok(())
} }
} }
pub struct WalletTx { pub struct WalletTx {
block: i32, pub block: i32,
// Txid of this transcation. It's duplicated here (It is also the Key in the HashMap that points to this
// WalletTx in LightWallet::txs)
pub txid: TxId,
// List of all notes recieved in this tx. Note that some of these might be change notes.
pub notes: Vec<SaplingNoteData>, pub notes: Vec<SaplingNoteData>,
// Total shielded value spent in this Tx. Note that this is the value of notes spent,
// the change is returned in the notes above. Subtract the two to get the actual value spent.
// Also note that even after subtraction, you might need to account for transparent inputs and outputs
// to make sure the value is accurate.
pub total_shielded_value_spent: u64,
} }
impl WalletTx { impl WalletTx {
pub fn serialized_version() -> u64 { pub fn serialized_version() -> u64 {
return 1; return 1;
} }
pub fn read<R: Read>(mut reader: R) -> io::Result<Self> { pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
let version = reader.read_u64::<LittleEndian>()?; let version = reader.read_u64::<LittleEndian>()?;
assert_eq!(version, WalletTx::serialized_version()); assert_eq!(version, WalletTx::serialized_version());
let block = reader.read_i32::<LittleEndian>()?; let block = reader.read_i32::<LittleEndian>()?;
let mut txid_bytes = [0u8; 32];
reader.read_exact(&mut txid_bytes)?;
let txid = TxId{0: txid_bytes};
let notes = Vector::read(&mut reader, |r| SaplingNoteData::read(r))?; let notes = Vector::read(&mut reader, |r| SaplingNoteData::read(r))?;
let total_shielded_value_spent = reader.read_u64::<LittleEndian>()?;
Ok(WalletTx{ Ok(WalletTx{
block, block,
notes txid,
notes,
total_shielded_value_spent
}) })
} }
@ -277,8 +306,12 @@ impl WalletTx {
writer.write_i32::<LittleEndian>(self.block)?; writer.write_i32::<LittleEndian>(self.block)?;
writer.write_all(&self.txid.0)?;
Vector::write(&mut writer, &self.notes, |w, nd| nd.write(w))?; Vector::write(&mut writer, &self.notes, |w, nd| nd.write(w))?;
writer.write_u64::<LittleEndian>(self.total_shielded_value_spent)?;
Ok(()) Ok(())
} }
} }
@ -613,7 +646,10 @@ impl LightWallet {
for tx in new_txs { for tx in new_txs {
// Mark notes as spent. // Mark notes as spent.
let mut total_shielded_value_spent: u64 = 0;
for spend in &tx.shielded_spends { for spend in &tx.shielded_spends {
// TODO: Add up the spent value here and add it to the WalletTx as a Spent
let txid = nfs let txid = nfs
.iter() .iter()
.find(|(nf, _, _)| &nf[..] == &spend.nf[..]) .find(|(nf, _, _)| &nf[..] == &spend.nf[..])
@ -627,13 +663,16 @@ impl LightWallet {
.find(|nd| &nd.nullifier[..] == &spend.nf[..]) .find(|nd| &nd.nullifier[..] == &spend.nf[..])
.unwrap(); .unwrap();
spent_note.spent = Some(tx.txid); spent_note.spent = Some(tx.txid);
total_shielded_value_spent += spent_note.note.value;
} }
// Find the existing transaction entry, or create a new one. // Find the existing transaction entry, or create a new one.
if !txs.contains_key(&tx.txid) { if !txs.contains_key(&tx.txid) {
let tx_entry = WalletTx { let tx_entry = WalletTx {
block: block_data.height, block: block_data.height,
txid: tx.txid,
notes: vec![], notes: vec![],
total_shielded_value_spent
}; };
txs.insert(tx.txid, tx_entry); txs.insert(tx.txid, tx_entry);
} }