Skip to content

Commit

Permalink
chore: remove unnecessary async (#2394)
Browse files Browse the repository at this point in the history
Co-authored-by: Hubert Bugaj <hubert@chainsafe.io>
  • Loading branch information
lemmih and LesnyRumcajs authored Jan 11, 2023
1 parent 09816bf commit a1f2567
Show file tree
Hide file tree
Showing 55 changed files with 602 additions and 825 deletions.
5 changes: 4 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ num-rational = "0.4"
num-traits = "0.2"
num_cpus = "1.14"
once_cell = "1.15"
parking_lot = "0.12"
pin-project-lite = "0.2"
prometheus = "0.13"
prometheus-client = "0.18"
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ lint: license clean
cargo fmt --all --check
taplo fmt --check
taplo lint
cargo clippy --all-targets -- -D warnings
cargo clippy --all-targets --no-default-features --features forest_deleg_cns,paritydb,instrumented_kernel -- -D warnings
cargo clippy --features slow_tests --all-targets -- -D warnings -W clippy::unused_async
cargo clippy --all-targets --no-default-features --features forest_deleg_cns,paritydb,instrumented_kernel -- -D warnings -W clippy::unused_async

# Formats Rust and TOML files
fmt:
Expand Down
1 change: 1 addition & 0 deletions blockchain/beacon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ forest_encoding.workspace = true
forest_utils.workspace = true
fvm_shared = { workspace = true, default-features = false }
hex.workspace = true
parking_lot.workspace = true
serde = { workspace = true, features = ["derive"] }
sha2 = { workspace = true, default-features = false }
tokio = { workspace = true, features = ["sync"] }
Expand Down
23 changes: 6 additions & 17 deletions blockchain/beacon/src/drand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use byteorder::{BigEndian, WriteBytesExt};
use forest_utils::net::{https_client, HyperBodyExt};
use fvm_shared::clock::ChainEpoch;
use fvm_shared::version::NetworkVersion;
use parking_lot::RwLock;
use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
use sha2::Digest;
use std::borrow::Cow;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Environmental Variable to ignore `Drand`. Lotus parallel is `LOTUS_IGNORE_DRAND`
pub const IGNORE_DRAND_VAR: &str = "IGNORE_DRAND";
Expand Down Expand Up @@ -134,11 +134,7 @@ where
Self: Sized + Send + Sync + 'static,
{
/// Verify a new beacon entry against the most recent one before it.
async fn verify_entry(
&self,
curr: &BeaconEntry,
prev: &BeaconEntry,
) -> Result<bool, anyhow::Error>;
fn verify_entry(&self, curr: &BeaconEntry, prev: &BeaconEntry) -> Result<bool, anyhow::Error>;

/// Returns a `BeaconEntry` given a round. It fetches the `BeaconEntry` from a `Drand` node over [`gRPC`](https://grpc.io/)
/// In the future, we will cache values, and support streaming.
Expand Down Expand Up @@ -232,11 +228,7 @@ impl DrandBeacon {

#[async_trait]
impl Beacon for DrandBeacon {
async fn verify_entry(
&self,
curr: &BeaconEntry,
prev: &BeaconEntry,
) -> Result<bool, anyhow::Error> {
fn verify_entry(&self, curr: &BeaconEntry, prev: &BeaconEntry) -> Result<bool, anyhow::Error> {
// TODO: Handle Genesis better
if prev.round() == 0 {
return Ok(true);
Expand All @@ -253,18 +245,15 @@ impl Beacon for DrandBeacon {
let sig_match = bls_signatures::verify_messages(&sig, &[&digest], &[self.pub_key.key()?]);

// Cache the result
let contains_curr = self.local_cache.read().await.contains_key(&curr.round());
let contains_curr = self.local_cache.read().contains_key(&curr.round());
if sig_match && !contains_curr {
self.local_cache
.write()
.await
.insert(curr.round(), curr.clone());
self.local_cache.write().insert(curr.round(), curr.clone());
}
Ok(sig_match)
}

async fn entry(&self, round: u64) -> Result<BeaconEntry, anyhow::Error> {
let cached: Option<BeaconEntry> = self.local_cache.read().await.get(&round).cloned();
let cached: Option<BeaconEntry> = self.local_cache.read().get(&round).cloned();
match cached {
Some(cached_entry) => Ok(cached_entry),
None => {
Expand Down
6 changes: 1 addition & 5 deletions blockchain/beacon/src/mock_beacon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ impl MockBeacon {

#[async_trait]
impl Beacon for MockBeacon {
async fn verify_entry(
&self,
curr: &BeaconEntry,
prev: &BeaconEntry,
) -> Result<bool, anyhow::Error> {
fn verify_entry(&self, curr: &BeaconEntry, prev: &BeaconEntry) -> Result<bool, anyhow::Error> {
let oe = Self::entry_for_index(prev.round());
Ok(oe.data() == curr.data())
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/beacon/tests/drand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ async fn ask_and_verify_beacon_entry_fail() {

let e2 = beacon.entry(2).await.unwrap();
let e3 = beacon.entry(3).await.unwrap();
assert!(!beacon.verify_entry(&e2, &e3).await.unwrap());
assert!(!beacon.verify_entry(&e2, &e3).unwrap());
}
1 change: 0 additions & 1 deletion blockchain/blocks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ forest_test_utils.workspace = true
hex.workspace = true
quickcheck_macros.workspace = true
serde_json.workspace = true
tokio.workspace = true
23 changes: 9 additions & 14 deletions blockchain/blocks/src/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ impl BlockHeader {
}

/// Validates if the current header's Beacon entries are valid to ensure randomness was generated correctly
pub async fn validate_block_drand<B: Beacon>(
pub fn validate_block_drand<B: Beacon>(
&self,
network_version: NetworkVersion,
b_schedule: &BeaconSchedule<B>,
Expand All @@ -376,7 +376,6 @@ impl BlockHeader {

curr_beacon
.verify_entry(&self.beacon_entries[1], &self.beacon_entries[0])
.await
.map_err(|e| Error::Validation(e.to_string()))?;

return Ok(());
Expand Down Expand Up @@ -413,7 +412,6 @@ impl BlockHeader {
for curr in &self.beacon_entries {
if !curr_beacon
.verify_entry(curr, prev)
.await
.map_err(|e| Error::Validation(e.to_string()))?
{
return Err(Error::Validation(format!(
Expand Down Expand Up @@ -475,8 +473,8 @@ mod tests {
.unwrap();
}

#[tokio::test]
async fn beacon_entry_exists() {
#[test]
fn beacon_entry_exists() {
// Setup
let block_header = BlockHeader::builder()
.miner_address(Address::new_id(0))
Expand All @@ -490,15 +488,12 @@ mod tests {
let chain_epoch = 0;
let beacon_entry = BeaconEntry::new(1, vec![]);
// Validate_block_drand
if let Err(e) = block_header
.validate_block_drand(
NetworkVersion::V16,
&beacon_schedule,
chain_epoch,
&beacon_entry,
)
.await
{
if let Err(e) = block_header.validate_block_drand(
NetworkVersion::V16,
&beacon_schedule,
chain_epoch,
&beacon_entry,
) {
// Assert error is for not including a beacon entry in the block
match e {
Error::Validation(why) => {
Expand Down
1 change: 1 addition & 0 deletions blockchain/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fvm_shared = { workspace = true, default-features = false }
log.workspace = true
lru.workspace = true
once_cell.workspace = true
parking_lot.workspace = true
serde = { workspace = true, features = ["derive"] }
thiserror.workspace = true
tokio = { workspace = true, features = ["sync"] }
Expand Down
Loading

0 comments on commit a1f2567

Please sign in to comment.