Skip to content

Commit

Permalink
Merge pull request #334 from SaitoTech/feature/on-chain-reorganizatio…
Browse files Browse the repository at this point in the history
…n_update

Feature: on chain reorganization update
  • Loading branch information
trevelyan authored Jan 7, 2022
2 parents af96015 + 43a17d1 commit f56b108
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,32 @@ impl Blockchain {
" ... blockchain.add_block_success: {:?}",
create_timestamp()
);
let block_id;
// print blockring longest_chain_block_hash infor
self.print();

//
// save to disk
//
{
let block = self.get_mut_block(&block_hash).await;
block_id = block.get_id();
if block.get_block_type() != BlockType::Header {
Storage::write_block_to_disk(block);
}
}

//
// TODO: clean up mempool - I think we shouldn't cleanup mempool here.
// because that's already happening in send_blocks_to_blockchain
// So who is in charge here?
// is send_blocks_to_blockchain calling add_block or
// is blockchain calling mempool.on_chain_reorganization?
//
//
// mempool.delete_transactions(&block.get_transactions());

//
// propagate block to network
//
if self.broadcast_channel_sender.is_some() {
self.broadcast_channel_sender
.as_ref()
Expand All @@ -393,15 +407,15 @@ impl Blockchain {
trace!(" ... block save done: {:?}", create_timestamp());

//
// update_genesis_period and prune old data
// update_genesis_period and prune old data - MOVED to on_chain_reorganization()
//
self.update_genesis_period().await;
// self.update_genesis_period().await;

//
// fork id
// fork id - MOVED to on_chain_reorganization()
//
let fork_id = self.generate_fork_id(block_id);
self.set_fork_id(fork_id);
// let fork_id = self.generate_fork_id(block_id);
// self.set_fork_id(fork_id);

//
// ensure pruning of next block OK will have the right CVs
Expand Down Expand Up @@ -953,6 +967,7 @@ impl Blockchain {

// utxoset update
block.on_chain_reorganization(&mut self.utxoset, true);

trace!(" ... before blockring ocr: {:?}", create_timestamp());

// blockring update
Expand All @@ -976,6 +991,9 @@ impl Blockchain {
trace!(" ... wallet processing stop: {}", create_timestamp());
}

let block_id = block.get_id();
self.on_chain_reorganization(block_id, true).await;

//
// we cannot pass the UTXOSet into the staking object to update as that would
// require multiple mutable borrows of the blockchain object, so we receive
Expand Down Expand Up @@ -1176,6 +1194,31 @@ impl Blockchain {
}
}

/// keeps any blockchain variables like fork_id or genesis_period
/// tracking variables updated as the chain gets new blocks. also
/// pre-loads any blocks needed to improve performance.
async fn on_chain_reorganization(&mut self, block_id: u64, longest_chain: bool) {
// skip out if earlier than we need to be vis-a-vis last_block_id
if self.get_latest_block_id() >= block_id {
return;
}

if longest_chain {
// update genesis period, purge old data
//
self.update_genesis_period().await;

//
// generate fork_id
//
//
let fork_id = self.generate_fork_id(block_id);
self.set_fork_id(fork_id);
}

self.downgrade_blockchain_data().await;
}

pub async fn update_genesis_period(&mut self) {
//
// we need to make sure this is not a random block that is disconnected
Expand Down Expand Up @@ -1203,7 +1246,8 @@ impl Blockchain {
self.delete_blocks(purge_bid).await;
}

self.downgrade_blockchain_data().await;
//TODO: we already had in update_genesis_period() in self method - maybe no need to call here?
// self.downgrade_blockchain_data().await;
}

//
Expand Down

0 comments on commit f56b108

Please sign in to comment.