Skip to content

Commit

Permalink
Merge branch 'testnet2' into docker-builds
Browse files Browse the repository at this point in the history
  • Loading branch information
wcannon committed Nov 16, 2021
2 parents 2316829 + 27b77a3 commit fda8a5d
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 159 deletions.
12 changes: 6 additions & 6 deletions ledger/src/helpers/block_locators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,30 @@ impl<N: Network> FromBytes for BlockLocators<N> {
let mut block_locators = BTreeMap::new();
let mut block_headers_bytes = Vec::with_capacity(num_locators as usize);

for index in 0..num_locators {
for _ in 0..num_locators {
let height: u32 = FromBytes::read_le(&mut reader)?;
let hash: N::BlockHash = FromBytes::read_le(&mut reader)?;
let header_exists: bool = FromBytes::read_le(&mut reader)?;

if header_exists {
let mut buffer = vec![0u8; N::HEADER_SIZE_IN_BYTES];
reader.read_exact(&mut buffer)?;
block_headers_bytes.push((index, buffer));
block_headers_bytes.push((height, buffer));
};

block_locators.insert(height, (hash, None));
}

let block_headers = block_headers_bytes
.par_iter()
.flat_map(|(index, bytes)| match !bytes.is_empty() {
true => Some((index, BlockHeader::<N>::read_le(&bytes[..]).unwrap())),
.flat_map(|(height, bytes)| match !bytes.is_empty() {
true => Some((height, BlockHeader::<N>::read_le(&bytes[..]).unwrap())),
false => None,
})
.collect::<Vec<_>>();

for (index, block_header) in block_headers.into_iter() {
if let Some((_, header)) = block_locators.get_mut(index) {
for (height, block_header) in block_headers.into_iter() {
if let Some((_, header)) = block_locators.get_mut(height) {
*header = Some(block_header);
}
}
Expand Down
106 changes: 47 additions & 59 deletions ledger/src/state/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,65 +446,53 @@ impl<N: Network> LedgerState<N> {
return Ok(false);
}

// // Get the remaining block locators (excluding the genesis block).
// let remaining_block_locators = &block_locators[..block_locators.len() - 1];
// let num_block_headers = std::cmp::min(MAXIMUM_LINEAR_BLOCK_LOCATORS as usize, remaining_block_locators.len());
//
// // Check that the block headers are formed correctly (linear).
// // let mut last_block_height = remaining_block_locators[0].0 + 1;
// for (_block_height, _block_hash, block_header) in &remaining_block_locators[..num_block_headers] {
// // // Check that the block height is decrementing.
// // match last_block_height == *block_height + 1 {
// // true => last_block_height = *block_height,
// // false => return Ok(false)
// // }
//
// // Check that the block header is present.
// let _block_header = match block_header {
// Some(header) => header,
// None => return Ok(false),
// };
//
// // // Check that the expected block hash is correct.
// // if let Ok(expected_block_hash) = self.get_block_hash(*block_height) {
// // if &expected_block_hash != block_hash {
// // return Ok(false);
// // }
// // }
// //
// // // Check that the expected block headers is correct.
// // if let Ok(expected_block_header) = self.get_block_header(*block_height) {
// // if &expected_block_header != block_header {
// // return Ok(false);
// // }
// // }
// }

// // Check that the block hashes are formed correctly (power of two).
// if block_locators.len() > MAXIMUM_LINEAR_BLOCK_LOCATORS as usize {
// let mut previous_block_height = u32::MAX;
//
// for (block_height, _block_hash, block_header) in &block_locators[num_block_headers..] {
// // Check that the block heights increment by a power of two.
// if previous_block_height != u32::MAX && previous_block_height / 2 != *block_height {
// return Ok(false);
// }
//
// // Check that there is no block header.
// if block_header.is_some() {
// return Ok(false);
// }
//
// // // Check that the expected block hash is correct.
// // if let Ok(expected_block_hash) = self.get_block_hash(*block_height) {
// // if &expected_block_hash != block_hash {
// // return Ok(false);
// // }
// // }
//
// previous_block_height = *block_height;
// }
// }
let num_linear_block_headers = std::cmp::min(MAXIMUM_LINEAR_BLOCK_LOCATORS as usize, block_locators.len() - 1);
let num_quadratic_block_headers = block_locators.len().saturating_sub(num_linear_block_headers + 1);

// Check that the block headers are formed correctly (linear).
let mut last_block_height = match block_locators.keys().max() {
Some(height) => *height,
None => return Ok(false),
};

for (block_height, (_block_hash, block_header)) in block_locators.iter().skip(num_linear_block_headers).rev() {
// Check that the block height is decrementing.
match last_block_height == *block_height {
true => last_block_height = block_height.saturating_sub(1),
false => return Ok(false),
}

// Check that the block header is present.
let block_header = match block_header {
Some(header) => header,
None => return Ok(false),
};

// Check the block height matches in the block header.
if block_height != &block_header.height() {
return Ok(false);
}
}

// Check that the remaining block hashes are formed correctly (power of two).
if block_locators.len() > MAXIMUM_LINEAR_BLOCK_LOCATORS as usize {
let mut previous_block_height = u32::MAX;

// Iterate through all the quadratic ranged block locators excluding the genesis locator.
for (block_height, (_block_hash, block_header)) in block_locators.iter().skip(1).take(num_quadratic_block_headers - 1).rev() {
// Check that the block heights increment by a power of two.
if previous_block_height != u32::MAX && previous_block_height / 2 != *block_height {
return Ok(false);
}

// Check that there is no block header.
if block_header.is_some() {
return Ok(false);
}

previous_block_height = *block_height;
}
}

Ok(true)
}
Expand Down
15 changes: 9 additions & 6 deletions src/network/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,15 @@ impl<N: Network, E: Environment> Ledger<N, E> {
} else if self.unconfirmed_blocks.contains_key(&block.previous_block_hash()) {
trace!("Memory pool already contains unconfirmed block {}", block.height());
} else {
// Process the unconfirmed block.
self.add_block(block.clone());
// Propagate the unconfirmed block to the connected peers.
let request = PeersRequest::MessagePropagate(peer_ip, Message::UnconfirmedBlock(block));
if let Err(error) = peers_router.send(request).await {
warn!("[UnconfirmedBlock] {}", error);
// Ensure the unconfirmed block is at least within 10 blocks of the latest block height.
if block.height() + 10 > self.latest_block_height() {
// Process the unconfirmed block.
self.add_block(block.clone());
// Propagate the unconfirmed block to the connected peers.
let request = PeersRequest::MessagePropagate(peer_ip, Message::UnconfirmedBlock(block));
if let Err(error) = peers_router.send(request).await {
warn!("[UnconfirmedBlock] {}", error);
}
}
}
}
Expand Down
Loading

0 comments on commit fda8a5d

Please sign in to comment.