2019-03-08 18:16:00 -08:00
|
|
|
//! *An SQLite-based Zcash light client.*
|
|
|
|
//!
|
|
|
|
//! `zcash_client_backend` contains a set of APIs that collectively implement an
|
|
|
|
//! SQLite-based light client for the Zcash network.
|
|
|
|
//!
|
|
|
|
//! # Design
|
|
|
|
//!
|
|
|
|
//! The light client is built around two SQLite databases:
|
|
|
|
//!
|
|
|
|
//! - A cache database, used to inform the light client about new [`CompactBlock`]s. It is
|
|
|
|
//! read-only within all light client APIs *except* for [`init_cache_database`] which
|
|
|
|
//! can be used to initialize the database.
|
|
|
|
//!
|
|
|
|
//! - A data database, where the light client's state is stored. It is read-write within
|
|
|
|
//! the light client APIs, and **assumed to be read-only outside these APIs**. Callers
|
|
|
|
//! **MUST NOT** write to the database without using these APIs. Callers **MAY** read
|
|
|
|
//! the database directly in order to extract information for display to users.
|
|
|
|
//!
|
2019-04-08 04:24:49 -07:00
|
|
|
//! # Features
|
|
|
|
//!
|
|
|
|
//! The `mainnet` feature configures the light client for use with the Zcash mainnet. By
|
|
|
|
//! default, the light client is configured for use with the Zcash testnet.
|
|
|
|
//!
|
2019-03-08 18:16:00 -08:00
|
|
|
//! [`CompactBlock`]: zcash_client_backend::proto::compact_formats::CompactBlock
|
|
|
|
//! [`init_cache_database`]: crate::init::init_cache_database
|
|
|
|
|
2021-01-11 17:13:40 -08:00
|
|
|
use std::collections::HashMap;
|
2021-01-12 17:24:18 -08:00
|
|
|
use std::fmt;
|
2020-08-05 18:14:45 -07:00
|
|
|
use std::path::Path;
|
2019-03-08 18:16:00 -08:00
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
use rusqlite::{Connection, Statement, NO_PARAMS};
|
2020-08-18 12:48:59 -07:00
|
|
|
|
2020-08-05 13:27:40 -07:00
|
|
|
use zcash_primitives::{
|
2020-08-05 18:14:45 -07:00
|
|
|
block::BlockHash,
|
2020-08-05 13:27:40 -07:00
|
|
|
consensus::{self, BlockHeight},
|
2020-08-18 12:48:59 -07:00
|
|
|
merkle_tree::{CommitmentTree, IncrementalWitness},
|
2020-08-25 17:31:21 -07:00
|
|
|
note_encryption::Memo,
|
2021-01-08 20:49:38 -08:00
|
|
|
primitives::{Nullifier, PaymentAddress},
|
2020-08-06 17:13:24 -07:00
|
|
|
sapling::Node,
|
2020-08-25 14:02:44 -07:00
|
|
|
transaction::{components::Amount, Transaction, TxId},
|
2020-08-05 13:27:40 -07:00
|
|
|
zip32::ExtendedFullViewingKey,
|
2019-04-08 04:24:49 -07:00
|
|
|
};
|
|
|
|
|
2020-08-05 18:14:45 -07:00
|
|
|
use zcash_client_backend::{
|
2020-08-25 17:31:21 -07:00
|
|
|
address::RecipientAddress,
|
2021-01-13 14:20:11 -08:00
|
|
|
data_api::{BlockSource, ShieldedOutput, WalletRead, WalletWrite},
|
2020-08-25 14:51:11 -07:00
|
|
|
encoding::encode_payment_address,
|
2020-08-05 18:14:45 -07:00
|
|
|
proto::compact_formats::CompactBlock,
|
2020-08-26 14:47:47 -07:00
|
|
|
wallet::{AccountId, SpendableNote, WalletTx},
|
2020-08-25 14:02:44 -07:00
|
|
|
DecryptedOutput,
|
2020-08-05 18:14:45 -07:00
|
|
|
};
|
|
|
|
|
2021-01-13 17:06:42 -08:00
|
|
|
use crate::error::SqliteClientError;
|
2020-08-06 13:11:25 -07:00
|
|
|
|
2019-05-02 03:27:43 -07:00
|
|
|
pub mod chain;
|
2019-03-08 18:16:00 -08:00
|
|
|
pub mod error;
|
2020-08-25 14:20:12 -07:00
|
|
|
pub mod wallet;
|
2019-03-08 18:16:00 -08:00
|
|
|
|
2021-01-11 17:13:40 -08:00
|
|
|
/// A newtype wrapper for sqlite primary key values for the notes
|
|
|
|
/// table.
|
2020-08-18 12:48:59 -07:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2020-08-06 13:11:25 -07:00
|
|
|
pub struct NoteId(pub i64);
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2020-08-18 12:48:59 -07:00
|
|
|
impl fmt::Display for NoteId {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Note {}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
/// A wrapper for the sqlite connection to the wallet database.
|
|
|
|
pub struct WalletDB<P> {
|
|
|
|
conn: Connection,
|
|
|
|
params: P,
|
|
|
|
}
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
impl<P: consensus::Parameters> WalletDB<P> {
|
2021-01-11 17:13:40 -08:00
|
|
|
/// Construct a connection to the wallet database stored at the specified path.
|
2021-01-12 17:24:18 -08:00
|
|
|
pub fn for_path<F: AsRef<Path>>(path: F, params: P) -> Result<Self, rusqlite::Error> {
|
|
|
|
Connection::open(path).map(move |conn| WalletDB { conn, params })
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
2021-01-08 11:49:10 -08:00
|
|
|
|
2021-01-11 17:13:40 -08:00
|
|
|
/// Given a wallet database connection, obtain a handle for the write operations
|
|
|
|
/// for that database. This operation may eagerly initialize and cache sqlite
|
|
|
|
/// prepared statements that are used in write operations.
|
2021-01-12 17:24:18 -08:00
|
|
|
pub fn get_update_ops<'a>(&'a self) -> Result<DataConnStmtCache<'a, P>, SqliteClientError> {
|
2021-01-08 11:49:10 -08:00
|
|
|
Ok(
|
|
|
|
DataConnStmtCache {
|
2021-01-12 17:24:18 -08:00
|
|
|
wallet_db: self,
|
|
|
|
stmt_insert_block: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"INSERT INTO blocks (height, hash, time, sapling_tree)
|
|
|
|
VALUES (?, ?, ?, ?)",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_insert_tx_meta: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"INSERT INTO transactions (txid, block, tx_index)
|
|
|
|
VALUES (?, ?, ?)",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_update_tx_meta: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"UPDATE transactions
|
|
|
|
SET block = ?, tx_index = ? WHERE txid = ?",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_insert_tx_data: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"INSERT INTO transactions (txid, created, expiry_height, raw)
|
|
|
|
VALUES (?, ?, ?, ?)",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_update_tx_data: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"UPDATE transactions
|
|
|
|
SET expiry_height = ?, raw = ? WHERE txid = ?",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_select_tx_ref: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"SELECT id_tx FROM transactions WHERE txid = ?",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_mark_recived_note_spent: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"UPDATE received_notes SET spent = ? WHERE nf = ?"
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_insert_received_note: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"INSERT INTO received_notes (tx, output_index, account, diversifier, value, rcm, memo, nf, is_change)
|
|
|
|
VALUES (:tx, :output_index, :account, :diversifier, :value, :rcm, :memo, :nf, :is_change)",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_update_received_note: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"UPDATE received_notes
|
|
|
|
SET account = :account,
|
|
|
|
diversifier = :diversifier,
|
|
|
|
value = :value,
|
|
|
|
rcm = :rcm,
|
|
|
|
nf = IFNULL(:nf, nf),
|
|
|
|
memo = IFNULL(:memo, memo),
|
|
|
|
is_change = IFNULL(:is_change, is_change)
|
|
|
|
WHERE tx = :tx AND output_index = :output_index",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_select_received_note: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"SELECT id_note FROM received_notes WHERE tx = ? AND output_index = ?"
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_update_sent_note: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"UPDATE sent_notes
|
|
|
|
SET from_account = ?, address = ?, value = ?, memo = ?
|
|
|
|
WHERE tx = ? AND output_index = ?",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_insert_sent_note: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"INSERT INTO sent_notes (tx, output_index, from_account, address, value, memo)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_insert_witness: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"INSERT INTO sapling_witnesses (note, block, witness)
|
|
|
|
VALUES (?, ?, ?)",
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_prune_witnesses: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"DELETE FROM sapling_witnesses WHERE block < ?"
|
|
|
|
)?,
|
2021-01-12 17:24:18 -08:00
|
|
|
stmt_update_expired: self.conn.prepare(
|
2021-01-08 11:49:10 -08:00
|
|
|
"UPDATE received_notes SET spent = NULL WHERE EXISTS (
|
|
|
|
SELECT id_tx FROM transactions
|
|
|
|
WHERE id_tx = received_notes.spent AND block IS NULL AND expiry_height < ?
|
|
|
|
)",
|
|
|
|
)?,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
impl<P: consensus::Parameters> WalletRead for WalletDB<P> {
|
2021-01-13 14:20:11 -08:00
|
|
|
type Error = SqliteClientError;
|
2020-08-20 10:41:43 -07:00
|
|
|
type NoteRef = NoteId;
|
2020-08-26 14:47:47 -07:00
|
|
|
type TxRef = i64;
|
2020-08-05 18:14:45 -07:00
|
|
|
|
|
|
|
fn block_height_extrema(&self) -> Result<Option<(BlockHeight, BlockHeight)>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::block_height_extrema(self).map_err(SqliteClientError::from)
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_block_hash(&self, block_height: BlockHeight) -> Result<Option<BlockHash>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_block_hash(self, block_height).map_err(SqliteClientError::from)
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 14:02:44 -07:00
|
|
|
fn get_tx_height(&self, txid: TxId) -> Result<Option<BlockHeight>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_tx_height(self, txid).map_err(SqliteClientError::from)
|
2020-08-25 14:02:44 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn get_extended_full_viewing_keys(
|
2020-08-26 14:47:47 -07:00
|
|
|
&self,
|
2021-01-11 17:13:40 -08:00
|
|
|
) -> Result<HashMap<AccountId, ExtendedFullViewingKey>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_extended_full_viewing_keys(self)
|
2020-08-26 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn get_address(&self, account: AccountId) -> Result<Option<PaymentAddress>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_address(self, account)
|
2020-08-06 13:11:25 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn is_valid_account_extfvk(
|
2020-08-26 14:47:47 -07:00
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
extfvk: &ExtendedFullViewingKey,
|
|
|
|
) -> Result<bool, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::is_valid_account_extfvk(self, account, extfvk)
|
2020-08-26 14:47:47 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 10:41:43 -07:00
|
|
|
fn get_balance(&self, account: AccountId) -> Result<Amount, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_balance(self, account)
|
2020-08-06 13:11:25 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 14:47:47 -07:00
|
|
|
fn get_verified_balance(
|
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
anchor_height: BlockHeight,
|
|
|
|
) -> Result<Amount, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_verified_balance(self, account, anchor_height)
|
2020-08-05 16:01:22 -07:00
|
|
|
}
|
2020-08-06 09:23:15 -07:00
|
|
|
|
2020-08-06 13:11:25 -07:00
|
|
|
fn get_received_memo_as_utf8(
|
|
|
|
&self,
|
2020-08-20 10:41:43 -07:00
|
|
|
id_note: Self::NoteRef,
|
2020-08-06 13:11:25 -07:00
|
|
|
) -> Result<Option<String>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_received_memo_as_utf8(self, id_note)
|
2020-08-06 09:23:15 -07:00
|
|
|
}
|
|
|
|
|
2020-08-20 10:41:43 -07:00
|
|
|
fn get_sent_memo_as_utf8(&self, id_note: Self::NoteRef) -> Result<Option<String>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_sent_memo_as_utf8(self, id_note)
|
2020-08-17 10:46:44 -07:00
|
|
|
}
|
|
|
|
|
2020-08-06 17:13:24 -07:00
|
|
|
fn get_commitment_tree(
|
|
|
|
&self,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Option<CommitmentTree<Node>>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_commitment_tree(self, block_height)
|
2020-08-06 17:13:24 -07:00
|
|
|
}
|
2020-08-18 12:48:59 -07:00
|
|
|
|
|
|
|
fn get_witnesses(
|
|
|
|
&self,
|
|
|
|
block_height: BlockHeight,
|
2020-08-20 10:41:43 -07:00
|
|
|
) -> Result<Vec<(Self::NoteRef, IncrementalWitness<Node>)>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_witnesses(self, block_height)
|
2020-08-18 12:48:59 -07:00
|
|
|
}
|
2020-08-18 13:49:00 -07:00
|
|
|
|
2021-01-08 20:49:38 -08:00
|
|
|
fn get_nullifiers(&self) -> Result<Vec<(Nullifier, AccountId)>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::get_nullifiers(self)
|
2020-08-18 13:49:00 -07:00
|
|
|
}
|
2020-08-20 10:41:43 -07:00
|
|
|
|
2021-01-12 19:33:53 -08:00
|
|
|
fn get_spendable_notes(
|
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
anchor_height: BlockHeight,
|
|
|
|
) -> Result<Vec<SpendableNote>, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::transact::get_spendable_notes(self, account, anchor_height)
|
2021-01-12 19:33:53 -08:00
|
|
|
}
|
|
|
|
|
2020-08-26 14:47:47 -07:00
|
|
|
fn select_spendable_notes(
|
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
target_value: Amount,
|
|
|
|
anchor_height: BlockHeight,
|
|
|
|
) -> Result<Vec<SpendableNote>, Self::Error> {
|
2020-08-26 16:52:21 -07:00
|
|
|
wallet::transact::select_spendable_notes(self, account, target_value, anchor_height)
|
2020-08-26 14:47:47 -07:00
|
|
|
}
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
pub struct DataConnStmtCache<'a, P> {
|
|
|
|
wallet_db: &'a WalletDB<P>,
|
2020-08-20 10:41:43 -07:00
|
|
|
stmt_insert_block: Statement<'a>,
|
2020-08-25 14:02:44 -07:00
|
|
|
|
|
|
|
stmt_insert_tx_meta: Statement<'a>,
|
|
|
|
stmt_update_tx_meta: Statement<'a>,
|
|
|
|
|
|
|
|
stmt_insert_tx_data: Statement<'a>,
|
|
|
|
stmt_update_tx_data: Statement<'a>,
|
|
|
|
stmt_select_tx_ref: Statement<'a>,
|
|
|
|
|
|
|
|
stmt_mark_recived_note_spent: Statement<'a>,
|
|
|
|
|
|
|
|
stmt_insert_received_note: Statement<'a>,
|
|
|
|
stmt_update_received_note: Statement<'a>,
|
|
|
|
stmt_select_received_note: Statement<'a>,
|
|
|
|
|
|
|
|
stmt_insert_sent_note: Statement<'a>,
|
|
|
|
stmt_update_sent_note: Statement<'a>,
|
|
|
|
|
2020-08-20 10:41:43 -07:00
|
|
|
stmt_insert_witness: Statement<'a>,
|
|
|
|
stmt_prune_witnesses: Statement<'a>,
|
|
|
|
stmt_update_expired: Statement<'a>,
|
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
impl<'a, P: consensus::Parameters> WalletRead for DataConnStmtCache<'a, P> {
|
2021-01-13 14:20:11 -08:00
|
|
|
type Error = SqliteClientError;
|
2020-08-20 10:41:43 -07:00
|
|
|
type NoteRef = NoteId;
|
2021-01-08 11:49:10 -08:00
|
|
|
type TxRef = i64;
|
|
|
|
|
|
|
|
fn block_height_extrema(&self) -> Result<Option<(BlockHeight, BlockHeight)>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.block_height_extrema()
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_block_hash(&self, block_height: BlockHeight) -> Result<Option<BlockHash>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_block_hash(block_height)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_tx_height(&self, txid: TxId) -> Result<Option<BlockHeight>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_tx_height(txid)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn get_extended_full_viewing_keys(
|
2021-01-08 11:49:10 -08:00
|
|
|
&self,
|
2021-01-11 17:13:40 -08:00
|
|
|
) -> Result<HashMap<AccountId, ExtendedFullViewingKey>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_extended_full_viewing_keys()
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
2020-08-20 10:41:43 -07:00
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn get_address(&self, account: AccountId) -> Result<Option<PaymentAddress>, Self::Error> {
|
|
|
|
self.wallet_db.get_address(account)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn is_valid_account_extfvk(
|
2021-01-08 11:49:10 -08:00
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
extfvk: &ExtendedFullViewingKey,
|
|
|
|
) -> Result<bool, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.is_valid_account_extfvk(account, extfvk)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_balance(&self, account: AccountId) -> Result<Amount, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_balance(account)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_verified_balance(
|
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
anchor_height: BlockHeight,
|
|
|
|
) -> Result<Amount, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_verified_balance(account, anchor_height)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_received_memo_as_utf8(
|
|
|
|
&self,
|
|
|
|
id_note: Self::NoteRef,
|
|
|
|
) -> Result<Option<String>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_received_memo_as_utf8(id_note)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_sent_memo_as_utf8(&self, id_note: Self::NoteRef) -> Result<Option<String>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_sent_memo_as_utf8(id_note)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_commitment_tree(
|
|
|
|
&self,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Option<CommitmentTree<Node>>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_commitment_tree(block_height)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_witnesses(
|
|
|
|
&self,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
) -> Result<Vec<(Self::NoteRef, IncrementalWitness<Node>)>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_witnesses(block_height)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:49:38 -08:00
|
|
|
fn get_nullifiers(&self) -> Result<Vec<(Nullifier, AccountId)>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db.get_nullifiers()
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
|
2021-01-12 19:33:53 -08:00
|
|
|
fn get_spendable_notes(
|
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
anchor_height: BlockHeight,
|
|
|
|
) -> Result<Vec<SpendableNote>, Self::Error> {
|
|
|
|
self.wallet_db.get_spendable_notes(account, anchor_height)
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:49:10 -08:00
|
|
|
fn select_spendable_notes(
|
|
|
|
&self,
|
|
|
|
account: AccountId,
|
|
|
|
target_value: Amount,
|
|
|
|
anchor_height: BlockHeight,
|
|
|
|
) -> Result<Vec<SpendableNote>, Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
self.wallet_db
|
|
|
|
.select_spendable_notes(account, target_value, anchor_height)
|
2021-01-08 11:49:10 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> {
|
2020-08-26 16:52:21 -07:00
|
|
|
fn transactionally<F, A>(&mut self, f: F) -> Result<A, Self::Error>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self) -> Result<A, Self::Error>,
|
|
|
|
{
|
2021-01-13 14:20:11 -08:00
|
|
|
self.wallet_db.conn.execute("BEGIN IMMEDIATE", NO_PARAMS)?;
|
2020-08-26 16:52:21 -07:00
|
|
|
match f(self) {
|
|
|
|
Ok(result) => {
|
2021-01-13 14:20:11 -08:00
|
|
|
self.wallet_db.conn.execute("COMMIT", NO_PARAMS)?;
|
2020-08-26 16:52:21 -07:00
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
Err(error) => {
|
2021-01-12 17:24:18 -08:00
|
|
|
match self.wallet_db.conn.execute("ROLLBACK", NO_PARAMS) {
|
2020-08-26 16:52:21 -07:00
|
|
|
Ok(_) => Err(error),
|
|
|
|
Err(e) =>
|
|
|
|
// REVIEW: If rollback fails, what do we want to do? I think that
|
|
|
|
// panicking here is probably the right thing to do, because it
|
|
|
|
// means the database is corrupt?
|
|
|
|
panic!(
|
|
|
|
"Rollback failed with error {} while attempting to recover from error {}; database is likely corrupt.",
|
|
|
|
e,
|
2021-01-12 17:24:18 -08:00
|
|
|
error
|
2020-08-26 16:52:21 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 10:41:43 -07:00
|
|
|
fn insert_block(
|
|
|
|
&mut self,
|
|
|
|
block_height: BlockHeight,
|
|
|
|
block_hash: BlockHash,
|
|
|
|
block_time: u32,
|
|
|
|
commitment_tree: &CommitmentTree<Node>,
|
|
|
|
) -> Result<(), Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
wallet::insert_block(self, block_height, block_hash, block_time, commitment_tree)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn rewind_to_height(&mut self, block_height: BlockHeight) -> Result<(), Self::Error> {
|
|
|
|
wallet::rewind_to_height(self.wallet_db, block_height)
|
2020-08-26 16:52:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 14:02:44 -07:00
|
|
|
fn put_tx_meta(
|
|
|
|
&mut self,
|
|
|
|
tx: &WalletTx,
|
|
|
|
height: BlockHeight,
|
|
|
|
) -> Result<Self::TxRef, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::put_tx_meta(self, tx, height)
|
2020-08-25 14:02:44 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 14:47:47 -07:00
|
|
|
fn put_tx_data(
|
|
|
|
&mut self,
|
|
|
|
tx: &Transaction,
|
|
|
|
created_at: Option<time::OffsetDateTime>,
|
|
|
|
) -> Result<Self::TxRef, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::put_tx_data(self, tx, created_at)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
2021-01-08 20:49:38 -08:00
|
|
|
fn mark_spent(&mut self, tx_ref: Self::TxRef, nf: &Nullifier) -> Result<(), Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::mark_spent(self, tx_ref, nf)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
2020-08-25 14:02:44 -07:00
|
|
|
// Assumptions:
|
|
|
|
// - A transaction will not contain more than 2^63 shielded outputs.
|
|
|
|
// - A note value will never exceed 2^63 zatoshis.
|
|
|
|
fn put_received_note<T: ShieldedOutput>(
|
2020-08-20 10:41:43 -07:00
|
|
|
&mut self,
|
2020-08-25 14:02:44 -07:00
|
|
|
output: &T,
|
2021-01-08 20:49:38 -08:00
|
|
|
nf_opt: &Option<Nullifier>,
|
2020-08-20 10:41:43 -07:00
|
|
|
tx_ref: Self::TxRef,
|
|
|
|
) -> Result<Self::NoteRef, Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::put_received_note(self, output, nf_opt, tx_ref)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_witness(
|
|
|
|
&mut self,
|
|
|
|
note_id: Self::NoteRef,
|
|
|
|
witness: &IncrementalWitness<Node>,
|
|
|
|
height: BlockHeight,
|
|
|
|
) -> Result<(), Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::insert_witness(self, note_id, witness, height)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn prune_witnesses(&mut self, below_height: BlockHeight) -> Result<(), Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::prune_witnesses(self, below_height)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn update_expired_notes(&mut self, height: BlockHeight) -> Result<(), Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::update_expired_notes(self, height)
|
2020-08-20 10:41:43 -07:00
|
|
|
}
|
2020-08-25 14:02:44 -07:00
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn put_sent_note(
|
2020-08-25 14:02:44 -07:00
|
|
|
&mut self,
|
|
|
|
output: &DecryptedOutput,
|
|
|
|
tx_ref: Self::TxRef,
|
|
|
|
) -> Result<(), Self::Error> {
|
2021-01-13 14:20:11 -08:00
|
|
|
wallet::put_sent_note(self, output, tx_ref)
|
2020-08-25 14:02:44 -07:00
|
|
|
}
|
2020-08-25 17:31:21 -07:00
|
|
|
|
2021-01-12 17:24:18 -08:00
|
|
|
fn insert_sent_note(
|
2020-08-25 17:31:21 -07:00
|
|
|
&mut self,
|
|
|
|
tx_ref: Self::TxRef,
|
|
|
|
output_index: usize,
|
|
|
|
account: AccountId,
|
|
|
|
to: &RecipientAddress,
|
|
|
|
value: Amount,
|
|
|
|
memo: Option<Memo>,
|
|
|
|
) -> Result<(), Self::Error> {
|
2021-01-12 17:24:18 -08:00
|
|
|
wallet::insert_sent_note(self, tx_ref, output_index, account, to, value, memo)
|
2020-08-25 17:31:21 -07:00
|
|
|
}
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
|
|
|
|
2020-10-21 12:08:09 -07:00
|
|
|
pub struct BlockDB(Connection);
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2020-10-21 12:08:09 -07:00
|
|
|
impl BlockDB {
|
2020-08-05 18:14:45 -07:00
|
|
|
pub fn for_path<P: AsRef<Path>>(path: P) -> Result<Self, rusqlite::Error> {
|
2020-10-21 12:08:09 -07:00
|
|
|
Connection::open(path).map(BlockDB)
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 12:08:09 -07:00
|
|
|
impl BlockSource for BlockDB {
|
2021-01-13 14:20:11 -08:00
|
|
|
type Error = SqliteClientError;
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2020-10-19 15:52:48 -07:00
|
|
|
fn with_blocks<F>(
|
2020-08-18 15:33:34 -07:00
|
|
|
&self,
|
|
|
|
from_height: BlockHeight,
|
|
|
|
limit: Option<u32>,
|
|
|
|
with_row: F,
|
|
|
|
) -> Result<(), Self::Error>
|
|
|
|
where
|
2020-10-19 15:52:48 -07:00
|
|
|
F: FnMut(CompactBlock) -> Result<(), Self::Error>,
|
2020-08-18 15:33:34 -07:00
|
|
|
{
|
2020-10-19 15:52:48 -07:00
|
|
|
chain::with_blocks(self, from_height, limit, with_row)
|
2020-08-18 15:33:34 -07:00
|
|
|
}
|
2020-08-05 18:14:45 -07:00
|
|
|
}
|
2019-04-08 04:24:49 -07:00
|
|
|
|
2020-08-05 13:27:40 -07:00
|
|
|
fn address_from_extfvk<P: consensus::Parameters>(
|
|
|
|
params: &P,
|
|
|
|
extfvk: &ExtendedFullViewingKey,
|
|
|
|
) -> String {
|
2019-03-08 18:16:00 -08:00
|
|
|
let addr = extfvk.default_address().unwrap().1;
|
2020-08-05 13:27:40 -07:00
|
|
|
encode_payment_address(params.hrp_sapling_payment_address(), &addr)
|
2019-03-08 18:16:00 -08:00
|
|
|
}
|
2019-03-08 18:53:38 -08:00
|
|
|
|
2019-03-08 19:12:31 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-05 00:21:42 -07:00
|
|
|
use ff::PrimeField;
|
2020-07-01 13:26:54 -07:00
|
|
|
use group::GroupEncoding;
|
2019-03-08 19:12:31 -08:00
|
|
|
use protobuf::Message;
|
|
|
|
use rand_core::{OsRng, RngCore};
|
2021-01-12 20:10:34 -08:00
|
|
|
use rusqlite::params;
|
2020-08-05 13:27:40 -07:00
|
|
|
|
2019-03-08 19:12:31 -08:00
|
|
|
use zcash_client_backend::proto::compact_formats::{
|
|
|
|
CompactBlock, CompactOutput, CompactSpend, CompactTx,
|
|
|
|
};
|
2020-08-05 13:27:40 -07:00
|
|
|
|
2019-03-08 19:12:31 -08:00
|
|
|
use zcash_primitives::{
|
|
|
|
block::BlockHash,
|
2020-08-05 13:27:40 -07:00
|
|
|
consensus::{BlockHeight, Network, NetworkUpgrade, Parameters},
|
2019-03-08 19:12:31 -08:00
|
|
|
note_encryption::{Memo, SaplingNoteEncryption},
|
2021-01-11 17:13:40 -08:00
|
|
|
primitives::{Note, Nullifier, PaymentAddress},
|
2019-03-08 19:12:31 -08:00
|
|
|
transaction::components::Amount,
|
2020-08-05 00:21:42 -07:00
|
|
|
util::generate_random_rseed,
|
2019-03-08 19:12:31 -08:00
|
|
|
zip32::ExtendedFullViewingKey,
|
|
|
|
};
|
|
|
|
|
2020-10-21 12:08:09 -07:00
|
|
|
use super::BlockDB;
|
2020-08-05 18:14:45 -07:00
|
|
|
|
2020-08-05 13:27:40 -07:00
|
|
|
#[cfg(feature = "mainnet")]
|
|
|
|
pub(crate) fn network() -> Network {
|
|
|
|
Network::MainNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "mainnet"))]
|
|
|
|
pub(crate) fn network() -> Network {
|
|
|
|
Network::TestNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "mainnet")]
|
|
|
|
pub(crate) fn sapling_activation_height() -> BlockHeight {
|
|
|
|
Network::MainNetwork
|
|
|
|
.activation_height(NetworkUpgrade::Sapling)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "mainnet"))]
|
|
|
|
pub(crate) fn sapling_activation_height() -> BlockHeight {
|
|
|
|
Network::TestNetwork
|
|
|
|
.activation_height(NetworkUpgrade::Sapling)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2019-03-08 19:12:31 -08:00
|
|
|
/// Create a fake CompactBlock at the given height, containing a single output paying
|
|
|
|
/// the given address. Returns the CompactBlock and the nullifier for the new note.
|
|
|
|
pub(crate) fn fake_compact_block(
|
2020-08-05 13:08:58 -07:00
|
|
|
height: BlockHeight,
|
2019-03-08 19:12:31 -08:00
|
|
|
prev_hash: BlockHash,
|
|
|
|
extfvk: ExtendedFullViewingKey,
|
|
|
|
value: Amount,
|
2021-01-11 17:13:40 -08:00
|
|
|
) -> (CompactBlock, Nullifier) {
|
2019-03-08 19:12:31 -08:00
|
|
|
let to = extfvk.default_address().unwrap().1;
|
|
|
|
|
|
|
|
// Create a fake Note for the account
|
|
|
|
let mut rng = OsRng;
|
2020-08-05 13:27:40 -07:00
|
|
|
let rseed = generate_random_rseed(&network(), height, &mut rng);
|
2019-03-08 19:12:31 -08:00
|
|
|
let note = Note {
|
2020-07-01 13:26:54 -07:00
|
|
|
g_d: to.diversifier().g_d().unwrap(),
|
2019-03-08 19:12:31 -08:00
|
|
|
pk_d: to.pk_d().clone(),
|
|
|
|
value: value.into(),
|
2020-07-30 09:44:23 -07:00
|
|
|
rseed,
|
2019-03-08 19:12:31 -08:00
|
|
|
};
|
|
|
|
let encryptor = SaplingNoteEncryption::new(
|
2020-08-28 08:12:37 -07:00
|
|
|
Some(extfvk.fvk.ovk),
|
2019-03-08 19:12:31 -08:00
|
|
|
note.clone(),
|
|
|
|
to.clone(),
|
|
|
|
Memo::default(),
|
2020-08-05 21:47:35 -07:00
|
|
|
&mut rng,
|
2019-03-08 19:12:31 -08:00
|
|
|
);
|
2020-08-21 10:33:22 -07:00
|
|
|
let cmu = note.cmu().to_repr().as_ref().to_vec();
|
2020-07-01 13:26:54 -07:00
|
|
|
let epk = encryptor.epk().to_bytes().to_vec();
|
2019-03-08 19:12:31 -08:00
|
|
|
let enc_ciphertext = encryptor.encrypt_note_plaintext();
|
|
|
|
|
|
|
|
// Create a fake CompactBlock containing the note
|
|
|
|
let mut cout = CompactOutput::new();
|
|
|
|
cout.set_cmu(cmu);
|
|
|
|
cout.set_epk(epk);
|
|
|
|
cout.set_ciphertext(enc_ciphertext[..52].to_vec());
|
|
|
|
let mut ctx = CompactTx::new();
|
|
|
|
let mut txid = vec![0; 32];
|
|
|
|
rng.fill_bytes(&mut txid);
|
|
|
|
ctx.set_hash(txid);
|
|
|
|
ctx.outputs.push(cout);
|
|
|
|
let mut cb = CompactBlock::new();
|
2020-08-05 13:08:58 -07:00
|
|
|
cb.set_height(u64::from(height));
|
2019-03-08 19:12:31 -08:00
|
|
|
cb.hash.resize(32, 0);
|
|
|
|
rng.fill_bytes(&mut cb.hash);
|
|
|
|
cb.prevHash.extend_from_slice(&prev_hash.0);
|
|
|
|
cb.vtx.push(ctx);
|
2020-07-01 13:26:54 -07:00
|
|
|
(cb, note.nf(&extfvk.fvk.vk, 0))
|
2019-03-08 19:12:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a fake CompactBlock at the given height, spending a single note from the
|
|
|
|
/// given address.
|
|
|
|
pub(crate) fn fake_compact_block_spending(
|
2020-08-05 13:08:58 -07:00
|
|
|
height: BlockHeight,
|
2019-03-08 19:12:31 -08:00
|
|
|
prev_hash: BlockHash,
|
2021-01-11 17:13:40 -08:00
|
|
|
(nf, in_value): (Nullifier, Amount),
|
2019-03-08 19:12:31 -08:00
|
|
|
extfvk: ExtendedFullViewingKey,
|
2020-07-01 13:26:54 -07:00
|
|
|
to: PaymentAddress,
|
2019-03-08 19:12:31 -08:00
|
|
|
value: Amount,
|
|
|
|
) -> CompactBlock {
|
|
|
|
let mut rng = OsRng;
|
2020-08-05 13:27:40 -07:00
|
|
|
let rseed = generate_random_rseed(&network(), height, &mut rng);
|
2019-03-08 19:12:31 -08:00
|
|
|
|
|
|
|
// Create a fake CompactBlock containing the note
|
|
|
|
let mut cspend = CompactSpend::new();
|
2021-01-11 17:13:40 -08:00
|
|
|
cspend.set_nf(nf.to_vec());
|
2019-03-08 19:12:31 -08:00
|
|
|
let mut ctx = CompactTx::new();
|
|
|
|
let mut txid = vec![0; 32];
|
|
|
|
rng.fill_bytes(&mut txid);
|
|
|
|
ctx.set_hash(txid);
|
|
|
|
ctx.spends.push(cspend);
|
|
|
|
|
|
|
|
// Create a fake Note for the payment
|
|
|
|
ctx.outputs.push({
|
|
|
|
let note = Note {
|
2020-07-01 13:26:54 -07:00
|
|
|
g_d: to.diversifier().g_d().unwrap(),
|
2019-03-08 19:12:31 -08:00
|
|
|
pk_d: to.pk_d().clone(),
|
|
|
|
value: value.into(),
|
2020-07-30 09:44:23 -07:00
|
|
|
rseed,
|
2019-03-08 19:12:31 -08:00
|
|
|
};
|
2020-08-05 21:47:35 -07:00
|
|
|
let encryptor = SaplingNoteEncryption::new(
|
2020-08-28 08:12:37 -07:00
|
|
|
Some(extfvk.fvk.ovk),
|
2020-08-05 21:47:35 -07:00
|
|
|
note.clone(),
|
|
|
|
to,
|
|
|
|
Memo::default(),
|
|
|
|
&mut rng,
|
|
|
|
);
|
2020-08-21 10:33:22 -07:00
|
|
|
let cmu = note.cmu().to_repr().as_ref().to_vec();
|
2020-07-01 13:26:54 -07:00
|
|
|
let epk = encryptor.epk().to_bytes().to_vec();
|
2019-03-08 19:12:31 -08:00
|
|
|
let enc_ciphertext = encryptor.encrypt_note_plaintext();
|
|
|
|
|
|
|
|
let mut cout = CompactOutput::new();
|
|
|
|
cout.set_cmu(cmu);
|
|
|
|
cout.set_epk(epk);
|
|
|
|
cout.set_ciphertext(enc_ciphertext[..52].to_vec());
|
|
|
|
cout
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a fake Note for the change
|
|
|
|
ctx.outputs.push({
|
|
|
|
let change_addr = extfvk.default_address().unwrap().1;
|
2020-08-05 13:27:40 -07:00
|
|
|
let rseed = generate_random_rseed(&network(), height, &mut rng);
|
2019-03-08 19:12:31 -08:00
|
|
|
let note = Note {
|
2020-07-01 13:26:54 -07:00
|
|
|
g_d: change_addr.diversifier().g_d().unwrap(),
|
2019-03-08 19:12:31 -08:00
|
|
|
pk_d: change_addr.pk_d().clone(),
|
|
|
|
value: (in_value - value).into(),
|
2020-07-30 09:44:23 -07:00
|
|
|
rseed,
|
2019-03-08 19:12:31 -08:00
|
|
|
};
|
|
|
|
let encryptor = SaplingNoteEncryption::new(
|
2020-08-28 08:12:37 -07:00
|
|
|
Some(extfvk.fvk.ovk),
|
2019-03-08 19:12:31 -08:00
|
|
|
note.clone(),
|
|
|
|
change_addr,
|
|
|
|
Memo::default(),
|
2020-08-05 21:47:35 -07:00
|
|
|
&mut rng,
|
2019-03-08 19:12:31 -08:00
|
|
|
);
|
2020-08-21 10:33:22 -07:00
|
|
|
let cmu = note.cmu().to_repr().as_ref().to_vec();
|
2020-07-01 13:26:54 -07:00
|
|
|
let epk = encryptor.epk().to_bytes().to_vec();
|
2019-03-08 19:12:31 -08:00
|
|
|
let enc_ciphertext = encryptor.encrypt_note_plaintext();
|
|
|
|
|
|
|
|
let mut cout = CompactOutput::new();
|
|
|
|
cout.set_cmu(cmu);
|
|
|
|
cout.set_epk(epk);
|
|
|
|
cout.set_ciphertext(enc_ciphertext[..52].to_vec());
|
|
|
|
cout
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut cb = CompactBlock::new();
|
2020-08-05 13:08:58 -07:00
|
|
|
cb.set_height(u64::from(height));
|
2019-03-08 19:12:31 -08:00
|
|
|
cb.hash.resize(32, 0);
|
|
|
|
rng.fill_bytes(&mut cb.hash);
|
|
|
|
cb.prevHash.extend_from_slice(&prev_hash.0);
|
|
|
|
cb.vtx.push(ctx);
|
|
|
|
cb
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a fake CompactBlock into the cache DB.
|
2020-10-21 12:08:09 -07:00
|
|
|
pub(crate) fn insert_into_cache(db_cache: &BlockDB, cb: &CompactBlock) {
|
2019-03-08 19:12:31 -08:00
|
|
|
let cb_bytes = cb.write_to_bytes().unwrap();
|
2020-08-05 18:14:45 -07:00
|
|
|
db_cache
|
|
|
|
.0
|
2019-03-08 19:12:31 -08:00
|
|
|
.prepare("INSERT INTO compactblocks (height, data) VALUES (?, ?)")
|
|
|
|
.unwrap()
|
2021-01-12 17:24:18 -08:00
|
|
|
.execute(params![u32::from(cb.height()), cb_bytes,])
|
2019-03-08 19:12:31 -08:00
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|