From dd2972d3acc96f497490c73cb332b242cc9939cb Mon Sep 17 00:00:00 2001 From: Francisco Gindre Date: Tue, 22 Dec 2020 11:10:13 -0300 Subject: [PATCH] PoC Storing UTXOs on BlockDb remove unused imports rename index column of utxos table fix: move Script back to Vec Add missing imports [WIP] move code to chain.rs and catch up with latest changes reset wallet.rs fix SQL query --- zcash_client_sqlite/src/chain.rs | 70 ++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index b60f9ef067..91443da96f 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -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 { + pub address: RecipientAddress, + pub txid: TxId, + pub index: i32, + pub script: Vec, + pub value: Amount, + pub height: BlockHeight, +} + +pub fn get_confirmed_utxos_for_address( + params: &P, + cache: &BlockDB, + anchor_height: BlockHeight, + address: &str +) -> Result,SqliteClientError> { + let mut stmt_blocks = cache.0.prepare( + "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(); + let id: Vec = row.get(1)?; + + let mut txid = TxId([0u8; 32]); + txid.0.copy_from_slice(&id); + let index: i32 = row.get(2)?; + let script: Vec = 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(), + height: BlockHeight::from(height) + }) + }, + )?; + + let mut utxos = Vec::::new(); + + for utxo in rows { + utxos.push(utxo.unwrap()) + } + Ok(utxos) +} + struct CompactBlockRow { height: BlockHeight, data: Vec,