Move get_target_and_anchor_heights to data_api.
This commit is contained in:
parent
9874abfd6c
commit
f3f3512068
|
@ -1,4 +1,6 @@
|
|||
use zcash_primitives::consensus::{self, NetworkUpgrade};
|
||||
use std::cmp;
|
||||
|
||||
use zcash_primitives::consensus::{self, BlockHeight, NetworkUpgrade};
|
||||
|
||||
use crate::data_api::{
|
||||
error::{ChainInvalid, Error},
|
||||
|
@ -77,3 +79,27 @@ pub fn validate_combined_chain<
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines the target height for a transaction, and the height from which to
|
||||
/// select anchors, based on the current synchronised block chain.
|
||||
pub fn get_target_and_anchor_heights<E, D: DBOps<Error = Error<E>>>(
|
||||
data: &D,
|
||||
) -> Result<(BlockHeight, BlockHeight), Error<E>> {
|
||||
data.block_height_extrema().and_then(|heights| {
|
||||
match heights {
|
||||
Some((min_height, max_height)) => {
|
||||
let target_height = max_height + 1;
|
||||
|
||||
// Select an anchor ANCHOR_OFFSET back from the target block,
|
||||
// unless that would be before the earliest block we have.
|
||||
let anchor_height = BlockHeight::from(cmp::max(
|
||||
u32::from(target_height).saturating_sub(ANCHOR_OFFSET),
|
||||
u32::from(min_height),
|
||||
));
|
||||
|
||||
Ok((target_height, anchor_height))
|
||||
}
|
||||
None => Err(Error::ScanRequired),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -24,8 +24,7 @@
|
|||
//! [`CompactBlock`]: zcash_client_backend::proto::compact_formats::CompactBlock
|
||||
//! [`init_cache_database`]: crate::init::init_cache_database
|
||||
|
||||
use rusqlite::{Connection, NO_PARAMS};
|
||||
use std::cmp;
|
||||
use rusqlite::Connection;
|
||||
use std::path::Path;
|
||||
|
||||
use zcash_primitives::{
|
||||
|
@ -35,13 +34,11 @@ use zcash_primitives::{
|
|||
};
|
||||
|
||||
use zcash_client_backend::{
|
||||
data_api::{chain::ANCHOR_OFFSET, error::Error, CacheOps, DBOps},
|
||||
data_api::{error::Error, CacheOps, DBOps},
|
||||
encoding::encode_payment_address,
|
||||
proto::compact_formats::CompactBlock,
|
||||
};
|
||||
|
||||
use crate::error::SqliteClientError;
|
||||
|
||||
pub mod chain;
|
||||
pub mod error;
|
||||
pub mod init;
|
||||
|
@ -110,38 +107,6 @@ fn address_from_extfvk<P: consensus::Parameters>(
|
|||
encode_payment_address(params.hrp_sapling_payment_address(), &addr)
|
||||
}
|
||||
|
||||
/// Determines the target height for a transaction, and the height from which to
|
||||
/// select anchors, based on the current synchronised block chain.
|
||||
fn get_target_and_anchor_heights(
|
||||
data: &DataConnection,
|
||||
) -> Result<(BlockHeight, BlockHeight), SqliteClientError> {
|
||||
data.0.query_row_and_then(
|
||||
"SELECT MIN(height), MAX(height) FROM blocks",
|
||||
NO_PARAMS,
|
||||
|row| match (row.get::<_, u32>(0), row.get::<_, u32>(1)) {
|
||||
// If there are no blocks, the query returns NULL.
|
||||
(Err(rusqlite::Error::InvalidColumnType(_, _, _)), _)
|
||||
| (_, Err(rusqlite::Error::InvalidColumnType(_, _, _))) => {
|
||||
Err(Error::ScanRequired.into())
|
||||
}
|
||||
(Err(e), _) | (_, Err(e)) => Err(e.into()),
|
||||
(Ok(min_height), Ok(max_height)) => {
|
||||
let target_height = max_height + 1;
|
||||
|
||||
// Select an anchor ANCHOR_OFFSET back from the target block,
|
||||
// unless that would be before the earliest block we have.
|
||||
let anchor_height =
|
||||
cmp::max(target_height.saturating_sub(ANCHOR_OFFSET), min_height);
|
||||
|
||||
Ok((
|
||||
BlockHeight::from(target_height),
|
||||
BlockHeight::from(anchor_height),
|
||||
))
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ff::PrimeField;
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
use zcash_primitives::{note_encryption::Memo, transaction::components::Amount};
|
||||
|
||||
use zcash_client_backend::data_api::error::Error;
|
||||
use zcash_client_backend::data_api::{chain::get_target_and_anchor_heights, error::Error};
|
||||
|
||||
use crate::{error::SqliteClientError, get_target_and_anchor_heights, DataConnection};
|
||||
use crate::{error::SqliteClientError, DataConnection};
|
||||
|
||||
/// Returns the address for the account.
|
||||
///
|
||||
|
@ -90,7 +90,7 @@ pub fn get_verified_balance(
|
|||
data: &DataConnection,
|
||||
account: u32,
|
||||
) -> Result<Amount, SqliteClientError> {
|
||||
let (_, anchor_height) = get_target_and_anchor_heights(&data)?;
|
||||
let (_, anchor_height) = get_target_and_anchor_heights(data)?;
|
||||
|
||||
let balance = data.0.query_row(
|
||||
"SELECT SUM(value) FROM received_notes
|
||||
|
|
|
@ -3,9 +3,7 @@
|
|||
use ff::PrimeField;
|
||||
use rusqlite::{types::ToSql, NO_PARAMS};
|
||||
use std::convert::TryInto;
|
||||
use zcash_client_backend::{
|
||||
address::RecipientAddress, data_api::error::Error, encoding::encode_extended_full_viewing_key,
|
||||
};
|
||||
|
||||
use zcash_primitives::{
|
||||
consensus,
|
||||
keys::OutgoingViewingKey,
|
||||
|
@ -21,7 +19,13 @@ use zcash_primitives::{
|
|||
zip32::{ExtendedFullViewingKey, ExtendedSpendingKey},
|
||||
};
|
||||
|
||||
use crate::{error::SqliteClientError, get_target_and_anchor_heights, DataConnection};
|
||||
use zcash_client_backend::{
|
||||
address::RecipientAddress,
|
||||
data_api::{chain::get_target_and_anchor_heights, error::Error},
|
||||
encoding::encode_extended_full_viewing_key,
|
||||
};
|
||||
|
||||
use crate::{error::SqliteClientError, DataConnection};
|
||||
|
||||
/// Describes a policy for which outgoing viewing key should be able to decrypt
|
||||
/// transaction outputs.
|
||||
|
@ -164,7 +168,7 @@ pub fn create_to_address<P: consensus::Parameters>(
|
|||
};
|
||||
|
||||
// Target the next block, assuming we are up-to-date.
|
||||
let (height, anchor_height) = get_target_and_anchor_heights(&data)?;
|
||||
let (height, anchor_height) = get_target_and_anchor_heights(data)?;
|
||||
|
||||
// The goal of this SQL statement is to select the oldest notes until the required
|
||||
// value has been reached, and then fetch the witnesses at the desired height for the
|
||||
|
|
Loading…
Reference in New Issue