Skip to content

Commit

Permalink
Update dockertests to new serai-client
Browse files Browse the repository at this point in the history
  • Loading branch information
kayabaNerve committed Oct 14, 2023
1 parent e3e9939 commit 7d4e8b5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 60 deletions.
1 change: 1 addition & 0 deletions substrate/client/src/serai/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl Serai {
.map_err(SeraiError::RpcError)
}

/// Create a TemporalSerai using whatever is currently the latest block.
pub async fn with_current_latest_block(&self) -> Result<TemporalSerai, SeraiError> {
let latest = self.latest_block_hash().await?;
Ok(TemporalSerai(self, latest))
Expand Down
2 changes: 1 addition & 1 deletion tests/coordinator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Processor {
for _ in 0 .. 60 {
tokio::time::sleep(Duration::from_secs(1)).await;
let Ok(client) = Serai::new(&serai_rpc).await else { continue };
if client.get_latest_block_hash().await.is_err() {
if client.latest_block_hash().await.is_err() {
continue;
}
break;
Expand Down
12 changes: 6 additions & 6 deletions tests/coordinator/src/tests/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub async fn batch(
let batch = SignedBatch { batch, signature };

let serai = processors[0].serai().await;
let mut last_serai_block = serai.get_latest_block().await.unwrap().number();
let mut last_serai_block = serai.latest_block().await.unwrap().number();

for processor in processors.iter_mut() {
processor
Expand All @@ -187,11 +187,11 @@ pub async fn batch(
tokio::time::sleep(Duration::from_secs(6)).await;
}

while last_serai_block <= serai.get_latest_block().await.unwrap().number() {
while last_serai_block <= serai.latest_block().await.unwrap().number() {
let batch_events = serai
.get_batch_events(
serai.get_block_by_number(last_serai_block).await.unwrap().unwrap().hash(),
)
.as_of(serai.block_by_number(last_serai_block).await.unwrap().unwrap().hash())
.in_instructions()
.batch_events()
.await
.unwrap();

Expand All @@ -213,7 +213,7 @@ pub async fn batch(
}

// Verify the coordinator sends SubstrateBlock to all processors
let last_block = serai.get_block_by_number(last_serai_block).await.unwrap().unwrap();
let last_block = serai.block_by_number(last_serai_block).await.unwrap().unwrap();
for processor in processors.iter_mut() {
assert_eq!(
processor.recv_message().await,
Expand Down
14 changes: 8 additions & 6 deletions tests/coordinator/src/tests/key_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub async fn key_gen<C: Ciphersuite>(
let network_key = (C::generator() * *network_priv_key).to_bytes().as_ref().to_vec();

let serai = processors[0].serai().await;
let mut last_serai_block = serai.get_latest_block().await.unwrap().number();
let mut last_serai_block = serai.latest_block().await.unwrap().number();

wait_for_tributary().await;
for (i, processor) in processors.iter_mut().enumerate() {
Expand Down Expand Up @@ -148,11 +148,11 @@ pub async fn key_gen<C: Ciphersuite>(
tokio::time::sleep(Duration::from_secs(6)).await;
}

while last_serai_block <= serai.get_latest_block().await.unwrap().number() {
while last_serai_block <= serai.latest_block().await.unwrap().number() {
if !serai
.get_key_gen_events(
serai.get_block_by_number(last_serai_block).await.unwrap().unwrap().hash(),
)
.as_of(serai.block_by_number(last_serai_block).await.unwrap().unwrap().hash())
.validator_sets()
.key_gen_events()
.await
.unwrap()
.is_empty()
Expand Down Expand Up @@ -196,7 +196,9 @@ pub async fn key_gen<C: Ciphersuite>(
}
assert_eq!(
serai
.get_keys(set, serai.get_block_by_number(last_serai_block).await.unwrap().unwrap().hash())
.as_of(serai.block_by_number(last_serai_block).await.unwrap().unwrap().hash())
.validator_sets()
.keys(set)
.await
.unwrap()
.unwrap(),
Expand Down
64 changes: 27 additions & 37 deletions tests/coordinator/src/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ use serai_client::{
NetworkId, Coin, Amount, Balance, BlockHash, SeraiAddress, ExternalAddress,
insecure_pair_from_name,
},
tokens::{
coins::{
primitives::{OutInstruction, OutInstructionWithBalance},
TokensEvent,
},
in_instructions::primitives::{InInstruction, InInstructionWithBalance, Batch},
SeraiCoins,
};
use messages::{sign::SignId, SubstrateContext, CoordinatorMessage};

Expand Down Expand Up @@ -210,7 +211,7 @@ async fn sign_test() {
&serai
.sign(
&PairSigner::new(insecure_pair_from_name("Ferdie")),
&Serai::transfer_sri(address, Amount(1_000_000_000)),
&SeraiCoins::transfer_sri(address, Amount(1_000_000_000)),
0,
Default::default(),
)
Expand Down Expand Up @@ -243,27 +244,21 @@ async fn sign_test() {
)
.await;

let block_included_in_hash =
serai.get_block_by_number(block_included_in).await.unwrap().unwrap().hash();

assert_eq!(
serai.get_sri_balance(block_included_in_hash, serai_addr).await.unwrap(),
1_000_000_000
);

// Verify the mint occurred as expected
assert_eq!(
serai.get_mint_events(block_included_in_hash).await.unwrap(),
vec![TokensEvent::Mint { address: serai_addr, balance }]
);
assert_eq!(
serai.get_token_supply(block_included_in_hash, Coin::Bitcoin).await.unwrap(),
amount
);
assert_eq!(
serai.get_token_balance(block_included_in_hash, Coin::Bitcoin, serai_addr).await.unwrap(),
amount
);
{
let block_included_in_hash =
serai.block_by_number(block_included_in).await.unwrap().unwrap().hash();

let serai = serai.as_of(block_included_in_hash).coins();
assert_eq!(serai.sri_balance(serai_addr).await.unwrap(), 1_000_000_000);

// Verify the mint occurred as expected
assert_eq!(
serai.mint_events().await.unwrap(),
vec![TokensEvent::Mint { address: serai_addr, balance }]
);
assert_eq!(serai.token_supply(Coin::Bitcoin).await.unwrap(), amount);
assert_eq!(serai.token_balance(Coin::Bitcoin, serai_addr).await.unwrap(), amount);
}

// Trigger a burn
let out_instruction =
Expand All @@ -273,7 +268,7 @@ async fn sign_test() {
&serai
.sign(
&serai_pair,
&Serai::burn(balance, out_instruction.clone()),
&SeraiCoins::burn(balance, out_instruction.clone()),
0,
Default::default(),
)
Expand All @@ -290,11 +285,11 @@ async fn sign_test() {
tokio::time::sleep(Duration::from_secs(6)).await;
}

while last_serai_block <= serai.get_latest_block().await.unwrap().number() {
while last_serai_block <= serai.latest_block().await.unwrap().number() {
let burn_events = serai
.get_burn_events(
serai.get_block_by_number(last_serai_block).await.unwrap().unwrap().hash(),
)
.as_of(serai.block_by_number(last_serai_block).await.unwrap().unwrap().hash())
.coins()
.burn_events()
.await
.unwrap();

Expand All @@ -314,16 +309,11 @@ async fn sign_test() {
}
}

let last_serai_block = serai.get_block_by_number(last_serai_block).await.unwrap().unwrap();
let last_serai_block = serai.block_by_number(last_serai_block).await.unwrap().unwrap();
let last_serai_block_hash = last_serai_block.hash();
assert_eq!(
serai.get_token_supply(last_serai_block_hash, Coin::Bitcoin).await.unwrap(),
Amount(0)
);
assert_eq!(
serai.get_token_balance(last_serai_block_hash, Coin::Bitcoin, serai_addr).await.unwrap(),
Amount(0)
);
let serai = serai.as_of(last_serai_block_hash).coins();
assert_eq!(serai.token_supply(Coin::Bitcoin).await.unwrap(), Amount(0));
assert_eq!(serai.token_balance(Coin::Bitcoin, serai_addr).await.unwrap(), Amount(0));

let mut plan_id = [0; 32];
OsRng.fill_bytes(&mut plan_id);
Expand Down
2 changes: 1 addition & 1 deletion tests/full-stack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Handles {
for _ in 0 .. 60 {
tokio::time::sleep(Duration::from_secs(1)).await;
let Ok(client) = Serai::new(&serai_rpc).await else { continue };
if client.get_latest_block_hash().await.is_err() {
if client.latest_block_hash().await.is_err() {
continue;
}
return client;
Expand Down
23 changes: 14 additions & 9 deletions tests/full-stack/src/tests/mint_and_burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use serai_client::{
},
validator_sets::primitives::{Session, ValidatorSet},
in_instructions::primitives::Shorthand,
tokens::primitives::OutInstruction,
PairTrait, PairSigner,
coins::primitives::OutInstruction,
PairTrait, PairSigner, SeraiCoins,
};

use crate::tests::*;
Expand Down Expand Up @@ -196,10 +196,11 @@ async fn mint_and_burn_test() {
let print_at = halt_at / 2;
for i in 0 .. halt_at {
if let Some(key_pair) = serai
.get_keys(
ValidatorSet { network, session: Session(0) },
serai.get_latest_block_hash().await.unwrap(),
)
.with_current_latest_block()
.await
.unwrap()
.validator_sets()
.keys(ValidatorSet { network, session: Session(0) })
.await
.unwrap()
{
Expand Down Expand Up @@ -240,7 +241,7 @@ async fn mint_and_burn_test() {
&serai
.sign(
&PairSigner::new(insecure_pair_from_name("Ferdie")),
&Serai::transfer_sri(address, Amount(1_000_000_000)),
&SeraiCoins::transfer_sri(address, Amount(1_000_000_000)),
0,
Default::default(),
)
Expand Down Expand Up @@ -408,7 +409,11 @@ async fn mint_and_burn_test() {
let print_at = halt_at / 2;
for i in 0 .. halt_at {
if serai
.get_last_batch_for_network(serai.get_latest_block_hash().await.unwrap(), network)
.with_current_latest_block()
.await
.unwrap()
.in_instructions()
.last_batch_for_network(network)
.await
.unwrap()
.is_some()
Expand Down Expand Up @@ -490,7 +495,7 @@ async fn mint_and_burn_test() {
&serai
.sign(
serai_pair,
&Serai::burn(Balance { coin, amount: Amount(amount) }, out_instruction),
&SeraiCoins::burn(Balance { coin, amount: Amount(amount) }, out_instruction),
nonce,
Default::default(),
)
Expand Down

0 comments on commit 7d4e8b5

Please sign in to comment.