Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use new api in validate_chunk_headers #12265

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chain/chain/src/blocks_delay_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl BlocksDelayTracker {
let height = block.header().height();
let chunks = block
.chunks()
.iter()
.iter_deprecated()
.map(|chunk| {
if chunk.height_included() == height {
let chunk_hash = chunk.chunk_hash();
Expand Down
87 changes: 49 additions & 38 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use near_chain_configs::{MutableConfigValue, MutableValidatorSigner};
use near_chain_primitives::error::{BlockKnownError, Error, LogTransientStorageError};
use near_epoch_manager::shard_tracker::ShardTracker;
use near_epoch_manager::EpochManagerAdapter;
use near_primitives::block::{genesis_chunks, Block, BlockValidityError, Tip};
use near_primitives::block::{genesis_chunks, Block, BlockValidityError, MaybeNew, Tip};
use near_primitives::block_header::BlockHeader;
use near_primitives::challenge::{
BlockDoubleSign, Challenge, ChallengeBody, ChallengesResult, ChunkProofs, ChunkState,
Expand Down Expand Up @@ -664,7 +664,8 @@ impl Chain {
epoch_manager: &dyn EpochManagerAdapter,
store_update: &mut ChainStoreUpdate,
) -> Result<(), Error> {
for (chunk_header, state_root) in genesis.chunks().iter().zip(state_roots.iter()) {
for (chunk_header, state_root) in genesis.chunks().iter_deprecated().zip(state_roots.iter())
{
let congestion_info =
if ProtocolFeature::CongestionControl.enabled(chain_genesis.protocol_version) {
genesis
Expand Down Expand Up @@ -835,7 +836,7 @@ impl Chain {
let epoch_id = block.header().epoch_id();
let shard_layout = epoch_manager.get_shard_layout(&epoch_id)?;

for (shard_index, chunk_header) in block.chunks().iter().enumerate() {
for (shard_index, chunk_header) in block.chunks().iter_deprecated().enumerate() {
let shard_id = shard_layout.get_shard_id(shard_index);
if chunk_header.height_created() == genesis_block.header().height() {
// Special case: genesis chunks can be in non-genesis blocks and don't have a signature
Expand Down Expand Up @@ -1240,34 +1241,33 @@ impl Chain {
for (chunk_header, prev_chunk_header) in
block.chunks().iter().zip(prev_chunk_headers.iter())
{
if chunk_header.height_included() == block.header().height() {
// new chunk
if chunk_header.prev_block_hash() != block.header().prev_hash() {
return Err(Error::InvalidChunk(format!(
"Invalid prev_block_hash, chunk hash {:?}, chunk prev block hash {}, block prev block hash {}",
chunk_header.chunk_hash(),
chunk_header.prev_block_hash(),
block.header().prev_hash()
)));
match chunk_header {
MaybeNew::New(chunk_header) => {
if chunk_header.prev_block_hash() != block.header().prev_hash() {
return Err(Error::InvalidChunk(format!(
"Invalid prev_block_hash, chunk hash {:?}, chunk prev block hash {}, block prev block hash {}",
chunk_header.chunk_hash(),
chunk_header.prev_block_hash(),
block.header().prev_hash()
)));
}
}
} else {
// old chunk
if prev_chunk_header != chunk_header {
return Err(Error::InvalidChunk(format!(
"Invalid chunk header, prev chunk hash {:?}, chunk hash {:?}",
prev_chunk_header.chunk_hash(),
chunk_header.chunk_hash()
)));
MaybeNew::Old(chunk_header) => {
if prev_chunk_header != chunk_header {
return Err(Error::InvalidChunk(format!(
"Invalid chunk header, prev chunk hash {:?}, chunk hash {:?}",
prev_chunk_header.chunk_hash(),
chunk_header.chunk_hash()
)));
}
}
}
}

// Verify that proposals from chunks match block header proposals.
let block_height = block.header().height();
for pair in block
.chunks()
.iter()
.filter(|chunk| chunk.is_new_chunk(block_height))
.iter_new_chunks()
.flat_map(|chunk| chunk.prev_validator_proposals())
.zip_longest(block.header().prev_validator_proposals())
{
Expand Down Expand Up @@ -1329,13 +1329,14 @@ impl Chain {
let epoch_id = block.header().epoch_id();
let shard_layout = self.epoch_manager.get_shard_layout(&epoch_id)?;

for (shard_index, chunk_header) in block.chunks().iter().enumerate() {
for (shard_index, chunk_header) in block.chunks().iter_deprecated().enumerate() {
let shard_id = shard_layout.get_shard_id(shard_index);
// Check if any chunks are invalid in this block.
if let Some(encoded_chunk) =
self.chain_store.is_invalid_chunk(&chunk_header.chunk_hash())?
{
let merkle_paths = Block::compute_chunk_headers_root(block.chunks().iter()).1;
let merkle_paths =
Block::compute_chunk_headers_root(block.chunks().iter_deprecated()).1;
let merkle_proof =
merkle_paths.get(shard_index).ok_or_else(|| Error::InvalidShardId(shard_id))?;
let chunk_proof = ChunkProofs {
Expand Down Expand Up @@ -1419,7 +1420,7 @@ impl Chain {
let block_height = block.header().height();
let mut receipt_proofs_by_shard_id = HashMap::new();

for chunk_header in block.chunks().iter() {
for chunk_header in block.chunks().iter_deprecated() {
if !chunk_header.is_new_chunk(block_height) {
continue;
}
Expand Down Expand Up @@ -1698,17 +1699,25 @@ impl Chain {
// sync hash block. The logic below adjusts the new_tail so that every
// shard is guaranteed to have at least one new chunk in the blocks
// leading to the sync hash block.
let min_height_included =
prev_block.chunks().iter().map(|chunk| chunk.height_included()).min().unwrap();
let min_height_included = prev_block
.chunks()
.iter_deprecated()
.map(|chunk| chunk.height_included())
.min()
.unwrap();

tracing::debug!(target: "sync", ?min_height_included, ?new_tail, "adjusting tail for missing chunks");
new_tail = std::cmp::min(new_tail, min_height_included.saturating_sub(1));

// In order to find the right new_chunk_tail we need to find the minimum
// of chunk height_created for chunks in the new tail block.
let new_tail_block = self.get_block_by_height(new_tail)?;
let new_chunk_tail =
new_tail_block.chunks().iter().map(|chunk| chunk.height_created()).min().unwrap();
let new_chunk_tail = new_tail_block
.chunks()
.iter_deprecated()
.map(|chunk| chunk.height_created())
.min()
.unwrap();

let tip = Tip::from_header(prev_block.header());
let final_head = Tip::from_header(self.genesis.header());
Expand Down Expand Up @@ -2107,7 +2116,7 @@ impl Chain {

let last_final_block_chunks = last_final_block.chunks();
let chunk_header = last_final_block_chunks
.iter()
.iter_deprecated()
.find(|chunk| chunk.shard_id() == shard_id)
.ok_or_else(|| Error::InvalidShardId(shard_id))?;
let new_flat_head = *chunk_header.prev_block_hash();
Expand Down Expand Up @@ -2521,7 +2530,7 @@ impl Chain {
let (chunk_headers_root, chunk_proofs) = merklize(
&sync_prev_block
.chunks()
.iter()
.iter_deprecated()
.map(|shard_chunk| {
ChunkHashHeight(shard_chunk.chunk_hash(), shard_chunk.height_included())
})
Expand Down Expand Up @@ -2550,7 +2559,7 @@ impl Chain {
let (prev_chunk_headers_root, prev_chunk_proofs) = merklize(
&prev_block
.chunks()
.iter()
.iter_deprecated()
.map(|shard_chunk| {
ChunkHashHeight(shard_chunk.chunk_hash(), shard_chunk.height_included())
})
Expand Down Expand Up @@ -2597,7 +2606,7 @@ impl Chain {
let (block_receipts_root, block_receipts_proofs) = merklize(
&block
.chunks()
.iter()
.iter_deprecated()
.map(|chunk| chunk.prev_outgoing_receipts_root())
.collect::<Vec<CryptoHash>>(),
);
Expand Down Expand Up @@ -3140,7 +3149,8 @@ impl Chain {
chunk: &ShardChunk,
) -> Result<(), Error> {
if !validate_transactions_order(chunk.transactions()) {
let merkle_paths = Block::compute_chunk_headers_root(block.chunks().iter()).1;
let merkle_paths =
Block::compute_chunk_headers_root(block.chunks().iter_deprecated()).1;
let epoch_id = block.header().epoch_id();
let shard_layout = self.epoch_manager.get_shard_layout(&epoch_id)?;
let shard_id = chunk.shard_id();
Expand Down Expand Up @@ -3468,8 +3478,9 @@ impl Chain {
let shard_layout = self.epoch_manager.get_shard_layout(&epoch_id)?;
let shard_id = chunk_header.shard_id();
let shard_index = shard_layout.get_shard_index(shard_id);
let prev_merkle_proofs = Block::compute_chunk_headers_root(prev_block.chunks().iter()).1;
let merkle_proofs = Block::compute_chunk_headers_root(block.chunks().iter()).1;
let prev_merkle_proofs =
Block::compute_chunk_headers_root(prev_block.chunks().iter_deprecated()).1;
let merkle_proofs = Block::compute_chunk_headers_root(block.chunks().iter_deprecated()).1;
let prev_chunk =
self.get_chunk_clone_from_header(&prev_block.chunks()[shard_index].clone()).unwrap();

Expand Down Expand Up @@ -3606,7 +3617,7 @@ impl Chain {

let mut maybe_jobs = vec![];
for (shard_index, (chunk_header, prev_chunk_header)) in
block.chunks().iter().zip(prev_chunk_headers.iter()).enumerate()
block.chunks().iter_deprecated().zip(prev_chunk_headers.iter()).enumerate()
{
// XXX: This is a bit questionable -- sandbox state patching works
// only for a single shard. This so far has been enough.
Expand Down
10 changes: 6 additions & 4 deletions chain/chain/src/garbage_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl ChainStore {
let prev_block = self.get_block(&prev_hash);
if let Ok(prev_block) = prev_block {
let min_height_included =
prev_block.chunks().iter().map(|chunk| chunk.height_included()).min();
prev_block.chunks().iter_deprecated().map(|chunk| chunk.height_included()).min();
if let Some(min_height_included) = min_height_included {
tracing::debug!(target: "sync", ?min_height_included, ?gc_height, "adjusting gc_height for missing chunks");
gc_height = std::cmp::min(gc_height, min_height_included - 1);
Expand Down Expand Up @@ -650,7 +650,7 @@ impl<'a> ChainStoreUpdate<'a> {
// 6. Canonical Chain only clearing
// Delete chunks, chunk-indexed data and block headers
let mut min_chunk_height = self.tail()?;
for chunk_header in block.chunks().iter() {
for chunk_header in block.chunks().iter_deprecated() {
if min_chunk_height > chunk_header.height_created() {
min_chunk_height = chunk_header.height_created();
}
Expand Down Expand Up @@ -832,8 +832,10 @@ impl<'a> ChainStoreUpdate<'a> {
fn gc_outcomes(&mut self, block: &Block) -> Result<(), Error> {
let block_hash = block.hash();
let store_update = self.store().store_update();
for chunk_header in
block.chunks().iter().filter(|h| h.height_included() == block.header().height())
for chunk_header in block
.chunks()
.iter_deprecated()
.filter(|h| h.height_included() == block.header().height())
{
// It is ok to use the shard id from the header because it is a new
// chunk. An old chunk may have the shard id from the parent shard.
Expand Down
4 changes: 3 additions & 1 deletion chain/chain/src/stateless_validation/chunk_endorsement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ pub fn validate_chunk_endorsements_in_block(

let epoch_id = epoch_manager.get_epoch_id_from_prev_block(block.header().prev_hash())?;
let shard_layout = epoch_manager.get_shard_layout(&epoch_id)?;
for (chunk_header, signatures) in block.chunks().iter().zip(block.chunk_endorsements()) {
for (chunk_header, signatures) in
block.chunks().iter_deprecated().zip(block.chunk_endorsements())
{
// For old chunks, we optimize the block by not including the chunk endorsements.
if chunk_header.height_included() != block.header().height() {
if !signatures.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/stateless_validation/chunk_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ fn validate_source_receipt_proofs(
// Collect all receipts coming from this block.
let mut block_receipt_proofs = Vec::new();

for chunk in block.chunks().iter() {
for chunk in block.chunks().iter_deprecated() {
if !chunk.is_new_chunk(block.header().height()) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Chain {
let final_block = chain_store.get_block(&final_block_hash)?;
let final_block_chunk_created_heights = final_block
.chunks()
.iter()
.iter_deprecated()
.map(|chunk| (chunk.shard_id(), chunk.height_created()))
.collect::<Vec<_>>();
clear_before_last_final_block(chain_store, &final_block_chunk_created_heights)?;
Expand Down
4 changes: 2 additions & 2 deletions chain/chain/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl ChainStore {
block_hash: &CryptoHash,
) -> Result<HashMap<ShardId, Vec<ExecutionOutcomeWithIdAndProof>>, Error> {
let block = self.get_block(block_hash)?;
let chunk_headers = block.chunks().iter().cloned().collect::<Vec<_>>();
let chunk_headers = block.chunks().iter_deprecated().cloned().collect::<Vec<_>>();

let mut res = HashMap::new();
for chunk_header in chunk_headers {
Expand Down Expand Up @@ -2181,7 +2181,7 @@ impl<'a> ChainStoreUpdate<'a> {
source_store.get_chunk_extra(block_hash, &shard_uid)?.clone(),
);
}
for (shard_index, chunk_header) in block.chunks().iter().enumerate() {
for (shard_index, chunk_header) in block.chunks().iter_deprecated().enumerate() {
let shard_id = shard_layout.get_shard_id(shard_index);
let chunk_hash = chunk_header.chunk_hash();
chain_store_update
Expand Down
6 changes: 3 additions & 3 deletions chain/chain/src/store_validator/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ pub(crate) fn block_chunks_exist(
// for single-shard, no-missing-chunks state sync or epoch sync tests.
return Ok(());
}
for chunk_header in block.chunks().iter() {
for chunk_header in block.chunks().iter_deprecated() {
if chunk_header.height_included() == block.header().height() {
if let Some(me) = &sv.me {
let cares_about_shard = sv.shard_tracker.care_about_shard(
Expand Down Expand Up @@ -419,7 +419,7 @@ pub(crate) fn block_chunks_height_validity(
_block_hash: &CryptoHash,
block: &Block,
) -> Result<(), StoreValidatorError> {
for chunk_header in block.chunks().iter() {
for chunk_header in block.chunks().iter_deprecated() {
if chunk_header.height_created() > block.header().height() {
err!(
"Invalid ShardChunk included, chunk_header = {:?}, block = {:?}",
Expand Down Expand Up @@ -704,7 +704,7 @@ pub(crate) fn outcome_indexed_by_block_hash(
"Can't get Block {} from DB",
block_hash
);
for chunk_header in block.chunks().iter() {
for chunk_header in block.chunks().iter_deprecated() {
if chunk_header.height_included() == block.header().height() {
let shard_uid = sv
.epoch_manager
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ pub fn display_chain(me: &Option<AccountId>, chain: &mut Chain, tail: bool) {
}
);
if let Some(block) = maybe_block {
for chunk_header in block.chunks().iter() {
for chunk_header in block.chunks().iter_deprecated() {
let chunk_producer = epoch_manager
.get_chunk_producer(
&epoch_id,
Expand Down
9 changes: 6 additions & 3 deletions chain/chain/src/tests/simple_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn build_chain_with_orphans() {
last_block.header(),
10,
last_block.header().block_ordinal() + 1,
last_block.chunks().iter().cloned().collect(),
last_block.chunks().iter_deprecated().cloned().collect(),
vec![vec![]; last_block.chunks().len()],
*last_block.header().epoch_id(),
*last_block.header().next_epoch_id(),
Expand Down Expand Up @@ -268,21 +268,24 @@ fn block_chunk_headers_iter() {
let chunks = block.chunks();

let new_headers: Vec<&ShardChunkHeader> = chunks
.iter_annotated()
.iter()
.filter_map(|chunk| match chunk {
MaybeNew::New(chunk) => Some(chunk),
_ => None,
})
.collect();

let old_headers: Vec<&ShardChunkHeader> = chunks
.iter_annotated()
.iter()
.filter_map(|chunk| match chunk {
MaybeNew::Old(chunk) => Some(chunk),
_ => None,
})
.collect();

let raw_headers: Vec<&ShardChunkHeader> = chunks.iter_raw().collect();

assert_eq!(old_headers.len(), 8);
assert_eq!(new_headers.len(), 8);
assert_eq!(raw_headers.len(), old_headers.len() + new_headers.len());
}
4 changes: 2 additions & 2 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl Client {
) -> Result<(), Error> {
let epoch_id = self.epoch_manager.get_epoch_id(block.hash())?;
let shard_layout = self.epoch_manager.get_shard_layout(&epoch_id)?;
for (shard_index, chunk_header) in block.chunks().iter().enumerate() {
for (shard_index, chunk_header) in block.chunks().iter_deprecated().enumerate() {
let shard_id = shard_layout.get_shard_id(shard_index);
let shard_uid = self.epoch_manager.shard_id_to_uid(shard_id, &epoch_id)?;
if block.header().height() == chunk_header.height_included() {
Expand Down Expand Up @@ -454,7 +454,7 @@ impl Client {
let epoch_id = self.epoch_manager.get_epoch_id(block.hash())?;
let shard_layout = self.epoch_manager.get_shard_layout(&epoch_id)?;

for (shard_index, chunk_header) in block.chunks().iter().enumerate() {
for (shard_index, chunk_header) in block.chunks().iter_deprecated().enumerate() {
let shard_id = shard_layout.get_shard_id(shard_index);
let shard_uid = self.epoch_manager.shard_id_to_uid(shard_id, &epoch_id)?;

Expand Down
Loading
Loading