Skip to content

Commit

Permalink
refactor: Introduce constants for commonly used chain names (#9666)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikurt authored Oct 11, 2023
1 parent e685e72 commit ae1c618
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion chain/indexer/src/streamer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async fn build_streamer_message(
// ExecutionOutcomes appear.
// ref: https://github.com/near/nearcore/pull/4248
if PROBLEMATIC_BLOCKS.contains(&block.header.hash)
&& &protocol_config_view.chain_id == "mainnet"
&& &protocol_config_view.chain_id == near_primitives::chains::MAINNET
{
let mut restored_receipts: Vec<views::ReceiptView> = vec![];
let receipt_ids_included: std::collections::HashSet<CryptoHash> =
Expand Down
4 changes: 3 additions & 1 deletion core/chain-configs/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ pub struct GenesisConfig {

impl GenesisConfig {
pub fn use_production_config(&self) -> bool {
self.use_production_config || self.chain_id == "testnet" || self.chain_id == "mainnet"
self.use_production_config
|| self.chain_id == near_primitives::chains::TESTNET
|| self.chain_id == near_primitives::chains::MAINNET
}
}

Expand Down
13 changes: 13 additions & 0 deletions core/primitives/src/chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// Chain IDs of commonly used environment.

/// Main production environment.
pub const MAINNET: &str = "mainnet";

/// Primary testing environment.
pub const TESTNET: &str = "testnet";

/// Used for testing certain new protocol features.
pub const STAKEWARS: &str = "stakewars";

/// Nightly testing.
pub const BETANET: &str = "betanet";
1 change: 1 addition & 0 deletions core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use near_primitives_core::serialize;
pub mod action;
pub mod block;
pub mod block_header;
pub mod chains;
pub mod challenge;
pub mod epoch_manager;
pub mod epoch_sync;
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/runtime/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl RuntimeConfigStore {
/// need to override it specifically to preserve compatibility.
pub fn for_chain_id(chain_id: &str) -> Self {
match chain_id {
"testnet" => {
crate::chains::TESTNET => {
let genesis_runtime_config = RuntimeConfig::initial_testnet_config();
Self::new(Some(&genesis_runtime_config))
}
Expand Down
5 changes: 3 additions & 2 deletions genesis-tools/genesis-csv-to-json/src/csv_to_json_configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ const ACCOUNTS_FILE: &str = "accounts.csv";
const NUM_SHARDS: NumShards = 8;

fn verify_total_supply(total_supply: Balance, chain_id: &str) {
if chain_id == "mainnet" {
if chain_id == near_primitives::chains::MAINNET {
assert_eq!(
total_supply,
1_000_000_000 * NEAR_BASE,
"Total supply should be exactly 1 billion"
);
} else if total_supply > 10_000_000_000 * NEAR_BASE
&& (chain_id == "testnet" || chain_id == "stakewars")
&& (chain_id == near_primitives::chains::TESTNET
|| chain_id == near_primitives::chains::STAKEWARS)
{
panic!("Total supply should not be more than 10 billion");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn process_blocks_with_storage_usage_fix(
fn test_fix_storage_usage_migration() {
init_test_logger();
process_blocks_with_storage_usage_fix(
"mainnet".to_string(),
near_primitives::chains::MAINNET.to_string(),
|account_id: AccountId, block_height: u64, storage_usage: u64| {
if account_id.as_ref() == "near" && block_height >= 11 {
assert_eq!(storage_usage, 4378);
Expand All @@ -77,7 +77,7 @@ fn test_fix_storage_usage_migration() {
},
);
process_blocks_with_storage_usage_fix(
"testnet".to_string(),
near_primitives::chains::TESTNET.to_string(),
|_: AccountId, _: u64, storage_usage: u64| {
assert_eq!(storage_usage, 182);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,25 +119,25 @@ fn run_test(
#[test]
fn test_no_chunks_missing() {
// If there are no chunks missing, all receipts should be applied
run_test("mainnet", 1, 0, true);
run_test(near_primitives::chains::MAINNET, 1, 0, true);
}

#[test]
fn test_first_chunk_in_epoch_missing() {
// If the first chunk in the first epoch with needed protocol version is missing,
// all receipts should still be applied
run_test("mainnet", 8, 12, true);
run_test(near_primitives::chains::MAINNET, 8, 12, true);
}

#[test]
fn test_all_chunks_in_epoch_missing() {
// If all chunks are missing in the first epoch, no receipts should be applied
run_test("mainnet", 11, 11 + EPOCH_LENGTH, false);
run_test(near_primitives::chains::MAINNET, 11, 11 + EPOCH_LENGTH, false);
}

#[test]
fn test_run_for_testnet() {
// Run the same process for chain other than mainnet to ensure that blocks are produced
// successfully during the protocol upgrade.
run_test("testnet", 1, 0, true);
run_test(near_primitives::chains::TESTNET, 1, 0, true);
}
19 changes: 11 additions & 8 deletions nearcore/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ pub const GENESIS_CONFIG_FILENAME: &str = "genesis.json";
pub const NODE_KEY_FILE: &str = "node_key.json";
pub const VALIDATOR_KEY_FILE: &str = "validator_key.json";

pub const MAINNET: &str = "mainnet";
pub const TESTNET: &str = "testnet";
pub const BETANET: &str = "betanet";

pub const MAINNET_TELEMETRY_URL: &str = "https://explorer.mainnet.near.org/api/nodes";
pub const NETWORK_TELEMETRY_URL: &str = "https://explorer.{}.near.org/api/nodes";

Expand Down Expand Up @@ -894,7 +890,9 @@ fn generate_or_load_keys(
) -> anyhow::Result<()> {
generate_or_load_key(dir, &config.node_key_file, Some("node".parse().unwrap()), None)?;
match chain_id {
MAINNET | TESTNET | BETANET => {
near_primitives::chains::MAINNET
| near_primitives::chains::TESTNET
| near_primitives::chains::BETANET => {
generate_or_load_key(dir, &config.validator_key_file, account_id, None)?;
}
_ => {
Expand Down Expand Up @@ -979,7 +977,7 @@ pub fn init_configs(
// Before finalizing the Config and Genesis, make sure the node and validator keys exist.
generate_or_load_keys(dir, &config, &chain_id, account_id, test_seed)?;
match chain_id.as_ref() {
MAINNET => {
near_primitives::chains::MAINNET => {
if test_seed.is_some() {
bail!("Test seed is not supported for {chain_id}");
}
Expand All @@ -998,7 +996,7 @@ pub fn init_configs(
genesis.to_file(dir.join(config.genesis_file));
info!(target: "near", "Generated mainnet genesis file in {}", dir.display());
}
TESTNET | BETANET => {
near_primitives::chains::TESTNET | near_primitives::chains::BETANET => {
if test_seed.is_some() {
bail!("Test seed is not supported for {chain_id}");
}
Expand Down Expand Up @@ -1427,7 +1425,12 @@ pub fn load_config(
validation_errors.push_errors(e)
};
if validator_signer.is_some()
&& matches!(genesis.config.chain_id.as_ref(), MAINNET | TESTNET | BETANET)
&& matches!(
genesis.config.chain_id.as_ref(),
near_primitives::chains::MAINNET
| near_primitives::chains::TESTNET
| near_primitives::chains::BETANET
)
&& config.tracked_shards.is_empty()
{
// Make sure validators tracks all shards, see
Expand Down
14 changes: 8 additions & 6 deletions nearcore/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use near_store::{DBCol, Store};
/// Fix an issue with block ordinal (#5761)
// This migration takes at least 3 hours to complete on mainnet
pub fn migrate_30_to_31(store: &Store, near_config: &crate::NearConfig) -> anyhow::Result<()> {
if near_config.client_config.archive && &near_config.genesis.config.chain_id == "mainnet" {
if near_config.client_config.archive
&& &near_config.genesis.config.chain_id == near_primitives::chains::MAINNET
{
do_migrate_30_to_31(store, &near_config.genesis.config)?;
}
Ok(())
Expand Down Expand Up @@ -56,7 +58,7 @@ pub fn do_migrate_30_to_31(
const GAS_USED_FOR_STORAGE_USAGE_DELTA_MIGRATION: Gas = 1_000_000_000_000_000;

pub fn load_migration_data(chain_id: &str) -> MigrationData {
let is_mainnet = chain_id == "mainnet";
let is_mainnet = chain_id == near_primitives::chains::MAINNET;
MigrationData {
storage_usage_delta: if is_mainnet {
near_mainnet_res::mainnet_storage_usage_delta()
Expand Down Expand Up @@ -132,9 +134,9 @@ mod tests {
.to_string(),
"2fEgaLFBBJZqgLQEvHPsck4NS3sFzsgyKaMDqTw5HVvQ"
);
let mainnet_migration_data = load_migration_data("mainnet");
let mainnet_migration_data = load_migration_data(near_primitives::chains::MAINNET);
assert_eq!(mainnet_migration_data.storage_usage_delta.len(), 3112);
let testnet_migration_data = load_migration_data("testnet");
let testnet_migration_data = load_migration_data(near_primitives::chains::TESTNET);
assert_eq!(testnet_migration_data.storage_usage_delta.len(), 0);
}

Expand All @@ -145,9 +147,9 @@ mod tests {
.to_string(),
"48ZMJukN7RzvyJSW9MJ5XmyQkQFfjy2ZxPRaDMMHqUcT"
);
let mainnet_migration_data = load_migration_data("mainnet");
let mainnet_migration_data = load_migration_data(near_primitives::chains::MAINNET);
assert_eq!(mainnet_migration_data.restored_receipts.get(&0u64).unwrap().len(), 383);
let testnet_migration_data = load_migration_data("testnet");
let testnet_migration_data = load_migration_data(near_primitives::chains::TESTNET);
assert!(testnet_migration_data.restored_receipts.is_empty());
}
}
10 changes: 6 additions & 4 deletions neard/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ fn check_release_build(chain: &str) {
let is_release_build = option_env!("NEAR_RELEASE_BUILD") == Some("release")
&& !cfg!(feature = "nightly")
&& !cfg!(feature = "nightly_protocol");
if !is_release_build && ["mainnet", "testnet"].contains(&chain) {
if !is_release_build
&& [near_primitives::chains::MAINNET, near_primitives::chains::TESTNET].contains(&chain)
{
warn!(
target: "neard",
"Running a neard executable which wasn’t built with `make release` \
Expand Down Expand Up @@ -490,9 +492,9 @@ impl RunCmd {

#[cfg(feature = "sandbox")]
{
if near_config.client_config.chain_id == "mainnet"
|| near_config.client_config.chain_id == "testnet"
|| near_config.client_config.chain_id == "betanet"
if near_config.client_config.chain_id == near_primitives::chains::MAINNET
|| near_config.client_config.chain_id == near_primitives::chains::TESTNET
|| near_config.client_config.chain_id == near_primitives::chains::BETANET
{
eprintln!(
"Sandbox node can only run dedicate localnet, cannot connect to a network"
Expand Down
6 changes: 3 additions & 3 deletions tools/chainsync-loadtest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use std::sync::Arc;

fn genesis_hash(chain_id: &str) -> CryptoHash {
return match chain_id {
"mainnet" => "EPnLgE7iEq9s7yTkos96M3cWymH5avBAPm3qx3NXqR8H",
"testnet" => "FWJ9kR6KFWoyMoNjpLXXGHeuiy7tEY6GmoFeCA5yuc6b",
"betanet" => "6hy7VoEJhPEUaJr1d5ePBhKdgeDWKCjLoUAn7XS9YPj",
near_primitives::chains::MAINNET => "EPnLgE7iEq9s7yTkos96M3cWymH5avBAPm3qx3NXqR8H",
near_primitives::chains::TESTNET => "FWJ9kR6KFWoyMoNjpLXXGHeuiy7tEY6GmoFeCA5yuc6b",
near_primitives::chains::BETANET => "6hy7VoEJhPEUaJr1d5ePBhKdgeDWKCjLoUAn7XS9YPj",
_ => {
return Default::default();
}
Expand Down
4 changes: 2 additions & 2 deletions tools/ping/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ pub struct ChainInfo {

pub static CHAIN_INFO: &[ChainInfo] = &[
ChainInfo {
chain_id: "mainnet",
chain_id: near_primitives::chains::MAINNET,
genesis_hash: CryptoHash([
198, 253, 249, 28, 142, 130, 248, 249, 23, 204, 25, 117, 233, 222, 28, 100, 190, 17,
137, 158, 50, 29, 253, 245, 254, 188, 251, 183, 49, 63, 20, 134,
]),
},
ChainInfo {
chain_id: "testnet",
chain_id: near_primitives::chains::TESTNET,
genesis_hash: CryptoHash([
215, 132, 218, 90, 158, 94, 102, 102, 133, 22, 193, 154, 128, 149, 68, 143, 197, 74,
34, 162, 137, 113, 220, 51, 15, 0, 153, 223, 148, 55, 148, 16,
Expand Down

0 comments on commit ae1c618

Please sign in to comment.