Skip to content

Commit f9ff545

Browse files
committed
Tweak timeboost-config.
1 parent 5b5be36 commit f9ff545

File tree

9 files changed

+205
-216
lines changed

9 files changed

+205
-216
lines changed

timeboost-config/src/binaries/mkconfig.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use multisig::x25519;
1313
use secp256k1::rand::SeedableRng as _;
1414
use timeboost_config::{ChainConfig, ParentChain};
1515
use timeboost_config::{
16-
CommitteeConfig, CommitteeMember, InternalNet, NodeConfig, NodeKeyConfig, NodeKeypairConfig,
17-
NodeNetConfig, PublicNet,
16+
CommitteeConfig, CommitteeMember, InternalNet, NodeConfig, NodeKeypair, NodeKeys, NodeNet,
17+
PublicNet,
1818
};
1919
use timeboost_crypto::prelude::{DkgDecKey, DkgEncKey};
2020
use url::Url;
@@ -129,7 +129,7 @@ impl Args {
129129
};
130130

131131
let config = NodeConfig {
132-
net: NodeNetConfig {
132+
net: NodeNet {
133133
public: PublicNet {
134134
address: public_addr,
135135
},
@@ -138,32 +138,30 @@ impl Args {
138138
nitro: nitro_addr,
139139
},
140140
},
141-
keys: NodeKeyConfig {
142-
signing: NodeKeypairConfig {
141+
keys: NodeKeys {
142+
signing: NodeKeypair {
143143
secret: signing_keypair.secret_key(),
144144
public: signing_keypair.public_key(),
145145
},
146-
dh: NodeKeypairConfig {
146+
dh: NodeKeypair {
147147
secret: dh_keypair.secret_key(),
148148
public: dh_keypair.public_key(),
149149
},
150-
dkg: NodeKeypairConfig {
150+
dkg: NodeKeypair {
151151
secret: dkg_dec_key.clone(),
152152
public: DkgEncKey::from(&dkg_dec_key),
153153
},
154154
},
155-
chain: ChainConfig::builder()
156-
.namespace(self.chain_namespace)
157-
.parent(
158-
ParentChain::builder()
159-
.id(self.parent_chain_id)
160-
.rpc_url(self.parent_rpc_url.clone())
161-
.ibox_contract(self.parent_ibox_contract)
162-
.key_manager_contract(self.key_manager_contract)
163-
.block_tag(self.parent_block_tag)
164-
.build(),
165-
)
166-
.build(),
155+
chain: ChainConfig {
156+
namespace: self.chain_namespace,
157+
parent: ParentChain {
158+
id: self.parent_chain_id,
159+
rpc_url: self.parent_rpc_url.clone(),
160+
ibox_contract: self.parent_ibox_contract,
161+
block_tag: self.parent_block_tag,
162+
key_manager_contract: self.key_manager_contract,
163+
},
164+
},
167165
};
168166

169167
members.push(CommitteeMember {

timeboost-config/src/chain.rs

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,15 @@ use url::Url;
66

77
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
88
pub struct ChainConfig {
9-
namespace: u64,
10-
parent: ParentChain,
9+
pub namespace: u64,
10+
pub parent: ParentChain,
1111
}
1212

1313
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
1414
pub struct ParentChain {
15-
id: u64,
16-
rpc_url: Url,
17-
ibox_contract: Address,
18-
block_tag: BlockNumberOrTag,
19-
key_manager_contract: Address,
20-
}
21-
22-
impl ChainConfig {
23-
pub fn namespace(&self) -> u64 {
24-
self.namespace
25-
}
26-
27-
pub fn parent(&self) -> &ParentChain {
28-
&self.parent
29-
}
30-
}
31-
32-
impl ParentChain {
33-
pub fn chain_id(&self) -> u64 {
34-
self.id
35-
}
36-
37-
pub fn rpc_url(&self) -> &Url {
38-
&self.rpc_url
39-
}
40-
41-
pub fn ibox_contract(&self) -> &Address {
42-
&self.ibox_contract
43-
}
44-
45-
pub fn block_tag(&self) -> BlockNumberOrTag {
46-
self.block_tag
47-
}
48-
49-
pub fn key_manager_contract(&self) -> &Address {
50-
&self.key_manager_contract
51-
}
15+
pub id: u64,
16+
pub rpc_url: Url,
17+
pub ibox_contract: Address,
18+
pub block_tag: BlockNumberOrTag,
19+
pub key_manager_contract: Address,
5220
}

timeboost-config/src/committee.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use core::fmt;
2+
use std::{path::Path, str::FromStr};
3+
4+
use cliquenet::Address;
5+
use multisig::x25519;
6+
use serde::{Deserialize, Serialize};
7+
use timeboost_crypto::prelude::DkgEncKey;
8+
9+
use crate::ConfigError;
10+
11+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
12+
pub struct CommitteeConfig {
13+
pub effective_timestamp: jiff::Timestamp,
14+
#[serde(default)]
15+
pub members: Vec<CommitteeMember>,
16+
}
17+
18+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
19+
pub struct CommitteeMember {
20+
pub signing_key: multisig::PublicKey,
21+
pub dh_key: x25519::PublicKey,
22+
pub dkg_enc_key: DkgEncKey,
23+
pub public_address: Address,
24+
}
25+
26+
impl CommitteeConfig {
27+
pub async fn read<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
28+
tokio::fs::read_to_string(path.as_ref())
29+
.await
30+
.map_err(|e| ConfigError(Box::new(e)))?
31+
.parse()
32+
}
33+
}
34+
35+
impl FromStr for CommitteeConfig {
36+
type Err = ConfigError;
37+
38+
fn from_str(s: &str) -> Result<Self, Self::Err> {
39+
toml::from_str(s).map_err(|e| ConfigError(Box::new(e)))
40+
}
41+
}
42+
43+
impl fmt::Display for CommitteeConfig {
44+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45+
let s = toml::to_string_pretty(self).map_err(|_| fmt::Error)?;
46+
f.write_str(&s)
47+
}
48+
}

timeboost-config/src/lib.rs

Lines changed: 5 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,12 @@
1-
use anyhow::Result;
2-
use cliquenet::Address;
3-
use multisig::x25519;
4-
use serde::{Deserialize, Serialize};
5-
use timeboost_crypto::prelude::{DkgDecKey, DkgEncKey};
6-
use tracing::error;
7-
81
mod chain;
2+
mod committee;
3+
mod node;
94

105
pub use chain::{ChainConfig, ChainConfigBuilder, ParentChain, ParentChainBuilder};
11-
12-
pub const DECRYPTER_PORT_OFFSET: u16 = 1;
13-
pub const CERTIFIER_PORT_OFFSET: u16 = 2;
14-
15-
/// Config for each node, containing private keys, public keys, chain_config, network addresses etc
16-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
17-
pub struct NodeConfig {
18-
pub net: NodeNetConfig,
19-
pub keys: NodeKeyConfig,
20-
pub chain: ChainConfig,
21-
}
22-
23-
/// Network addresses in [`NodeConfig`]
24-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
25-
pub struct NodeNetConfig {
26-
pub public: PublicNet,
27-
pub internal: InternalNet,
28-
}
29-
30-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
31-
pub struct PublicNet {
32-
pub address: Address,
33-
}
34-
35-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
36-
pub struct InternalNet {
37-
pub address: Address,
38-
#[serde(skip_serializing_if = "Option::is_none")]
39-
pub nitro: Option<Address>,
40-
}
41-
42-
/// Key materials in [`NodeConfig`]
43-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
44-
pub struct NodeKeyConfig {
45-
pub signing: NodeKeypairConfig<multisig::SecretKey, multisig::PublicKey>,
46-
pub dh: NodeKeypairConfig<x25519::SecretKey, x25519::PublicKey>,
47-
pub dkg: NodeKeypairConfig<DkgDecKey, DkgEncKey>,
48-
}
49-
50-
/// A keypair
51-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
52-
pub struct NodeKeypairConfig<SK, PK> {
53-
pub secret: SK,
54-
pub public: PK,
55-
}
56-
57-
/// Config for the new committee info to be updated to the KeyManager contract.
58-
/// This file is written during per-node run of `mkconfig`
59-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
60-
pub struct CommitteeConfig {
61-
pub effective_timestamp: jiff::Timestamp,
62-
#[serde(default)]
63-
pub members: Vec<CommitteeMember>,
64-
}
65-
66-
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
67-
pub struct CommitteeMember {
68-
pub signing_key: multisig::PublicKey,
69-
pub dh_key: x25519::PublicKey,
70-
pub dkg_enc_key: DkgEncKey,
71-
pub public_address: Address,
72-
}
6+
pub use committee::{CommitteeConfig, CommitteeMember};
7+
pub use node::{CERTIFIER_PORT_OFFSET, DECRYPTER_PORT_OFFSET};
8+
pub use node::{InternalNet, NodeConfig, NodeKeypair, NodeKeys, NodeNet, PublicNet};
739

7410
#[derive(Debug, thiserror::Error)]
7511
#[error("config error: {0}")]
7612
pub struct ConfigError(#[source] Box<dyn std::error::Error + Send + Sync>);
77-
78-
macro_rules! ConfigImpls {
79-
($t:ty) => {
80-
impl core::str::FromStr for $t {
81-
type Err = ConfigError;
82-
83-
fn from_str(s: &str) -> Result<Self, Self::Err> {
84-
toml::from_str(s).map_err(|e| ConfigError(Box::new(e)))
85-
}
86-
}
87-
88-
impl core::fmt::Display for $t {
89-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
90-
let s = toml::to_string_pretty(self).map_err(|_| core::fmt::Error)?;
91-
f.write_str(&s)
92-
}
93-
}
94-
95-
impl $t {
96-
pub async fn read<P: AsRef<std::path::Path>>(path: P) -> Result<Self, ConfigError> {
97-
tokio::fs::read_to_string(path.as_ref())
98-
.await
99-
.map_err(|e| ConfigError(Box::new(e)))?
100-
.parse()
101-
}
102-
}
103-
};
104-
}
105-
106-
ConfigImpls!(NodeConfig);
107-
ConfigImpls!(CommitteeConfig);
108-
109-
#[cfg(test)]
110-
mod tests {
111-
// generated via `just mkconfig 1`
112-
const CONFIG: &str = r#"
113-
[net.public]
114-
address = "127.0.0.1:8001"
115-
116-
[net.internal]
117-
address = "127.0.0.1:11001"
118-
119-
[keys.signing]
120-
secret = "AS9HxHVwMNyAYsdGntcD56iBbobBn7RPBi32qEBGDSSb"
121-
public = "23as9Uo6W2AeGronB6nMpcbs8Nxo6CoJ769uePw9sf6Ud"
122-
123-
[keys.dh]
124-
secret = "HeTQaTvq1337kSEd5jt4cdF35Touns1WB7xfs24sKxqM"
125-
public = "3V1LzAgCwubtAb1MT1YgTH2scXg6d2bQEhhsAMeyNo6X"
126-
127-
[keys.dkg]
128-
secret = "Cab231aFJTQYmZV7Qw4qa2x49K58fbyTEsM4Tz2CKi1"
129-
public = "7jdMG9MUWoN4avAc3mbf2tTTGdKSmmGZWTgR3NJ9hJPn6dHj9Vdqspcs3j6zTThfjC"
130-
131-
[chain]
132-
namespace = 10101
133-
134-
[chain.parent]
135-
id = 31337
136-
rpc_url = "http://127.0.0.1:8545/"
137-
ibox_contract = "0x4dbd4fc535ac27206064b68ffcf827b0a60bab3f"
138-
block_tag = "finalized"
139-
key_manager_contract = "0x2bbf15bc655c4cc157b769cfcb1ea9924b9e1a35"
140-
"#;
141-
142-
use super::NodeConfig;
143-
144-
#[test]
145-
fn serialisation_roundtrip() {
146-
let a: NodeConfig = CONFIG.parse().unwrap();
147-
let b: NodeConfig = a.to_string().parse().unwrap();
148-
assert_eq!(a, b);
149-
}
150-
}

0 commit comments

Comments
 (0)