Skip to content

Commit

Permalink
feat: allow syncing of uncles (#84)
Browse files Browse the repository at this point in the history
Description
---
This allows he syncing of uncles and make sure we ask and return the
uncles as well when we sync
  • Loading branch information
SWvheerden authored Oct 18, 2024
1 parent bfc05d9 commit 23e7137
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/sharechain/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,15 @@ impl InMemoryShareChain {
if p2_chain.get_parent_block(&block).is_none() {
return Err(Self::try_calculate_last_known_parent(p2_chain, block));
}
// lets check the uncles
for uncle in block.uncles.iter() {
let _block = p2_chain
.get_at_height(uncle.0)
.ok_or(Error::BlockParentDoesNotExist { num_missing_parents: 3 })?
.blocks
.get(&uncle.1)
.ok_or(Error::BlockParentDoesNotExist { num_missing_parents: 3 })?;
}

// this is safe as we already checked it does exist
let tip_height = p2_chain.get_tip().unwrap().height;
Expand Down Expand Up @@ -369,7 +378,6 @@ impl ShareChain for InMemoryShareChain {
let mut p2_chain_write_lock = self.p2_chain.write().await;

let mut blocks = blocks.to_vec();
blocks.reverse();

for block in blocks {
match self
Expand Down Expand Up @@ -424,16 +432,12 @@ impl ShareChain for InMemoryShareChain {
(MAIN_REWARD_SHARE, new_tip_block.miner_coinbase_extra.clone()),
);
for uncle in new_tip_block.uncles.iter() {
let uncle_block = match chain_read_lock
let uncle_block = chain_read_lock
.get_at_height(uncle.0)
.ok_or_else(|| Error::UncleBlockNotFound)?
.blocks
.get(&uncle.1)
{
Some(v) => v,
None => continue,
};
// .ok_or_else(|| Error::UncleBlockNotFound)?;
.ok_or_else(|| Error::UncleBlockNotFound)?;
miners_to_shares.insert(
uncle_block.miner_wallet_address.to_base58(),
(UNCLE_REWARD_SHARE, uncle_block.miner_coinbase_extra.clone()),
Expand Down Expand Up @@ -560,10 +564,33 @@ impl ShareChain for InMemoryShareChain {
return Ok(res);
}

for height in (from_height..=tip_height).rev() {
// lets look for uncles and make sure we return those as well
for height in from_height..from_height + 3 {
let main_block = p2_chain_read_lock
.get_at_height(height)
.ok_or(Error::BlockLevelNotFound)?
.block_in_main_chain()
.ok_or(Error::BlockNotFound)?;
for uncle in main_block.uncles.iter() {
// we only care about uncles here that are less than from height, as the rest will be added below
if uncle.0 < from_height {
res.push(
p2_chain_read_lock
.get_at_height(uncle.0)
.ok_or(Error::BlockLevelNotFound)?
.blocks
.get(&uncle.1)
.ok_or(Error::BlockNotFound)?
.clone(),
);
}
}
}

for height in from_height..=tip_height {
p2_chain_read_lock
.get_at_height(height)
.ok_or(Error::BlockNotFound)?
.ok_or(Error::BlockLevelNotFound)?
.blocks
.iter()
.for_each(|(_, block)| {
Expand Down

0 comments on commit 23e7137

Please sign in to comment.