Skip to content

Commit fd29e9f

Browse files
authored
Hersir move keys outside (#4101)
* 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
1 parent c8e7d44 commit fd29e9f

File tree

77 files changed

+1814
-1279
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1814
-1279
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jormungandr-lib/src/interfaces/block0_configuration/initial_fragment.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ pub struct Destination {
6666
pub value: Value,
6767
}
6868

69+
impl From<InitialUTxO> for Destination {
70+
fn from(utxo: InitialUTxO) -> Self {
71+
Self {
72+
address: utxo.address,
73+
value: utxo.value,
74+
}
75+
}
76+
}
77+
6978
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7079
pub struct InitialToken {
7180
pub token_id: TokenIdentifier,

jormungandr-lib/src/interfaces/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ pub use self::{
8181
utxo_info::{UTxOInfo, UTxOOutputInfo},
8282
value::{Value, ValueDef},
8383
vote::{
84-
serde_base64_bytes, AccountVotes, PrivateTallyState, Tally, TallyResult, VotePayload,
85-
VotePlan, VotePlanId, VotePlanStatus, VotePrivacy, VoteProposalStatus,
84+
serde_base64_bytes, serde_choices, serde_committee_member_public_keys,
85+
serde_external_proposal_id, serde_proposals, AccountVotes, PrivateTallyState, Tally,
86+
TallyResult, VotePayload, VotePlan, VotePlanId, VotePlanStatus, VotePrivacy,
87+
VoteProposalStatus,
8688
},
8789
};

jormungandr-lib/src/interfaces/vote.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ impl Serialize for SerdeMemberPublicKey {
149149

150150
#[derive(Clone, Deserialize, Serialize, Debug, Eq, PartialEq)]
151151
pub struct VotePlan {
152-
payload_type: VotePrivacy,
153-
vote_start: BlockDate,
154-
vote_end: BlockDate,
155-
committee_end: BlockDate,
152+
pub payload_type: VotePrivacy,
153+
pub vote_start: BlockDate,
154+
pub vote_end: BlockDate,
155+
pub committee_end: BlockDate,
156156
#[serde(with = "serde_proposals")]
157-
proposals: Proposals,
157+
pub proposals: Proposals,
158158
#[serde(with = "serde_committee_member_public_keys", default = "Vec::new")]
159159
pub committee_member_public_keys: Vec<chain_vote::MemberPublicKey>,
160-
voting_token: TokenIdentifier,
160+
pub voting_token: TokenIdentifier,
161161
}
162162

163163
#[derive(Deserialize, Serialize)]
@@ -233,7 +233,7 @@ impl From<VotePlan> for certificate::VotePlan {
233233
}
234234
}
235235

236-
mod serde_committee_member_public_keys {
236+
pub mod serde_committee_member_public_keys {
237237
use crate::interfaces::vote::SerdeMemberPublicKey;
238238
use serde::{
239239
de::{SeqAccess, Visitor},
@@ -291,7 +291,7 @@ impl From<VoteProposalDef> for Proposal {
291291
}
292292
}
293293

294-
mod serde_external_proposal_id {
294+
pub mod serde_external_proposal_id {
295295
use super::*;
296296
use serde::{Deserializer, Serialize, Serializer};
297297
pub fn deserialize<'de, D>(deserializer: D) -> Result<ExternalProposalId, D::Error>
@@ -352,7 +352,7 @@ mod serde_external_proposal_id {
352352
}
353353
}
354354

355-
mod serde_choices {
355+
pub mod serde_choices {
356356
use super::*;
357357
use serde::{Deserializer, Serialize, Serializer};
358358
pub fn deserialize<'de, D>(deserializer: D) -> Result<Options, D::Error>
@@ -391,7 +391,7 @@ mod serde_choices {
391391
}
392392
}
393393

394-
mod serde_proposals {
394+
pub mod serde_proposals {
395395
use super::*;
396396
use serde::{ser::SerializeSeq, Deserialize, Serialize, Serializer};
397397
#[derive(Deserialize, Serialize)]

testing/hersir/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ chain-core = { git = "https://github.com/input-output-hk/chain-libs.git", b
1414
chain-crypto = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", features = [ "property-test-api" ] }
1515
chain-addr = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master", features = [ "property-test-api" ] }
1616
chain-impl-mockchain = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
17+
chain-vote = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
1718
indicatif = "0.15"
1819
jormungandr-automation = { path = "../jormungandr-automation" }
1920
jormungandr-lib = { path = "../../jormungandr-lib" }

testing/hersir/res/example.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,31 @@ blockchain:
1717
constant: 1
1818
coefficient: 1
1919
certificate: 1
20+
21+
wallets:
22+
- alias: alice
23+
value: 100
24+
discrimination: test
25+
wallet_type: account
26+
delegate: passive
27+
tokens:
28+
00000000000000000000000000000000000000000000000000000000.f9b18e37d9b7e42d594fa1d697414fdedecea7398f7e60bb2d6317c596beb884: 1000
29+
30+
committees:
31+
- alias: alice
32+
vote_plans:
33+
- committees:
34+
- alias: alice
35+
vote_start: '0.1'
36+
vote_end: '0.2'
37+
committee_end: '0.3'
38+
proposals:
39+
- external_id: 1c641eac9e5e5008b974edf4892e55fd7b65c6c5e14d589a4fb59785f042d055
40+
options: 2
41+
action: off_chain
42+
voting_token: 00000000000000000000000000000000000000000000000000000000.f9b18e37d9b7e42d594fa1d697414fdedecea7398f7e60bb2d6317c596beb884
43+
private:
44+
crs: voting
45+
threshold: 1
46+
alias: fund9
47+
owner_alias: alice
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
use crate::{builder::Wallet, config::CommitteeTemplate};
2+
use chain_crypto::bech32::Bech32;
3+
use chain_vote::{
4+
committee::MemberCommunicationPublicKey, MemberCommunicationKey, MemberPublicKey,
5+
};
6+
use jormungandr_lib::interfaces::CommitteeIdDef;
7+
use thor::{
8+
wallet::committee::{
9+
CommitteeCommunicationData, CommitteeCommunicationDataManager, CommitteeMembershipData,
10+
CommitteeMembershipDataManager,
11+
},
12+
CommitteeDataManager, WalletAlias,
13+
};
14+
15+
pub fn generate_committee_data(
16+
wallets: &[Wallet],
17+
committees_templates: &[CommitteeTemplate],
18+
) -> Result<CommitteeDataManager, Error> {
19+
let mut comm_manager = CommitteeCommunicationDataManager::default();
20+
let mut member_manager = CommitteeMembershipDataManager::default();
21+
22+
let mut rng = rand::thread_rng();
23+
24+
for committee_template in committees_templates.iter() {
25+
match committee_template {
26+
CommitteeTemplate::Generated {
27+
alias,
28+
member_pk,
29+
communication_pk,
30+
} => {
31+
let id: CommitteeIdDef = wallets
32+
.iter()
33+
.find(|w| w.has_alias(alias))
34+
.ok_or_else(|| Error::CannotFindAlias(alias.clone()))?
35+
.committee_id()?
36+
.into();
37+
38+
if let Some(member_pk) = member_pk {
39+
member_manager
40+
.committees_mut()
41+
.push(CommitteeMembershipData::public(
42+
id,
43+
MemberPublicKey::try_from_bech32_str(member_pk)?,
44+
));
45+
} else if let Some(communication_pk) = communication_pk {
46+
comm_manager
47+
.committees_mut()
48+
.push(CommitteeCommunicationData::public(
49+
id,
50+
MemberCommunicationPublicKey::try_from_bech32_str(communication_pk)?,
51+
));
52+
} else {
53+
comm_manager
54+
.committees_mut()
55+
.push(CommitteeCommunicationData::private(
56+
id,
57+
MemberCommunicationKey::new(&mut rng),
58+
));
59+
}
60+
}
61+
CommitteeTemplate::External {
62+
id: hex,
63+
member_pk,
64+
communication_pk,
65+
} => {
66+
let id = CommitteeIdDef::from_hex(hex).unwrap();
67+
68+
if let Some(member_pk) = member_pk {
69+
member_manager
70+
.committees_mut()
71+
.push(CommitteeMembershipData::public(
72+
id,
73+
MemberPublicKey::try_from_bech32_str(member_pk)?,
74+
));
75+
} else if let Some(communication_pk) = communication_pk {
76+
comm_manager
77+
.committees_mut()
78+
.push(CommitteeCommunicationData::public(
79+
id,
80+
MemberCommunicationPublicKey::try_from_bech32_str(communication_pk)?,
81+
));
82+
} else {
83+
comm_manager
84+
.committees_mut()
85+
.push(CommitteeCommunicationData::private(
86+
id,
87+
MemberCommunicationKey::new(&mut rng),
88+
));
89+
}
90+
}
91+
}
92+
}
93+
94+
Ok(CommitteeDataManager {
95+
communication: comm_manager.into(),
96+
membership: member_manager,
97+
})
98+
}
99+
100+
#[derive(thiserror::Error, Debug)]
101+
pub enum Error {
102+
#[error("cannot find alias '{0}' for any defined wallet")]
103+
CannotFindAlias(WalletAlias),
104+
#[error(transparent)]
105+
Wallet(#[from] crate::builder::settings::wallet::Error),
106+
#[error(transparent)]
107+
Bech3(#[from] chain_crypto::bech32::Error),
108+
}

testing/hersir/src/builder/mod.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
pub mod blockchain;
1+
mod committee;
22
pub mod rng;
33
pub mod settings;
4-
pub mod spawn_params;
4+
mod stake_pool;
55
pub mod topology;
66
pub mod vote;
7-
mod vote_plan_settings;
87
pub mod wallet;
98

109
pub use crate::controller::Error as ControllerError;
1110
use crate::{
12-
config::SessionSettings,
11+
config::{
12+
Blockchain, CommitteeTemplate, Config, SessionSettings, VotePlanTemplate, WalletTemplate,
13+
},
1314
controller::{Controller, Error},
1415
utils::Dotifier,
1516
};
16-
pub use blockchain::Blockchain;
1717
pub use jormungandr_automation::jormungandr::NodeAlias;
1818
use jormungandr_automation::{
1919
jormungandr::NodeConfigBuilder,
2020
testing::observer::{Event, Observable, Observer},
2121
};
2222
use jormungandr_lib::{crypto::key::SigningKey, interfaces::NodeSecret};
2323
pub use rng::{Random, Seed};
24-
pub use settings::{NodeSetting, Settings};
25-
pub use spawn_params::SpawnParams;
24+
pub use settings::{vote_plan::VotePlanSettings, wallet::Wallet, NodeSetting, Settings};
2625
use std::{
2726
collections::HashMap,
2827
path::Path,
2928
rc::{Rc, Weak},
3029
};
3130
pub use topology::{Node, Topology};
3231
pub use vote::VotePlanKey;
33-
pub use vote_plan_settings::VotePlanSettings;
34-
pub use wallet::{ExternalWalletTemplate, Wallet, WalletTemplate, WalletType};
3532

3633
#[derive(Default)]
3734
pub struct NetworkBuilder {
3835
topology: Topology,
3936
blockchain: Blockchain,
4037
session_settings: SessionSettings,
4138
wallet_templates: Vec<WalletTemplate>,
39+
committee_templates: Vec<CommitteeTemplate>,
40+
vote_plan_templates: Vec<VotePlanTemplate>,
4241
observers: Vec<Weak<dyn Observer>>,
4342
}
4443

@@ -66,6 +65,15 @@ impl Observable for NetworkBuilder {
6665
}
6766

6867
impl NetworkBuilder {
68+
pub fn apply_config(self, config: Config) -> Self {
69+
self.topology(config.build_topology())
70+
.blockchain_config(config.build_blockchain())
71+
.session_settings(config.session)
72+
.wallet_templates(config.wallets)
73+
.vote_plan_templates(config.vote_plans)
74+
.committees(config.committees)
75+
}
76+
6977
pub fn topology(mut self, topology: Topology) -> Self {
7078
self.topology = topology;
7179
self
@@ -76,17 +84,32 @@ impl NetworkBuilder {
7684
self
7785
}
7886

87+
pub fn wallet_templates(mut self, wallets: Vec<WalletTemplate>) -> Self {
88+
self.wallet_templates = wallets;
89+
self
90+
}
91+
7992
pub fn wallet_template(mut self, wallet: WalletTemplate) -> Self {
8093
self.wallet_templates.push(wallet);
8194
self
8295
}
8396

97+
pub fn vote_plan_templates(mut self, vote_plans: Vec<VotePlanTemplate>) -> Self {
98+
self.vote_plan_templates = vote_plans;
99+
self
100+
}
101+
102+
pub fn committees(mut self, committee_templates: Vec<CommitteeTemplate>) -> Self {
103+
self.committee_templates = committee_templates;
104+
self
105+
}
106+
84107
pub fn session_settings(mut self, session_settings: SessionSettings) -> Self {
85108
self.session_settings = session_settings;
86109
self
87110
}
88111

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

116-
for wallet in &self.wallet_templates {
117-
self.blockchain = self.blockchain.with_wallet(wallet.clone());
118-
}
119-
120139
self.notify_all(Event::new("building block0.."));
121-
let settings = Settings::new(nodes, self.blockchain.clone(), &mut random);
140+
let settings = Settings::new(
141+
nodes,
142+
&self.blockchain,
143+
&self.wallet_templates,
144+
&self.committee_templates,
145+
&self.vote_plan_templates,
146+
&mut random,
147+
)?;
122148

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

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

140-
for wallet in settings.wallets.values() {
166+
for wallet in &settings.wallets {
141167
wallet.save_to(path)?;
142168
}
143169

0 commit comments

Comments
 (0)