Skip to content

Commit

Permalink
Hersir move keys outside (#4101)
Browse files Browse the repository at this point in the history
* refactor CommitteeData in thor in order to allow to specify external CommitteKeys

* Refactor hersir: pull out all keys generation and allow to define external wallets or committess and voteplans, this will allow external clients to create their own keys which will be used for tally operations.

* update tests with hersir changes
  • Loading branch information
dkijania authored Sep 29, 2022
1 parent c8e7d44 commit fd29e9f
Show file tree
Hide file tree
Showing 77 changed files with 1,814 additions and 1,279 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ pub struct Destination {
pub value: Value,
}

impl From<InitialUTxO> for Destination {
fn from(utxo: InitialUTxO) -> Self {
Self {
address: utxo.address,
value: utxo.value,
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct InitialToken {
pub token_id: TokenIdentifier,
Expand Down
6 changes: 4 additions & 2 deletions jormungandr-lib/src/interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ pub use self::{
utxo_info::{UTxOInfo, UTxOOutputInfo},
value::{Value, ValueDef},
vote::{
serde_base64_bytes, AccountVotes, PrivateTallyState, Tally, TallyResult, VotePayload,
VotePlan, VotePlanId, VotePlanStatus, VotePrivacy, VoteProposalStatus,
serde_base64_bytes, serde_choices, serde_committee_member_public_keys,
serde_external_proposal_id, serde_proposals, AccountVotes, PrivateTallyState, Tally,
TallyResult, VotePayload, VotePlan, VotePlanId, VotePlanStatus, VotePrivacy,
VoteProposalStatus,
},
};
20 changes: 10 additions & 10 deletions jormungandr-lib/src/interfaces/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ impl Serialize for SerdeMemberPublicKey {

#[derive(Clone, Deserialize, Serialize, Debug, Eq, PartialEq)]
pub struct VotePlan {
payload_type: VotePrivacy,
vote_start: BlockDate,
vote_end: BlockDate,
committee_end: BlockDate,
pub payload_type: VotePrivacy,
pub vote_start: BlockDate,
pub vote_end: BlockDate,
pub committee_end: BlockDate,
#[serde(with = "serde_proposals")]
proposals: Proposals,
pub proposals: Proposals,
#[serde(with = "serde_committee_member_public_keys", default = "Vec::new")]
pub committee_member_public_keys: Vec<chain_vote::MemberPublicKey>,
voting_token: TokenIdentifier,
pub voting_token: TokenIdentifier,
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -233,7 +233,7 @@ impl From<VotePlan> for certificate::VotePlan {
}
}

mod serde_committee_member_public_keys {
pub mod serde_committee_member_public_keys {
use crate::interfaces::vote::SerdeMemberPublicKey;
use serde::{
de::{SeqAccess, Visitor},
Expand Down Expand Up @@ -291,7 +291,7 @@ impl From<VoteProposalDef> for Proposal {
}
}

mod serde_external_proposal_id {
pub mod serde_external_proposal_id {
use super::*;
use serde::{Deserializer, Serialize, Serializer};
pub fn deserialize<'de, D>(deserializer: D) -> Result<ExternalProposalId, D::Error>
Expand Down Expand Up @@ -352,7 +352,7 @@ mod serde_external_proposal_id {
}
}

mod serde_choices {
pub mod serde_choices {
use super::*;
use serde::{Deserializer, Serialize, Serializer};
pub fn deserialize<'de, D>(deserializer: D) -> Result<Options, D::Error>
Expand Down Expand Up @@ -391,7 +391,7 @@ mod serde_choices {
}
}

mod serde_proposals {
pub mod serde_proposals {
use super::*;
use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer};
#[derive(Deserialize, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions testing/hersir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ chain-core = { git = "https://github.com/input-output-hk/chain-libs.git", b
chain-crypto = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", features = [ "property-test-api" ] }
chain-addr = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", features = [ "property-test-api" ] }
chain-impl-mockchain = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
chain-vote = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
indicatif = "0.15"
jormungandr-automation = { path = "../jormungandr-automation" }
jormungandr-lib = { path = "../../jormungandr-lib" }
Expand Down
28 changes: 28 additions & 0 deletions testing/hersir/res/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,31 @@ blockchain:
constant: 1
coefficient: 1
certificate: 1

wallets:
- alias: alice
value: 100
discrimination: test
wallet_type: account
delegate: passive
tokens:
00000000000000000000000000000000000000000000000000000000.f9b18e37d9b7e42d594fa1d697414fdedecea7398f7e60bb2d6317c596beb884: 1000

committees:
- alias: alice
vote_plans:
- committees:
- alias: alice
vote_start: '0.1'
vote_end: '0.2'
committee_end: '0.3'
proposals:
- external_id: 1c641eac9e5e5008b974edf4892e55fd7b65c6c5e14d589a4fb59785f042d055
options: 2
action: off_chain
voting_token: 00000000000000000000000000000000000000000000000000000000.f9b18e37d9b7e42d594fa1d697414fdedecea7398f7e60bb2d6317c596beb884
private:
crs: voting
threshold: 1
alias: fund9
owner_alias: alice
108 changes: 108 additions & 0 deletions testing/hersir/src/builder/committee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use crate::{builder::Wallet, config::CommitteeTemplate};
use chain_crypto::bech32::Bech32;
use chain_vote::{
committee::MemberCommunicationPublicKey, MemberCommunicationKey, MemberPublicKey,
};
use jormungandr_lib::interfaces::CommitteeIdDef;
use thor::{
wallet::committee::{
CommitteeCommunicationData, CommitteeCommunicationDataManager, CommitteeMembershipData,
CommitteeMembershipDataManager,
},
CommitteeDataManager, WalletAlias,
};

pub fn generate_committee_data(
wallets: &[Wallet],
committees_templates: &[CommitteeTemplate],
) -> Result<CommitteeDataManager, Error> {
let mut comm_manager = CommitteeCommunicationDataManager::default();
let mut member_manager = CommitteeMembershipDataManager::default();

let mut rng = rand::thread_rng();

for committee_template in committees_templates.iter() {
match committee_template {
CommitteeTemplate::Generated {
alias,
member_pk,
communication_pk,
} => {
let id: CommitteeIdDef = wallets
.iter()
.find(|w| w.has_alias(alias))
.ok_or_else(|| Error::CannotFindAlias(alias.clone()))?
.committee_id()?
.into();

if let Some(member_pk) = member_pk {
member_manager
.committees_mut()
.push(CommitteeMembershipData::public(
id,
MemberPublicKey::try_from_bech32_str(member_pk)?,
));
} else if let Some(communication_pk) = communication_pk {
comm_manager
.committees_mut()
.push(CommitteeCommunicationData::public(
id,
MemberCommunicationPublicKey::try_from_bech32_str(communication_pk)?,
));
} else {
comm_manager
.committees_mut()
.push(CommitteeCommunicationData::private(
id,
MemberCommunicationKey::new(&mut rng),
));
}
}
CommitteeTemplate::External {
id: hex,
member_pk,
communication_pk,
} => {
let id = CommitteeIdDef::from_hex(hex).unwrap();

if let Some(member_pk) = member_pk {
member_manager
.committees_mut()
.push(CommitteeMembershipData::public(
id,
MemberPublicKey::try_from_bech32_str(member_pk)?,
));
} else if let Some(communication_pk) = communication_pk {
comm_manager
.committees_mut()
.push(CommitteeCommunicationData::public(
id,
MemberCommunicationPublicKey::try_from_bech32_str(communication_pk)?,
));
} else {
comm_manager
.committees_mut()
.push(CommitteeCommunicationData::private(
id,
MemberCommunicationKey::new(&mut rng),
));
}
}
}
}

Ok(CommitteeDataManager {
communication: comm_manager.into(),
membership: member_manager,
})
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("cannot find alias '{0}' for any defined wallet")]
CannotFindAlias(WalletAlias),
#[error(transparent)]
Wallet(#[from] crate::builder::settings::wallet::Error),
#[error(transparent)]
Bech3(#[from] chain_crypto::bech32::Error),
}
58 changes: 42 additions & 16 deletions testing/hersir/src/builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,43 @@
pub mod blockchain;
mod committee;
pub mod rng;
pub mod settings;
pub mod spawn_params;
mod stake_pool;
pub mod topology;
pub mod vote;
mod vote_plan_settings;
pub mod wallet;

pub use crate::controller::Error as ControllerError;
use crate::{
config::SessionSettings,
config::{
Blockchain, CommitteeTemplate, Config, SessionSettings, VotePlanTemplate, WalletTemplate,
},
controller::{Controller, Error},
utils::Dotifier,
};
pub use blockchain::Blockchain;
pub use jormungandr_automation::jormungandr::NodeAlias;
use jormungandr_automation::{
jormungandr::NodeConfigBuilder,
testing::observer::{Event, Observable, Observer},
};
use jormungandr_lib::{crypto::key::SigningKey, interfaces::NodeSecret};
pub use rng::{Random, Seed};
pub use settings::{NodeSetting, Settings};
pub use spawn_params::SpawnParams;
pub use settings::{vote_plan::VotePlanSettings, wallet::Wallet, NodeSetting, Settings};
use std::{
collections::HashMap,
path::Path,
rc::{Rc, Weak},
};
pub use topology::{Node, Topology};
pub use vote::VotePlanKey;
pub use vote_plan_settings::VotePlanSettings;
pub use wallet::{ExternalWalletTemplate, Wallet, WalletTemplate, WalletType};

#[derive(Default)]
pub struct NetworkBuilder {
topology: Topology,
blockchain: Blockchain,
session_settings: SessionSettings,
wallet_templates: Vec<WalletTemplate>,
committee_templates: Vec<CommitteeTemplate>,
vote_plan_templates: Vec<VotePlanTemplate>,
observers: Vec<Weak<dyn Observer>>,
}

Expand Down Expand Up @@ -66,6 +65,15 @@ impl Observable for NetworkBuilder {
}

impl NetworkBuilder {
pub fn apply_config(self, config: Config) -> Self {
self.topology(config.build_topology())
.blockchain_config(config.build_blockchain())
.session_settings(config.session)
.wallet_templates(config.wallets)
.vote_plan_templates(config.vote_plans)
.committees(config.committees)
}

pub fn topology(mut self, topology: Topology) -> Self {
self.topology = topology;
self
Expand All @@ -76,17 +84,32 @@ impl NetworkBuilder {
self
}

pub fn wallet_templates(mut self, wallets: Vec<WalletTemplate>) -> Self {
self.wallet_templates = wallets;
self
}

pub fn wallet_template(mut self, wallet: WalletTemplate) -> Self {
self.wallet_templates.push(wallet);
self
}

pub fn vote_plan_templates(mut self, vote_plans: Vec<VotePlanTemplate>) -> Self {
self.vote_plan_templates = vote_plans;
self
}

pub fn committees(mut self, committee_templates: Vec<CommitteeTemplate>) -> Self {
self.committee_templates = committee_templates;
self
}

pub fn session_settings(mut self, session_settings: SessionSettings) -> Self {
self.session_settings = session_settings;
self
}

pub fn build(mut self) -> Result<Controller, ControllerError> {
pub fn build(self) -> Result<Controller, ControllerError> {
self.notify_all(Event::new("building topology..."));
let nodes: HashMap<NodeAlias, NodeSetting> = self
.topology
Expand All @@ -113,12 +136,15 @@ impl NetworkBuilder {
let seed = Seed::generate(rand::rngs::OsRng);
let mut random = Random::new(seed);

for wallet in &self.wallet_templates {
self.blockchain = self.blockchain.with_wallet(wallet.clone());
}

self.notify_all(Event::new("building block0.."));
let settings = Settings::new(nodes, self.blockchain.clone(), &mut random);
let settings = Settings::new(
nodes,
&self.blockchain,
&self.wallet_templates,
&self.committee_templates,
&self.vote_plan_templates,
&mut random,
)?;

self.notify_all(Event::new("dumping wallet secret keys.."));

Expand All @@ -137,7 +163,7 @@ fn document(path: &Path, settings: &Settings) -> Result<(), Error> {
let dotifier = Dotifier;
dotifier.dottify(settings, file)?;

for wallet in settings.wallets.values() {
for wallet in &settings.wallets {
wallet.save_to(path)?;
}

Expand Down
Loading

0 comments on commit fd29e9f

Please sign in to comment.