Skip to content

Commit

Permalink
[stateless_validation] Deprecate ChunkEndorsementTrackerV1 (#12131)
Browse files Browse the repository at this point in the history
Follow up on #11978

Now that ChunkEndorsementV2 is in production, we can get rid of
ChunkEndorsementV1 with the next protocol upgrade.

Note: To be merged only after branch cut for 2.3.0
  • Loading branch information
shreyan-gupta authored Nov 4, 2024
1 parent ad96419 commit 524eca9
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 588 deletions.
16 changes: 3 additions & 13 deletions chain/chain/src/test_utils/kv_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@ use near_primitives::hash::{hash, CryptoHash};
use near_primitives::receipt::{ActionReceipt, Receipt, ReceiptEnum, ReceiptV0};
use near_primitives::shard_layout;
use near_primitives::shard_layout::{ShardLayout, ShardUId};
use near_primitives::sharding::{ChunkHash, ShardChunkHeader};
use near_primitives::sharding::ChunkHash;
use near_primitives::state_part::PartId;
use near_primitives::stateless_validation::chunk_endorsement::{
ChunkEndorsementV1, ChunkEndorsementV2,
};
use near_primitives::stateless_validation::chunk_endorsement::ChunkEndorsement;
use near_primitives::stateless_validation::contract_distribution::ChunkContractAccesses;
use near_primitives::stateless_validation::partial_witness::PartialEncodedStateWitness;
use near_primitives::stateless_validation::validator_assignment::ChunkValidatorAssignments;
Expand Down Expand Up @@ -1016,17 +1014,9 @@ impl EpochManagerAdapter for MockEpochManager {
}
}

fn verify_chunk_endorsement(
&self,
_chunk_header: &ShardChunkHeader,
_endorsement: &ChunkEndorsementV1,
) -> Result<bool, Error> {
Ok(true)
}

fn verify_chunk_endorsement_signature(
&self,
_endorsement: &ChunkEndorsementV2,
_endorsement: &ChunkEndorsement,
) -> Result<bool, Error> {
Ok(true)
}
Expand Down
15 changes: 2 additions & 13 deletions chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,6 @@ impl Client {
persist_chunk(partial_chunk, shard_chunk, self.chain.mut_chain_store())
.expect("Could not persist chunk");

self.chunk_endorsement_tracker.tracker_v1.process_pending_endorsements(&chunk_header);
// We're marking chunk as accepted.
self.chain.blocks_with_missing_chunks.accept_chunk(&chunk_header.chunk_hash());
// If this was the last chunk that was missing for a block, it will be processed now.
Expand Down Expand Up @@ -1968,17 +1967,6 @@ impl Client {
}
}

pub fn mark_chunk_header_ready_for_inclusion(
&mut self,
chunk_header: ShardChunkHeader,
chunk_producer: AccountId,
) {
// If endorsement was received before chunk header, we can process it only now.
self.chunk_endorsement_tracker.tracker_v1.process_pending_endorsements(&chunk_header);
self.chunk_inclusion_tracker
.mark_chunk_header_ready_for_inclusion(chunk_header, chunk_producer);
}

pub fn persist_and_distribute_encoded_chunk(
&mut self,
encoded_chunk: EncodedShardChunk,
Expand Down Expand Up @@ -2012,7 +2000,8 @@ impl Client {
}
}

self.mark_chunk_header_ready_for_inclusion(chunk_header, validator_id);
self.chunk_inclusion_tracker
.mark_chunk_header_ready_for_inclusion(chunk_header, validator_id);
self.shards_manager_adapter.send(ShardsManagerRequestFromClient::DistributeEncodedChunk {
partial_chunk,
encoded_chunk,
Expand Down
6 changes: 4 additions & 2 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,7 +2135,9 @@ impl Handler<ShardsManagerResponse> for ClientActorInner {
chunk_header,
chunk_producer,
} => {
self.client.mark_chunk_header_ready_for_inclusion(chunk_header, chunk_producer);
self.client
.chunk_inclusion_tracker
.mark_chunk_header_ready_for_inclusion(chunk_header, chunk_producer);
}
}
}
Expand Down Expand Up @@ -2165,7 +2167,7 @@ impl Handler<ChunkStateWitnessMessage> for ClientActorInner {
impl Handler<ChunkEndorsementMessage> for ClientActorInner {
#[perf]
fn handle(&mut self, msg: ChunkEndorsementMessage) {
if let Err(err) = self.client.process_chunk_endorsement(msg.0) {
if let Err(err) = self.client.chunk_endorsement_tracker.process_chunk_endorsement(msg.0) {
tracing::error!(target: "client", ?err, "Error processing chunk endorsement");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use std::sync::Arc;

use lru::LruCache;
use near_chain_primitives::Error;
use near_crypto::Signature;
use near_epoch_manager::EpochManagerAdapter;
use near_primitives::sharding::ShardChunkHeader;
use near_primitives::stateless_validation::chunk_endorsement::ChunkEndorsementV2;
use near_primitives::sharding::{ChunkHash, ShardChunkHeader};
use near_primitives::stateless_validation::chunk_endorsement::ChunkEndorsement;
use near_primitives::stateless_validation::validator_assignment::ChunkEndorsementsState;
use near_primitives::stateless_validation::ChunkProductionKey;
use near_primitives::types::AccountId;
use near_primitives::version::ProtocolFeature;
use near_store::Store;

use crate::stateless_validation::validate::validate_chunk_endorsement;
use super::validate::validate_chunk_endorsement;

// This is the number of unique chunks for which we would track the chunk endorsements.
// Ideally, we should not be processing more than num_shards chunks at a time.
Expand All @@ -25,12 +26,11 @@ pub struct ChunkEndorsementTracker {
/// Used to find the chain HEAD when validating partial witnesses.
store: Store,
/// We store the validated chunk endorsements received from chunk validators.
/// Interior mutability is required to update the cache in process_chunk_endorsement & compute_chunk_endorsements.
chunk_endorsements: LruCache<ChunkProductionKey, HashMap<AccountId, ChunkEndorsementV2>>,
chunk_endorsements: LruCache<ChunkProductionKey, HashMap<AccountId, (ChunkHash, Signature)>>,
}

impl ChunkEndorsementTracker {
pub(crate) fn new(epoch_manager: Arc<dyn EpochManagerAdapter>, store: Store) -> Self {
pub fn new(epoch_manager: Arc<dyn EpochManagerAdapter>, store: Store) -> Self {
Self {
epoch_manager,
store,
Expand All @@ -41,9 +41,9 @@ impl ChunkEndorsementTracker {
}

// Validate the chunk endorsement and store it in the cache.
pub(crate) fn process_chunk_endorsement(
pub fn process_chunk_endorsement(
&mut self,
endorsement: ChunkEndorsementV2,
endorsement: ChunkEndorsement,
) -> Result<(), Error> {
// Check if we have already received chunk endorsement from this validator.
let key = endorsement.chunk_production_key();
Expand All @@ -57,13 +57,13 @@ impl ChunkEndorsementTracker {
if validate_chunk_endorsement(self.epoch_manager.as_ref(), &endorsement, &self.store)? {
self.chunk_endorsements
.get_or_insert_mut(key, || HashMap::new())
.insert(account_id.clone(), endorsement);
.insert(account_id.clone(), (endorsement.chunk_hash(), endorsement.signature()));
};
Ok(())
}

/// This function is called by block producer potentially multiple times if there's not enough stake.
pub(crate) fn collect_chunk_endorsements(
pub fn collect_chunk_endorsements(
&mut self,
chunk_header: &ShardChunkHeader,
) -> Result<ChunkEndorsementsState, Error> {
Expand Down Expand Up @@ -97,8 +97,8 @@ impl ChunkEndorsementTracker {
let entry = self.chunk_endorsements.get_or_insert(key, || HashMap::new());
let validator_signatures = entry
.into_iter()
.filter(|(_, endorsement)| endorsement.chunk_hash() == &chunk_header.chunk_hash())
.map(|(account_id, endorsement)| (account_id, endorsement.signature()))
.filter(|(_, (chunk_hash, _))| chunk_hash == &chunk_header.chunk_hash())
.map(|(account_id, (_, signature))| (account_id, signature.clone()))
.collect();

Ok(chunk_validator_assignments.compute_endorsement_state(validator_signatures))
Expand Down
81 changes: 0 additions & 81 deletions chain/client/src/stateless_validation/chunk_endorsement/mod.rs

This file was deleted.

Loading

0 comments on commit 524eca9

Please sign in to comment.