Skip to content

Commit

Permalink
v0_single_shard() -> single_shard()
Browse files Browse the repository at this point in the history
  • Loading branch information
eagr committed Oct 27, 2024
1 parent 89dbf98 commit e8a78cc
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion chain/chain/src/resharding/event_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ mod tests {

// Shard layouts V0 and V1 are rejected.
assert!(ReshardingEventType::from_shard_layout(
&ShardLayout::v0_single_shard(),
&ShardLayout::single_shard(),
block,
prev_block
)
Expand Down
2 changes: 1 addition & 1 deletion chain/epoch-manager/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn epoch_config_with_production_config(
num_chunk_producer_seats,
..Default::default()
},
shard_layout: ShardLayout::v0(num_shards, 0),
shard_layout: ShardLayout::of_num_shards(num_shards, 0),
validator_max_kickout_stake_perc: 100,
};
AllEpochConfig::new(use_production_config, PROTOCOL_VERSION, epoch_config, "test-chain")
Expand Down
6 changes: 3 additions & 3 deletions chain/epoch-manager/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2294,7 +2294,7 @@ fn test_protocol_version_switch_with_shard_layout_change() {
epoch_manager.get_epoch_info(&epochs[1]).unwrap().protocol_version(),
new_protocol_version - 1
);
assert_eq!(epoch_manager.get_shard_layout(&epochs[1]).unwrap(), ShardLayout::v0_single_shard(),);
assert_eq!(epoch_manager.get_shard_layout(&epochs[1]).unwrap(), ShardLayout::single_shard(),);
assert_eq!(
epoch_manager.get_epoch_info(&epochs[2]).unwrap().protocol_version(),
new_protocol_version
Expand Down Expand Up @@ -2331,7 +2331,7 @@ fn test_protocol_version_switch_with_many_seats() {
online_max_threshold: Ratio::new(99, 100),
protocol_upgrade_stake_threshold: Ratio::new(80, 100),
minimum_stake_divisor: 1,
shard_layout: ShardLayout::v0_single_shard(),
shard_layout: ShardLayout::single_shard(),
validator_selection_config: Default::default(),
validator_max_kickout_stake_perc: 100,
};
Expand Down Expand Up @@ -3237,7 +3237,7 @@ fn test_chunk_validator_exempted() {

#[test]
/// Tests the case where a chunk validator has low endorsement stats and is kicked out (not exempted).
/// In this test, first 3 accounts are block and chunk producers and next 2 are chunk validator only.
/// In this test, first 3 accounts are block and chunk producers and next 2 are chunk validator only.
fn test_chunk_validator_kicked_out_for_low_endorsement() {
test_chunk_validator_kickout(
HashMap::from([(
Expand Down
4 changes: 2 additions & 2 deletions core/chain-configs/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn default_protocol_upgrade_stake_threshold() -> Rational32 {
}

fn default_shard_layout() -> ShardLayout {
ShardLayout::v0_single_shard()
ShardLayout::v0(1, 0)
}

fn default_minimum_stake_ratio() -> Rational32 {
Expand Down Expand Up @@ -194,7 +194,7 @@ pub struct GenesisConfig {
pub minimum_stake_divisor: u64,
/// Layout information regarding how to split accounts to shards
#[serde(default = "default_shard_layout")]
#[default(ShardLayout::v0_single_shard())]
#[default(ShardLayout::v0(1, 0))]
pub shard_layout: ShardLayout,
#[serde(default = "default_num_chunk_only_producer_seats")]
#[default(300)]
Expand Down
4 changes: 2 additions & 2 deletions core/chain-configs/src/test_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl TestGenesisBuilder {
pub fn epoch_config_mut(&mut self) -> &mut EpochConfig {
if self.epoch_config.is_none() {
let mut epoch_config =
Genesis::test_epoch_config(1, ShardLayout::v0_single_shard(), 100);
Genesis::test_epoch_config(1, ShardLayout::single_shard(), 100);
epoch_config.block_producer_kickout_threshold = 0;
epoch_config.chunk_producer_kickout_threshold = 0;
epoch_config.chunk_validator_only_kickout_threshold = 0;
Expand Down Expand Up @@ -121,7 +121,7 @@ impl TestGenesisBuilder {
}

pub fn shard_layout_single(&mut self) -> &mut Self {
self.epoch_config_mut().shard_layout = ShardLayout::v0_single_shard();
self.epoch_config_mut().shard_layout = ShardLayout::single_shard();
self
}

Expand Down
2 changes: 1 addition & 1 deletion core/chain-configs/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl Genesis {
accounts,
num_validator_seats,
vec![num_validator_seats],
ShardLayout::v0_single_shard(),
ShardLayout::single_shard(),
)
}

Expand Down
31 changes: 28 additions & 3 deletions core/primitives/src/shard_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,34 @@ pub enum ShardLayoutError {
}

impl ShardLayout {
/* Some constructors */
pub fn v0_single_shard() -> Self {
Self::v0(1, 0)
/// Construct a layout with a single shard
pub fn single_shard() -> Self {
Self::of_num_shards(1, 0)
}

/// Construct a layout with given number of shards
pub fn of_num_shards(num_shards: NumShards, version: ShardVersion) -> Self {
assert!(num_shards > 0, "at least 1 shard is required");

let boundary_accounts = (0..num_shards - 1)
.map(|i| format!("shard{}.test.near", i).parse().unwrap())
.collect::<Vec<AccountId>>();
let shard_ids = (0..num_shards).map(ShardId::new).collect::<Vec<ShardId>>();
let (id_to_index_map, index_to_id_map) = shard_ids
.iter()
.enumerate()
.map(|(i, &shard_id)| ((shard_id, i), (i, shard_id)))
.unzip();

Self::V2(ShardLayoutV2 {
boundary_accounts,
shard_ids,
id_to_index_map,
index_to_id_map,
shards_split_map: None,
shards_parent_map: None,
version,
})
}

/// Return a V0 Shardlayout
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/client/flat_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ fn test_catchup_succeeds_even_if_no_new_blocks() {
init_test_logger();
let genesis = Genesis::test(vec!["test0".parse().unwrap()], 1);
let store = create_test_store().flat_store();
let shard_uid = ShardLayout::v0_single_shard().shard_uids().next().unwrap();
let shard_uid = ShardLayout::single_shard().shard_uids().next().unwrap();

// Process some blocks with flat storage. Then remove flat storage data from disk.
{
Expand Down Expand Up @@ -503,7 +503,7 @@ fn test_flat_storage_iter() {
fn test_not_supported_block() {
init_test_logger();
let genesis = Genesis::test(vec!["test0".parse().unwrap()], 1);
let shard_layout = ShardLayout::v0_single_shard();
let shard_layout = ShardLayout::single_shard();
let shard_uid = shard_layout.shard_uids().next().unwrap();
let store = create_test_store();

Expand Down
2 changes: 1 addition & 1 deletion nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ pub fn init_configs(
1,
)
} else {
ShardLayout::v0_single_shard()
ShardLayout::v0(1, 0)
};

let genesis_config = GenesisConfig {
Expand Down

0 comments on commit e8a78cc

Please sign in to comment.