Skip to content

Commit

Permalink
Fix a flaky test by unbanning a peer
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Feb 7, 2025
1 parent a8a4845 commit 8dab353
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
5 changes: 4 additions & 1 deletion domains/client/domain-operator/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,10 @@ async fn test_domain_tx_propagate() -> Result<(), tokio::time::error::Elapsed> {

async {
while alice.sync_service.is_major_syncing() || bob.sync_service.is_major_syncing() {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
// Unfortunately, this test contains a race condition where Alice sometimes bans Bob
// for making multiple requests for the same block.
alice.unban_peer(bob.addr.clone());
}
}
.timeout()
Expand Down
31 changes: 30 additions & 1 deletion domains/test/service/src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi;
use sc_client_api::HeaderBackend;
use sc_domains::RuntimeExecutor;
use sc_network::service::traits::NetworkService;
use sc_network::{NetworkRequest, NetworkStateInfo};
use sc_network::{NetworkRequest, NetworkStateInfo, ReputationChange};
use sc_network_sync::SyncingService;
use sc_service::config::MultiaddrWithPeerId;
use sc_service::{BasePath, Role, RpcHandlers, TFullBackend, TaskManager};
Expand Down Expand Up @@ -406,6 +406,35 @@ where
let function = function.into();
UncheckedExtrinsicFor::<Runtime>::new_unsigned(function)
}

/// Give the peer at `addr` the minimum reputation, which will ban it.
// TODO: also ban/unban in the DSN
pub fn ban_peer(&self, addr: MultiaddrWithPeerId) {
// If unban_peer() has been called on the peer, we need to bump it twice
// to give it the minimal reputation.
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new_fatal("Peer banned by test (1)"),
);
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new_fatal("Peer banned by test (2)"),
);
}

/// Give the peer at `addr` a high reputation, which guarantees it is un-banned it.
pub fn unban_peer(&self, addr: MultiaddrWithPeerId) {
// If ReputationChange::new_fatal() has been called on the peer, we need to bump it twice
// to give it a positive reputation.
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new(i32::MAX, "Peer unbanned by test (1)"),
);
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new(i32::MAX, "Peer unbanned by test (2)"),
);
}
}

impl<Runtime, RuntimeApi> DomainNode<Runtime, RuntimeApi>
Expand Down
33 changes: 32 additions & 1 deletion test/subspace-test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ use sc_consensus::{BasicQueue, BlockImport, StateAction, Verifier as VerifierT};
use sc_domains::ExtensionsFactory as DomainsExtensionFactory;
use sc_network::config::{NetworkConfiguration, TransportConfig};
use sc_network::service::traits::NetworkService;
use sc_network::{multiaddr, NetworkWorker, NotificationMetrics, NotificationService};
use sc_network::{
multiaddr, NetworkWorker, NotificationMetrics, NotificationService, ReputationChange,
};
use sc_service::config::{
DatabaseSource, ExecutorConfiguration, KeystoreConfig, MultiaddrWithPeerId,
OffchainWorkerConfig, RpcBatchRequestConfig, RpcConfiguration, WasmExecutionMethod,
Expand Down Expand Up @@ -848,6 +850,35 @@ impl MockConsensusNode {
.free_balance(self.client.info().best_hash, account_id)
.expect("Fail to get account free balance")
}

/// Give the peer at `addr` the minimum reputation, which will ban it.
// TODO: also ban/unban in the DSN
pub fn ban_peer(&self, addr: MultiaddrWithPeerId) {
// If unban_peer() has been called on the peer, we need to bump it twice
// to give it the minimal reputation.
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new_fatal("Peer banned by test (1)"),
);
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new_fatal("Peer banned by test (2)"),
);
}

/// Give the peer at `addr` a high reputation, which guarantees it is un-banned it.
pub fn unban_peer(&self, addr: MultiaddrWithPeerId) {
// If ReputationChange::new_fatal() has been called on the peer, we need to bump it twice
// to give it a positive reputation.
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new(i32::MAX, "Peer unbanned by test (1)"),
);
self.network_service.report_peer(
addr.peer_id,
ReputationChange::new(i32::MAX, "Peer unbanned by test (2)"),
);
}
}

impl MockConsensusNode {
Expand Down

0 comments on commit 8dab353

Please sign in to comment.