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

[Draft] Shield Funds from cached UTXOs #339

Closed
wants to merge 1 commit into from
Closed
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
70 changes: 67 additions & 3 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,80 @@
//! Functions for enforcing chain validity and handling chain reorgs.
use protobuf::Message;

use rusqlite::params;
use rusqlite::{params};

use zcash_primitives::consensus::BlockHeight;
use zcash_primitives::{
transaction::{components::Amount, TxId},
consensus::{self, BlockHeight},
};

use zcash_client_backend::{data_api::error::Error, proto::compact_formats::CompactBlock};
use zcash_client_backend::{
data_api::error::Error,
proto::compact_formats::CompactBlock,
address::RecipientAddress,
};

use crate::{error::SqliteClientError, BlockDB};

pub mod init;


pub struct UnspentTransactionOutput {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type should probably go in zcash-client-backend; we don't actually want any functions depending on any types or calling anything in zcash_client_sqlite directly; the only things that should be used from this crate (not through the DAA) are the WalletDB and BlockDB initialization functions.

pub address: RecipientAddress,
pub txid: TxId,
pub index: i32,
pub script: Vec<u8>,
pub value: Amount,
pub height: BlockHeight,
}

pub fn get_confirmed_utxos_for_address<P: consensus::Parameters>(
params: &P,
cache: &BlockDB,
anchor_height: BlockHeight,
address: &str
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this address be wrapped in a better type? You could pass the Magna Carta here. :)

) -> Result<Vec<UnspentTransactionOutput>,SqliteClientError> {
let mut stmt_blocks = cache.0.prepare(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is inserting data into this table?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rust welding on mobile wallets. The idea is that libruszcash only reads from this table as it only reads the compact_blocks table. Should I add the insertion code as well to make this 'self-sufficient' ? what's the compact_blocks approach?

"SELECT address, txid, idx, script, value_zat, height FROM utxos WHERE address = ? AND height <= ?",
)?;

let rows = stmt_blocks.query_map(
params![address, u32::from(anchor_height)],
|row| {
let addr: String = row.get(0)?;
let address = RecipientAddress::decode(params, &addr)
. ok_or(format!(
"Could not interpret {} as a valid Zcash address.",
addr
)).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unwrap() should never be used, this error needs to be propagated out. Here's an example of something similar: https://github.com/zcash/librustzcash/blob/master/zcash_client_sqlite/src/wallet.rs#L79-L85

let id: Vec<u8> = row.get(1)?;

let mut txid = TxId([0u8; 32]);
txid.0.copy_from_slice(&id);
let index: i32 = row.get(2)?;
let script: Vec<u8> = row.get(3)?;
let value: i64 = row.get(4)?;
let height: u32 = row.get(5)?;

Ok(UnspentTransactionOutput{
address: address,
txid: txid,
index: index,
script: script,
value: Amount::from_i64(value).unwrap(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about unwrap() here, you don't want to crash the process.

height: BlockHeight::from(height)
})
},
)?;

let mut utxos = Vec::<UnspentTransactionOutput>::new();

for utxo in rows {
utxos.push(utxo.unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, no unwrap, this is a place to use collect()

}
Ok(utxos)
}

struct CompactBlockRow {
height: BlockHeight,
data: Vec<u8>,
Expand Down