Skip to content

Commit

Permalink
added wallet proxy and vit station settings
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Oct 15, 2020
1 parent 06e1714 commit 8b50ae2
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 8 deletions.
1 change: 1 addition & 0 deletions testing/jormungandr-testing-utils/Cargo.toml
Expand Up @@ -25,6 +25,7 @@ chain-time = { path = "../../chain-deps/chain-time" }
cardano-legacy-address = { path = "../../chain-deps/cardano-legacy-address" }
jormungandr-lib = { path = "../../jormungandr-lib" }
jortestkit = { path = "../jortestkit"}
vit-servicing-station-lib = { git = "https://github.com/input-output-hk/vit-servicing-station" }
typed-bytes = { path = "../../chain-deps/typed-bytes" }
rand = "0.7"
rand_core = "0.5"
Expand Down
@@ -1,5 +1,6 @@
use super::{NodeAlias, WalletAlias, WalletTemplate};
use super::{LegacyWalletTemplate, NodeAlias, WalletAlias, WalletTemplate};
pub use chain_impl_mockchain::chaintypes::ConsensusVersion;
use chain_impl_mockchain::testing::scenario::template::VotePlanDef;
use jormungandr_lib::interfaces::{
ActiveSlotCoefficient, KESUpdateSpeed, NumberOfSlotsPerEpoch, SlotDuration,
};
Expand All @@ -11,6 +12,9 @@ pub struct Blockchain {
slots_per_epoch: NumberOfSlotsPerEpoch,
slot_duration: SlotDuration,
leaders: Vec<NodeAlias>,
committees: Vec<WalletAlias>,
vote_plans: Vec<VotePlanDef>,
legacy_wallets: Vec<LegacyWalletTemplate>,
wallets: HashMap<WalletAlias, WalletTemplate>,
kes_update_speed: KESUpdateSpeed,
consensus_genesis_praos_active_slot_coeff: ActiveSlotCoefficient,
Expand All @@ -28,13 +32,47 @@ impl Blockchain {
consensus,
leaders: Vec::new(),
wallets: HashMap::new(),
committees: Vec::new(),
vote_plans: Vec::new(),
legacy_wallets: Vec::new(),
slots_per_epoch,
slot_duration,
kes_update_speed,
consensus_genesis_praos_active_slot_coeff,
}
}

pub fn committees(&self) -> Vec<WalletAlias> {
self.committees.clone()
}

pub fn legacy_wallets(&self) -> Vec<LegacyWalletTemplate> {
self.legacy_wallets.clone()
}

pub fn vote_plans(&self) -> Vec<VotePlanDef> {
self.vote_plans.clone()
}

pub fn vote_plan(&self, alias: &str) -> Option<VotePlanDef> {
self.vote_plans()
.iter()
.cloned()
.find(|x| x.alias() == alias)
}

pub fn add_committee<S: Into<NodeAlias>>(&mut self, alias: S) {
self.committees.push(alias.into())
}

pub fn add_legacy_wallet(&mut self, legacy_wallet: LegacyWalletTemplate) {
self.legacy_wallets.push(legacy_wallet);
}

pub fn add_vote_plan(&mut self, vote_plan_template: VotePlanDef) {
self.vote_plans.push(vote_plan_template);
}

pub fn add_leader<S: Into<NodeAlias>>(&mut self, alias: S) {
self.leaders.push(alias.into())
}
Expand Down
Expand Up @@ -8,11 +8,11 @@ mod wallet;
pub use blockchain::Blockchain;
use chain_impl_mockchain::header::HeaderId;
pub use rng::{Random, Seed};
pub use settings::{NodeSetting, Settings};
pub use settings::{NodeSetting, Settings, WalletProxySettings};
pub use spawn_params::SpawnParams;
use std::path::PathBuf;
pub use topology::{Node, NodeAlias, Topology, TopologyBuilder};
pub use wallet::{Wallet, WalletAlias, WalletTemplate, WalletType};
pub use wallet::{LegacyWalletTemplate, Wallet, WalletAlias, WalletTemplate, WalletType};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LeadershipMode {
Expand Down
@@ -1,19 +1,33 @@
use super::LegacyWalletTemplate;
use crate::testing::network_builder::{
Blockchain as BlockchainTemplate, Node as NodeTemplate, NodeAlias, Random, Wallet, WalletAlias,
WalletTemplate, WalletType,
};
use crate::testing::node::RestSettings;
use crate::{stake_pool::StakePool, testing::signed_stake_pool_cert, wallet::Wallet as WalletLib};
use chain_crypto::Ed25519;
use chain_impl_mockchain::{chaintypes::ConsensusVersion, fee::LinearFee};
use chain_impl_mockchain::{
certificate::VotePlan,
chaintypes::ConsensusVersion,
fee::LinearFee,
testing::{create_initial_vote_plan, scenario::template::VotePlanDef},
vote::CommitteeId,
};
use jormungandr_lib::{
crypto::key::SigningKey,
interfaces::{
ActiveSlotCoefficient, Bft, Block0Configuration, BlockchainConfiguration, GenesisPraos,
Initial, InitialUTxO, NodeConfig, NodeSecret,
try_initials_vec_from_messages, ActiveSlotCoefficient, Bft, Block0Configuration,
BlockchainConfiguration, CommitteeIdDef, GenesisPraos, Initial, InitialUTxO, LegacyUTxO,
NodeConfig, NodeSecret, OldAddress,
},
};
use rand_core::{CryptoRng, RngCore};
use std::collections::{HashMap, HashSet};
use std::net::SocketAddr;
use std::str::FromStr;
use vit_servicing_station_lib::server::settings::ServiceSettings;

pub type VotePlanAlias = String;

/// contains all the data to start or interact with a node
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -56,21 +70,67 @@ impl NodeSetting {
}
}

#[derive(Clone, Debug)]
pub struct WalletProxySettings {
pub proxy_address: SocketAddr,
pub vit_station_address: SocketAddr,
pub node_backend_address: Option<SocketAddr>,
}

impl WalletProxySettings {
pub fn base_address(&self) -> SocketAddr {
self.proxy_address.clone()
}

pub fn base_vit_address(&self) -> SocketAddr {
self.vit_station_address.clone()
}

pub fn base_node_backend_address(&self) -> Option<SocketAddr> {
self.node_backend_address.clone()
}

pub fn address(&self) -> String {
format!("http://{}", self.base_address())
}

pub fn vit_address(&self) -> String {
format!("http://{}", self.base_vit_address())
}

pub fn node_backend_address(&self) -> String {
format!(
"http://{}/api/v0",
self.base_node_backend_address().unwrap()
)
}
}

#[derive(Debug)]
pub struct Settings {
pub nodes: HashMap<NodeAlias, NodeSetting>,

pub vit_stations: HashMap<NodeAlias, ServiceSettings>,

pub wallet_proxies: HashMap<NodeAlias, WalletProxySettings>,

pub wallets: HashMap<WalletAlias, Wallet>,

pub legacy_wallets: HashMap<WalletAlias, LegacyWalletTemplate>,

pub block0: Block0Configuration,

pub stake_pools: HashMap<NodeAlias, StakePool>,

pub vote_plans: HashMap<VotePlanAlias, VotePlan>,
}

impl Settings {
pub fn new<RNG>(
nodes: HashMap<NodeAlias, NodeSetting>,
blockchain: BlockchainTemplate,
vit_stations: HashMap<NodeAlias, ServiceSettings>,
wallet_proxies: HashMap<NodeAlias, WalletProxySettings>,
rng: &mut Random<RNG>,
) -> Self
where
Expand All @@ -87,16 +147,62 @@ impl Settings {
),
initial: Vec::new(),
},
vit_stations,
wallet_proxies: wallet_proxies,
legacy_wallets: HashMap::new(),
stake_pools: HashMap::new(),
vote_plans: HashMap::new(),
};

settings.populate_trusted_peers();
settings.populate_block0_blockchain_configuration(&blockchain, rng);
settings.populate_block0_blockchain_initials(blockchain.wallets(), rng);
settings.populate_block0_blockchain_configuration(&blockchain, rng);
settings.populate_block0_blockchain_legacy(blockchain.legacy_wallets(), rng);
settings.populate_block0_blockchain_vote_plans(blockchain.vote_plans());

println!("{:?}", settings);

settings
}

fn populate_block0_blockchain_legacy<RNG>(
&mut self,
legacy_wallets: Vec<LegacyWalletTemplate>,
rng: &mut Random<RNG>,
) where
RNG: RngCore + CryptoRng,
{
for template in legacy_wallets {
let legacy_fragment = Initial::LegacyFund(vec![LegacyUTxO {
address: template.address().parse().unwrap(),
value: *template.value(),
}]);

self.legacy_wallets
.insert(template.alias().clone(), template.clone());
self.block0.initial.push(legacy_fragment);
}
}

fn populate_block0_blockchain_vote_plans(&mut self, vote_plans: Vec<VotePlanDef>) {
let mut vote_plans_fragments = Vec::new();
for vote_plan_def in vote_plans {
let owner = self.wallets.get(&vote_plan_def.owner()).expect(&format!(
"Owner {} of {} is unknown wallet ",
vote_plan_def.owner(),
vote_plan_def.alias()
));
let vote_plan: VotePlan = vote_plan_def.into();
vote_plans_fragments.push(create_initial_vote_plan(
&vote_plan,
&[owner.clone().into()],
));
}
self.block0
.initial
.extend(try_initials_vec_from_messages(vote_plans_fragments.iter()).unwrap())
}

fn populate_block0_blockchain_configuration<RNG>(
&mut self,
blockchain: &BlockchainTemplate,
Expand Down Expand Up @@ -128,8 +234,20 @@ impl Settings {
}
leader_ids
};
blockchain_configuration.committees = {
let mut committees = Vec::new();
for committee in blockchain.committees() {
let wallet = self
.wallets
.get(&committee)
.expect(&format!("committee not defined {}", committee));
committees.push(CommitteeIdDef::from(wallet.committee_id()));
}
committees
};
blockchain_configuration.slots_per_epoch = *blockchain.slots_per_epoch();
blockchain_configuration.slot_duration = *blockchain.slot_duration();
blockchain_configuration.treasury = Some(1_000_000.into());
// TODO blockchain_configuration.linear_fees = ;
blockchain_configuration.kes_update_speed = *blockchain.kes_update_speed();
blockchain_configuration.consensus_genesis_praos_active_slot_coeff =
Expand Down
Expand Up @@ -4,7 +4,7 @@ use crate::wallet::{
};
use chain_impl_mockchain::{
certificate::PoolId, fee::LinearFee, fragment::Fragment,
transaction::UnspecifiedAccountIdentifier,
transaction::UnspecifiedAccountIdentifier, vote::CommitteeId,
};
use jormungandr_lib::{
crypto::hash::Hash,
Expand Down Expand Up @@ -68,6 +68,43 @@ impl WalletTemplate {
}
}

#[derive(Clone, Debug)]
pub struct LegacyWalletTemplate {
alias: WalletAlias,
address: String,
value: Value,
mnemonics: String,
}

impl LegacyWalletTemplate {
#[inline]
pub fn new<S: Into<WalletAlias>>(
alias: S,
value: Value,
address: String,
mnemonics: String,
) -> Self {
Self {
alias: alias.into(),
value,
address,
mnemonics,
}
}

pub fn alias(&self) -> &WalletAlias {
&self.alias
}

pub fn value(&self) -> &Value {
&self.value
}

pub fn address(&self) -> String {
self.address.clone()
}
}

/// wallet to utilise when testing jormungandr
///
/// This can be used for a faucet
Expand Down Expand Up @@ -113,6 +150,10 @@ impl Wallet {
self.inner.address()
}

pub fn committee_id(&self) -> CommitteeId {
CommitteeId::from(self.address().1.public_key().unwrap().clone())
}

pub fn stake_key(&self) -> Option<UnspecifiedAccountIdentifier> {
self.inner.stake_key()
}
Expand Down Expand Up @@ -152,3 +193,12 @@ impl Into<Inner> for Wallet {
self.inner
}
}

pub type WalletLib = chain_impl_mockchain::testing::data::Wallet;

impl Into<WalletLib> for Wallet {
fn into(self) -> WalletLib {
let inner: Inner = self.into();
inner.into()
}
}

0 comments on commit 8b50ae2

Please sign in to comment.