Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit c2c9926

Browse files
committed
Cleanup
1 parent 537b951 commit c2c9926

File tree

6 files changed

+140
-293
lines changed

6 files changed

+140
-293
lines changed

lib/src/blaze/block_witness_data.rs

Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::{
2-
compact_formats::{CompactBlock, TreeState},
2+
compact_formats::{CompactBlock, CompactTx, TreeState},
33
grpc_connector::GrpcConnector,
44
lightclient::{
55
checkpoints::get_all_main_checkpoints,
66
lightclient_config::{LightClientConfig, MAX_REORG},
77
},
88
lightwallet::{
9-
data::{BlockData, CCompactTx, WitnessCache},
9+
data::{BlockData, WalletTx, WitnessCache},
1010
wallet_txns::WalletTxns,
1111
},
1212
};
@@ -78,7 +78,7 @@ impl BlockAndWitnessData {
7878

7979
pub async fn setup_sync(&mut self, existing_blocks: Vec<BlockData>, verified_tree: Option<TreeState>) {
8080
if !existing_blocks.is_empty() {
81-
if existing_blocks.first().unwrap().height() < existing_blocks.last().unwrap().height() {
81+
if existing_blocks.first().unwrap().height < existing_blocks.last().unwrap().height {
8282
panic!("Blocks are in wrong order");
8383
}
8484
}
@@ -105,16 +105,20 @@ impl BlockAndWitnessData {
105105
}
106106
}
107107

108-
pub async fn get_ctx_for_nf_at_height(&self, nullifier: &Nullifier, height: u64) -> (CCompactTx, u32) {
108+
pub async fn get_ctx_for_nf_at_height(&self, nullifier: &Nullifier, height: u64) -> (CompactTx, u32) {
109109
self.wait_for_block(height).await;
110110

111-
let blocks = self.blocks.read().await;
112-
let pos = blocks.first().unwrap().height() - height;
113-
let cb = blocks.get(pos as usize).unwrap();
111+
let cb = {
112+
let blocks = self.blocks.read().await;
113+
let pos = blocks.first().unwrap().height - height;
114+
let bd = blocks.get(pos as usize).unwrap();
115+
116+
bd.cb()
117+
};
114118

115-
for ctx in &cb.txns {
116-
for nf in &ctx.nfs {
117-
if nf.to_vec() == nullifier.to_vec() {
119+
for ctx in &cb.vtx {
120+
for cs in &ctx.spends {
121+
if cs.nf == nullifier.to_vec() {
118122
return (ctx.clone(), cb.time);
119123
}
120124
}
@@ -209,7 +213,7 @@ impl BlockAndWitnessData {
209213
{
210214
let blocks = blocks.read().await;
211215

212-
let top_block = blocks.first().unwrap().height();
216+
let top_block = blocks.first().unwrap().height;
213217
let start_pos = (top_block - ct.height - 1) as usize;
214218
let end_pos = (top_block - vt.height) as usize;
215219

@@ -219,10 +223,10 @@ impl BlockAndWitnessData {
219223
}
220224

221225
for i in (end_pos..start_pos + 1).rev() {
222-
let cb = blocks.get(i as usize).unwrap();
223-
for ctx in &cb.txns {
224-
for cmu in &ctx.cmus {
225-
let node = Node::new(cmu.clone());
226+
let cb = &blocks.get(i as usize).unwrap().cb();
227+
for ctx in &cb.vtx {
228+
for co in &ctx.outputs {
229+
let node = Node::new(co.cmu().unwrap().into());
226230
tree.append(node).unwrap();
227231
}
228232
}
@@ -260,7 +264,7 @@ impl BlockAndWitnessData {
260264
) {
261265
// First, pop the first block (which is the top block) in the existing_blocks.
262266
let top_wallet_block = existing_blocks.write().await.drain(0..1).next().unwrap();
263-
if top_wallet_block.height() != reorg_height {
267+
if top_wallet_block.height != reorg_height {
264268
panic!("Wrong block reorg'd");
265269
}
266270

@@ -321,7 +325,7 @@ impl BlockAndWitnessData {
321325
None
322326
} else {
323327
// send a reorg signal
324-
Some(top_block.height())
328+
Some(top_block.height)
325329
}
326330
}
327331
None => {
@@ -371,13 +375,13 @@ impl BlockAndWitnessData {
371375
//info!("Waiting for first block, blocks are empty!");
372376
}
373377

374-
self.blocks.read().await.first().unwrap().height()
378+
self.blocks.read().await.first().unwrap().height
375379
}
376380

377381
async fn wait_for_block(&self, height: u64) {
378382
self.wait_for_first_block().await;
379383

380-
while self.blocks.read().await.last().unwrap().height() > height {
384+
while self.blocks.read().await.last().unwrap().height > height {
381385
yield_now().await;
382386
sleep(Duration::from_millis(100)).await;
383387

@@ -395,15 +399,15 @@ impl BlockAndWitnessData {
395399
{
396400
// Read Lock
397401
let blocks = self.blocks.read().await;
398-
let pos = blocks.first().unwrap().height() - after_height;
402+
let pos = blocks.first().unwrap().height - after_height;
399403
let nf = nf.to_vec();
400404

401405
for i in (0..pos + 1).rev() {
402-
let cb = blocks.get(i as usize).unwrap();
403-
for ctx in &cb.txns {
404-
for s_nf in &ctx.nfs {
405-
if s_nf.to_vec() == nf {
406-
return Some(cb.height());
406+
let cb = &blocks.get(i as usize).unwrap().cb();
407+
for ctx in &cb.vtx {
408+
for cs in &ctx.spends {
409+
if cs.nf == nf {
410+
return Some(cb.height);
407411
}
408412
}
409413
}
@@ -419,8 +423,8 @@ impl BlockAndWitnessData {
419423

420424
{
421425
let blocks = self.blocks.read().await;
422-
let pos = blocks.first().unwrap().height() - height;
423-
blocks.get(pos as usize).unwrap().time
426+
let pos = blocks.first().unwrap().height - height;
427+
blocks.get(pos as usize).unwrap().cb().time
424428
}
425429
}
426430

@@ -451,10 +455,12 @@ impl BlockAndWitnessData {
451455
self.wait_for_block(height).await;
452456

453457
{
454-
let blocks = self.blocks.read().await;
458+
let mut blocks = self.blocks.write().await;
455459

456-
let pos = blocks.first().unwrap().height() - height;
457-
blocks.get(pos as usize).unwrap().clone()
460+
let pos = blocks.first().unwrap().height - height;
461+
let bd = blocks.get_mut(pos as usize).unwrap();
462+
463+
bd.cb()
458464
}
459465
};
460466

@@ -463,9 +469,9 @@ impl BlockAndWitnessData {
463469

464470
// Go over all the outputs. Remember that all the numbers are inclusive, i.e., we have to scan upto and including
465471
// block_height, tx_num and output_num
466-
for (t_num, ctx) in cb.txns.iter().enumerate() {
467-
for (o_num, cmu) in ctx.cmus.iter().enumerate() {
468-
let node = Node::new(cmu.clone());
472+
for (t_num, ctx) in cb.vtx.iter().enumerate() {
473+
for (o_num, co) in ctx.outputs.iter().enumerate() {
474+
let node = Node::new(co.cmu().unwrap().into());
469475
tree.append(node).unwrap();
470476
if t_num == tx_num && o_num == output_num {
471477
return Ok(IncrementalWitness::from_tree(&tree));
@@ -490,18 +496,18 @@ impl BlockAndWitnessData {
490496

491497
let top_block = {
492498
let mut blocks = self.blocks.read().await;
493-
let top_block = blocks.first().unwrap().height();
499+
let top_block = blocks.first().unwrap().height;
494500
let pos = top_block - height;
495501

496502
// Get the last witness, and then use that.
497503
let mut w = witnesses.last().unwrap().clone();
498504
witnesses.into_fsb(&mut fsb);
499505

500506
for i in (0..pos + 1).rev() {
501-
let cb = &blocks.get(i as usize).unwrap();
502-
for ctx in &cb.txns {
503-
for cmu in &ctx.cmus {
504-
let node = Node::new(cmu.clone());
507+
let cb = &blocks.get(i as usize).unwrap().cb();
508+
for ctx in &cb.vtx {
509+
for co in &ctx.outputs {
510+
let node = Node::new(co.cmu().unwrap().into());
505511
w.append(node).unwrap();
506512
}
507513
}
@@ -539,21 +545,21 @@ impl BlockAndWitnessData {
539545

540546
{
541547
let blocks = self.blocks.read().await;
542-
let pos = blocks.first().unwrap().height() - height;
548+
let pos = blocks.first().unwrap().height - height;
543549

544550
let mut txid_found = false;
545551
let mut output_found = false;
546552

547-
let cb = &blocks.get(pos as usize).unwrap();
548-
for ctx in &cb.txns {
549-
if !txid_found && ctx.hash == txid.0 {
553+
let cb = &blocks.get(pos as usize).unwrap().cb();
554+
for ctx in &cb.vtx {
555+
if !txid_found && WalletTx::new_txid(&ctx.hash) == *txid {
550556
txid_found = true;
551557
}
552-
for j in 0..ctx.cmus.len() as u32 {
558+
for j in 0..ctx.outputs.len() as u32 {
553559
// If we've already passed the txid and output_num, stream the results
554560
if txid_found && output_found {
555-
let cmu = ctx.cmus.get(j as usize).unwrap();
556-
let node = Node::new(cmu.clone());
561+
let co = ctx.outputs.get(j as usize).unwrap();
562+
let node = Node::new(co.cmu().unwrap().into());
557563
w.append(node).unwrap();
558564
}
559565

@@ -643,8 +649,8 @@ mod test {
643649
// Blocks are in reverse order
644650
assert!(blocks.first().unwrap().height > blocks.last().unwrap().height);
645651

646-
let start_block = blocks.first().unwrap().height();
647-
let end_block = blocks.last().unwrap().height();
652+
let start_block = blocks.first().unwrap().height;
653+
let end_block = blocks.last().unwrap().height;
648654

649655
let sync_status = Arc::new(RwLock::new(SyncStatus::default()));
650656
let mut nw = BlockAndWitnessData::new(&config, sync_status);
@@ -664,7 +670,7 @@ mod test {
664670
let send_h: JoinHandle<Result<(), String>> = tokio::spawn(async move {
665671
for block in blocks {
666672
cb_sender
667-
.send(block.to_cb())
673+
.send(block.cb())
668674
.map_err(|e| format!("Couldn't send block: {}", e))?;
669675
}
670676
if let Some(Some(_h)) = reorg_rx.recv().await {
@@ -696,8 +702,8 @@ mod test {
696702
// Use the first 50 blocks as "existing", and then sync the other 150 blocks.
697703
let existing_blocks = blocks.split_off(150);
698704

699-
let start_block = blocks.first().unwrap().height();
700-
let end_block = blocks.last().unwrap().height();
705+
let start_block = blocks.first().unwrap().height;
706+
let end_block = blocks.last().unwrap().height;
701707

702708
let mut nw = BlockAndWitnessData::new_with_batchsize(&config, 25);
703709
nw.setup_sync(existing_blocks, None).await;
@@ -716,7 +722,7 @@ mod test {
716722
let send_h: JoinHandle<Result<(), String>> = tokio::spawn(async move {
717723
for block in blocks {
718724
cb_sender
719-
.send(block.to_cb())
725+
.send(block.cb())
720726
.map_err(|e| format!("Couldn't send block: {}", e))?;
721727
}
722728
if let Some(Some(_h)) = reorg_rx.recv().await {
@@ -736,8 +742,8 @@ mod test {
736742

737743
let finished_blks = nw.finish_get_blocks(100).await;
738744
assert_eq!(finished_blks.len(), 100);
739-
assert_eq!(finished_blks.first().unwrap().height(), start_block);
740-
assert_eq!(finished_blks.last().unwrap().height(), start_block - 100 + 1);
745+
assert_eq!(finished_blks.first().unwrap().height, start_block);
746+
assert_eq!(finished_blks.last().unwrap().height, start_block - 100 + 1);
741747
}
742748

743749
#[tokio::test]
@@ -767,37 +773,33 @@ mod test {
767773
OsRng.fill_bytes(&mut hash);
768774

769775
if i == 0 {
770-
let mut cb = blocks.pop().unwrap();
771-
cb.prev_hash = hash;
772-
blocks.push(cb);
776+
let mut cb = blocks.pop().unwrap().cb();
777+
cb.prev_hash = hash.to_vec();
778+
blocks.push(BlockData::new(cb));
773779
} else {
774-
let mut cb = reorged_blocks[i - 1].clone();
775-
cb.prev_hash = hash;
776-
reorged_blocks[i - 1] = cb;
780+
let mut cb = reorged_blocks[i - 1].cb();
781+
cb.prev_hash = hash.to_vec();
782+
reorged_blocks[i - 1] = BlockData::new(cb);
777783
}
778784

779-
let mut cb = reorged_blocks[i].clone();
780-
cb.hash = hash;
781-
reorged_blocks[i] = cb;
785+
let mut cb = reorged_blocks[i].cb();
786+
cb.hash = hash.to_vec();
787+
reorged_blocks[i] = BlockData::new(cb);
782788
}
783789
{
784-
let mut cb = reorged_blocks[4].clone();
785-
let mut prev_hash_bytes = [0u8; 32];
786-
prev_hash_bytes.copy_from_slice(
787-
existing_blocks
788-
.iter()
789-
.find(|b| b.height() == 45)
790-
.unwrap()
791-
.to_cb()
792-
.hash
793-
.as_slice(),
794-
);
795-
cb.prev_hash = prev_hash_bytes;
796-
reorged_blocks[4] = cb;
790+
let mut cb = reorged_blocks[4].cb();
791+
cb.prev_hash = existing_blocks
792+
.iter()
793+
.find(|b| b.height == 45)
794+
.unwrap()
795+
.cb()
796+
.hash
797+
.to_vec();
798+
reorged_blocks[4] = BlockData::new(cb);
797799
}
798800

799-
let start_block = blocks.first().unwrap().height();
800-
let end_block = blocks.last().unwrap().height();
801+
let start_block = blocks.first().unwrap().height;
802+
let end_block = blocks.last().unwrap().height;
801803

802804
let sync_status = Arc::new(RwLock::new(SyncStatus::default()));
803805
let mut nw = BlockAndWitnessData::new(&config, sync_status);
@@ -818,7 +820,7 @@ mod test {
818820
// Send the normal blocks
819821
for block in blocks {
820822
cb_sender
821-
.send(block.to_cb())
823+
.send(block.cb())
822824
.map_err(|e| format!("Couldn't send block: {}", e))?;
823825
}
824826

@@ -833,7 +835,7 @@ mod test {
833835
sent_ctr += 1;
834836

835837
cb_sender
836-
.send(reorged_blocks.drain(0..1).next().unwrap().to_cb())
838+
.send(reorged_blocks.drain(0..1).next().unwrap().cb())
837839
.map_err(|e| format!("Couldn't send block: {}", e))?;
838840
}
839841

@@ -854,13 +856,13 @@ mod test {
854856

855857
let finished_blks = nw.finish_get_blocks(100).await;
856858
assert_eq!(finished_blks.len(), 100);
857-
assert_eq!(finished_blks.first().unwrap().height(), start_block);
858-
assert_eq!(finished_blks.last().unwrap().height(), start_block - 100 + 1);
859+
assert_eq!(finished_blks.first().unwrap().height, start_block);
860+
assert_eq!(finished_blks.last().unwrap().height, start_block - 100 + 1);
859861

860862
// Verify the hashes
861863
for i in 0..(finished_blks.len() - 1) {
862-
assert_eq!(finished_blks[i].prev_hash, finished_blks[i + 1].hash);
863-
assert_eq!(finished_blks[i].hash(), finished_blks[i].to_cb().hash().to_string());
864+
assert_eq!(finished_blks[i].cb().prev_hash, finished_blks[i + 1].cb().hash);
865+
assert_eq!(finished_blks[i].hash(), finished_blks[i].cb().hash().to_string());
864866
}
865867
}
866868
}

lib/src/blaze/update_notes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lightwallet::wallet_txns::WalletTxns;
1+
use crate::lightwallet::{data::WalletTx, wallet_txns::WalletTxns};
22
use std::sync::Arc;
33

44
use futures::future::join_all;
@@ -135,7 +135,7 @@ impl UpdateNotes {
135135
.get_ctx_for_nf_at_height(&nf, spent_height)
136136
.await;
137137

138-
let spent_txid = TxId { 0: ctx.hash };
138+
let spent_txid = WalletTx::new_txid(&ctx.hash);
139139
let spent_at_height = BlockHeight::from_u32(spent_height as u32);
140140

141141
// Mark this note as being spent

lib/src/lightclient/lightclient_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{grpc_connector::GrpcConnector, lightclient::checkpoints};
2525
pub const DEFAULT_SERVER: &str = "https://lwdv3.zecwallet.co";
2626
pub const WALLET_NAME: &str = "zecwallet-light-wallet.dat";
2727
pub const LOGFILE_NAME: &str = "zecwallet-light-wallet.debug.log";
28-
pub const ANCHOR_OFFSET: [u32; 5] = [9, 4, 2, 1, 0];
28+
pub const ANCHOR_OFFSET: [u32; 5] = [4, 0, 0, 0, 0];
2929
pub const MAX_REORG: usize = 100;
3030
pub const GAP_RULE_UNUSED_ADDRESSES: usize = if cfg!(any(target_os = "ios", target_os = "android")) {
3131
0

0 commit comments

Comments
 (0)