Skip to content

Commit b75a0b3

Browse files
committed
modularize committee sync and event stream logic
1 parent d07bd02 commit b75a0b3

File tree

8 files changed

+277
-342
lines changed

8 files changed

+277
-342
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.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ ethereum_ssz = "0.9.0"
6969
futures = { version = "0.3", default-features = false, features = ["alloc"] }
7070
generic-array = { version = "0.14.7", features = ["serde", "zeroize"] }
7171
http = "1.3.1"
72+
itertools = "0.14.0"
7273
jiff = { version = "0.2", default-features = false, features = ["serde", "std"] }
7374
minicbor = { version = "2.1.1", features = ["full"] }
7475
nohash-hasher = "0.2"

timeboost/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cliquenet = { path = "../cliquenet" }
2828
committable = { workspace = true }
2929
futures = { workspace = true }
3030
http = { workspace = true }
31+
itertools = { workspace = true }
3132
metrics = { path = "../metrics", features = ["prometheus"]}
3233
multisig = { path = "../multisig" }
3334
prost = { workspace = true }

timeboost/src/binaries/sailfish.rs

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
use std::{iter::repeat_with, path::PathBuf, sync::Arc, time::Duration};
22

33
use ::metrics::prometheus::PrometheusMetrics;
4-
use alloy::providers::{Provider, ProviderBuilder};
54
use anyhow::{Context, Result};
65
use cliquenet::{Network, NetworkMetrics, Overlay};
76
use committable::{Commitment, Committable, RawCommitmentBuilder};
8-
use multisig::{Committee, CommitteeId, Keypair, x25519};
7+
use multisig::{CommitteeId, Keypair, x25519};
98
use sailfish::{
109
Coordinator,
1110
consensus::{Consensus, ConsensusMetrics},
1211
rbc::{Rbc, RbcConfig, RbcMetrics},
13-
types::{Action, HasTime, Timestamp, UNKNOWN_COMMITTEE_ID},
12+
types::{Action, HasTime, Timestamp},
1413
};
1514
use serde::{Deserialize, Serialize};
16-
use timeboost::config::NodeConfig;
17-
use timeboost_contract::{CommitteeMemberSol, KeyManager};
15+
use timeboost::{committee::CommitteeInfo, config::NodeConfig};
1816
use timeboost_utils::types::logging;
1917
use tokio::{select, signal, time::sleep};
2018
use tracing::{error, info};
@@ -91,60 +89,33 @@ async fn main() -> Result<()> {
9189
let dh_keypair = x25519::Keypair::from(config.keys.dh.secret.clone());
9290

9391
// syncing with contract to get peers keys and network addresses
94-
let provider = ProviderBuilder::new().connect_http(config.chain.parent.rpc_url);
95-
assert_eq!(
96-
provider.get_chain_id().await?,
97-
config.chain.parent.id,
98-
"Parent chain RPC has mismatched chain_id"
99-
);
100-
101-
let contract = KeyManager::new(config.chain.parent.key_manager_contract, &provider);
102-
let members: Vec<CommitteeMemberSol> = contract
103-
.getCommitteeById(cli.committee_id.into())
104-
.call()
105-
.await?
106-
.members;
92+
let comm_info = CommitteeInfo::fetch(
93+
config.chain.parent.rpc_url,
94+
config.chain.parent.key_manager_contract,
95+
cli.committee_id.into(),
96+
)
97+
.await?;
10798
info!(label = %config.keys.signing.public, committee_id = %cli.committee_id, "committee info synced");
10899

109-
let peer_hosts_and_keys = members
110-
.iter()
111-
.map(|peer| {
112-
let sig_key = multisig::PublicKey::try_from(peer.sigKey.as_ref())
113-
.expect("Failed to parse sigKey");
114-
let dh_key =
115-
x25519::PublicKey::try_from(peer.dhKey.as_ref()).expect("Failed to parse dhKey");
116-
let sailfish_address = cliquenet::Address::try_from(peer.networkAddress.as_ref())
117-
.expect("Failed to parse networkAddress");
118-
(sig_key, dh_key, sailfish_address)
119-
})
120-
.collect::<Vec<_>>();
121-
122100
let prom = Arc::new(PrometheusMetrics::default());
123101
let sf_metrics = ConsensusMetrics::new(prom.as_ref());
124102
let net_metrics = NetworkMetrics::new(
125103
"sailfish",
126104
prom.as_ref(),
127-
peer_hosts_and_keys.iter().map(|(k, ..)| *k),
105+
comm_info.signing_keys().iter().cloned(),
128106
);
129107
let rbc_metrics = RbcMetrics::new(prom.as_ref());
130108
let network = Network::create(
131109
"sailfish",
132110
config.net.public.address.clone(),
133111
signing_keypair.public_key(),
134112
dh_keypair.clone(),
135-
peer_hosts_and_keys.clone(),
113+
comm_info.group(),
136114
net_metrics,
137115
)
138116
.await?;
139117

140-
let committee = Committee::new(
141-
UNKNOWN_COMMITTEE_ID,
142-
peer_hosts_and_keys
143-
.iter()
144-
.map(|b| b.0)
145-
.enumerate()
146-
.map(|(i, key)| (i as u8, key)),
147-
);
118+
let committee = comm_info.committee();
148119

149120
// If the stamp file exists we need to recover from a previous run.
150121
let recover = if cli.ignore_stamp {

timeboost/src/binaries/timeboost.rs

Lines changed: 13 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use std::path::PathBuf;
22

3-
use alloy::providers::{Provider, ProviderBuilder};
43
use anyhow::{Context, Result, bail};
5-
use cliquenet::AddressableCommittee;
64
use multisig::CommitteeId;
7-
use multisig::{Committee, Keypair, x25519};
5+
use multisig::{Keypair, x25519};
6+
use timeboost::committee::CommitteeInfo;
87
use timeboost::{Timeboost, TimeboostConfig};
98
use timeboost_builder::robusta;
10-
use timeboost_contract::{CommitteeMemberSol, KeyManager};
11-
use timeboost_crypto::prelude::DkgEncKey;
12-
use timeboost_types::{KeyStore, ThresholdKeyCell};
9+
use timeboost_types::ThresholdKeyCell;
1310
use tokio::select;
1411
use tokio::signal;
1512
use tokio::task::spawn;
@@ -73,99 +70,18 @@ async fn main() -> Result<()> {
7370
let dh_keypair = x25519::Keypair::from(node_config.keys.dh.secret.clone());
7471

7572
// syncing with contract to get peers keys and network addresses
76-
let provider = ProviderBuilder::new().connect_http(node_config.chain.parent.rpc_url.clone());
77-
let chain_id = provider.get_chain_id().await?;
78-
79-
assert_eq!(
80-
chain_id, node_config.chain.parent.id,
81-
"parent chain rpc has mismatched chain_id"
82-
);
83-
84-
let contract = KeyManager::new(node_config.chain.parent.key_manager_contract, &provider);
85-
86-
let members: Vec<CommitteeMemberSol> = contract
87-
.getCommitteeById(cli.committee_id.into())
88-
.call()
89-
.await?
90-
.members;
91-
73+
let comm_info = CommitteeInfo::fetch(
74+
node_config.chain.parent.rpc_url.clone(),
75+
node_config.chain.parent.key_manager_contract,
76+
cli.committee_id.into(),
77+
)
78+
.await?;
9279
info!(label = %sign_keypair.public_key(), committee_id = %cli.committee_id, "committee info synced");
9380

94-
let peer_hosts_and_keys = members
95-
.iter()
96-
.map(|peer| -> Result<_> {
97-
let sig_key = multisig::PublicKey::try_from(peer.sigKey.as_ref())
98-
.with_context(|| "Failed to parse sigKey bytes")?;
99-
let dh_key = x25519::PublicKey::try_from(peer.dhKey.as_ref())
100-
.with_context(|| "Failed to parse dhKey bytes")?;
101-
let dkg_enc_key = DkgEncKey::from_bytes(peer.dkgKey.as_ref())
102-
.with_context(|| "Failed to parse dkgKey bytes")?;
103-
let sailfish_address = cliquenet::Address::try_from(peer.networkAddress.as_ref())
104-
.with_context(|| "Failed to parse networkAddress string")?;
105-
Ok((sig_key, dh_key, dkg_enc_key, sailfish_address))
106-
})
107-
.collect::<Result<Vec<_>>>()?;
108-
109-
let mut sailfish_peer_hosts_and_keys = Vec::new();
110-
let mut decrypt_peer_hosts_and_keys = Vec::new();
111-
let mut certifier_peer_hosts_and_keys = Vec::new();
112-
let mut dkg_enc_keys = Vec::new();
113-
114-
for (signing_key, dh_key, dkg_enc_key, sailfish_addr) in peer_hosts_and_keys.iter().cloned() {
115-
sailfish_peer_hosts_and_keys.push((signing_key, dh_key, sailfish_addr.clone()));
116-
decrypt_peer_hosts_and_keys.push((
117-
signing_key,
118-
dh_key,
119-
sailfish_addr.clone().with_offset(DECRYPTER_PORT_OFFSET),
120-
));
121-
certifier_peer_hosts_and_keys.push((
122-
signing_key,
123-
dh_key,
124-
sailfish_addr.clone().with_offset(CERTIFIER_PORT_OFFSET),
125-
));
126-
dkg_enc_keys.push(dkg_enc_key.clone());
127-
}
128-
129-
let sailfish_committee = {
130-
let c = Committee::new(
131-
cli.committee_id,
132-
sailfish_peer_hosts_and_keys
133-
.iter()
134-
.enumerate()
135-
.map(|(i, (k, ..))| (i as u8, *k)),
136-
);
137-
AddressableCommittee::new(c, sailfish_peer_hosts_and_keys.iter().cloned())
138-
};
139-
140-
let decrypt_committee = {
141-
let c = Committee::new(
142-
cli.committee_id,
143-
decrypt_peer_hosts_and_keys
144-
.iter()
145-
.enumerate()
146-
.map(|(i, (k, ..))| (i as u8, *k)),
147-
);
148-
AddressableCommittee::new(c, decrypt_peer_hosts_and_keys.iter().cloned())
149-
};
150-
151-
let certifier_committee = {
152-
let c = Committee::new(
153-
cli.committee_id,
154-
certifier_peer_hosts_and_keys
155-
.iter()
156-
.enumerate()
157-
.map(|(i, (k, ..))| (i as u8, *k)),
158-
);
159-
AddressableCommittee::new(c, certifier_peer_hosts_and_keys.iter().cloned())
160-
};
161-
162-
let key_store = KeyStore::new(
163-
sailfish_committee.committee().clone(),
164-
dkg_enc_keys
165-
.into_iter()
166-
.enumerate()
167-
.map(|(i, k)| (i as u8, k)),
168-
);
81+
let sailfish_committee = comm_info.sailfish_committee();
82+
let decrypt_committee = comm_info.decrypt_committee();
83+
let certifier_committee = comm_info.certifier_committee();
84+
let key_store = comm_info.dkg_key_store();
16985

17086
let is_recover = !cli.ignore_stamp && node_config.stamp.is_file();
17187

0 commit comments

Comments
 (0)