Skip to content

Commit fbc48a5

Browse files
alxiongtwittner
andcommitted
improve code quality
Co-authored-by: Toralf Wittner <[email protected]>
1 parent d4d1e3c commit fbc48a5

File tree

6 files changed

+59
-33
lines changed

6 files changed

+59
-33
lines changed

multisig/src/committee.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::num::{NonZeroUsize, ParseIntError};
2+
use std::ops::{Add, Sub};
23
use std::{fmt, str::FromStr};
34

45
use std::sync::Arc;
@@ -107,6 +108,7 @@ impl Committee {
107108
#[derive(
108109
Debug,
109110
Copy,
111+
Default,
110112
Clone,
111113
PartialEq,
112114
Eq,
@@ -163,3 +165,19 @@ impl Committable for CommitteeId {
163165
.finalize()
164166
}
165167
}
168+
169+
impl Add<u64> for CommitteeId {
170+
type Output = Self;
171+
172+
fn add(self, rhs: u64) -> Self::Output {
173+
Self(self.0 + rhs)
174+
}
175+
}
176+
177+
impl Sub<u64> for CommitteeId {
178+
type Output = Self;
179+
180+
fn sub(self, rhs: u64) -> Self::Output {
181+
Self(self.0 - rhs)
182+
}
183+
}

timeboost-contract/src/provider.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ use std::{ops::Deref, pin::Pin};
66
use alloy::{
77
eips::BlockNumberOrTag,
88
network::EthereumWallet,
9-
primitives::Address,
9+
primitives::{Address, BlockNumber},
1010
providers::{Provider, ProviderBuilder},
1111
rpc::types::{Filter, Log},
1212
signers::local::{LocalSignerError, MnemonicBuilder, PrivateKeySigner, coins_bip39::English},
1313
sol_types::SolEvent,
1414
transports::{http::reqwest::Url, ws::WsConnect},
1515
};
1616
use futures::{Stream, StreamExt};
17-
use timeboost_types::{HttpProvider, HttpProviderWithWallet};
17+
use timeboost_types::{HttpProvider, HttpProviderWithWallet, Timestamp};
1818
use tracing::error;
1919

2020
/// Build a local signer from wallet mnemonic and account index
@@ -46,6 +46,7 @@ pub struct PubSubProvider(HttpProvider);
4646

4747
impl Deref for PubSubProvider {
4848
type Target = HttpProvider;
49+
4950
fn deref(&self) -> &Self::Target {
5051
&self.0
5152
}
@@ -64,10 +65,6 @@ impl PubSubProvider {
6465
Ok(Self(provider))
6566
}
6667

67-
pub fn inner(&self) -> &HttpProvider {
68-
&self.0
69-
}
70-
7168
/// create an event stream of event type `E`, subscribing since `from_block` on `contract`
7269
pub async fn event_stream<E: SolEvent>(
7370
&self,
@@ -89,22 +86,24 @@ impl PubSubProvider {
8986
.into_stream();
9087

9188
let validated = events.filter_map(|log| async move {
92-
let Ok(event) = log.log_decode_validate::<E>() else {
93-
error!("fail to parse CommitteeCreated event log");
94-
return None;
95-
};
96-
Some(event)
89+
match log.log_decode_validate::<E>() {
90+
Ok(event) => Some(event),
91+
Err(err) => {
92+
error!(%err, "fail to parse `CommitteeCreated` event log");
93+
None
94+
}
95+
}
9796
});
9897

9998
Ok(Box::pin(validated))
10099
}
101100

102101
/// Returns the smallest block number whose timestamp is >= `target_ts` through binary search.
103-
/// Useful to determine `from_block` input of `Self::event_strea()` subscription.
102+
/// Useful to determine `from_block` input of `Self::event_stream()` subscription.
104103
pub async fn get_block_number_by_timestamp(
105104
&self,
106-
target_ts: u64,
107-
) -> anyhow::Result<Option<u64>> {
105+
target_ts: Timestamp,
106+
) -> anyhow::Result<Option<BlockNumber>> {
108107
let latest = self.get_block_number().await?;
109108
let mut lo: u64 = 0;
110109
let mut hi: u64 = latest;
@@ -124,7 +123,7 @@ impl PubSubProvider {
124123
}
125124
};
126125

127-
if block.header.timestamp >= target_ts {
126+
if block.header.timestamp >= target_ts.into() {
128127
if mid == 0 {
129128
return Ok(Some(0));
130129
}

timeboost/src/binaries/sailfish.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async fn main() -> Result<()> {
9292
let comm_info = CommitteeInfo::fetch(
9393
config.chain.parent.rpc_url,
9494
config.chain.parent.key_manager_contract,
95-
cli.committee_id.into(),
95+
cli.committee_id,
9696
)
9797
.await?;
9898
info!(label = %config.keys.signing.public, committee_id = %cli.committee_id, "committee info synced");
@@ -110,7 +110,7 @@ async fn main() -> Result<()> {
110110
config.net.public.address.clone(),
111111
signing_keypair.public_key(),
112112
dh_keypair.clone(),
113-
comm_info.group(),
113+
comm_info.address_info(),
114114
net_metrics,
115115
)
116116
.await?;

timeboost/src/binaries/timeboost.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async fn main() -> Result<()> {
7373
let comm_info = CommitteeInfo::fetch(
7474
node_config.chain.parent.rpc_url.clone(),
7575
node_config.chain.parent.key_manager_contract,
76-
cli.committee_id.into(),
76+
cli.committee_id,
7777
)
7878
.await?;
7979
info!(label = %sign_keypair.public_key(), committee_id = %cli.committee_id, "committee info synced");
@@ -95,8 +95,8 @@ async fn main() -> Result<()> {
9595
let pubkey = sign_keypair.public_key();
9696

9797
// optionally fetch previous committee info
98-
let cid: u64 = cli.committee_id.into();
99-
let prev_comm = if cid > 0u64 {
98+
let cid = cli.committee_id;
99+
let prev_comm = if cid > CommitteeId::default() {
100100
let c = &node_config.chain.parent;
101101
let prev_comm =
102102
CommitteeInfo::fetch(c.rpc_url.clone(), c.key_manager_contract, cid - 1).await?;

timeboost/src/committee.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,25 @@ pub struct CommitteeInfo {
3737

3838
impl CommitteeInfo {
3939
/// Fetch the committee info for `committee_id` from `key_manager_addr` on chain
40-
pub async fn fetch(rpc: Url, key_manager_addr: Address, committee_id: u64) -> Result<Self> {
40+
pub async fn fetch(
41+
rpc: Url,
42+
key_manager_addr: Address,
43+
committee_id: CommitteeId,
44+
) -> Result<Self> {
4145
let provider = ProviderBuilder::new().connect_http(rpc);
4246
Self::fetch_with(provider, key_manager_addr, committee_id).await
4347
}
4448

4549
pub(crate) async fn fetch_with(
4650
provider: impl Provider,
4751
key_manager_addr: Address,
48-
committee_id: u64,
52+
committee_id: CommitteeId,
4953
) -> Result<Self> {
5054
let contract = KeyManager::new(key_manager_addr, &provider);
51-
let c = contract.getCommitteeById(committee_id).call().await?;
55+
let c = contract
56+
.getCommitteeById(committee_id.into())
57+
.call()
58+
.await?;
5259

5360
let (signing_keys, dh_keys, dkg_keys, public_addresses) = c
5461
.members
@@ -69,7 +76,7 @@ impl CommitteeInfo {
6976
.multiunzip();
7077

7178
Ok(Self {
72-
id: committee_id.into(),
79+
id: committee_id,
7380
timestamp: c.effectiveTimestamp.into(),
7481
signing_keys,
7582
dh_keys,
@@ -100,7 +107,7 @@ impl CommitteeInfo {
100107
)
101108
}
102109

103-
pub fn group(
110+
pub fn address_info(
104111
&self,
105112
) -> impl Iterator<Item = (multisig::PublicKey, x25519::PublicKey, cliquenet::Address)> {
106113
izip!(
@@ -111,7 +118,7 @@ impl CommitteeInfo {
111118
}
112119

113120
pub fn sailfish_committee(&self) -> AddressableCommittee {
114-
AddressableCommittee::new(self.committee(), self.group())
121+
AddressableCommittee::new(self.committee(), self.address_info())
115122
}
116123

117124
pub fn decrypt_committee(&self) -> AddressableCommittee {
@@ -157,7 +164,7 @@ impl CommitteeInfo {
157164
config: &ParentChain,
158165
) -> Result<NewCommitteeStream> {
159166
let from_block = provider
160-
.get_block_number_by_timestamp(start_ts.into())
167+
.get_block_number_by_timestamp(start_ts)
161168
.await?
162169
.unwrap_or_default();
163170
let events = provider
@@ -176,11 +183,13 @@ impl CommitteeInfo {
176183
let s = events.filter_map(move |log| {
177184
let provider = provider.clone();
178185
async move {
179-
let id = log.data().id;
180-
match CommitteeInfo::fetch_with(provider.inner(), key_manager_contract, id).await {
186+
let committee_id: CommitteeId = log.data().id.into();
187+
match CommitteeInfo::fetch_with(&*provider, key_manager_contract, committee_id)
188+
.await
189+
{
181190
Ok(comm_info) => Some(comm_info),
182-
Err(_) => {
183-
error!(committee_id = %id, "fail to fetch new CommitteeInfo");
191+
Err(err) => {
192+
error!(%committee_id, %err, "fail to fetch new `CommitteeInfo`");
184193
None
185194
}
186195
}

timeboost/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ impl Timeboost {
154154
},
155155
res = self.events.next() => match res {
156156
Some(comm_info) => {
157-
let cur: u64 = self.config.key_store.committee().id().into();
158-
let new_id: u64 = comm_info.id().into();
157+
let cur = self.config.key_store.committee().id();
158+
let new_id = comm_info.id();
159159

160160
// contract ensures consecutive CommitteeId assignment
161161
if new_id == cur + 1 {

0 commit comments

Comments
 (0)