Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genesis-builder: Add option to add history root #1733

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions genesis-builder/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ pub struct GenesisConfig {
#[serde(default)]
pub seed_message: Option<String>,

/// Timestamp for the genesis block.
#[serde(with = "time::serde::rfc3339::option")]
pub timestamp: Option<OffsetDateTime>,

/// VRF seed for the genesis block.
pub vrf_seed: Option<VrfSeed>,

Expand All @@ -24,14 +28,13 @@ pub struct GenesisConfig {
/// Hash of the parent block for the genesis block.
pub parent_hash: Option<Blake2bHash>,

/// Merkle root over all of the transactions previous the genesis block.
pub history_root: Option<Blake2bHash>,

/// The genesis block number.
#[serde(default)]
pub block_number: u32,

/// Timestamp for the genesis block.
#[serde(with = "time::serde::rfc3339::option")]
pub timestamp: Option<OffsetDateTime>,

/// The set of validators for the genesis state.
#[serde(default)]
pub validators: Vec<GenesisValidator>,
Expand Down
21 changes: 16 additions & 5 deletions genesis-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ pub enum GenesisBuilderError {
#[error("No VRF seed to generate genesis block")]
NoVrfSeed,
/// Serialization failed.
#[error("Serialization failed")]
#[error("Serialization failed: {0}")]
SerializingError(#[from] DeserializeError),
/// I/O error
#[error("I/O error")]
#[error("I/O error: {0}")]
IoError(#[from] IoError),
/// Failure at parsing TOML file
#[error("Failed to parse TOML file")]
#[error("Failed to parse TOML file: {0}")]
TomlError(#[from] TomlError),
/// Failure at staking
#[error("Failed to stake")]
#[error("Failed to stake: {0}")]
StakingError(#[from] AccountError),
}

Expand Down Expand Up @@ -78,6 +78,8 @@ pub struct GenesisBuilder {
pub parent_hash: Option<Blake2bHash>,
/// The parent election hash of the genesis block.
pub parent_election_hash: Option<Blake2bHash>,
/// Merkle root over all of the transactions previous the genesis block.
pub history_root: Option<Blake2bHash>,
/// The set of validators for the genesis state.
pub validators: Vec<config::GenesisValidator>,
/// The set of stakers for the genesis state.
Expand Down Expand Up @@ -106,6 +108,7 @@ impl GenesisBuilder {
vrf_seed: None,
parent_election_hash: None,
parent_hash: None,
history_root: None,
validators: vec![],
stakers: vec![],
basic_accounts: vec![],
Expand Down Expand Up @@ -151,6 +154,11 @@ impl GenesisBuilder {
self
}

pub fn with_history_root(&mut self, history_root: Blake2bHash) -> &mut Self {
self.history_root = Some(history_root);
self
}

pub fn with_genesis_validator(
&mut self,
validator_address: Address,
Expand Down Expand Up @@ -197,6 +205,7 @@ impl GenesisBuilder {
vrf_seed,
parent_election_hash,
parent_hash,
history_root,
block_number,
mut validators,
mut stakers,
Expand All @@ -209,6 +218,7 @@ impl GenesisBuilder {
timestamp.map(|t| self.with_timestamp(t));
parent_election_hash.map(|hash| self.with_parent_election_hash(hash));
parent_hash.map(|hash| self.with_parent_hash(hash));
history_root.map(|history_root| self.with_history_root(history_root));
self.validators.append(&mut validators);
self.stakers.append(&mut stakers);
self.basic_accounts.append(&mut basic_accounts);
Expand All @@ -223,6 +233,7 @@ impl GenesisBuilder {
let timestamp = self.timestamp.unwrap_or_else(OffsetDateTime::now_utc);
let parent_election_hash = self.parent_election_hash.clone().unwrap_or_default();
let parent_hash = self.parent_hash.clone().unwrap_or_default();
let history_root = self.history_root.clone().unwrap_or_default();

// Initialize the accounts.
let accounts = Accounts::new(env.clone());
Expand Down Expand Up @@ -351,7 +362,7 @@ impl GenesisBuilder {
state_root,
body_root,
diff_root: TreeProof::empty().root_hash(),
history_root: Blake2bHash::default(),
history_root,
};

// Genesis hash
Expand Down
Loading