Skip to content

Commit

Permalink
get_block_range running, more testing needed..
Browse files Browse the repository at this point in the history
  • Loading branch information
idky137 committed Jun 14, 2024
1 parent 0e612aa commit 9498f72
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 116 deletions.
33 changes: 22 additions & 11 deletions integration-tests/tests/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,21 @@ mod wallet {
)])
.await
.unwrap();
test_manager.regtest_manager.generate_n_blocks(2).unwrap();
// zingo_client
// .do_send(vec![(
// &zingolib::get_base_address!(zingo_client, "sapling"),
// 250_000,
// None,
// )])
// .await
// .unwrap();
test_manager.regtest_manager.generate_n_blocks(1).unwrap();
zingo_client.do_sync(false).await.unwrap();

tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
// test_manager.regtest_manager.generate_n_blocks(1).unwrap();
// zingo_client.do_sync(false).await.unwrap();
// tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;

let balance = zingo_client.do_balance().await;
println!("@zingoproxytest: zingo_client balance: \n{:#?}.", balance);
Expand All @@ -84,7 +95,7 @@ mod wallet {
TestManager::launch(online.clone()).await;
let zingo_client = test_manager.build_lightclient().await;

test_manager.regtest_manager.generate_n_blocks(1).unwrap();
test_manager.regtest_manager.generate_n_blocks(2).unwrap();
zingo_client.do_sync(false).await.unwrap();
zingo_client
.do_send(vec![(
Expand All @@ -94,20 +105,20 @@ mod wallet {
)])
.await
.unwrap();
zingo_client
.do_send(vec![(
&zingolib::get_base_address!(zingo_client, "transparent"),
250_000,
None,
)])
.await
.unwrap();
// zingo_client
// .do_send(vec![(
// &zingolib::get_base_address!(zingo_client, "transparent"),
// 250_000,
// None,
// )])
// .await
// .unwrap();
test_manager.regtest_manager.generate_n_blocks(1).unwrap();
zingo_client.do_sync(false).await.unwrap();
let balance = zingo_client.do_balance().await;
println!("@zingoproxytest: zingo_client balance: \n{:#?}.", balance);

assert_eq!(balance.transparent_balance.unwrap(), 500_000);
assert_eq!(balance.transparent_balance.unwrap(), 250_000);
drop_test_manager(
Some(test_manager.temp_conf_dir.path().to_path_buf()),
regtest_handler,
Expand Down
42 changes: 34 additions & 8 deletions zingo-rpc/src/blockcache/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,18 @@ impl ParseFromSlice for BlockHeaderData {
fn parse_from_slice(
data: &[u8],
txid: Option<Vec<Vec<u8>>>,
tx_version: Option<u32>,
) -> Result<(&[u8], Self), ParseError> {
if txid != None {
return Err(ParseError::InvalidData(
"txid must be None for BlockHeaderData::parse_from_slice".to_string(),
));
}
if tx_version != None {
return Err(ParseError::InvalidData(
"tx_version must be None for BlockHeaderData::parse_from_slice".to_string(),
));
}
let mut cursor = Cursor::new(data);

let version = read_i32(&mut cursor, "Error reading BlockHeaderData::version")?;
Expand Down Expand Up @@ -219,15 +225,24 @@ impl ParseFromSlice for FullBlock {
fn parse_from_slice(
data: &[u8],
txid: Option<Vec<Vec<u8>>>,
tx_version: Option<u32>,
) -> Result<(&[u8], Self), ParseError> {
let txid = txid.ok_or_else(|| {
ParseError::InvalidData("txid must be used for FullBlock::parse_from_slice".to_string())
})?;
if tx_version != None {
return Err(ParseError::InvalidData(
"tx_version must be None for FullBlock::parse_from_slice".to_string(),
));
}
let mut cursor = Cursor::new(data);

let (remaining_data, block_header_data) =
BlockHeaderData::parse_from_slice(&data[cursor.position() as usize..], None)?;
BlockHeaderData::parse_from_slice(&data[cursor.position() as usize..], None, None)?;
cursor.set_position(data.len() as u64 - remaining_data.len() as u64);

println!("Block Header decoded: {:?}", block_header_data);

let tx_count = CompactSize::read(&mut cursor)?;
if txid.len() != tx_count as usize {
return Err(ParseError::InvalidData(format!(
Expand All @@ -244,12 +259,24 @@ impl ParseFromSlice for FullBlock {
"parsing block transactions: not enough data for transaction.",
)));
}
println!(
"\nremaining data before tx: {} bytes.\n",
remaining_data.len()
);

let (new_remaining_data, tx) = FullTransaction::parse_from_slice(
&data[cursor.position() as usize..],
Some(vec![txid_item.clone()]),
None,
)?;
transactions.push(tx);
remaining_data = new_remaining_data;
cursor.set_position(data.len() as u64 - remaining_data.len() as u64);

println!(
"\nremaining data after tx: {} bytes.\n",
remaining_data.len()
);
}
let block_height = Self::get_block_height(&transactions)?;
let block_hash = block_header_data.get_hash()?;
Expand Down Expand Up @@ -298,15 +325,12 @@ impl FullBlock {

/// Decodes a hex encoded zcash full block into a FullBlock struct.
pub fn parse_full_block(data: &[u8], txid: Option<Vec<Vec<u8>>>) -> Result<Self, ParseError> {
println!(
"\nIn parse_full_block with inputs:\ndata: {:?}\n\ntxid: {:?}\n",
data, txid
);
let (remaining_data, full_block) = Self::parse_from_slice(data, txid)?;
println!("Starting Parse Full Block");
let (remaining_data, full_block) = Self::parse_from_slice(data, txid, None)?;
if remaining_data.len() != 0 {
return Err(ParseError::InvalidData(format!(
"Error decoding full block - Data Remaining: ({:?}) - Compact Block: ({:?})",
remaining_data,
"Error decoding full block - {} bytes of Remaining data. Compact Block Created: ({:?})",
remaining_data.len(),
full_block.to_compact(0, 0)
)));
}
Expand Down Expand Up @@ -360,6 +384,7 @@ impl FullBlock {
sapling_commitment_tree_size: u32,
orchard_commitment_tree_size: u32,
) -> Result<CompactBlock, ParseError> {
let test_block = Self::parse_full_block(data, txid.clone()).unwrap(); // TEST CODE REMOVE BEFORE MERGING!
Ok(Self::parse_full_block(data, txid)?
.to_compact(sapling_commitment_tree_size, orchard_commitment_tree_size)?)
}
Expand Down Expand Up @@ -391,6 +416,7 @@ pub async fn get_block_from_node(
tx,
trees,
}) => {
println!("\nTxids in Block: {:?}", tx);
let block_0 = zebrad_client.get_block(hash.0.to_string(), Some(0)).await;
match block_0 {
Ok(GetBlockResponse::Object {
Expand Down
Loading

0 comments on commit 9498f72

Please sign in to comment.