Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Feb 27, 2024
1 parent 360cd02 commit 4dfaf31
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 65 deletions.
4 changes: 2 additions & 2 deletions substrate/client/tests/common/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub async fn allocate_stake(
nonce: u32,
) -> [u8; 32] {
// get the call
let tx = serai.sign(&pair, SeraiValidatorSets::allocate(network, amount), nonce, 0);
let tx = serai.sign(pair, SeraiValidatorSets::allocate(network, amount), nonce, 0);
publish_tx(serai, &tx).await
}

Expand All @@ -86,6 +86,6 @@ pub async fn deallocate_stake(
nonce: u32,
) -> [u8; 32] {
// get the call
let tx = serai.sign(&pair, SeraiValidatorSets::deallocate(network, amount), nonce, 0);
let tx = serai.sign(pair, SeraiValidatorSets::deallocate(network, amount), nonce, 0);
publish_tx(serai, &tx).await
}
6 changes: 3 additions & 3 deletions substrate/client/tests/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async fn validator_set_rotation() {

// we start the chain with 4 default participants that has a single key share each
participants.sort();
verify_session_and_active_validators(&serai, network, 0, &participants).await;
verify_session_and_active_validators(&serai, network, 0, participants).await;

// add 1 participant & verify
let hash =
Expand All @@ -190,7 +190,7 @@ async fn validator_set_rotation() {
&serai,
network,
get_active_session(&serai, network, hash).await,
&participants,
participants,
)
.await;

Expand All @@ -201,7 +201,7 @@ async fn validator_set_rotation() {
participants.swap_remove(participants.iter().position(|k| *k == pair2.public()).unwrap());
let active_session = get_active_session(&serai, network, hash).await;
participants.sort();
verify_session_and_active_validators(&serai, network, active_session, &participants).await;
verify_session_and_active_validators(&serai, network, active_session, participants).await;

// check pending deallocations
let pending = serai
Expand Down
2 changes: 1 addition & 1 deletion substrate/validator-sets/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ pub mod pallet {
// If not Serai, check the prior session had its keys cleared, which happens once its
// retired
return (network == NetworkId::Serai) ||
(Keys::<T>::contains_key(ValidatorSet {
(!Keys::<T>::contains_key(ValidatorSet {
network,
session: Session(current_session.0 - 1),
}));
Expand Down
2 changes: 1 addition & 1 deletion tests/coordinator/src/tests/key_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub async fn key_gen<C: Ciphersuite>(
assert_eq!(key_pair.0 .0, substrate_key);
assert_eq!(&key_pair.1, &network_key);
}
_ => panic!("coordinator didn't respond with ConfirmKeyPair msg: {:?} ", msg),
_ => panic!("coordinator didn't respond with ConfirmKeyPair msg: {msg:?}"),
}
message = Some(msg);
} else {
Expand Down
2 changes: 0 additions & 2 deletions tests/coordinator/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ mod sign;
pub use sign::sign;

mod rotation;
#[allow(unused_imports)]
pub use rotation::rotate;

pub(crate) const COORDINATORS: usize = 4;
pub(crate) const THRESHOLD: usize = ((COORDINATORS * 2) / 3) + 1;
Expand Down
83 changes: 27 additions & 56 deletions tests/coordinator/src/tests/rotation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use tokio::time::{sleep, Duration};

use zeroize::Zeroizing;
use ciphersuite::{Ciphersuite, Ristretto, Secp256k1};
use ciphersuite::Secp256k1;

use serai_client::{
primitives::{insecure_pair_from_name, NetworkId},
Expand Down Expand Up @@ -64,7 +63,7 @@ async fn allocate_stake(
) -> [u8; 32] {
// get the call
let tx =
serai.sign(&pair, validator_sets::SeraiValidatorSets::allocate(network, amount), nonce, 0);
serai.sign(pair, validator_sets::SeraiValidatorSets::allocate(network, amount), nonce, 0);
publish_tx(serai, &tx).await
}

Expand All @@ -78,7 +77,7 @@ async fn deallocate_stake(
) -> [u8; 32] {
// get the call
let tx =
serai.sign(&pair, validator_sets::SeraiValidatorSets::deallocate(network, amount), nonce, 0);
serai.sign(pair, validator_sets::SeraiValidatorSets::deallocate(network, amount), nonce, 0);
publish_tx(serai, &tx).await
}

Expand All @@ -95,8 +94,6 @@ async fn wait_till_next_epoch(serai: &Serai, current_epoch: u32) -> Session {
.await
.unwrap()
.unwrap();

println!("current session: {} ", session.0);
}
session
}
Expand All @@ -114,8 +111,7 @@ async fn new_set_events(
let mut current_session = get_session(serai, current_block.hash(), network).await;

while current_session == session {
let mut events =
serai.as_of(current_block.hash()).validator_sets().new_set_events().await.unwrap();
let events = serai.as_of(current_block.hash()).validator_sets().new_set_events().await.unwrap();
if !events.is_empty() {
return events;
}
Expand All @@ -127,51 +123,6 @@ async fn new_set_events(
panic!("can't find the new set events for session: {} ", session.0);
}

pub async fn rotate(
processors: &mut Vec<Processor>,
excluded: Processor,
_: &[u8],
_: &Zeroizing<<Ristretto as Ciphersuite>::F>,
) {
// accounts
let pair1 = insecure_pair_from_name("Alice");
let pair5 = insecure_pair_from_name("Eve");

let network = NetworkId::Bitcoin;
let amount = Amount(1_000_000 * 10_u64.pow(8));
let serai = processors[0].serai().await;

// add the last participant into validator set for btc network
let block = allocate_stake(&serai, network, amount, &pair5, 0).await;

// wait until next session to see the effect on coordinator
let current_epoch = get_session(&serai, block, NetworkId::Serai).await;
let session = wait_till_next_epoch(&serai, current_epoch.0).await;

// verfiy that coordinator received new_set
let events = new_set_events(&serai, session, network).await;
assert!(events.contains(&ValidatorSetsEvent::NewSet { set: ValidatorSet { session, network } }));

// do the keygen
processors.push(excluded);
let _ = key_gen::<Secp256k1>(processors, session).await;

// pop 1 participant
let block = deallocate_stake(&serai, network, amount, &pair1, 0).await;

// wait for this epoch to end
let current_epoch = get_session(&serai, block, NetworkId::Serai).await;
let session = wait_till_next_epoch(&serai, current_epoch.0).await;

// verfiy that coordinator received new_set
let events = new_set_events(&serai, session, network).await;
assert!(events.contains(&ValidatorSetsEvent::NewSet { set: ValidatorSet { session, network } }));

// do the keygen
processors.remove(0);
let _ = key_gen::<Secp256k1>(processors, session).await;
}

#[tokio::test]
async fn set_rotation_test() {
new_test(
Expand All @@ -180,10 +131,30 @@ async fn set_rotation_test() {
let excluded = processors.pop().unwrap();
assert_eq!(processors.len(), COORDINATORS);

let (processor_is, substrate_key, _) =
key_gen::<Secp256k1>(&mut processors, Session(0)).await;
// genesis keygen
let _ = key_gen::<Secp256k1>(&mut processors, Session(0)).await;

let pair5 = insecure_pair_from_name("Eve");
let network = NetworkId::Bitcoin;
let amount = Amount(1_000_000 * 10_u64.pow(8));
let serai = processors[0].serai().await;

// add the last participant into validator set for btc network
let block = allocate_stake(&serai, network, amount, &pair5, 0).await;

// wait until next session to see the effect on coordinator
let current_epoch = get_session(&serai, block, NetworkId::Serai).await;
let session = wait_till_next_epoch(&serai, current_epoch.0).await;

// verfiy that coordinator received new_set
let events = new_set_events(&serai, session, network).await;
assert!(
events.contains(&ValidatorSetsEvent::NewSet { set: ValidatorSet { session, network } })
);

rotate(&mut processors, excluded, &processor_is, &substrate_key).await;
// add the last participant & do the keygen
processors.push(excluded);
let _ = key_gen::<Secp256k1>(&mut processors, session).await;
},
true,
)
Expand Down

0 comments on commit 4dfaf31

Please sign in to comment.