Skip to content

Commit

Permalink
NDRS-797: update chainspec structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser999 committed Feb 2, 2021
1 parent b2e4b04 commit 7a7dd02
Show file tree
Hide file tree
Showing 65 changed files with 1,846 additions and 1,675 deletions.
8 changes: 4 additions & 4 deletions execution_engine/src/shared/system_config.rs
Expand Up @@ -18,7 +18,7 @@ use crate::storage::protocol_data::DEFAULT_WASMLESS_TRANSFER_COST;
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Debug, DataSize)]
pub struct SystemConfig {
/// Wasmless transfer cost expressed in gas.
wasmless_transfer_cost: u64,
wasmless_transfer_cost: u32,

/// Configuration of auction entrypoint costs.
auction_costs: AuctionCosts,
Expand All @@ -35,7 +35,7 @@ pub struct SystemConfig {

impl SystemConfig {
pub fn new(
wasmless_transfer_cost: u64,
wasmless_transfer_cost: u32,
auction_costs: AuctionCosts,
mint_costs: MintCosts,
proof_of_stake_costs: ProofOfStakeCosts,
Expand All @@ -50,7 +50,7 @@ impl SystemConfig {
}
}

pub fn wasmless_transfer_cost(&self) -> u64 {
pub fn wasmless_transfer_cost(&self) -> u32 {
self.wasmless_transfer_cost
}

Expand Down Expand Up @@ -149,7 +149,7 @@ pub mod gens {

prop_compose! {
pub fn system_config_arb()(
wasmless_transfer_cost in num::u64::ANY,
wasmless_transfer_cost in num::u32::ANY,
auction_costs in auction_costs_arb(),
mint_costs in mint_costs_arb(),
proof_of_stake_costs in proof_of_stake_costs_arb(),
Expand Down
2 changes: 1 addition & 1 deletion execution_engine/src/storage/protocol_data.rs
Expand Up @@ -8,7 +8,7 @@ use casper_types::{
use crate::shared::{system_config::SystemConfig, wasm_config::WasmConfig};

const DEFAULT_ADDRESS: [u8; 32] = [0; 32];
pub const DEFAULT_WASMLESS_TRANSFER_COST: u64 = 10_000;
pub const DEFAULT_WASMLESS_TRANSFER_COST: u32 = 10_000;

/// Represents a protocol's data. Intended to be associated with a given protocol version.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down
6 changes: 3 additions & 3 deletions node/src/components/block_proposer.rs
Expand Up @@ -22,12 +22,12 @@ use semver::Version;
use tracing::{debug, error, info, trace, warn};

use crate::{
components::{chainspec_loader::DeployConfig, Component},
components::Component,
effect::{
requests::{BlockProposerRequest, ProtoBlockRequest, StateStoreRequest, StorageRequest},
EffectBuilder, EffectExt, Effects,
},
types::{DeployHash, DeployHeader, ProtoBlock, Timestamp},
types::{chainspec::DeployConfig, DeployHash, DeployHeader, ProtoBlock, Timestamp},
NodeRng,
};
use casper_execution_engine::shared::gas::Gas;
Expand Down Expand Up @@ -155,7 +155,7 @@ where
sets: sets
.unwrap_or_default()
.with_next_finalized(next_finalized_block),
deploy_config: chainspec.genesis.deploy_config,
deploy_config: chainspec.deploy_config,
state_key: deploy_sets::create_storage_key(&chainspec),
request_queue: Default::default(),
unhandled_finalized: Default::default(),
Expand Down
7 changes: 2 additions & 5 deletions node/src/components/block_proposer/deploy_sets.rs
Expand Up @@ -7,10 +7,7 @@ use datasize::DataSize;
use serde::{Deserialize, Serialize};

use super::{event::DeployType, BlockHeight, FinalizationQueue};
use crate::{
types::{DeployHash, DeployHeader, Timestamp},
Chainspec,
};
use crate::types::{Chainspec, DeployHash, DeployHeader, Timestamp};

/// Stores the internal state of the BlockProposer.
#[derive(Clone, DataSize, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -71,7 +68,7 @@ impl Display for BlockProposerDeploySets {
pub fn create_storage_key(chainspec: &Chainspec) -> Vec<u8> {
format!(
"block_proposer_deploy_sets:version={},chain_name={}",
chainspec.genesis.protocol_version, chainspec.genesis.name
chainspec.protocol_config.version, chainspec.network_config.name
)
.into()
}
Expand Down
3 changes: 1 addition & 2 deletions node/src/components/block_proposer/event.rs
Expand Up @@ -11,8 +11,7 @@ use serde::{Deserialize, Serialize};
use super::{BlockHeight, BlockProposerDeploySets};
use crate::{
effect::requests::BlockProposerRequest,
types::{DeployHash, DeployHeader, ProtoBlock},
Chainspec,
types::{Chainspec, DeployHash, DeployHeader, ProtoBlock},
};
use casper_execution_engine::shared::motes::Motes;

Expand Down
8 changes: 4 additions & 4 deletions node/src/components/block_proposer/tests.rs
@@ -1,18 +1,18 @@
use casper_execution_engine::core::engine_state::executable_deploy_item::ExecutableDeployItem;
use casper_types::{
bytesrepr::{Bytes, ToBytes},
runtime_args, RuntimeArgs, SecretKey,
runtime_args,
standard_payment::ARG_AMOUNT,
RuntimeArgs, SecretKey,
};

use super::*;
use crate::{
crypto::AsymmetricKeyExt,
testing::TestRng,
types::{BlockLike, Deploy, DeployHash, TimeDiff},
};

use super::*;
use casper_types::standard_payment::ARG_AMOUNT;

const DEFAULT_TEST_GAS_PRICE: u64 = 1;

fn default_gas_payment() -> Gas {
Expand Down
12 changes: 6 additions & 6 deletions node/src/components/block_validator.rs
Expand Up @@ -29,8 +29,8 @@ use crate::{
requests::{BlockValidationRequest, FetcherRequest, StorageRequest},
EffectBuilder, EffectExt, EffectOptionExt, Effects, Responder,
},
types::{BlockLike, Deploy, DeployHash},
Chainspec, NodeRng,
types::{BlockLike, Chainspec, Deploy, DeployHash},
NodeRng,
};
use keyed_counter::KeyedCounter;

Expand Down Expand Up @@ -151,10 +151,10 @@ where
move |result: FetchResult<Deploy, I>| match result {
FetchResult::FromStorage(deploy)
| FetchResult::FromPeer(deploy, _) => {
if deploy.header().is_valid(
&chainspec.genesis.deploy_config,
block_timestamp,
) {
if deploy
.header()
.is_valid(&chainspec.deploy_config, block_timestamp)
{
Event::DeployFound(deploy_hash)
} else {
Event::DeployMissing(deploy_hash)
Expand Down
91 changes: 65 additions & 26 deletions node/src/components/chainspec_loader.rs
@@ -1,18 +1,16 @@
//! Chainspec loader component.
//!
//! The chainspec loader initializes a node by reading information from the chainspec, running
//! initial system contracts and storing the result in the permanent storage. This kind of
//! initialization only happens at genesis.
//! The chainspec loader initializes a node by reading information from the chainspec, and
//! committing it to the permanent storage.
//!
//! See
//! <https://casperlabs.atlassian.net/wiki/spaces/EN/pages/135528449/Genesis+Process+Specification>
//! for full details.

mod chainspec;
mod config;
mod error;

use std::fmt::{self, Display, Formatter};
use std::{
fmt::{self, Display, Formatter},
path::{Path, PathBuf},
};

use datasize::DataSize;
use derive_more::From;
Expand All @@ -23,6 +21,8 @@ use tracing::{debug, error, info, trace};

use casper_execution_engine::core::engine_state::{self, genesis::GenesisResult};

#[cfg(test)]
use crate::utils::RESOURCES_PATH;
use crate::{
components::Component,
crypto::hash::Digest,
Expand All @@ -31,11 +31,10 @@ use crate::{
EffectBuilder, EffectExt, Effects,
},
rpcs::docs::DocExample,
types::{chainspec::Error, Chainspec},
utils::Loadable,
NodeRng,
};
pub use chainspec::Chainspec;
pub(crate) use chainspec::{DeployConfig, HighwayConfig, UpgradePoint};
pub use error::Error;

static CHAINSPEC_INFO: Lazy<ChainspecInfo> = Lazy::new(|| ChainspecInfo {
name: String::from("casper-example"),
Expand Down Expand Up @@ -101,41 +100,81 @@ impl DocExample for ChainspecInfo {
impl From<ChainspecLoader> for ChainspecInfo {
fn from(chainspec_loader: ChainspecLoader) -> Self {
ChainspecInfo::new(
chainspec_loader.chainspec.genesis.name.clone(),
chainspec_loader.chainspec.network_config.name.clone(),
chainspec_loader.genesis_state_root_hash,
)
}
}

#[derive(Clone, DataSize, Debug, Serialize, Deserialize)]
#[derive(Clone, DataSize, Debug)]
pub struct ChainspecLoader {
chainspec: Chainspec,
// If `Some`, we're finished. The value of the bool indicates success (true) or not.
/// The path to the folder where all chainspec and upgrade_point files will be stored in
/// subdirs corresponding to their versions.
root_dir: PathBuf,
/// If `Some`, we're finished. The value of the bool indicates success (true) or not.
completed_successfully: Option<bool>,
// If `Some` then genesis process returned a valid state root hash.
/// If `Some` then genesis process returned a valid state root hash.
genesis_state_root_hash: Option<Digest>,
}

impl ChainspecLoader {
pub(crate) fn new<REv>(
pub(crate) fn new<P, REv>(
chainspec_dir: P,
effect_builder: EffectBuilder<REv>,
) -> Result<(Self, Effects<Event>), Error>
where
P: AsRef<Path>,
REv: From<Event> + From<StorageRequest> + Send,
{
Ok(Self::new_with_chainspec_and_path(
Chainspec::from_path(&chainspec_dir.as_ref())?,
chainspec_dir,
effect_builder,
))
}

#[cfg(test)]
pub(crate) fn new_with_chainspec<REv>(
chainspec: Chainspec,
effect_builder: EffectBuilder<REv>,
) -> (Self, Effects<Event>)
where
REv: From<Event> + From<StorageRequest> + Send,
{
Self::new_with_chainspec_and_path(chainspec, &RESOURCES_PATH.join("local"), effect_builder)
}

fn new_with_chainspec_and_path<P, REv>(
chainspec: Chainspec,
chainspec_dir: P,
effect_builder: EffectBuilder<REv>,
) -> (Self, Effects<Event>)
where
P: AsRef<Path>,
REv: From<Event> + From<StorageRequest> + Send,
{
let version = chainspec.genesis.protocol_version.clone();
chainspec.validate_config();
let root_dir = chainspec_dir
.as_ref()
.parent()
.unwrap_or_else(|| {
panic!("chainspec dir must have a parent");
})
.to_path_buf();

let version = chainspec.protocol_config.version.clone();
let effects = effect_builder
.put_chainspec(chainspec.clone())
.event(|_| Event::PutToStorage { version });
(
ChainspecLoader {
chainspec,
completed_successfully: None,
genesis_state_root_hash: None,
},
effects,
)
let chainspec_loader = ChainspecLoader {
chainspec,
root_dir,
completed_successfully: None,
genesis_state_root_hash: None,
};

(chainspec_loader, effects)
}

pub(crate) fn is_stopped(&self) -> bool {
Expand Down Expand Up @@ -192,7 +231,7 @@ where
post_state_hash,
effect,
} => {
info!("chainspec name {}", self.chainspec.genesis.name);
info!("chainspec name {}", self.chainspec.network_config.name);
info!("genesis state root hash {}", post_state_hash);
trace!(%post_state_hash, ?effect);
self.completed_successfully = Some(true);
Expand Down

0 comments on commit 7a7dd02

Please sign in to comment.