Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace lazy_static with LazyLock #5716

Merged
14 changes: 0 additions & 14 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,6 @@ kvdb-memorydb = { version = "0.13.0" }
kvdb-rocksdb = { version = "0.19.0" }
kvdb-shared-tests = { version = "0.11.0" }
landlock = { version = "0.3.0" }
lazy_static = { version = "1.5.0" }
libc = { version = "0.2.155" }
libfuzzer-sys = { version = "0.4" }
libp2p = { version = "0.52.4" }
Expand Down
1 change: 0 additions & 1 deletion polkadot/node/network/dispute-distribution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ sp-tracing = { workspace = true, default-features = true }
sc-keystore = { workspace = true, default-features = true }
futures-timer = { workspace = true }
assert_matches = { workspace = true }
lazy_static = { workspace = true }
polkadot-primitives-test-helpers = { workspace = true }
102 changes: 48 additions & 54 deletions polkadot/node/network/dispute-distribution/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@

use std::{
collections::{HashMap, HashSet},
sync::Arc,
sync::{Arc, LazyLock, OnceLock},
time::Instant,
};

use async_trait::async_trait;
use lazy_static::lazy_static;

use polkadot_node_network_protocol::{authority_discovery::AuthorityDiscovery, PeerId};
use sc_keystore::LocalKeystore;
Expand Down Expand Up @@ -60,64 +59,59 @@ pub const ALICE_INDEX: ValidatorIndex = ValidatorIndex(1);
pub const BOB_INDEX: ValidatorIndex = ValidatorIndex(2);
pub const CHARLIE_INDEX: ValidatorIndex = ValidatorIndex(3);

lazy_static! {

/// Mocked `AuthorityDiscovery` service.
pub static ref MOCK_AUTHORITY_DISCOVERY: MockAuthorityDiscovery = MockAuthorityDiscovery::new();
pub static MOCK_AUTHORITY_DISCOVERY: OnceLock<MockAuthorityDiscovery> = OnceLock::new();
programskillforverification marked this conversation as resolved.
Show resolved Hide resolved
// Creating an innocent looking `SessionInfo` is really expensive in a debug build. Around
// 700ms on my machine, We therefore cache those keys here:
pub static ref MOCK_VALIDATORS_DISCOVERY_KEYS: HashMap<Sr25519Keyring, AuthorityDiscoveryId> =
MOCK_VALIDATORS
.iter()
.chain(MOCK_AUTHORITIES_NEXT_SESSION.iter())
.map(|v| (*v, v.public().into()))
.collect()
;
pub static ref FERDIE_DISCOVERY_KEY: AuthorityDiscoveryId =
MOCK_VALIDATORS_DISCOVERY_KEYS.get(&Sr25519Keyring::Ferdie).unwrap().clone();

pub static ref MOCK_SESSION_INFO: SessionInfo =
SessionInfo {
validators: MOCK_VALIDATORS.iter().take(4).map(|k| k.public().into()).collect(),
discovery_keys: MOCK_VALIDATORS
pub static MOCK_VALIDATORS_DISCOVERY_KEYS: LazyLock<HashMap<Sr25519Keyring, AuthorityDiscoveryId>> =
LazyLock::new(|| {
MOCK_VALIDATORS
.iter()
.map(|k| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&k).unwrap().clone())
.collect(),
assignment_keys: vec![],
validator_groups: Default::default(),
n_cores: 0,
zeroth_delay_tranche_width: 0,
relay_vrf_modulo_samples: 0,
n_delay_tranches: 0,
no_show_slots: 0,
needed_approvals: 0,
active_validator_indices: vec![],
dispute_period: 6,
random_seed: [0u8; 32],
};
.chain(MOCK_AUTHORITIES_NEXT_SESSION.iter())
.map(|v| (*v, v.public().into()))
.collect()
});
pub static FERDIE_DISCOVERY_KEY: LazyLock<AuthorityDiscoveryId> =
LazyLock::new(|| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&Sr25519Keyring::Ferdie).unwrap().clone());

pub static MOCK_SESSION_INFO: LazyLock<SessionInfo> = LazyLock::new(|| SessionInfo {
validators: MOCK_VALIDATORS.iter().take(4).map(|k| k.public().into()).collect(),
discovery_keys: MOCK_VALIDATORS
.iter()
.map(|k| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&k).unwrap().clone())
.collect(),
assignment_keys: vec![],
validator_groups: Default::default(),
n_cores: 0,
zeroth_delay_tranche_width: 0,
relay_vrf_modulo_samples: 0,
n_delay_tranches: 0,
no_show_slots: 0,
needed_approvals: 0,
active_validator_indices: vec![],
dispute_period: 6,
random_seed: [0u8; 32],
});

/// `SessionInfo` for the second session. (No more validators, but two more authorities.
pub static ref MOCK_NEXT_SESSION_INFO: SessionInfo =
SessionInfo {
discovery_keys:
MOCK_AUTHORITIES_NEXT_SESSION
.iter()
.map(|k| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&k).unwrap().clone())
.collect(),
validators: Default::default(),
assignment_keys: vec![],
validator_groups: Default::default(),
n_cores: 0,
zeroth_delay_tranche_width: 0,
relay_vrf_modulo_samples: 0,
n_delay_tranches: 0,
no_show_slots: 0,
needed_approvals: 0,
active_validator_indices: vec![],
dispute_period: 6,
random_seed: [0u8; 32],
};
}
pub static MOCK_NEXT_SESSION_INFO: LazyLock<SessionInfo> = LazyLock::new(|| SessionInfo {
discovery_keys: MOCK_AUTHORITIES_NEXT_SESSION
.iter()
.map(|k| MOCK_VALIDATORS_DISCOVERY_KEYS.get(&k).unwrap().clone())
.collect(),
validators: Default::default(),
assignment_keys: vec![],
validator_groups: Default::default(),
n_cores: 0,
zeroth_delay_tranche_width: 0,
relay_vrf_modulo_samples: 0,
n_delay_tranches: 0,
no_show_slots: 0,
needed_approvals: 0,
active_validator_indices: vec![],
dispute_period: 6,
random_seed: [0u8; 32],
});

pub fn make_candidate_receipt(relay_parent: Hash) -> CandidateReceipt {
CandidateReceipt {
Expand Down
43 changes: 34 additions & 9 deletions polkadot/node/network/dispute-distribution/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ fn received_request_triggers_import() {
nested_network_dispute_request(
&mut handle,
req_tx,
MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Alice),
MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Alice),
message.clone().into(),
ImportStatementsResult::ValidImport,
true,
Expand Down Expand Up @@ -237,7 +240,10 @@ fn batching_works() {
nested_network_dispute_request(
&mut handle,
req_tx,
MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Alice),
MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Alice),
message.clone().into(),
ImportStatementsResult::ValidImport,
true,
Expand All @@ -248,11 +254,17 @@ fn batching_works() {
let mut rx_responses = Vec::new();

let message = make_dispute_message(candidate.clone(), BOB_INDEX, FERDIE_INDEX);
let peer = MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Bob);
let peer = MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Bob);
rx_responses.push(send_network_dispute_request(req_tx, peer, message.clone().into()).await);

let message = make_dispute_message(candidate.clone(), CHARLIE_INDEX, FERDIE_INDEX);
let peer = MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Charlie);
let peer = MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Charlie);
rx_responses.push(send_network_dispute_request(req_tx, peer, message.clone().into()).await);
gum::trace!("Imported 3 votes into batch");

Expand All @@ -263,12 +275,18 @@ fn batching_works() {
gum::trace!("Importing duplicate votes");
let mut rx_responses_duplicate = Vec::new();
let message = make_dispute_message(candidate.clone(), BOB_INDEX, FERDIE_INDEX);
let peer = MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Bob);
let peer = MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Bob);
rx_responses_duplicate
.push(send_network_dispute_request(req_tx, peer, message.clone().into()).await);

let message = make_dispute_message(candidate.clone(), CHARLIE_INDEX, FERDIE_INDEX);
let peer = MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Charlie);
let peer = MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Charlie);
rx_responses_duplicate
.push(send_network_dispute_request(req_tx, peer, message.clone().into()).await);

Expand Down Expand Up @@ -362,7 +380,10 @@ fn receive_rate_limit_is_enforced() {
nested_network_dispute_request(
&mut handle,
req_tx,
MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Alice),
MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Alice),
message.clone().into(),
ImportStatementsResult::ValidImport,
true,
Expand All @@ -372,7 +393,10 @@ fn receive_rate_limit_is_enforced() {

let mut rx_responses = Vec::new();

let peer = MOCK_AUTHORITY_DISCOVERY.get_peer_id_by_authority(Sr25519Keyring::Bob);
let peer = MOCK_AUTHORITY_DISCOVERY
.get()
.unwrap()
.get_peer_id_by_authority(Sr25519Keyring::Bob);

let message = make_dispute_message(candidate.clone(), BOB_INDEX, FERDIE_INDEX);
rx_responses.push(send_network_dispute_request(req_tx, peer, message.clone().into()).await);
Expand Down Expand Up @@ -875,6 +899,7 @@ where
Fut: Future<Output = ()>,
{
sp_tracing::try_init_simple();
MOCK_AUTHORITY_DISCOVERY.get_or_init(|| mock::MockAuthorityDiscovery::new());
let keystore = make_ferdie_keystore();

let genesis_hash = Hash::repeat_byte(0xff);
Expand All @@ -886,7 +911,7 @@ where
let subsystem = DisputeDistributionSubsystem::new(
keystore,
req_receiver,
MOCK_AUTHORITY_DISCOVERY.clone(),
MOCK_AUTHORITY_DISCOVERY.get().unwrap().clone(),
Metrics::new_dummy(),
);

Expand Down
1 change: 0 additions & 1 deletion polkadot/node/network/gossip-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ polkadot-node-subsystem-test-helpers = { workspace = true }
assert_matches = { workspace = true }
async-trait = { workspace = true }
parking_lot = { workspace = true, default-features = true }
lazy_static = { workspace = true }
quickcheck = { workspace = true, default-features = true }
Loading
Loading