Clean up dead code
This commit is contained in:
parent
d53b9446d3
commit
f222187650
|
@ -25,13 +25,16 @@ rpcpassword=s!NWfgM!5X55
|
|||
- Start zcashd
|
||||
- Check status
|
||||
- Create a new account
|
||||
- Get a new address
|
||||
- List addresses
|
||||
- Mine 200 blocks
|
||||
- Check balance
|
||||
- Shield coinbase to our test zaddr
|
||||
- Shield coinbase to our zaddr. Don't forget to update the example
|
||||
- Check result
|
||||
- Mine 1 block
|
||||
- Mine 10 block
|
||||
- Check balance
|
||||
|
||||
Example
|
||||
```sh
|
||||
$ zcashd -datadir=$PWD --daemon
|
||||
$ zcash-cli -datadir=$PWD getinfo
|
||||
|
@ -39,9 +42,10 @@ $ zcash-cli -datadir=$PWD z_getnewaccount
|
|||
$ zcash-cli -datadir=$PWD listaddresses
|
||||
$ zcash-cli -datadir=$PWD generate 200
|
||||
$ zcash-cli -datadir=$PWD getbalance
|
||||
$ zcash-cli -datadir=$PWD z_sendmany "ANY_TADDR" '[{"address": "zregtestsapling1qzy9wafd2axnenul6t6wav76dys6s8uatsq778mpmdvmx4k9myqxsd9m73aqdgc7gwnv53wga4j", "amount": 6.24999}]'
|
||||
$ zcash-cli -datadir=$PWD z_sendmany "ANY_TADDR" '[{"address": "zregtestsapling1flaha7huh4vzlj5zlh29xca2u8wf8ygh2vl4t2v4nlada39fc5hm4tl2dpdp6ewzjadvj9cewzh", "amount": 624.99999}]'
|
||||
$ zcash-cli -datadir=$PWD z_getoperationresult
|
||||
$ zcash-cli -datadir=$PWD generate 1
|
||||
$ zcash-cli -datadir=$PWD generate 10
|
||||
$ zcash-cli -datadir=$PWD z_gettotalbalance
|
||||
```
|
||||
|
||||
## Lightwalletd
|
||||
|
@ -67,6 +71,8 @@ zec = { db_path = "./zec.db", lwd_url = "http://127.0.0.1:9067" }
|
|||
- Create account using the test seed phrase: `bleak regret excuse hold divide novel rain clutch once used another visual forward small tumble artefact jewel bundle kid wolf universe focus weekend melt`
|
||||
- Sync
|
||||
- Check balance: 624999000
|
||||
- Check latest height
|
||||
- Get UA address
|
||||
|
||||
```sh
|
||||
$ cargo b --features rpc --bin warp-rpc
|
||||
|
@ -74,4 +80,39 @@ $ ../../target/debug/warp-rpc
|
|||
$ curl -X POST -H 'Content-Type: application/json' -d '{"coin": 0, "name": "test", "key": "bleak regret excuse hold divide novel rain clutch once used another visual forward small tumble artefact jewel bundle kid wolf universe focus weekend melt"}' http://localhost:8000/new_account
|
||||
$ curl -X POST 'http://localhost:8000/sync?offset=0'
|
||||
$ curl -X GET http://localhost:8000/balance
|
||||
$ curl -X GET http://localhost:8000/latest_height
|
||||
$ curl -X GET http://localhost:8000/unified_address?t=1\&s=1\&o=1
|
||||
```
|
||||
|
||||
zcash-cli -datadir=$PWD z_sendmany "ANY_TADDR" '[{"address": "zregtestsapling1rlf8jpvk6qymgsn6pclkpnee0u77pajpz5g7955uzrxsefc837h326rkjag7rwuhn2cyympd8jh", "amount": 6.24999}]'
|
||||
zcash-cli -datadir=$PWD z_sendmany "zregtestsapling1rlf8jpvk6qymgsn6pclkpnee0u77pajpz5g7955uzrxsefc837h326rkjag7rwuhn2cyympd8jh" '[{"address": "tmWXoSBwPoCjJCNZjw4P7heoVMcT2Ronrqq", "amount": 6.24997}]'
|
||||
zcash-cli -datadir=$PWD z_sendmany "zregtestsapling1rlf8jpvk6qymgsn6pclkpnee0u77pajpz5g7955uzrxsefc837h326rkjag7rwuhn2cyympd8jh" '[{"address": "uregtest1mzt5lx5s5u8kczlfr82av97kjckmfjfuq8y9849h6cl9chhdekxsm6r9dklracflqwplrnfzm5rucp5txfdm04z5myrde8y3y5rayev8", "amount": 6.24997}]' 1 0.00001 "AllowRevealedAmounts"
|
||||
|
||||
## Addresses
|
||||
|
||||
- t
|
||||
tmWXoSBwPoCjJCNZjw4P7heoVMcT2Ronrqq
|
||||
|
||||
- s
|
||||
zregtestsapling1qzy9wafd2axnenul6t6wav76dys6s8uatsq778mpmdvmx4k9myqxsd9m73aqdgc7gwnv53wga4j
|
||||
|
||||
- o
|
||||
uregtest1mzt5lx5s5u8kczlfr82av97kjckmfjfuq8y9849h6cl9chhdekxsm6r9dklracflqwplrnfzm5rucp5txfdm04z5myrde8y3y5rayev8
|
||||
|
||||
- ts
|
||||
uregtest1yvucqfqnmq5ldc6fkvuudlsjhxg56hxph9ymmcnmpzpywd752ym8sr5l5d24wqn4enz3gakk6alf5hlpw2cjs3jjrcdae3nksrefyum5x400f9gs3ak9yllcr8czhrlnjufuuy7n5mh
|
||||
|
||||
- to
|
||||
uregtest1wqgc0cm50a7a647qrdglgj62fl40q8njsrcfkt2mzlsmj979rdmsdwuysypc6ewxjxz0zc48kmm35jwx4q6c4fgqwkmmqyhwlep4n2hc0229vf6cahcnesr38y7gyzfx6pa8zg9jvv9
|
||||
|
||||
- so
|
||||
uregtest1usu9eyxgqu48sa8lqug6ccjc7vcam3mt3a5t7jvyxj7pq5dgdtkjgkqzsyh9pfeav9970xddp2c9h5x44drwnz4f0zwc894k3vt380g6kfsg9j9fmnpljye9r56d94njsv40uaam392xvmky2v38dh3yhayz44z6xv402slujuhwy3mg
|
||||
|
||||
- tso
|
||||
uregtest1mxy5wq2n0xw57nuxa4lqpl358zw4vzyfgadsn5jungttmqcv6nx6cpx465dtpzjzw0vprjle4j4nqqzxtkuzm93regvgg4xce0un5ec6tedquc469zjhtdpkxz04kunqqyasv4rwvcweh3ue0ku0payn29stl2pwcrghyzscrrju9ar57rn36wgz74nmynwcyw27rjd8yk477l97ez8
|
||||
|
||||
Ex:`utxo!(1, 100), sapling!(2, 160), orchard!(3, 70), orchard!(4, 50)`
|
||||
```
|
||||
zcash-cli -datadir=$PWD z_sendmany "zregtestsapling1flaha7huh4vzlj5zlh29xca2u8wf8ygh2vl4t2v4nlada39fc5hm4tl2dpdp6ewzjadvj9cewzh" '[{"address": "tmWXoSBwPoCjJCNZjw4P7heoVMcT2Ronrqq", "amount": 0.001}, {"address": "zregtestsapling1qzy9wafd2axnenul6t6wav76dys6s8uatsq778mpmdvmx4k9myqxsd9m73aqdgc7gwnv53wga4j", "amount": 0.0016}, {"address": "uregtest1mzt5lx5s5u8kczlfr82av97kjckmfjfuq8y9849h6cl9chhdekxsm6r9dklracflqwplrnfzm5rucp5txfdm04z5myrde8y3y5rayev8", "amount": 0.0007}]' 1 0.00001 "AllowRevealedRecipients"
|
||||
zcash-cli -datadir=$PWD z_sendmany "zregtestsapling1flaha7huh4vzlj5zlh29xca2u8wf8ygh2vl4t2v4nlada39fc5hm4tl2dpdp6ewzjadvj9cewzh" '[{"address": "uregtest1mzt5lx5s5u8kczlfr82av97kjckmfjfuq8y9849h6cl9chhdekxsm6r9dklracflqwplrnfzm5rucp5txfdm04z5myrde8y3y5rayev8", "amount": 0.0005}]' 1 0.00001 "AllowRevealedRecipients"
|
||||
```
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
//! Warp Synchronize
|
||||
|
||||
use crate::coinconfig::CoinConfig;
|
||||
use crate::db::PlainNote;
|
||||
use crate::scan::{AMProgressCallback, Progress};
|
||||
use crate::{AccountData, BlockId, CompactTxStreamerClient, DbAdapter};
|
||||
use std::sync::Arc;
|
||||
|
@ -163,6 +162,7 @@ pub async fn get_block_by_time(time: u32) -> anyhow::Result<u32> {
|
|||
Ok(date_time)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn trial_decrypt(
|
||||
height: u32,
|
||||
cmu: &[u8],
|
||||
|
|
|
@ -32,7 +32,7 @@ use zcash_primitives::zip32::ExtendedFullViewingKey;
|
|||
use crate::gpu::cuda::{CudaProcessor, CUDA_CONTEXT};
|
||||
#[cfg(feature = "apple_metal")]
|
||||
use crate::gpu::metal::MetalProcessor;
|
||||
use crate::gpu::{trial_decrypt, USE_GPU};
|
||||
use crate::gpu::USE_GPU;
|
||||
|
||||
pub async fn get_latest_height(
|
||||
client: &mut CompactTxStreamerClient<Channel>,
|
||||
|
|
|
@ -46,6 +46,7 @@ pub fn set_coin_lwd_url(coin: u8, lwd_url: &str) {
|
|||
}
|
||||
|
||||
/// Get the URL of the lightwalletd server for a given coin
|
||||
#[allow(dead_code)] // Used by C FFI
|
||||
pub fn get_coin_lwd_url(coin: u8) -> String {
|
||||
let c = COIN_CONFIG[coin as usize].lock().unwrap();
|
||||
c.lwd_url.clone().unwrap_or_default()
|
||||
|
|
10
src/db.rs
10
src/db.rs
|
@ -75,16 +75,6 @@ pub struct AccountViewKey {
|
|||
pub viewonly: bool,
|
||||
}
|
||||
|
||||
impl AccountViewKey {
|
||||
pub fn from_fvk(fvk: &ExtendedFullViewingKey) -> AccountViewKey {
|
||||
AccountViewKey {
|
||||
fvk: fvk.clone(),
|
||||
ivk: fvk.fvk.vk.ivk(),
|
||||
viewonly: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct AccountBackup {
|
||||
pub coin: u8,
|
||||
|
|
|
@ -52,6 +52,7 @@ pub trait GPUProcessor {
|
|||
fn buffer_stride() -> usize;
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn trial_decrypt<
|
||||
'a,
|
||||
FVKIter: Iterator<Item = (&'a u32, &'a AccountViewKey)>,
|
||||
|
@ -80,6 +81,7 @@ pub fn trial_decrypt<
|
|||
Ok(processor.get_decrypted_blocks()?)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn collect_nf(blocks: Vec<CompactBlock>) -> Result<Vec<DecryptedBlock>> {
|
||||
let mut decrypted_blocks = vec![];
|
||||
// collect nullifiers
|
||||
|
@ -106,6 +108,7 @@ fn collect_nf(blocks: Vec<CompactBlock>) -> Result<Vec<DecryptedBlock>> {
|
|||
Ok(decrypted_blocks)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn collect_decrypted_notes(
|
||||
network: &Network,
|
||||
account: u32,
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use crate::coinconfig::CoinConfig;
|
||||
use bip39::{Language, Mnemonic, Seed};
|
||||
use zcash_client_backend::address::RecipientAddress;
|
||||
use zcash_client_backend::encoding::{
|
||||
decode_extended_full_viewing_key, decode_extended_spending_key,
|
||||
encode_extended_full_viewing_key, encode_extended_spending_key, encode_payment_address,
|
||||
|
@ -35,6 +34,7 @@ pub fn decode_key(
|
|||
res
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // Used by C FFI
|
||||
pub fn is_valid_key(coin: u8, key: &str) -> i8 {
|
||||
let c = CoinConfig::get(coin);
|
||||
let network = c.chain.network();
|
||||
|
@ -53,13 +53,15 @@ pub fn is_valid_key(coin: u8, key: &str) -> i8 {
|
|||
-1
|
||||
}
|
||||
|
||||
#[allow(dead_code)] // Used by C FFI
|
||||
pub fn is_valid_address(coin: u8, address: &str) -> bool {
|
||||
let c = CoinConfig::get(coin);
|
||||
let network = c.chain.network();
|
||||
let recipient = RecipientAddress::decode(network, address);
|
||||
let recipient = zcash_client_backend::address::RecipientAddress::decode(network, address);
|
||||
recipient.is_some()
|
||||
}
|
||||
|
||||
|
||||
fn derive_secret_key(
|
||||
network: &Network,
|
||||
mnemonic: &Mnemonic,
|
||||
|
|
|
@ -56,8 +56,6 @@
|
|||
#[path = "generated/cash.z.wallet.sdk.rpc.rs"]
|
||||
pub mod lw_rpc;
|
||||
|
||||
use zcash_params::coin::{get_branch, get_coin_type, CoinType};
|
||||
|
||||
// Mainnet
|
||||
const LWD_URL: &str = "https://mainnet.lightwalletd.com:9067";
|
||||
// pub const LWD_URL: &str = "https://lwdv3.zecwallet.co";
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use std::slice;
|
||||
use std::str::FromStr;
|
||||
use anyhow::anyhow;
|
||||
use jubjub::Fr;
|
||||
|
@ -9,8 +8,7 @@ use zcash_primitives::consensus::{BlockHeight, BranchId, Network, Parameters};
|
|||
use zcash_primitives::transaction::builder::Builder;
|
||||
use orchard::builder::Builder as OrchardBuilder;
|
||||
use orchard::bundle::Flags;
|
||||
use orchard::circuit::ProvingKey;
|
||||
use orchard::note::{ExtractedNoteCommitment, Nullifier};
|
||||
use orchard::note::Nullifier;
|
||||
use orchard::value::NoteValue;
|
||||
use rand::{CryptoRng, RngCore};
|
||||
use rand::rngs::OsRng;
|
||||
|
@ -18,8 +16,8 @@ use ripemd::{Digest, Ripemd160};
|
|||
use sha2::Sha256;
|
||||
use zcash_client_backend::encoding::decode_extended_spending_key;
|
||||
use zcash_primitives::legacy::TransparentAddress;
|
||||
use zcash_primitives::memo::{Memo, MemoBytes};
|
||||
use zcash_primitives::merkle_tree::{IncrementalWitness, MerklePath};
|
||||
use zcash_primitives::memo::MemoBytes;
|
||||
use zcash_primitives::merkle_tree::IncrementalWitness;
|
||||
use zcash_primitives::sapling::{Diversifier, Node, PaymentAddress, Rseed};
|
||||
use zcash_primitives::sapling::prover::TxProver;
|
||||
use zcash_primitives::transaction::components::{Amount, OutPoint, TxOut};
|
||||
|
@ -27,13 +25,11 @@ use zcash_primitives::transaction::{Transaction, TransactionData, TxVersion};
|
|||
use zcash_primitives::transaction::sighash::SignableInput;
|
||||
use zcash_primitives::transaction::sighash_v5::v5_signature_hash;
|
||||
use zcash_primitives::transaction::txid::TxIdDigester;
|
||||
use zcash_primitives::zip32::{DiversifierIndex, ExtendedFullViewingKey, ExtendedSpendingKey};
|
||||
use zcash_proofs::prover::LocalTxProver;
|
||||
use crate::{AccountData, broadcast_tx, CoinConfig, DbAdapter, init_coin, set_active, set_coin_lwd_url};
|
||||
use crate::api::payment::RecipientMemo;
|
||||
use zcash_primitives::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey};
|
||||
use crate::{AccountData, broadcast_tx, CoinConfig, init_coin, set_active, set_coin_lwd_url};
|
||||
use crate::coinconfig::get_prover;
|
||||
use crate::note_selection::types::TransactionPlan;
|
||||
use crate::note_selection::{decode, Destination, FeeFlat, fetch_utxos, note_select_with_fee, NoteSelectConfig, Pool, prepare_multi_payment, PrivacyPolicy, Source};
|
||||
use crate::note_selection::{decode, Destination, FeeFlat, fetch_utxos, note_select_with_fee, NoteSelectConfig, PrivacyPolicy, Source};
|
||||
use crate::orchard::{get_proving_key, ORCHARD_ROOTS, OrchardHasher};
|
||||
use crate::sapling::{SAPLING_ROOTS, SaplingHasher};
|
||||
use crate::sync::tree::TreeCheckpoint;
|
||||
|
@ -312,27 +308,3 @@ async fn submit_tx() {
|
|||
let r = broadcast_tx(&hex::decode(tx).unwrap()).await.unwrap();
|
||||
log::info!("{}", r);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tree_state() {
|
||||
let _ = env_logger::try_init();
|
||||
init_coin(0, "./zec.db").unwrap();
|
||||
set_coin_lwd_url(0, "http://127.0.0.1:9067");
|
||||
|
||||
let h = OrchardHasher::new();
|
||||
let c = CoinConfig::get(0);
|
||||
let db = c.db.as_ref().unwrap();
|
||||
let db = db.lock().unwrap();
|
||||
let tree = db.get_tree_by_name(235, "orchard").unwrap();
|
||||
let TreeCheckpoint { tree, witnesses } = tree;
|
||||
let root = tree.root(32, &ORCHARD_ROOTS, &h);
|
||||
println!("{}", hex::encode(root));
|
||||
|
||||
for witness in witnesses.iter() {
|
||||
let auth_path: Vec<_> = witness.auth_path(32, &ORCHARD_ROOTS, &h).iter()
|
||||
.map(|n| {
|
||||
orchard::tree::MerkleHashOrchard::from_bytes(n).unwrap()
|
||||
}).collect();
|
||||
let merkle_path = orchard::tree::MerklePath::from_parts(witness.position as u32, auth_path.try_into().unwrap());
|
||||
}
|
||||
}
|
|
@ -2,14 +2,13 @@ use std::cmp::min;
|
|||
use zcash_address::{AddressKind, ZcashAddress};
|
||||
use zcash_address::unified::{Container, Receiver};
|
||||
use zcash_primitives::memo::MemoBytes;
|
||||
use crate::note_selection::types::{PrivacyPolicy, Fill, Execution, Order, Pool, PoolAllocation, Destination, PoolPrecedence, PoolPriority};
|
||||
use crate::note_selection::types::{PrivacyPolicy, Fill, Execution, Order, Pool, PoolAllocation, Destination, PoolPrecedence};
|
||||
|
||||
/// Decode address and return it as an order
|
||||
///
|
||||
pub fn decode(id: u32, address: &str, amount: u64, memo: MemoBytes) -> anyhow::Result<Order> {
|
||||
let address = ZcashAddress::try_from_encoded(address)?;
|
||||
let mut order = Order::default();
|
||||
let mut precedence = order.priority.to_pool_precedence().clone();
|
||||
order.id = id;
|
||||
match address.kind {
|
||||
AddressKind::Sprout(_) => {}
|
||||
|
@ -18,7 +17,7 @@ pub fn decode(id: u32, address: &str, amount: u64, memo: MemoBytes) -> anyhow::R
|
|||
order.destinations[Pool::Sapling as usize] = Some(destination);
|
||||
}
|
||||
AddressKind::Unified(unified_address) => {
|
||||
for (i, address) in unified_address.items().into_iter().enumerate() {
|
||||
for address in unified_address.items() {
|
||||
match address {
|
||||
Receiver::Orchard(data) => {
|
||||
let destination = Destination::Orchard(data);
|
||||
|
@ -55,7 +54,7 @@ pub fn execute_orders(orders: &mut [Order], initial_pool: &PoolAllocation, use_t
|
|||
let mut fills = vec![];
|
||||
|
||||
for order in orders.iter_mut() {
|
||||
let order_precedence = order.priority.to_pool_precedence();
|
||||
let order_precedence = if order.is_fee { precedence } else { order.priority.to_pool_precedence() };
|
||||
// log::info!("Order {:?}", order);
|
||||
// Direct Shielded Fill - s2s, o2o
|
||||
// t2t only for fees
|
||||
|
|
|
@ -92,7 +92,7 @@ pub fn note_select_with_fee<F: FeeCalculator>(utxos: &[UTXO], orders: &mut [Orde
|
|||
let mut fee_order = Order {
|
||||
id: u32::MAX,
|
||||
destinations: ANY_DESTINATION,
|
||||
priority: fee_precedence.try_into().unwrap(),
|
||||
priority: PoolPriority::OS, // ignored for fees
|
||||
amount: fee,
|
||||
memo: MemoBytes::empty(),
|
||||
is_fee: true, // do not include in fee calculation
|
||||
|
|
|
@ -75,7 +75,7 @@ macro_rules! order {
|
|||
id: $id,
|
||||
amount: $q * 1000,
|
||||
destinations: $destinations,
|
||||
priority: PoolPriority::OST,
|
||||
priority: PoolPriority::OS,
|
||||
filled: 0,
|
||||
is_fee: false,
|
||||
memo: MemoBytes::empty(),
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
use std::ops::{Add, Sub};
|
||||
use anyhow::anyhow;
|
||||
use zcash_primitives::memo::MemoBytes;
|
||||
use serde::Serialize;
|
||||
use serde_with::serde_as;
|
||||
|
@ -85,7 +84,7 @@ impl Default for Order {
|
|||
Order {
|
||||
id: 0,
|
||||
destinations: [None; 3],
|
||||
priority: PoolPriority::OST,
|
||||
priority: PoolPriority::OS,
|
||||
amount: 0,
|
||||
memo: MemoBytes::empty(),
|
||||
is_fee: false,
|
||||
|
@ -212,38 +211,15 @@ impl Destination {
|
|||
|
||||
#[derive(Clone, Copy, Serialize, Debug)]
|
||||
pub enum PoolPriority {
|
||||
TSO = 1,
|
||||
OST = 2,
|
||||
TOS = 3,
|
||||
SOT = 4,
|
||||
OTS = 5,
|
||||
STO = 6,
|
||||
SO = 1,
|
||||
OS = 2,
|
||||
}
|
||||
|
||||
impl PoolPriority {
|
||||
const VALUES: [PoolPriority; 6] = [ PoolPriority::TSO, PoolPriority::OST, PoolPriority::TOS, PoolPriority::SOT, PoolPriority::OTS, PoolPriority::STO];
|
||||
pub fn to_pool_precedence(&self) -> &'static PoolPrecedence {
|
||||
match self {
|
||||
PoolPriority::TSO => &[ Pool:: Transparent, Pool::Sapling, Pool::Orchard ],
|
||||
PoolPriority::OST => &[ Pool::Orchard, Pool::Sapling, Pool:: Transparent ],
|
||||
PoolPriority::TOS => &[ Pool:: Transparent, Pool::Orchard, Pool::Sapling ],
|
||||
PoolPriority::SOT => &[ Pool::Sapling, Pool::Orchard, Pool:: Transparent ],
|
||||
PoolPriority::OTS => &[ Pool::Orchard, Pool:: Transparent, Pool::Sapling ],
|
||||
PoolPriority::STO => &[ Pool::Sapling, Pool:: Transparent, Pool::Orchard ],
|
||||
PoolPriority::SO => &[ Pool::Sapling, Pool::Orchard, Pool::Transparent ],
|
||||
PoolPriority::OS => &[ Pool::Orchard, Pool::Sapling, Pool::Transparent ],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<PoolPrecedence> for PoolPriority {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
fn try_from(value: PoolPrecedence) -> Result<Self, Self::Error> {
|
||||
let p: [Pool; 3] = value.try_into().unwrap();
|
||||
for pp in PoolPriority::VALUES {
|
||||
if p == *pp.to_pool_precedence() {
|
||||
return Ok(pp)
|
||||
}
|
||||
}
|
||||
Err(anyhow!("Pool preference invalid"))
|
||||
}
|
||||
}
|
||||
|
|
366
src/scan.rs
366
src/scan.rs
|
@ -1,34 +1,28 @@
|
|||
use crate::chain::{DecryptedBlock, get_latest_height, Nf, NfRef};
|
||||
use crate::db::{AccountViewKey, DbAdapter, PlainNote, ReceivedNote};
|
||||
use crate::chain::get_latest_height;
|
||||
use crate::db::{AccountViewKey, DbAdapter};
|
||||
use serde::Serialize;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use crate::transaction::retrieve_tx_info;
|
||||
use crate::{connect_lightwalletd, CompactBlock, CompactSaplingOutput, CompactTx, DbAdapterBuilder, chain, AccountRec};
|
||||
use crate::{connect_lightwalletd, CompactBlock, CompactSaplingOutput, CompactTx, DbAdapterBuilder};
|
||||
use crate::chain::{DecryptNode, download_chain};
|
||||
use ff::PrimeField;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use lazy_static::lazy_static;
|
||||
use std::collections::HashMap;
|
||||
use std::panic;
|
||||
use std::sync::Arc;
|
||||
use std::time::Instant;
|
||||
use bip39::{Language, Mnemonic};
|
||||
use orchard::keys::{FullViewingKey, SpendingKey};
|
||||
use orchard::note_encryption::OrchardDomain;
|
||||
use tokio::runtime::{Builder, Runtime};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::Mutex;
|
||||
use zcash_client_backend::encoding::decode_extended_full_viewing_key;
|
||||
use zcash_params::coin::{get_coin_chain, CoinType};
|
||||
use zcash_primitives::consensus::{Network, NetworkUpgrade, Parameters};
|
||||
use zcash_primitives::consensus::{Network, Parameters};
|
||||
|
||||
use zcash_primitives::sapling::{Node, Note};
|
||||
use zcash_primitives::sapling::Note;
|
||||
use zcash_primitives::sapling::note_encryption::SaplingDomain;
|
||||
use crate::orchard::{DecryptedOrchardNote, OrchardDecrypter, OrchardHasher, OrchardViewKey};
|
||||
use crate::sapling::{DecryptedSaplingNote, SaplingDecrypter, SaplingHasher, SaplingViewKey};
|
||||
use crate::sync::{CTree, Synchronizer, WarpProcessor};
|
||||
use crate::sync::{Synchronizer, WarpProcessor};
|
||||
|
||||
pub struct Blocks(pub Vec<CompactBlock>, pub usize);
|
||||
|
||||
|
@ -71,11 +65,11 @@ type OrchardSynchronizer = Synchronizer<Network, OrchardDomain, OrchardViewKey,
|
|||
pub async fn sync_async<'a>(
|
||||
coin_type: CoinType,
|
||||
_chunk_size: u32,
|
||||
get_tx: bool,
|
||||
_get_tx: bool, // TODO
|
||||
db_path: &'a str,
|
||||
target_height_offset: u32,
|
||||
max_cost: u32,
|
||||
progress_callback: AMProgressCallback,
|
||||
_progress_callback: AMProgressCallback, // TODO
|
||||
cancel: &'static std::sync::Mutex<bool>,
|
||||
ld_url: &'a str,
|
||||
) -> anyhow::Result<()> {
|
||||
|
@ -157,348 +151,6 @@ pub async fn sync_async<'a>(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn sync_async_old(
|
||||
coin_type: CoinType,
|
||||
_chunk_size: u32,
|
||||
get_tx: bool,
|
||||
db_path: &str,
|
||||
target_height_offset: u32,
|
||||
max_cost: u32,
|
||||
progress_callback: AMProgressCallback,
|
||||
cancel: &'static std::sync::Mutex<bool>,
|
||||
ld_url: &str,
|
||||
) -> anyhow::Result<()> {
|
||||
/*
|
||||
let ld_url = ld_url.to_owned();
|
||||
let db_path = db_path.to_string();
|
||||
let network = {
|
||||
let chain = get_coin_chain(coin_type);
|
||||
*chain.network()
|
||||
};
|
||||
|
||||
let mut client = connect_lightwalletd(&ld_url).await?;
|
||||
let (start_height, prev_hash, vks) = {
|
||||
let db = DbAdapter::new(coin_type, &db_path)?;
|
||||
let height = db.get_db_height()?;
|
||||
let hash = db.get_db_hash(height)?;
|
||||
let vks = db.get_fvks()?;
|
||||
(height, hash, vks)
|
||||
};
|
||||
let saplingvks: Vec<_> = vks.iter().map(|(&account, vk)| {
|
||||
SaplingViewKey {
|
||||
account,
|
||||
fvk: vk.fvk.clone(),
|
||||
ivk: vk.ivk.clone(),
|
||||
}
|
||||
}).collect();
|
||||
|
||||
let end_height = get_latest_height(&mut client).await?;
|
||||
let end_height = (end_height - target_height_offset).max(start_height);
|
||||
if start_height >= end_height {
|
||||
return Ok(());
|
||||
}
|
||||
let n_ivks = vks.len();
|
||||
|
||||
let (decryptor_tx, mut decryptor_rx) = mpsc::channel::<Blocks>(1);
|
||||
let (processor_tx, mut processor_rx) = mpsc::channel::<(Vec<DecryptedBlock>, usize)>(1);
|
||||
|
||||
let db_path2 = db_path.clone();
|
||||
|
||||
let downloader = tokio::spawn(async move {
|
||||
log::info!("download_scheduler");
|
||||
download_chain(
|
||||
&mut client,
|
||||
n_ivks,
|
||||
start_height,
|
||||
end_height,
|
||||
prev_hash,
|
||||
max_cost,
|
||||
decryptor_tx,
|
||||
cancel,
|
||||
)
|
||||
.await?;
|
||||
Ok::<_, anyhow::Error>(())
|
||||
});
|
||||
|
||||
let proc_callback = progress_callback.clone();
|
||||
|
||||
let decryptor = DECRYPTER_RUNTIME.spawn(async move {
|
||||
while let Some(blocks) = decryptor_rx.recv().await {
|
||||
let dec_blocks = decrypter.decrypt_blocks(&network, blocks.0); // this function may block
|
||||
let batch_decrypt_elapsed: usize = dec_blocks.iter().map(|b| b.elapsed).sum();
|
||||
log::info!(" Batch Decrypt: {} ms", batch_decrypt_elapsed);
|
||||
let _ = processor_tx.send((dec_blocks, blocks.1)).await;
|
||||
}
|
||||
Ok::<_, anyhow::Error>(())
|
||||
});
|
||||
|
||||
let mut progress = Progress {
|
||||
height: 0,
|
||||
trial_decryptions: 0,
|
||||
downloaded: 0,
|
||||
};
|
||||
|
||||
let processor = tokio::spawn(async move {
|
||||
let mut db = DbAdapter::new(coin_type, &db_path2)?;
|
||||
|
||||
while let Some((dec_blocks, blocks_size)) = processor_rx.recv().await {
|
||||
if dec_blocks.is_empty() {
|
||||
continue;
|
||||
}
|
||||
progress.downloaded += blocks_size;
|
||||
/* TODO
|
||||
- Change to WarpProcessors & Trial Decryptors - sapling & orchard
|
||||
- Feed block into WP sapling
|
||||
- Check height vs orchard activation height -> Feed block into WP orchard
|
||||
- Refactor into function
|
||||
- Remove new_idtx
|
||||
|
||||
*/
|
||||
|
||||
log::info!("start processing - {}", dec_blocks[0].height);
|
||||
log::info!("Time {:?}", chrono::offset::Local::now());
|
||||
let start = Instant::now();
|
||||
|
||||
let decrypter = DecryptNode::new(vks);
|
||||
|
||||
let mut sapling_synchronizer = SaplingSynchronizer::new(
|
||||
decrypter,
|
||||
warper,
|
||||
saplingvks.clone(),
|
||||
DbAdapterBuilder { coin_type, db_path: db_path.clone() },
|
||||
"sapling".to_string(),
|
||||
);
|
||||
sapling_synchronizer.initialize()?;
|
||||
|
||||
{
|
||||
// db tx scope
|
||||
let db_tx = db.begin_transaction()?;
|
||||
|
||||
/*
|
||||
let outputs = dec_blocks
|
||||
.iter()
|
||||
.map(|db| db.count_outputs as usize)
|
||||
.sum::<usize>();
|
||||
{
|
||||
progress.trial_decryptions += (n_ivks * outputs) as u64;
|
||||
}
|
||||
for b in dec_blocks.iter() {
|
||||
let mut my_nfs: Vec<Nf> = vec![];
|
||||
for nf in b.spends.iter() {
|
||||
if let Some(&nf_ref) = nfs.get(nf) {
|
||||
log::info!("NF FOUND {} {}", nf_ref.id_note, b.height);
|
||||
DbAdapter::mark_spent(nf_ref.id_note, b.height, &db_tx)?;
|
||||
my_nfs.push(*nf);
|
||||
nfs.remove(nf);
|
||||
}
|
||||
}
|
||||
if !b.notes.is_empty() {
|
||||
log::debug!("{} {}", b.height, b.notes.len());
|
||||
}
|
||||
|
||||
for n in b.notes.iter() {
|
||||
let p = absolute_position_at_block_start + n.position_in_block;
|
||||
|
||||
let note = &n.note;
|
||||
let rcm = note.rcm().to_repr();
|
||||
let nf = note.nf(&n.ivk.fvk.vk.nk, p as u64);
|
||||
|
||||
let id_tx = DbAdapter::store_transaction(
|
||||
&n.txid,
|
||||
n.account,
|
||||
n.height,
|
||||
b.compact_block.time,
|
||||
n.tx_index as u32,
|
||||
&db_tx,
|
||||
)?;
|
||||
new_ids_tx.insert(
|
||||
id_tx,
|
||||
TxIdHeight {
|
||||
id_tx,
|
||||
height: n.height,
|
||||
index: n.tx_index as u32,
|
||||
},
|
||||
);
|
||||
let id_note = DbAdapter::store_received_note(
|
||||
&ReceivedNote {
|
||||
account: n.account,
|
||||
height: n.height,
|
||||
output_index: n.output_index as u32,
|
||||
diversifier: n.pa.diversifier().0.to_vec(),
|
||||
value: note.value,
|
||||
rcm: rcm.to_vec(),
|
||||
rho: None,
|
||||
nf: nf.0.to_vec(),
|
||||
spent: None,
|
||||
},
|
||||
id_tx,
|
||||
n.position_in_block,
|
||||
&db_tx,
|
||||
)?;
|
||||
DbAdapter::add_value(id_tx, note.value as i64, &db_tx)?;
|
||||
nfs.insert(
|
||||
Nf(nf.0),
|
||||
NfRef {
|
||||
id_note,
|
||||
account: n.account,
|
||||
},
|
||||
);
|
||||
|
||||
let w = Witness::new(p as usize, id_note, Some(n.clone()));
|
||||
witnesses.push(w);
|
||||
}
|
||||
|
||||
if !my_nfs.is_empty() {
|
||||
for (tx_index, tx) in b.compact_block.vtx.iter().enumerate() {
|
||||
for cs in tx.spends.iter() {
|
||||
let mut nf = [0u8; 32];
|
||||
nf.copy_from_slice(&cs.nf);
|
||||
let nf = Nf(nf);
|
||||
if my_nfs.contains(&nf) {
|
||||
let (account, note_value) =
|
||||
DbAdapter::get_received_note_value(&nf, &db_tx)?;
|
||||
let txid = &*tx.hash;
|
||||
let id_tx = DbAdapter::store_transaction(
|
||||
txid,
|
||||
account,
|
||||
b.height,
|
||||
b.compact_block.time,
|
||||
tx_index as u32,
|
||||
&db_tx,
|
||||
)?;
|
||||
new_ids_tx.insert(
|
||||
id_tx,
|
||||
TxIdHeight {
|
||||
id_tx,
|
||||
height: b.height,
|
||||
index: tx_index as u32,
|
||||
},
|
||||
);
|
||||
DbAdapter::add_value(id_tx, -(note_value as i64), &db_tx)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
absolute_position_at_block_start += b.count_outputs as usize;
|
||||
|
||||
}
|
||||
*/
|
||||
log::info!("Dec end : {}", start.elapsed().as_millis());
|
||||
|
||||
db_tx.commit()?;
|
||||
}
|
||||
|
||||
/*
|
||||
let start = Instant::now();
|
||||
let mut nodes: Vec<Node> = vec![];
|
||||
for block in dec_blocks.iter() {
|
||||
let cb = &block.compact_block;
|
||||
for tx in cb.vtx.iter() {
|
||||
for co in tx.outputs.iter() {
|
||||
let mut cmu = [0u8; 32];
|
||||
cmu.copy_from_slice(&co.cmu);
|
||||
let node = Node::new(cmu);
|
||||
nodes.push(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !nodes.is_empty() {
|
||||
bp.add_nodes(&mut nodes, &witnesses);
|
||||
}
|
||||
// println!("NOTES = {}", nodes.len());
|
||||
|
||||
log::info!("Witness : {}", start.elapsed().as_millis());
|
||||
|
||||
let start = Instant::now();
|
||||
if get_tx && !new_ids_tx.is_empty() {
|
||||
let mut ids: Vec<_> = new_ids_tx.into_iter().map(|(_, v)| v).collect();
|
||||
ids.sort_by(|a, b| {
|
||||
let c = a.height.cmp(&b.height);
|
||||
if c == Ordering::Equal {
|
||||
return a.index.cmp(&b.index);
|
||||
}
|
||||
c
|
||||
});
|
||||
let ids: Vec<_> = ids.into_iter().map(|e| e.id_tx).collect();
|
||||
let mut client = connect_lightwalletd(&ld_url).await?;
|
||||
retrieve_tx_info(coin_type, &mut client, &db_path2, &ids).await?;
|
||||
}
|
||||
log::info!("Transaction Details : {}", start.elapsed().as_millis());
|
||||
|
||||
let (new_tree, new_witnesses) = bp.finalize();
|
||||
tree = new_tree;
|
||||
witnesses = new_witnesses;
|
||||
|
||||
|
||||
*/
|
||||
if let Some(dec_block) = dec_blocks.last() {
|
||||
{
|
||||
let block = &dec_block.compact_block;
|
||||
let mut db_transaction = db.begin_transaction()?;
|
||||
let height = block.height as u32;
|
||||
// for w in witnesses.iter() {
|
||||
// DbAdapter::store_witnesses(&db_transaction, w, height, w.id_note)?;
|
||||
// }
|
||||
// DbAdapter::store_block(
|
||||
// &mut db_transaction,
|
||||
// height,
|
||||
// &block.hash,
|
||||
// block.time,
|
||||
// &tree,
|
||||
// None,
|
||||
// )?;
|
||||
db_transaction.commit()?;
|
||||
// db_transaction is dropped here
|
||||
}
|
||||
progress.height = dec_block.height;
|
||||
log::info!("progress: {}", progress.height);
|
||||
let callback = proc_callback.lock().await;
|
||||
callback(progress.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let callback = progress_callback.lock().await;
|
||||
callback(progress);
|
||||
|
||||
db.purge_old_witnesses(end_height - 100)?;
|
||||
|
||||
Ok::<_, anyhow::Error>(())
|
||||
});
|
||||
|
||||
let res = tokio::try_join!(downloader, decryptor, processor);
|
||||
match res {
|
||||
Ok((d, dc, p)) => {
|
||||
if let Err(err) = d {
|
||||
log::info!("Downloader error = {}", err);
|
||||
return Err(err);
|
||||
}
|
||||
if let Err(err) = dc {
|
||||
log::info!("Decryptor error = {}", err);
|
||||
return Err(err);
|
||||
}
|
||||
if let Err(err) = p {
|
||||
log::info!("Processor error = {}", err);
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::info!("Sync error = {}", err);
|
||||
if err.is_panic() {
|
||||
panic::resume_unwind(err.into_panic());
|
||||
}
|
||||
anyhow::bail!("Join Error");
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("Sync completed");
|
||||
|
||||
|
||||
*/
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn latest_height(ld_url: &str) -> anyhow::Result<u32> {
|
||||
let mut client = connect_lightwalletd(ld_url).await?;
|
||||
let height = get_latest_height(&mut client).await?;
|
||||
|
@ -518,7 +170,7 @@ pub fn trial_decrypt_one(
|
|||
let mut vks = HashMap::new();
|
||||
let fvk =
|
||||
decode_extended_full_viewing_key(network.hrp_sapling_extended_full_viewing_key(), &fvk)
|
||||
.map_err(|e| anyhow!("Bech32 Decode Error"))?;
|
||||
.map_err(|_| anyhow!("Bech32 Decode Error"))?;
|
||||
let ivk = fvk.fvk.vk.ivk();
|
||||
vks.insert(
|
||||
0,
|
||||
|
|
Loading…
Reference in New Issue