Skip to content

Commit c153532

Browse files
committed
use registeredBlkNum from contract for from_block
1 parent fbc48a5 commit c153532

File tree

7 files changed

+30
-55
lines changed

7 files changed

+30
-55
lines changed

timeboost-contract/src/deployer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mod tests {
5353
use alloy::{
5454
eips::BlockNumberOrTag,
5555
node_bindings::Anvil,
56+
primitives::U256,
5657
providers::{Provider, ProviderBuilder, WalletProvider},
5758
rpc::types::Filter,
5859
sol_types::{SolEvent, SolValue},
@@ -94,8 +95,11 @@ mod tests {
9495
.await
9596
.unwrap()
9697
.abi_encode_sequence(),
98+
// deploy takes first 2 blocks: deploying implementation contract and proxy contract
99+
// setNextCommittee is the 3rd tx, thus in 3rd block
97100
CommitteeSol {
98101
id: 0,
102+
registeredBlockNumber: U256::from(3),
99103
effectiveTimestamp: timestamp,
100104
members,
101105
}

timeboost-contract/src/provider.rs

Lines changed: 2 additions & 41 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, BlockNumber},
9+
primitives::Address,
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, Timestamp};
17+
use timeboost_types::{HttpProvider, HttpProviderWithWallet};
1818
use tracing::error;
1919

2020
/// Build a local signer from wallet mnemonic and account index
@@ -97,43 +97,4 @@ impl PubSubProvider {
9797

9898
Ok(Box::pin(validated))
9999
}
100-
101-
/// Returns the smallest block number whose timestamp is >= `target_ts` through binary search.
102-
/// Useful to determine `from_block` input of `Self::event_stream()` subscription.
103-
pub async fn get_block_number_by_timestamp(
104-
&self,
105-
target_ts: Timestamp,
106-
) -> anyhow::Result<Option<BlockNumber>> {
107-
let latest = self.get_block_number().await?;
108-
let mut lo: u64 = 0;
109-
let mut hi: u64 = latest;
110-
111-
while lo <= hi {
112-
let mid = lo + (hi - lo) / 2;
113-
114-
let block = match self
115-
.0
116-
.get_block_by_number(BlockNumberOrTag::Number(mid))
117-
.await?
118-
{
119-
Some(b) => b,
120-
None => {
121-
lo = mid + 1;
122-
continue;
123-
}
124-
};
125-
126-
if block.header.timestamp >= target_ts.into() {
127-
if mid == 0 {
128-
return Ok(Some(0));
129-
}
130-
hi = mid - 1;
131-
} else {
132-
lo = mid + 1;
133-
}
134-
}
135-
136-
// At this point, `lo` is the smallest index with ts >= target_ts (if any)
137-
if lo > latest { Ok(None) } else { Ok(Some(lo)) }
138-
}
139100
}

timeboost/src/binaries/timeboost.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ async fn main() -> Result<()> {
107107

108108
let config = TimeboostConfig::builder()
109109
.sailfish_committee(sailfish_committee)
110+
.registered_blk(*comm_info.registered_block())
110111
.maybe_prev_committee(prev_comm)
111112
.decrypt_committee(decrypt_committee)
112113
.certifier_committee(certifier_committee)

timeboost/src/committee.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use timeboost_config::{CERTIFIER_PORT_OFFSET, DECRYPTER_PORT_OFFSET, ParentChain
1616
use timeboost_contract::KeyManager::{self, CommitteeCreated};
1717
use timeboost_contract::provider::PubSubProvider;
1818
use timeboost_crypto::prelude::DkgEncKey;
19-
use timeboost_types::{KeyStore, Timestamp};
19+
use timeboost_types::{BlockNumber, KeyStore, Timestamp};
2020
use tracing::error;
2121
use url::Url;
2222

@@ -29,6 +29,7 @@ pub type NewCommitteeStream = Pin<Box<dyn Stream<Item = CommitteeInfo>>>;
2929
pub struct CommitteeInfo {
3030
id: CommitteeId,
3131
timestamp: Timestamp,
32+
registered_blk: BlockNumber,
3233
signing_keys: Vec<multisig::PublicKey>,
3334
dh_keys: Vec<x25519::PublicKey>,
3435
dkg_keys: Vec<DkgEncKey>,
@@ -78,6 +79,7 @@ impl CommitteeInfo {
7879
Ok(Self {
7980
id: committee_id,
8081
timestamp: c.effectiveTimestamp.into(),
82+
registered_blk: c.registeredBlockNumber.to::<u64>().into(),
8183
signing_keys,
8284
dh_keys,
8385
dkg_keys,
@@ -93,6 +95,10 @@ impl CommitteeInfo {
9395
self.timestamp
9496
}
9597

98+
pub fn registered_block(&self) -> BlockNumber {
99+
self.registered_blk
100+
}
101+
96102
pub fn signing_keys(&self) -> &[multisig::PublicKey] {
97103
&self.signing_keys
98104
}
@@ -160,17 +166,13 @@ impl CommitteeInfo {
160166
/// subscribe an event stream
161167
pub async fn new_committee_stream(
162168
provider: &PubSubProvider,
163-
start_ts: Timestamp,
169+
from_block: BlockNumber,
164170
config: &ParentChain,
165171
) -> Result<NewCommitteeStream> {
166-
let from_block = provider
167-
.get_block_number_by_timestamp(start_ts)
168-
.await?
169-
.unwrap_or_default();
170172
let events = provider
171173
.event_stream::<CommitteeCreated>(
172174
config.key_manager_contract,
173-
BlockNumberOrTag::Number(from_block),
175+
BlockNumberOrTag::Number(*from_block),
174176
)
175177
.await
176178
.map_err(|e| {

timeboost/src/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloy::primitives::BlockNumber;
12
use bon::Builder;
23
use cliquenet::{Address, AddressableCommittee};
34
use multisig::{Keypair, x25519};
@@ -14,6 +15,9 @@ pub struct TimeboostConfig {
1415
/// The sailfish peers that this node will connect to.
1516
pub(crate) sailfish_committee: AddressableCommittee,
1617

18+
/// The block in which the current committee is registered
19+
pub(crate) registered_blk: BlockNumber,
20+
1721
/// Previous committee info stored on chain
1822
pub(crate) prev_committee: Option<CommitteeInfo>,
1923

timeboost/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,17 @@ impl Timeboost {
6868
};
6969

7070
let provider = PubSubProvider::new(cfg.chain_config.parent.ws_url.clone()).await?;
71-
let start_ts = cfg
71+
let from_blk = cfg
7272
.prev_committee
7373
.as_ref()
74-
.map(|c| c.effective_timestamp())
75-
.unwrap_or_default();
76-
let events =
77-
CommitteeInfo::new_committee_stream(&provider, start_ts, &cfg.chain_config.parent)
78-
.await?;
74+
.map_or_else(|| cfg.registered_blk, |prev| *prev.registered_block());
75+
76+
let events = CommitteeInfo::new_committee_stream(
77+
&provider,
78+
from_blk.into(),
79+
&cfg.chain_config.parent,
80+
)
81+
.await?;
7982

8083
let (tx, rx) = mpsc::channel(100);
8184

0 commit comments

Comments
 (0)