Skip to content

Commit

Permalink
fix(epoch_sync) Prevent Block Header sync during Epoch sync (#12868)
Browse files Browse the repository at this point in the history
Issue #12564. Epoch sync is restricted to nodes at genesis. At the
moment node will not initialize Block Header sync but it can handle
BlockHeadersResponse anyway.

The commit provides addtional check on processig BlockHeadersResponse to
ensure that receiver node is not at EpochSync state.
  • Loading branch information
mkamonMdt authored Feb 4, 2025
1 parent 409987d commit c4c7f6d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
7 changes: 6 additions & 1 deletion chain/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use near_chunks::client::ShardedTransactionPool;
use near_chunks::logic::{decode_encoded_chunk, persist_chunk};
use near_chunks::shards_manager_actor::ShardsManagerActor;
use near_client_primitives::debug::ChunkProduction;
use near_client_primitives::types::{Error, StateSyncStatus};
use near_client_primitives::types::{Error, StateSyncStatus, SyncStatus};
use near_epoch_manager::shard_assignment::{account_id_to_shard_id, shard_id_to_uid};
use near_epoch_manager::shard_tracker::ShardTracker;
use near_epoch_manager::EpochManagerAdapter;
Expand Down Expand Up @@ -1623,6 +1623,11 @@ impl Client {
headers: Vec<BlockHeader>,
signer: &Option<Arc<ValidatorSigner>>,
) -> Result<(), near_chain::Error> {
if matches!(self.sync_handler.sync_status, SyncStatus::EpochSync(_)) {
return Err(near_chain::Error::Other(
"Cannot sync block headers during an epoch sync".to_owned(),
));
};
let mut challenges = vec![];
self.chain.sync_block_headers(headers, &mut challenges)?;
self.send_challenges(challenges, signer);
Expand Down
51 changes: 51 additions & 0 deletions integration-tests/src/tests/client/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use near_o11y::WithSpanContextExt;
use near_parameters::{ActionCosts, ExtCosts};
use near_parameters::{RuntimeConfig, RuntimeConfigStore};
use near_primitives::block::Approval;
use near_primitives::block::GenesisId;
use near_primitives::block_header::BlockHeader;
use near_primitives::errors::TxExecutionError;
use near_primitives::errors::{ActionError, ActionErrorKind, InvalidTxError};
Expand Down Expand Up @@ -1889,6 +1890,56 @@ fn test_not_resync_old_blocks() {
}
}

#[test]
fn test_reject_block_headers_during_epoch_sync() {
let mut genesis = Genesis::test(vec!["test0".parse().unwrap(), "test1".parse().unwrap()], 1);
let epoch_length = 2;
genesis.config.epoch_length = epoch_length;
let mut env =
TestEnv::builder(&genesis.config).clients_count(2).nightshade_runtimes(&genesis).build();

let mut blocks = vec![];
for i in 1..=epoch_length + 1 {
let block = env.clients[0].produce_block(i).unwrap().unwrap();
env.process_block(0, block.clone(), Provenance::PRODUCED);
blocks.push(block);
}

let sync_client = &mut env.clients[1];
let status = &mut sync_client.sync_handler.sync_status;
let chain = &sync_client.chain;
let highest_height = sync_client.config.epoch_sync.epoch_sync_horizon + 1;
let highest_height_peers = vec![HighestHeightPeerInfo {
archival: false,
genesis_id: GenesisId::default(),
highest_block_hash: *blocks.last().unwrap().hash(),
highest_block_height: blocks.len() as u64,
tracked_shards: vec![],
peer_info: PeerInfo::random(),
}];

// Running epoch sync, sets SyncStatus::EpochSync
assert_matches!(
sync_client.sync_handler.epoch_sync.run(
status,
chain,
highest_height,
&highest_height_peers
),
Ok(()),
"Epoch sync failure"
);

let headers = blocks.iter().map(|b| b.header().clone()).collect::<Vec<_>>();
let signer = sync_client.validator_signer.get();
// actual attempt to sync headers during ongoing epoch sync
assert_matches!(
sync_client.sync_block_headers(headers, &signer),
Err(_),
"Block headers accepted during epoch sync"
);
}

#[test]
fn test_gc_tail_update() {
let mut genesis = Genesis::test(vec!["test0".parse().unwrap(), "test1".parse().unwrap()], 1);
Expand Down

0 comments on commit c4c7f6d

Please sign in to comment.