Skip to content

Commit

Permalink
Bugfix: recover from utreexo nodes hangup (#100)
Browse files Browse the repository at this point in the history
If all our utreexo connections are lost, and we take too long to
recover, we might download blocks out-of-order and mess our acc up.
This commit makes sure blocks are accepted in only in order.
  • Loading branch information
Davidson-Souza authored Nov 29, 2023
1 parent 2e3eb6e commit e51cb9a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,15 @@ impl<PersistedState: ChainStore> UpdatableChainstate for ChainState<PersistedSta
| DiskBlockHeader::InvalidChain(_) => return Ok(0),
DiskBlockHeader::HeadersOnly(_, height) => height,
};

// Check if this block is the next one in our chain, if we try
// to add them out-of-order, we'll have consensus issues with our
// accumulator
let expected_height = self.get_validation_index()? + 1;
if height != expected_height {
return Ok(height);
}

self.validate_block(block, height, inputs)?;
let acc = Consensus::update_acc(&self.acc(), block, height, proof, del_hashes)?;
let ibd = self.is_in_idb();
Expand Down
1 change: 0 additions & 1 deletion crates/floresta-chain/src/pruned_utreexo/chainstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ impl ChainStore for KvChainStore {
// Flush the default bucket with meta-info
let bucket = self.0.bucket::<&[u8], Vec<u8>>(None)?;
bucket.flush()?;

Ok(())
}
fn save_header(&self, header: &DiskBlockHeader) -> Result<(), Self::Error> {
Expand Down
9 changes: 9 additions & 0 deletions crates/floresta-wire/src/p2p_wire/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ where
{
try_and_log!(self.handle_notification(notification).await);
}

if *kill_signal.read().await {
self.shutdown().await;
break;
Expand Down Expand Up @@ -1324,6 +1325,14 @@ where
return Err(WireError::PeerMisbehaving);
}

let validation_index = self.chain.get_validation_index()?;
let validation_hash = self.chain.get_block_hash(validation_index)?;

// We've downloaded a block that's not the next we need, ignore it for now
if validation_hash != block.block.header.prev_blockhash {
return Ok(());
}

let (proof, del_hashes, inputs) = Self::process_proof(
&block.udata.unwrap(),
&block.block.txdata,
Expand Down

0 comments on commit e51cb9a

Please sign in to comment.