-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cae29d
commit 886a0e2
Showing
6 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
fendermint/testing/materializer/src/concurrency/collect.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2022-2024 Protocol Labs | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use crate::concurrency::signal::Signal; | ||
use ethers::prelude::H256; | ||
use ethers::providers::Http; | ||
use ethers::providers::{Middleware, Provider}; | ||
use ethers::types::Block; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use std::time::Duration; | ||
use tokio::time::sleep; | ||
|
||
pub async fn collect_blocks<F>( | ||
cancel: Arc<Signal>, | ||
provider: Provider<Http>, | ||
assert: F, | ||
) -> anyhow::Result<HashMap<u64, Block<H256>>> | ||
where | ||
F: Fn(&Block<H256>), | ||
{ | ||
let mut blocks = HashMap::new(); | ||
loop { | ||
if cancel.received() { | ||
break; | ||
} | ||
|
||
// TODO: improve: use less calls, make sure blocks cannot be missed | ||
let block_number = provider.get_block_number().await?; | ||
let block = provider.get_block(block_number).await?.unwrap(); | ||
assert(&block); | ||
blocks.insert(block_number.as_u64(), block); | ||
|
||
sleep(Duration::from_millis(100)).await; | ||
} | ||
Ok(blocks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2022-2024 Protocol Labs | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
pub struct Signal(tokio::sync::Semaphore); | ||
|
||
impl Signal { | ||
pub fn new() -> Self { | ||
Self(tokio::sync::Semaphore::new(0)) | ||
} | ||
|
||
pub fn send(&self) { | ||
self.0.close(); | ||
} | ||
|
||
pub fn received(&self) -> bool { | ||
self.0.is_closed() | ||
} | ||
} | ||
|
||
impl Default for Signal { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters