Fix Scan TAddrs

This commit is contained in:
Hanh 2023-03-13 17:49:15 +10:00
parent 09f2c10930
commit ee76725695
4 changed files with 62 additions and 63 deletions

View File

@ -271,7 +271,9 @@ struct CResult_____c_char shield_taddr(uint8_t coin,
uint64_t amount, uint64_t amount,
uint32_t confirmations); uint32_t confirmations);
struct CResult______u8 scan_transparent_accounts(uint32_t gap_limit); struct CResult______u8 scan_transparent_accounts(uint8_t coin,
uint32_t account,
uint32_t gap_limit);
struct CResult_____c_char prepare_multi_payment(uint8_t coin, struct CResult_____c_char prepare_multi_payment(uint8_t coin,
uint32_t account, uint32_t account,

View File

@ -4,7 +4,7 @@
use crate::coinconfig::CoinConfig; use crate::coinconfig::CoinConfig;
use crate::db::data_generated::fb::{ use crate::db::data_generated::fb::{
AddressBalance, AddressBalanceArgs, AddressBalanceVec, AddressBalanceVecArgs, BackupT, KeyPackT, BackupT, KeyPackT, AddressBalanceVecT, AddressBalanceT,
}; };
use crate::db::AccountData; use crate::db::AccountData;
use crate::key2::decode_key; use crate::key2::decode_key;
@ -265,35 +265,24 @@ pub async fn get_taddr_balance(coin: u8, id_account: u32) -> anyhow::Result<u64>
/// is exceeded and no balance was found /// is exceeded and no balance was found
/// # Arguments /// # Arguments
/// * `gap_limit`: number of accounts with 0 balance before the scan stops /// * `gap_limit`: number of accounts with 0 balance before the scan stops
pub async fn scan_transparent_accounts(gap_limit: usize) -> anyhow::Result<Vec<u8>> { pub async fn scan_transparent_accounts(coin: u8, account: u32, gap_limit: usize) -> anyhow::Result<AddressBalanceVecT> {
let c = CoinConfig::get_active(); let c = CoinConfig::get(coin);
let mut client = c.connect_lwd().await?; let db = c.db()?;
let addresses = let account_data = db.get_account_info(account)?;
crate::taddr::scan_transparent_accounts(c.chain.network(), &mut client, gap_limit).await?; let AccountData {
let mut builder = flatbuffers::FlatBufferBuilder::new(); seed, aindex, ..
let mut addrs = vec![]; } = account_data;
for a in addresses { let mut addresses = vec![];
let address = builder.create_string(&a.address); if let Some(seed) = seed {
let ab = AddressBalance::create( let mut client = c.connect_lwd().await?;
&mut builder, addresses.extend(crate::taddr::scan_transparent_accounts(c.chain.network(), &mut client, &seed, aindex, gap_limit).await?);
&AddressBalanceArgs {
index: a.index,
address: Some(address),
balance: a.balance,
},
);
addrs.push(ab);
} }
let addrs = builder.create_vector(&addrs); let addresses: Vec<_> = addresses.iter().map(|a| AddressBalanceT { index: a.index,
let addrs = AddressBalanceVec::create( address: Some(a.address.clone()), balance: a.balance }).collect();
&mut builder, let addresses = AddressBalanceVecT {
&AddressBalanceVecArgs { values: Some(addresses)
values: Some(addrs), };
}, Ok(addresses)
);
builder.finish(addrs, None);
let data = builder.finished_data().to_vec();
Ok(data)
} }
/// Get the backup string. It is either the passphrase, the secret key or the viewing key /// Get the backup string. It is either the passphrase, the secret key or the viewing key

View File

@ -507,9 +507,15 @@ pub async unsafe extern "C" fn shield_taddr(
#[tokio::main] #[tokio::main]
#[no_mangle] #[no_mangle]
pub async unsafe extern "C" fn scan_transparent_accounts(gap_limit: u32) -> CResult<*const u8> { pub async unsafe extern "C" fn scan_transparent_accounts(coin: u8, account: u32, gap_limit: u32) -> CResult<*const u8> {
let res = crate::api::account::scan_transparent_accounts(gap_limit as usize).await; let res = async {
to_cresult_bytes(res) let addresses = crate::api::account::scan_transparent_accounts(coin, account, gap_limit as usize).await?;
let mut builder = FlatBufferBuilder::new();
let root = addresses.pack(&mut builder);
builder.finish(root, None);
Ok(builder.finished_data().to_vec())
};
to_cresult_bytes(res.await)
} }
#[tokio::main] #[tokio::main]

View File

@ -1,6 +1,6 @@
use crate::api::payment_v2::build_tx_plan_with_utxos; use crate::api::payment_v2::build_tx_plan_with_utxos;
use crate::api::recipient::RecipientMemo; use crate::api::recipient::RecipientMemo;
use crate::chain::{get_checkpoint_height, EXPIRY_HEIGHT_OFFSET}; use crate::chain::{get_checkpoint_height, EXPIRY_HEIGHT_OFFSET, get_latest_height};
use crate::coinconfig::CoinConfig; use crate::coinconfig::CoinConfig;
use crate::db::AccountData; use crate::db::AccountData;
use crate::key2::split_key; use crate::key2::split_key;
@ -8,7 +8,7 @@ use crate::note_selection::{SecretKeys, Source, UTXO};
use crate::unified::orchard_as_unified; use crate::unified::orchard_as_unified;
use crate::{ use crate::{
broadcast_tx, build_tx, AddressList, BlockId, CompactTxStreamerClient, GetAddressUtxosArg, broadcast_tx, build_tx, AddressList, BlockId, CompactTxStreamerClient, GetAddressUtxosArg,
GetAddressUtxosReply, Hash, TransparentAddressBlockFilter, TxFilter, GetAddressUtxosReply, Hash, TransparentAddressBlockFilter, TxFilter, BlockRange,
}; };
use anyhow::anyhow; use anyhow::anyhow;
use base58check::FromBase58Check; use base58check::FromBase58Check;
@ -25,7 +25,7 @@ use tonic::transport::Channel;
use tonic::Request; use tonic::Request;
use zcash_client_backend::encoding::encode_transparent_address; use zcash_client_backend::encoding::encode_transparent_address;
use zcash_params::coin::get_branch; use zcash_params::coin::get_branch;
use zcash_primitives::consensus::{Network, Parameters}; use zcash_primitives::consensus::{Network, Parameters, NetworkUpgrade};
use zcash_primitives::legacy::TransparentAddress; use zcash_primitives::legacy::TransparentAddress;
use zcash_primitives::memo::Memo; use zcash_primitives::memo::Memo;
use zcash_primitives::transaction::components::OutPoint; use zcash_primitives::transaction::components::OutPoint;
@ -141,10 +141,11 @@ pub async fn get_ttx_history(
pub async fn get_taddr_tx_count( pub async fn get_taddr_tx_count(
client: &mut CompactTxStreamerClient<Channel>, client: &mut CompactTxStreamerClient<Channel>,
address: &str, address: &str,
range: &BlockRange,
) -> anyhow::Result<u32> { ) -> anyhow::Result<u32> {
let req = TransparentAddressBlockFilter { let req = TransparentAddressBlockFilter {
address: address.to_string(), address: address.to_string(),
range: None, range: Some(range.clone()),
}; };
let rep = client let rep = client
.get_taddress_txids(Request::new(req)) .get_taddress_txids(Request::new(req))
@ -173,39 +174,40 @@ pub async fn get_utxos(
pub async fn scan_transparent_accounts( pub async fn scan_transparent_accounts(
network: &Network, network: &Network,
client: &mut CompactTxStreamerClient<Channel>, client: &mut CompactTxStreamerClient<Channel>,
seed: &str,
mut aindex: u32,
gap_limit: usize, gap_limit: usize,
) -> anyhow::Result<Vec<TBalance>> { ) -> anyhow::Result<Vec<TBalance>> {
let c = CoinConfig::get_active(); let start: u32 = network.activation_height(NetworkUpgrade::Sapling).unwrap().into();
let end = get_latest_height(client).await?;
let range = BlockRange {
start: Some(BlockId { height: start as u64, hash: vec![] }),
end: Some(BlockId { height: end as u64, hash: vec![] }), spam_filter_threshold: 0 };
let mut addresses = vec![]; let mut addresses = vec![];
let db = c.db()?; let mut gap = 0;
let account_data = db.get_account_info(c.id_account)?; while gap < gap_limit {
let AccountData { let bip44_path = format!("m/44'/{}'/0'/0/{}", network.coin_type(), aindex);
seed, mut aindex, .. log::info!("{} {}", aindex, bip44_path);
} = account_data; let (_, address) = derive_tkeys(network, &seed, &bip44_path)?;
if let Some(seed) = seed { let balance = get_taddr_balance(client, &address).await?;
let mut gap = 0; if balance > 0 {
while gap < gap_limit { addresses.push(TBalance {
let bip44_path = format!("m/44'/{}'/0'/0/{}", network.coin_type(), aindex); index: aindex,
log::info!("{} {}", aindex, bip44_path); address,
let (_, address) = derive_tkeys(network, &seed, &bip44_path)?; balance,
let balance = get_taddr_balance(client, &address).await?; });
if balance > 0 { gap = 0;
addresses.push(TBalance { } else {
index: aindex, let tx_count = get_taddr_tx_count(client, &address, &range).await?;
address, if tx_count != 0 {
balance,
});
gap = 0; gap = 0;
} else { } else {
let tx_count = get_taddr_tx_count(client, &address).await?; gap += 1;
if tx_count != 0 {
gap = 0;
} else {
gap += 1;
}
} }
aindex += 1;
} }
aindex += 1;
} }
Ok(addresses) Ok(addresses)
} }