Skip to content

Commit

Permalink
refactor: chunk validators only in TestLoop setup (near#11535)
Browse files Browse the repository at this point in the history
We are not going to use chunk only producers for testing latest protocol
versions, and in fact they are replaced with chunk only validators, so I
rename the fields.
Also reducing nesting in couple places to which I want to add more logic
later.
  • Loading branch information
Longarithm authored Jun 10, 2024
1 parent 504e120 commit 14e5d92
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 70 deletions.
23 changes: 11 additions & 12 deletions chain/chain/src/state_snapshot_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,17 @@ impl StateSnapshotActor {
}
match res {
Ok(res_shard_uids) => {
if let Some(res_shard_uids) = res_shard_uids {
self.network_adapter.send(PeerManagerMessageRequest::NetworkRequests(
NetworkRequests::SnapshotHostInfo {
sync_hash: prev_block_hash,
epoch_height,
shards: res_shard_uids
.iter()
.map(|uid| uid.shard_id as ShardId)
.collect(),
},
));
}
let Some(res_shard_uids) = res_shard_uids else {
return;
};

self.network_adapter.send(PeerManagerMessageRequest::NetworkRequests(
NetworkRequests::SnapshotHostInfo {
sync_hash: prev_block_hash,
epoch_height,
shards: res_shard_uids.iter().map(|uid| uid.shard_id as ShardId).collect(),
},
));
}
Err(err) => {
tracing::error!(target: "state_snapshot", ?err, "State snapshot creation failed.\
Expand Down
57 changes: 30 additions & 27 deletions chain/client/src/client_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,35 +1294,38 @@ impl ClientActorInner {
/// Can return error, should be called with `produce_block` to handle errors and reschedule.
fn produce_block(&mut self, next_height: BlockHeight) -> Result<(), Error> {
let _span = tracing::debug_span!(target: "client", "produce_block", next_height).entered();
if let Some(block) = self.client.produce_block_on_head(next_height, false)? {
// If we produced the block, send it out before we apply the block.
self.network_adapter.send(PeerManagerMessageRequest::NetworkRequests(
NetworkRequests::Block { block: block.clone() },
));
// We’ve produced the block so that counts as validated block.
let block = MaybeValidated::from_validated(block);
let res = self.client.start_process_block(
block,
Provenance::PRODUCED,
Some(self.myself_sender.apply_chunks_done.clone()),
);
if let Err(e) = &res {
match e {
near_chain::Error::ChunksMissing(_) => {
debug!(target: "client", "chunks missing");
// missing chunks were already handled in Client::process_block, we don't need to
// do anything here
return Ok(());
}
_ => {
error!(target: "client", ?res, "Failed to process freshly produced block");
byzantine_assert!(false);
return res.map_err(|err| err.into());
}
}
let Some(block) = self.client.produce_block_on_head(next_height, false)? else {
return Ok(());
};

// If we produced the block, send it out before we apply the block.
self.network_adapter.send(PeerManagerMessageRequest::NetworkRequests(
NetworkRequests::Block { block: block.clone() },
));
// We’ve produced the block so that counts as validated block.
let block = MaybeValidated::from_validated(block);
let res = self.client.start_process_block(
block,
Provenance::PRODUCED,
Some(self.myself_sender.apply_chunks_done.clone()),
);
let Err(error) = res else {
return Ok(());
};

match error {
near_chain::Error::ChunksMissing(_) => {
debug!(target: "client", "chunks missing");
// missing chunks were already handled in Client::process_block, we don't need to
// do anything here
Ok(())
}
_ => {
error!(target: "client", ?error, "Failed to process freshly produced block");
byzantine_assert!(false);
Err(error.into())
}
}
Ok(())
}

fn send_chunks_metrics(&mut self, block: &Block) {
Expand Down
62 changes: 31 additions & 31 deletions core/chain-configs/src/test_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ pub struct TestGenesisBuilder {
#[derive(Debug, Clone)]
enum ValidatorsSpec {
DesiredRoles {
block_producers: Vec<String>,
chunk_only_producers: Vec<String>,
block_and_chunk_producers: Vec<String>,
chunk_validators_only: Vec<String>,
},
Raw {
validators: Vec<AccountInfo>,
num_block_producer_seats: NumSeats,
num_chunk_only_producer_seats: NumSeats,
num_chunk_producer_seats: NumSeats,
num_chunk_validator_seats: NumSeats,
},
Expand Down Expand Up @@ -169,12 +168,15 @@ impl TestGenesisBuilder {
/// validators are selected as specified.
pub fn validators_desired_roles(
&mut self,
block_producers: &[&str],
chunk_only_producers: &[&str],
block_and_chunk_producers: &[&str],
chunk_validators_only: &[&str],
) -> &mut Self {
self.validators = Some(ValidatorsSpec::DesiredRoles {
block_producers: block_producers.iter().map(|s| s.to_string()).collect(),
chunk_only_producers: chunk_only_producers.iter().map(|s| s.to_string()).collect(),
block_and_chunk_producers: block_and_chunk_producers
.iter()
.map(|s| s.to_string())
.collect(),
chunk_validators_only: chunk_validators_only.iter().map(|s| s.to_string()).collect(),
});
self
}
Expand All @@ -185,15 +187,15 @@ impl TestGenesisBuilder {
pub fn validators_raw(
&mut self,
validators: Vec<AccountInfo>,
num_block_producer_seats: NumSeats,
num_chunk_only_producer_seats: NumSeats,
num_block_and_chunk_producer_seats: NumSeats,
num_chunk_validator_only_seats: NumSeats,
) -> &mut Self {
self.validators = Some(ValidatorsSpec::Raw {
validators,
num_block_producer_seats,
num_chunk_only_producer_seats,
num_chunk_producer_seats: num_block_producer_seats,
num_chunk_validator_seats: num_block_producer_seats + num_chunk_only_producer_seats,
num_block_producer_seats: num_block_and_chunk_producer_seats,
num_chunk_producer_seats: num_block_and_chunk_producer_seats,
num_chunk_validator_seats: num_block_and_chunk_producer_seats
+ num_chunk_validator_only_seats,
});
self
}
Expand Down Expand Up @@ -316,8 +318,8 @@ impl TestGenesisBuilder {
});
let validator_specs = self.validators.clone().unwrap_or_else(|| {
let default = ValidatorsSpec::DesiredRoles {
block_producers: vec!["validator0".to_string()],
chunk_only_producers: vec![],
block_and_chunk_producers: vec!["validator0".to_string()],
chunk_validators_only: vec![],
};
tracing::warn!(
"Genesis validators not explicitly set, defaulting to a single validator setup {:?}.",
Expand Down Expand Up @@ -477,7 +479,7 @@ impl TestGenesisBuilder {
max_kickout_stake_perc: 100,
validators: derived_validator_setup.validators,
num_block_producer_seats: derived_validator_setup.num_block_producer_seats,
num_chunk_only_producer_seats: derived_validator_setup.num_chunk_only_producer_seats,
num_chunk_only_producer_seats: 0,
minimum_stake_ratio: derived_validator_setup.minimum_stake_ratio,
minimum_validators_per_shard,
minimum_stake_divisor: 10,
Expand Down Expand Up @@ -506,7 +508,6 @@ impl TestGenesisBuilder {
struct DerivedValidatorSetup {
validators: Vec<AccountInfo>,
num_block_producer_seats: NumSeats,
num_chunk_only_producer_seats: NumSeats,
num_chunk_producer_seats: NumSeats,
num_chunk_validator_seats: NumSeats,
minimum_stake_ratio: Rational32,
Expand All @@ -516,50 +517,49 @@ const ONE_NEAR: Balance = 1_000_000_000_000_000_000_000_000;

fn derive_validator_setup(specs: ValidatorsSpec) -> DerivedValidatorSetup {
match specs {
ValidatorsSpec::DesiredRoles { block_producers, chunk_only_producers } => {
let num_block_producer_seats = block_producers.len() as NumSeats;
let num_chunk_only_producer_seats = chunk_only_producers.len() as NumSeats;
ValidatorsSpec::DesiredRoles { block_and_chunk_producers, chunk_validators_only } => {
let num_block_and_chunk_producer_seats = block_and_chunk_producers.len() as NumSeats;
let num_chunk_validator_only_seats = chunk_validators_only.len() as NumSeats;
// Set minimum stake ratio to zero; that way, we don't have to worry about
// chunk producers not having enough stake to be selected as desired.
let minimum_stake_ratio = Rational32::new(0, 1);
let mut validators = Vec::new();
for i in 0..num_block_producer_seats as usize {
let account_id: AccountId = block_producers[i].parse().unwrap();
for i in 0..num_block_and_chunk_producer_seats as usize {
let account_id: AccountId = block_and_chunk_producers[i].parse().unwrap();
let account_info = AccountInfo {
public_key: create_test_signer(account_id.as_str()).public_key(),
account_id,
amount: ONE_NEAR * (10000 - i as Balance),
};
validators.push(account_info);
}
for i in 0..num_chunk_only_producer_seats as usize {
let account_id: AccountId = chunk_only_producers[i].parse().unwrap();
for i in 0..num_chunk_validator_only_seats as usize {
let account_id: AccountId = chunk_validators_only[i].parse().unwrap();
let account_info = AccountInfo {
public_key: create_test_signer(account_id.as_str()).public_key(),
account_id,
amount: ONE_NEAR * (10000 - i as Balance - num_block_producer_seats as Balance),
amount: ONE_NEAR
* (10000 - i as Balance - num_block_and_chunk_producer_seats as Balance),
};
validators.push(account_info);
}
DerivedValidatorSetup {
validators,
num_block_producer_seats,
num_chunk_only_producer_seats,
num_chunk_producer_seats: num_block_producer_seats,
num_chunk_validator_seats: num_block_producer_seats + num_chunk_only_producer_seats,
num_block_producer_seats: num_block_and_chunk_producer_seats,
num_chunk_producer_seats: num_block_and_chunk_producer_seats,
num_chunk_validator_seats: num_block_and_chunk_producer_seats
+ num_chunk_validator_only_seats,
minimum_stake_ratio,
}
}
ValidatorsSpec::Raw {
validators,
num_block_producer_seats,
num_chunk_only_producer_seats,
num_chunk_producer_seats,
num_chunk_validator_seats,
} => DerivedValidatorSetup {
validators,
num_block_producer_seats,
num_chunk_only_producer_seats,
num_chunk_producer_seats,
num_chunk_validator_seats,
minimum_stake_ratio: Rational32::new(160, 1000000),
Expand Down

0 comments on commit 14e5d92

Please sign in to comment.