Skip to content

Commit

Permalink
block
Browse files Browse the repository at this point in the history
  • Loading branch information
pugachAG committed Sep 7, 2023
1 parent 997c00c commit 2fec3c9
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 103 deletions.
10 changes: 7 additions & 3 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ impl Chain {
.iter()
.filter(|chunk| block_height == chunk.height_included())
.flat_map(|chunk| chunk.prev_validator_proposals())
.zip_longest(block.header().validator_proposals())
.zip_longest(block.header().prev_validator_proposals())
{
match pair {
itertools::EitherOrBoth::Both(cp, hp) => {
Expand Down Expand Up @@ -2961,7 +2961,7 @@ impl Chain {
.push(RootProof(root_proof, block_receipts_proofs[from_shard_id].clone()));

// Make sure we send something reasonable.
assert_eq!(block_header.chunk_receipts_root(), &block_receipts_root);
assert_eq!(block_header.prev_chunk_outgoing_receipts_root(), &block_receipts_root);
assert!(verify_path(root_proof, proof, &receipts_hash));
assert!(verify_path(
block_receipts_root,
Expand Down Expand Up @@ -3233,7 +3233,11 @@ impl Chain {
return Err(Error::Other("set_shard_state failed: invalid proofs".into()));
}
// 4f. Proving the outgoing_receipts_root matches that in the block
if !verify_path(*block_header.chunk_receipts_root(), block_proof, root) {
if !verify_path(
*block_header.prev_chunk_outgoing_receipts_root(),
block_proof,
root,
) {
byzantine_assert!(false);
return Err(Error::Other("set_shard_state failed: invalid proofs".into()));
}
Expand Down
6 changes: 3 additions & 3 deletions chain/client/src/tests/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_not_process_height_twice() {

let proposals =
vec![ValidatorStake::new("test1".parse().unwrap(), PublicKey::empty(KeyType::ED25519), 0)];
duplicate_block.mut_header().get_mut().inner_rest.validator_proposals = proposals;
duplicate_block.mut_header().get_mut().inner_rest.prev_validator_proposals = proposals;
duplicate_block.mut_header().resign(&validator_signer);
let dup_block_hash = *duplicate_block.hash();
// we should have dropped the block before we even tried to process it, so the result should be ok
Expand Down Expand Up @@ -76,8 +76,8 @@ fn test_bad_shard_id() {
chunks[0] = ShardChunkHeader::V3(modified_chunk);
block.mut_header().get_mut().inner_rest.chunk_headers_root =
Block::compute_chunk_headers_root(&chunks).0;
block.mut_header().get_mut().inner_rest.chunk_receipts_root =
Block::compute_chunk_receipts_root(&chunks);
block.mut_header().get_mut().inner_rest.prev_chunk_outgoing_receipts_root =
Block::compute_chunk_prev_outgoing_receipts_root(&chunks);
block.set_chunks(chunks);
block.mut_header().get_mut().inner_rest.block_body_hash =
block.compute_block_body_hash().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion chain/epoch-manager/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl BlockHeaderInfo {
random_value: *header.random_value(),
last_finalized_height,
last_finalized_block_hash: *header.last_final_block(),
proposals: header.validator_proposals().collect(),
proposals: header.prev_validator_proposals().collect(),
slashed_validators: vec![],
chunk_mask: header.chunk_mask().to_vec(),
total_supply: header.total_supply(),
Expand Down
20 changes: 12 additions & 8 deletions core/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl Block {
height,
Block::compute_state_root(&body.chunks),
Block::compute_block_body_hash_impl(&body),
Block::compute_chunk_receipts_root(&body.chunks),
Block::compute_chunk_prev_outgoing_receipts_root(&body.chunks),
Block::compute_chunk_headers_root(&body.chunks).0,
Block::compute_chunk_tx_root(&body.chunks),
body.chunks.len() as u64,
Expand Down Expand Up @@ -237,15 +237,15 @@ impl Block {
timestamp_override: Option<DateTime<chrono::Utc>>,
) -> Self {
// Collect aggregate of validators and gas usage/limits from chunks.
let mut validator_proposals = vec![];
let mut prev_validator_proposals = vec![];
let mut gas_used = 0;
// This computation of chunk_mask relies on the fact that chunks are ordered by shard_id.
let mut chunk_mask = vec![];
let mut balance_burnt = 0;
let mut gas_limit = 0;
for chunk in chunks.iter() {
if chunk.height_included() == height {
validator_proposals.extend(chunk.prev_validator_proposals());
prev_validator_proposals.extend(chunk.prev_validator_proposals());
gas_used += chunk.prev_gas_used();
gas_limit += chunk.prev_gas_limit();
balance_burnt += chunk.prev_balance_burnt();
Expand Down Expand Up @@ -299,14 +299,14 @@ impl Block {
*prev.hash(),
Block::compute_block_body_hash_impl(&body),
Block::compute_state_root(&body.chunks),
Block::compute_chunk_receipts_root(&body.chunks),
Block::compute_chunk_prev_outgoing_receipts_root(&body.chunks),
Block::compute_chunk_headers_root(&body.chunks).0,
Block::compute_chunk_tx_root(&body.chunks),
Block::compute_outcome_root(&body.chunks),
time,
Block::compute_challenges_root(&body.challenges),
random_value,
validator_proposals,
prev_validator_proposals,
chunk_mask,
block_ordinal,
epoch_id,
Expand Down Expand Up @@ -415,7 +415,10 @@ impl Block {
CryptoHash::hash_borsh(body)
}

pub fn compute_chunk_receipts_root<'a, T: IntoIterator<Item = &'a ShardChunkHeader>>(
pub fn compute_chunk_prev_outgoing_receipts_root<
'a,
T: IntoIterator<Item = &'a ShardChunkHeader>,
>(
chunks: T,
) -> CryptoHash {
merklize(
Expand Down Expand Up @@ -562,8 +565,9 @@ impl Block {
}

// Check that chunk receipts root stored in the header matches the state root of the chunks
let chunk_receipts_root = Block::compute_chunk_receipts_root(self.chunks().iter());
if self.header().chunk_receipts_root() != &chunk_receipts_root {
let chunk_receipts_root =
Block::compute_chunk_prev_outgoing_receipts_root(self.chunks().iter());
if self.header().prev_chunk_outgoing_receipts_root() != &chunk_receipts_root {
return Err(InvalidReceiptRoot);
}

Expand Down
Loading

0 comments on commit 2fec3c9

Please sign in to comment.