Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zingo sync scan transaction #1331

Merged
merged 28 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
855de74
prep for decryting bundles
Oscar-Pepper Aug 19, 2024
8e64075
added shielded bundle scanning
Oscar-Pepper Aug 20, 2024
e9c91c5
finished scan transaction for shielded bundles
Oscar-Pepper Aug 20, 2024
e6da6bb
Merge branch 'dev' into zingo_sync_scan_transaction
Oscar-Pepper Aug 21, 2024
c1108c9
rename fn to scan_notes
Oscar-Pepper Aug 21, 2024
4d2f8df
Merge branch 'dev' into zingo_sync_scan_transaction
Oscar-Pepper Aug 26, 2024
09ac2a3
Merge branch 'dev' into zingo_sync_scan_transaction
fluidvanadium Aug 30, 2024
b63d8c1
Merge branch 'dev' into zingo_sync_scan_transaction
zancas Oct 18, 2024
959fdea
add Cargo.lock
zancas Oct 18, 2024
9bfd1e6
reference qualified TxId behind sync fature
zancas Oct 18, 2024
74cccd0
fix compile protos fn name
zancas Oct 18, 2024
c6df7bf
Merge branch 'dev' into zingo_sync_scan_transaction
zancas Oct 19, 2024
0040fe5
readd Cargo.lock
zancas Oct 19, 2024
4ccc404
Merge remote-tracking branch 'zingolabs/dev' into zingo_sync_scan_tra…
zancas Oct 24, 2024
0870598
cargo update
zancas Oct 24, 2024
8e10848
mainnet test runs (if unignored) sync_test is unignored
zancas Oct 25, 2024
8ce1cf6
parameters is too vague
zancas Oct 25, 2024
14cd005
call them consensus parameters and add question about the uniqueness …
zancas Oct 25, 2024
61e429b
Merge branch 'dev' into zingo_sync_scan_transaction
zancas Oct 28, 2024
27a6282
Merge branch 'dev' into zingo_sync_scan_transaction
zancas Oct 30, 2024
6672eaf
make Parameters be consensus::Parameters
zancas Oct 30, 2024
688ba75
Merge branch 'dev' into zingo_sync_scan_transaction
zancas Oct 30, 2024
6c0da5d
make Parameters have explicit name
zancas Oct 31, 2024
378081e
there's a single fetch_handle, rm unnecessary try_join_all
zancas Nov 12, 2024
4eda547
prefer consensus_parameters
zancas Nov 12, 2024
7579485
add question about height comparison
zancas Nov 12, 2024
da2be52
cargo update, and new lock
zancas Nov 12, 2024
2a2459c
Merge remote-tracking branch 'zingolabs/dev' into zingo_sync_scan_tra…
zancas Nov 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
409 changes: 334 additions & 75 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions libtonode-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ tempfile.workspace = true
tracing-subscriber.workspace = true
proptest.workspace = true
bech32 = "0.11.0"

[dev-dependencies]
rustls.workspace = true
4 changes: 4 additions & 0 deletions libtonode-tests/tests/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use zingolib::{
#[ignore = "too slow, and flakey"]
#[tokio::test]
async fn sync_mainnet_test() {
rustls::crypto::ring::default_provider()
.install_default()
.expect("Ring to work as a default");
tracing_subscriber::fmt().init();

let uri = construct_lightwalletd_uri(Some(DEFAULT_LIGHTWALLETD_SERVER.to_string()));
Expand Down Expand Up @@ -62,6 +65,7 @@ async fn sync_test() {
.await
.unwrap();

dbg!(recipient.wallet.wallet_transactions());
dbg!(recipient.wallet.wallet_blocks());
dbg!(recipient.wallet.nullifier_map());
dbg!(recipient.wallet.sync_state());
Expand Down
34 changes: 27 additions & 7 deletions zingo-sync/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

use std::ops::Range;

use tokio::sync::{mpsc::UnboundedSender, oneshot};

use zcash_client_backend::{
data_api::chain::ChainState,
proto::{
compact_formats::CompactBlock,
service::{BlockId, TreeState},
},
};
use zcash_primitives::consensus::BlockHeight;

use tokio::sync::{mpsc::UnboundedSender, oneshot};
use zcash_primitives::{
consensus::BlockHeight,
transaction::{Transaction, TxId},
};

pub mod fetch;

Expand All @@ -26,6 +29,8 @@ pub enum FetchRequest {
CompactBlockRange(oneshot::Sender<Vec<CompactBlock>>, Range<BlockHeight>),
/// Gets the tree states for a specified block height..
TreeState(oneshot::Sender<TreeState>, BlockHeight),
/// Get a full transaction by txid.
Transaction(oneshot::Sender<(Transaction, BlockHeight)>, TxId),
}

/// Gets the height of the blockchain from the server.
Expand All @@ -34,7 +39,7 @@ pub enum FetchRequest {
pub async fn get_chain_height(
fetch_request_sender: UnboundedSender<FetchRequest>,
) -> Result<BlockHeight, ()> {
let (sender, receiver) = oneshot::channel::<BlockId>();
let (sender, receiver) = oneshot::channel();
fetch_request_sender
.send(FetchRequest::ChainTip(sender))
.unwrap();
Expand All @@ -49,22 +54,22 @@ pub async fn get_compact_block_range(
fetch_request_sender: UnboundedSender<FetchRequest>,
block_range: Range<BlockHeight>,
) -> Result<Vec<CompactBlock>, ()> {
let (sender, receiver) = oneshot::channel::<Vec<CompactBlock>>();
let (sender, receiver) = oneshot::channel();
fetch_request_sender
.send(FetchRequest::CompactBlockRange(sender, block_range))
.unwrap();
let compact_blocks = receiver.await.unwrap();

Ok(compact_blocks)
}
/// Gets the frontiers for a specified block height..
/// Gets the frontiers for a specified block height.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub async fn get_frontiers(
fetch_request_sender: UnboundedSender<FetchRequest>,
block_height: BlockHeight,
) -> Result<ChainState, ()> {
let (sender, receiver) = oneshot::channel::<TreeState>();
let (sender, receiver) = oneshot::channel();
fetch_request_sender
.send(FetchRequest::TreeState(sender, block_height))
.unwrap();
Expand All @@ -73,3 +78,18 @@ pub async fn get_frontiers(

Ok(frontiers)
}
/// Gets a full transaction for a specified txid.
///
/// Requires [`crate::client::fetch::fetch`] to be running concurrently, connected via the `fetch_request` channel.
pub async fn get_transaction_and_block_height(
fetch_request_sender: UnboundedSender<FetchRequest>,
txid: TxId,
) -> Result<(Transaction, BlockHeight), ()> {
let (sender, receiver) = oneshot::channel();
fetch_request_sender
.send(FetchRequest::Transaction(sender, txid))
.unwrap();
let transaction_and_block_height = receiver.await.unwrap();

Ok(transaction_and_block_height)
}
59 changes: 47 additions & 12 deletions zingo-sync/src/client/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use zcash_client_backend::proto::{
compact_formats::CompactBlock,
service::{
compact_tx_streamer_client::CompactTxStreamerClient, BlockId, BlockRange, ChainSpec,
TreeState,
TreeState, TxFilter,
},
};
use zcash_primitives::consensus::BlockHeight;
use zcash_primitives::{
consensus::{self, BlockHeight, BranchId},
transaction::{Transaction, TxId},
};

use crate::client::FetchRequest;

Expand All @@ -24,6 +27,7 @@ use crate::client::FetchRequest;
pub async fn fetch(
mut fetch_request_receiver: UnboundedReceiver<FetchRequest>,
mut client: CompactTxStreamerClient<zingo_netutils::UnderlyingService>,
consensus_parameters: impl consensus::Parameters,
) -> Result<(), ()> {
let mut fetch_request_queue: Vec<FetchRequest> = Vec::new();

Expand All @@ -37,7 +41,9 @@ pub async fn fetch(
let fetch_request = select_fetch_request(&mut fetch_request_queue);

if let Some(request) = fetch_request {
fetch_from_server(&mut client, request).await.unwrap();
fetch_from_server(&mut client, &consensus_parameters, request)
.await
.unwrap();
}
}
}
Expand Down Expand Up @@ -91,40 +97,46 @@ fn select_fetch_request(fetch_request_queue: &mut Vec<FetchRequest>) -> Option<F
//
async fn fetch_from_server(
client: &mut CompactTxStreamerClient<zingo_netutils::UnderlyingService>,
parameters: &impl consensus::Parameters,
fetch_request: FetchRequest,
) -> Result<(), ()> {
match fetch_request {
FetchRequest::ChainTip(sender) => {
tracing::info!("Fetching chain tip.");
let block_id = get_latest_block(client).await;
let block_id = get_latest_block(client).await.unwrap();
sender.send(block_id).unwrap();
}
FetchRequest::CompactBlockRange(sender, block_range) => {
tracing::info!("Fetching compact blocks. {:?}", &block_range);
let compact_blocks = get_block_range(client, block_range).await;
let compact_blocks = get_block_range(client, block_range).await.unwrap();
sender.send(compact_blocks).unwrap();
}
FetchRequest::TreeState(sender, block_height) => {
tracing::info!("Fetching tree state. {:?}", &block_height);
let tree_state = get_tree_state(client, block_height).await;
let tree_state = get_tree_state(client, block_height).await.unwrap();
sender.send(tree_state).unwrap();
}
FetchRequest::Transaction(sender, txid) => {
tracing::info!("Fetching transaction. {:?}", txid);
let transaction = get_transaction(client, parameters, txid).await.unwrap();
sender.send(transaction).unwrap();
}
}

Ok(())
}

async fn get_latest_block(
client: &mut CompactTxStreamerClient<zingo_netutils::UnderlyingService>,
) -> BlockId {
) -> Result<BlockId, ()> {
let request = tonic::Request::new(ChainSpec {});

client.get_latest_block(request).await.unwrap().into_inner()
Ok(client.get_latest_block(request).await.unwrap().into_inner())
}
async fn get_block_range(
client: &mut CompactTxStreamerClient<zingo_netutils::UnderlyingService>,
block_range: Range<BlockHeight>,
) -> Vec<CompactBlock> {
) -> Result<Vec<CompactBlock>, ()> {
let mut compact_blocks: Vec<CompactBlock> =
Vec::with_capacity(u64::from(block_range.end - block_range.start) as usize);

Expand All @@ -144,16 +156,39 @@ async fn get_block_range(
compact_blocks.push(compact_block);
}

compact_blocks
Ok(compact_blocks)
}
async fn get_tree_state(
client: &mut CompactTxStreamerClient<zingo_netutils::UnderlyingService>,
block_height: BlockHeight,
) -> TreeState {
) -> Result<TreeState, ()> {
let request = tonic::Request::new(BlockId {
height: block_height.into(),
hash: vec![],
});

client.get_tree_state(request).await.unwrap().into_inner()
Ok(client.get_tree_state(request).await.unwrap().into_inner())
}

async fn get_transaction(
client: &mut CompactTxStreamerClient<zingo_netutils::UnderlyingService>,
parameters: &impl consensus::Parameters,
txid: TxId,
) -> Result<(Transaction, BlockHeight), ()> {
let request = tonic::Request::new(TxFilter {
block: None,
index: 0,
hash: txid.as_ref().to_vec(),
});

let raw_transaction = client.get_transaction(request).await.unwrap().into_inner();
let block_height = BlockHeight::from_u32(raw_transaction.height as u32);

let transaction = Transaction::read(
&raw_transaction.data[..],
BranchId::for_height(parameters, block_height),
)
.unwrap();

Ok((transaction, block_height))
}
2 changes: 1 addition & 1 deletion zingo-sync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//! Entrypoint: [`crate::sync::sync`]

pub mod client;
pub mod interface;
pub(crate) mod keys;
#[allow(missing_docs)]
pub mod primitives;
pub(crate) mod scan;
pub mod sync;
pub mod traits;
pub mod witness;
Loading