Skip to content

Commit

Permalink
extract block0 builder only
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Sep 24, 2021
1 parent 0b62d60 commit 4fedbc5
Showing 1 changed file with 38 additions and 38 deletions.
Expand Up @@ -4,18 +4,18 @@ use crate::common::{
jormungandr::JormungandrProcess,
startup::{build_genesis_block, create_new_key_pair},
};
use assert_fs::fixture::{ChildPath, PathChild};
use chain_addr::Discrimination;
use chain_crypto::Ed25519;
use chain_impl_mockchain::{chaintypes::ConsensusVersion, fee::LinearFee};
use jormungandr_lib::crypto::key::KeyPair;
use jormungandr_lib::interfaces::Block0Configuration;
use jormungandr_lib::interfaces::{
ActiveSlotCoefficient, CommitteeIdDef, ConsensusLeaderId, EpochStabilityDepth, FeesGoTo,
Initial, InitialUTxO, KesUpdateSpeed, Log, LogEntry, LogOutput, Mempool, NodeConfig,
NodeSecret, NumberOfSlotsPerEpoch, Policy, SignedCertificate, SlotDuration, Tls, TrustedPeer,
Value,
};

use assert_fs::fixture::{ChildPath, PathChild};
use chain_addr::Discrimination;
use jormungandr_testing_utils::{
testing::{
Block0ConfigurationBuilder, JormungandrParams, NodeConfigBuilder, SecretModelFactory,
Expand Down Expand Up @@ -46,7 +46,7 @@ pub struct ConfigurationBuilder {
block_content_max_size: u32,
configure_default_log: bool,
committee_ids: Vec<CommitteeIdDef>,
leader_key_pair: Option<KeyPair<Ed25519>>,
leader_key_pair: KeyPair<Ed25519>,
discrimination: Discrimination,
tx_max_expiry_epochs: Option<u8>,
}
Expand Down Expand Up @@ -77,7 +77,7 @@ impl ConfigurationBuilder {
rewards_history: false,
configure_default_log: true,
committee_ids: vec![],
leader_key_pair: None,
leader_key_pair: create_new_key_pair::<Ed25519>(),
fees_go_to: None,
treasury: None,
total_reward_supply: None,
Expand Down Expand Up @@ -269,7 +269,7 @@ impl ConfigurationBuilder {
}

pub fn with_leader_key_pair(&mut self, leader_key_pair: KeyPair<Ed25519>) -> &mut Self {
self.leader_key_pair = Some(leader_key_pair);
self.leader_key_pair = leader_key_pair;
self
}

Expand All @@ -293,36 +293,9 @@ impl ConfigurationBuilder {
self
}

pub fn build(&self, temp_dir: &impl PathChild) -> JormungandrParams<NodeConfig> {
let mut node_config = self.node_config_builder.build();

//remove id from trusted peers
for trusted_peer in node_config.p2p.trusted_peers.iter_mut() {
trusted_peer.id = None;
}

let default_log_file = || temp_dir.child("node.log").path().to_path_buf();

let log_file_path = match (&node_config.log, self.configure_default_log) {
(Some(log), _) => log.file_path().map_or_else(default_log_file, Into::into),
(None, false) => default_log_file(),
(None, true) => {
let path = default_log_file();
node_config.log = Some(Log(LogEntry {
level: "trace".to_string(),
format: "json".to_string(),
output: LogOutput::Stdout,
}));
path
}
};

let leader_key_pair = self
.leader_key_pair
.clone()
.unwrap_or_else(create_new_key_pair::<Ed25519>);
pub fn build_block0(&self) -> Block0Configuration {
let mut leaders_ids = self.consensus_leader_ids.clone();
leaders_ids.push(leader_key_pair.identifier().into());
leaders_ids.push(self.leader_key_pair.identifier().into());

let mut initial: Vec<Initial> = Vec::new();
initial.extend(self.funds.iter().cloned());
Expand All @@ -336,7 +309,7 @@ impl ConfigurationBuilder {
.to_owned();
}

let block0_config = block0_config_builder
block0_config_builder
.with_discrimination(self.discrimination)
.with_initial(initial)
.with_leaders(leaders_ids)
Expand All @@ -352,7 +325,34 @@ impl ConfigurationBuilder {
.with_block_content_max_size(self.block_content_max_size.into())
.with_committee_ids(self.committee_ids.clone())
.with_total_rewards_supply(self.total_reward_supply)
.build();
.build()
}

pub fn build(&self, temp_dir: &impl PathChild) -> JormungandrParams<NodeConfig> {
let mut node_config = self.node_config_builder.build();

//remove id from trusted peers
for trusted_peer in node_config.p2p.trusted_peers.iter_mut() {
trusted_peer.id = None;
}

let default_log_file = || temp_dir.child("node.log").path().to_path_buf();

let log_file_path = match (&node_config.log, self.configure_default_log) {
(Some(log), _) => log.file_path().map_or_else(default_log_file, Into::into),
(None, false) => default_log_file(),
(None, true) => {
let path = default_log_file();
node_config.log = Some(Log(LogEntry {
level: "trace".to_string(),
format: "json".to_string(),
output: LogOutput::Stdout,
}));
path
}
};

let block0_config = self.build_block0();

let path_to_output_block = build_genesis_block(&block0_config, temp_dir);
let genesis_block_hash = match self.block0_hash {
Expand All @@ -372,7 +372,7 @@ impl ConfigurationBuilder {
let secret = self
.secret
.clone()
.unwrap_or_else(|| SecretModelFactory::bft(leader_key_pair.signing_key()));
.unwrap_or_else(|| SecretModelFactory::bft(self.leader_key_pair.signing_key()));
let output_file = temp_dir.child("node_secret.yaml");
write_secret(&secret, output_file)
};
Expand Down

0 comments on commit 4fedbc5

Please sign in to comment.