fix tests

This commit is contained in:
akildemir
2024-07-26 18:33:51 +03:00
parent 84b534a4f4
commit 8eb7e66a23
15 changed files with 270 additions and 190 deletions

View File

@@ -1,9 +1,7 @@
use sp_core::bounded_vec::BoundedVec;
use serai_abi::primitives::{SeraiAddress, Amount, Coin};
use scale::{decode_from_bytes, Encode};
use crate::{Serai, SeraiError, TemporalSerai};
use crate::{SeraiError, TemporalSerai};
pub type DexEvent = serai_abi::dex::Event;
@@ -63,19 +61,8 @@ impl<'a> SeraiDex<'a> {
}
/// Returns the reserves of `coin:SRI` pool.
pub async fn get_reserves(&self, coin: Coin) -> Result<Option<(Amount, Amount)>, SeraiError> {
let reserves = self
.0
.serai
.call(
"state_call",
["DexApi_get_reserves".to_string(), hex::encode((coin, Coin::Serai).encode())],
)
.await?;
let bytes = Serai::hex_decode(reserves)?;
let result = decode_from_bytes::<Option<(u64, u64)>>(bytes.into())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(result.map(|amounts| (Amount(amounts.0), Amount(amounts.1))))
pub async fn get_reserves(&self, coin: Coin) -> Result<Option<(u64, u64)>, SeraiError> {
self.0.runtime_api("DexApi_get_reserves", (coin, Coin::Serai)).await
}
pub async fn oracle_value(&self, coin: Coin) -> Result<Option<Amount>, SeraiError> {

View File

@@ -62,4 +62,8 @@ impl<'a> SeraiGenesisLiquidity<'a> {
pub async fn supply(&self, coin: Coin) -> Result<LiquidityAmount, SeraiError> {
Ok(self.0.storage(PALLET, "Supply", coin).await?.unwrap_or(LiquidityAmount::zero()))
}
pub async fn genesis_complete(&self) -> Result<Option<()>, SeraiError> {
self.0.storage(PALLET, "GenesisComplete", ()).await
}
}

View File

@@ -198,17 +198,6 @@ impl Serai {
Ok(())
}
// TODO: move this into substrate/client/src/validator_sets.rs
async fn active_network_validators(&self, network: NetworkId) -> Result<Vec<Public>, SeraiError> {
let validators: String = self
.call("state_call", ["SeraiRuntimeApi_validators".to_string(), hex::encode(network.encode())])
.await?;
let bytes = Self::hex_decode(validators)?;
let r = Vec::<Public>::decode(&mut bytes.as_slice())
.map_err(|e| SeraiError::ErrorInResponse(e.to_string()))?;
Ok(r)
}
pub async fn latest_finalized_block_hash(&self) -> Result<[u8; 32], SeraiError> {
let hash: String = self.call("chain_getFinalizedHead", ()).await?;
Self::hex_decode(hash)?.try_into().map_err(|_| {
@@ -378,6 +367,28 @@ impl<'a> TemporalSerai<'a> {
})?))
}
async fn runtime_api<P: Encode, R: Decode>(
&self,
method: &'static str,
params: P,
) -> Result<R, SeraiError> {
let result: String = self
.serai
.call(
"state_call",
[method.to_string(), hex::encode(params.encode()), hex::encode(self.block)],
)
.await?;
let bytes = Serai::hex_decode(result.clone())?;
R::decode(&mut bytes.as_slice()).map_err(|_| {
SeraiError::InvalidRuntime(format!(
"different type than what is expected returned, raw value: {}",
hex::encode(result)
))
})
}
pub fn coins(&'a self) -> SeraiCoins<'a> {
SeraiCoins(self)
}

View File

@@ -163,7 +163,7 @@ impl<'a> SeraiValidatorSets<'a> {
&self,
network: NetworkId,
) -> Result<Vec<Public>, SeraiError> {
self.0.serai.active_network_validators(network).await
self.0.runtime_api("SeraiRuntimeApi_validators", network).await
}
// TODO: Store these separately since we almost never need both at once?
@@ -178,6 +178,14 @@ impl<'a> SeraiValidatorSets<'a> {
self.0.storage(PALLET, "PendingSlashReport", network).await
}
pub async fn session_begin_block(
&self,
network: NetworkId,
session: Session,
) -> Result<Option<u64>, SeraiError> {
self.0.storage(PALLET, "SessionBeginBlock", (network, session)).await
}
pub fn set_keys(
network: NetworkId,
removed_participants: sp_runtime::BoundedVec<

View File

@@ -33,7 +33,7 @@ use serai_client::{
use crate::common::{in_instructions::provide_batch, tx::publish_tx};
#[allow(dead_code)]
pub async fn test_genesis_liquidity(serai: Serai) {
pub async fn test_genesis_liquidity(serai: Serai) -> HashMap<NetworkId, u32> {
// all coins except the native
let coins = COINS.into_iter().filter(|c| *c != Coin::native()).collect::<Vec<_>>();
@@ -79,21 +79,8 @@ pub async fn test_genesis_liquidity(serai: Serai) {
provide_batch(&serai, batch).await;
}
// wait until genesis ends
let genesis_blocks = 10; // TODO
let block_time = 6; // TODO
tokio::time::timeout(
tokio::time::Duration::from_secs(3 * (genesis_blocks * block_time)),
async {
while serai.latest_finalized_block().await.unwrap().number() < 10 {
tokio::time::sleep(Duration::from_secs(6)).await;
}
},
)
.await
.unwrap();
// set values relative to each other
// set values relative to each other. We can do that without checking for genesis period blocks
// since we are running in test(fast-epoch) mode.
// TODO: Random values here
let values = Values { monero: 184100, ether: 4785000, dai: 1500 };
set_values(&serai, &values).await;
@@ -103,8 +90,19 @@ pub async fn test_genesis_liquidity(serai: Serai) {
(Coin::Dai, values.dai),
]);
// wait a little bit..
tokio::time::sleep(Duration::from_secs(12)).await;
// wait until genesis is complete
while serai
.as_of_latest_finalized_block()
.await
.unwrap()
.genesis_liquidity()
.genesis_complete()
.await
.unwrap()
.is_none()
{
tokio::time::sleep(Duration::from_secs(1)).await;
}
// check total SRI supply is +100M
// there are 6 endowed accounts in dev-net. Take this into consideration when checking
@@ -147,8 +145,8 @@ pub async fn test_genesis_liquidity(serai: Serai) {
total_sri_distributed += sri;
let reserves = serai.dex().get_reserves(coin).await.unwrap().unwrap();
assert_eq!(u128::from(reserves.0 .0), pool_amounts[&coin].0); // coin side
assert_eq!(u128::from(reserves.1 .0), sri); // SRI side
assert_eq!(u128::from(reserves.0), pool_amounts[&coin].0); // coin side
assert_eq!(u128::from(reserves.1), sri); // SRI side
}
// check each liquidity provider got liquidity tokens proportional to their value
@@ -172,8 +170,9 @@ pub async fn test_genesis_liquidity(serai: Serai) {
assert_eq!(shares_ratio, amounts_ratio);
}
}
// TODO: test remove the liq before/after genesis ended.
batch_ids
}
#[allow(dead_code)]
@@ -181,8 +180,8 @@ async fn set_values(serai: &Serai, values: &Values) {
// prepare a Musig tx to oraclize the relative values
let pair = insecure_pair_from_name("Alice");
let public = pair.public();
// we publish the tx in set 4
let set = ValidatorSet { session: Session(4), network: NetworkId::Serai };
// we publish the tx in set 1
let set = ValidatorSet { session: Session(1), network: NetworkId::Serai };
let public_key = <Ristretto as Ciphersuite>::read_G::<&[u8]>(&mut public.0.as_ref()).unwrap();
let secret_key = <Ristretto as Ciphersuite>::read_F::<&[u8]>(

View File

@@ -10,7 +10,7 @@ use sp_core::Pair;
use serai_client::{
primitives::{insecure_pair_from_name, BlockHash, NetworkId, Balance, SeraiAddress},
validator_sets::primitives::{Session, ValidatorSet, KeyPair},
validator_sets::primitives::{ValidatorSet, KeyPair},
in_instructions::{
primitives::{Batch, SignedBatch, batch_message, InInstruction, InInstructionWithBalance},
InInstructionsEvent,
@@ -22,12 +22,12 @@ use crate::common::{tx::publish_tx, validator_sets::set_keys};
#[allow(dead_code)]
pub async fn provide_batch(serai: &Serai, batch: Batch) -> [u8; 32] {
// TODO: Get the latest session
let set = ValidatorSet { session: Session(0), network: batch.network };
let serai_latest = serai.as_of_latest_finalized_block().await.unwrap();
let session = serai_latest.validator_sets().session(batch.network).await.unwrap().unwrap();
let set = ValidatorSet { session, network: batch.network };
let pair = insecure_pair_from_name(&format!("ValidatorSet {set:?}"));
let keys = if let Some(keys) =
serai.as_of_latest_finalized_block().await.unwrap().validator_sets().keys(set).await.unwrap()
{
let keys = if let Some(keys) = serai_latest.validator_sets().keys(set).await.unwrap() {
keys
} else {
let keys = KeyPair(pair.public(), vec![].try_into().unwrap());

View File

@@ -1,14 +1,13 @@
use rand_core::{RngCore, OsRng};
use scale::Encode;
use sp_core::{Pair as PairTrait, bounded_vec::BoundedVec, hashing::blake2_256};
use sp_core::{Pair as PairTrait, bounded_vec::BoundedVec};
use serai_abi::in_instructions::primitives::DexCall;
use serai_client::{
primitives::{
Amount, NetworkId, Coin, Balance, BlockHash, insecure_pair_from_name, ExternalAddress,
SeraiAddress, PublicKey,
SeraiAddress,
},
in_instructions::primitives::{
InInstruction, InInstructionWithBalance, Batch, IN_INSTRUCTION_EXECUTOR, OutAddress,
@@ -28,33 +27,6 @@ use common::{
// TODO: Modularize common code
// TODO: Check Transfer events
serai_test!(
create_pool: (|serai: Serai| async move {
let block = serai.finalized_block_by_number(0).await.unwrap().unwrap().hash();
let events = serai.as_of(block).dex().events().await.unwrap();
assert_eq!(
events,
vec![
DexEvent::PoolCreated {
pool_id: Coin::Bitcoin,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Bitcoin.encode())).into(),
},
DexEvent::PoolCreated {
pool_id: Coin::Ether,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Ether.encode())).into(),
},
DexEvent::PoolCreated {
pool_id: Coin::Dai,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Dai.encode())).into(),
},
DexEvent::PoolCreated {
pool_id: Coin::Monero,
pool_account: PublicKey::from_raw(blake2_256(&Coin::Monero.encode())).into(),
},
]
);
})
add_liquidity: (|serai: Serai| async move {
let coin = Coin::Monero;
let pair = insecure_pair_from_name("Ferdie");

View File

@@ -1,10 +1,13 @@
use std::{time::Duration, collections::HashMap};
use rand_core::{RngCore, OsRng};
use serai_client::TemporalSerai;
use serai_abi::{
emissions::primitives::{INITIAL_REWARD_PER_BLOCK, SECURE_BY},
primitives::{Coin, COINS, NETWORKS},
in_instructions::primitives::Batch,
primitives::{NETWORKS, BlockHash},
validator_sets::primitives::Session,
};
use serai_client::{
@@ -13,7 +16,7 @@ use serai_client::{
};
mod common;
use common::genesis_liquidity::test_genesis_liquidity;
use common::{genesis_liquidity::test_genesis_liquidity, in_instructions::provide_batch};
serai_test_fast_epoch!(
emissions: (|serai: Serai| async move {
@@ -21,14 +24,35 @@ serai_test_fast_epoch!(
})
);
async fn send_batches(serai: &Serai, ids: &mut HashMap<NetworkId, u32>) {
for network in NETWORKS {
if network != NetworkId::Serai {
// set up batch id
ids
.entry(network)
.and_modify(|v| {
*v += 1;
})
.or_insert(0);
// set up block hash
let mut block = BlockHash([0; 32]);
OsRng.fill_bytes(&mut block.0);
provide_batch(serai, Batch { network, id: ids[&network], block, instructions: vec![] }).await;
}
}
}
async fn test_emissions(serai: Serai) {
// provide some genesis liquidity
test_genesis_liquidity(serai.clone()).await;
let mut batch_ids = test_genesis_liquidity(serai.clone()).await;
let mut last_epoch_start = 0;
for i in 1 .. 3 {
for _ in 0 .. 3 {
// get current stakes
let mut current_stake = HashMap::new();
for n in NETWORKS {
// TODO: investigate why serai network TAS isn't visible at session 0.
let stake = serai
.as_of_latest_finalized_block()
.await
@@ -42,21 +66,25 @@ async fn test_emissions(serai: Serai) {
current_stake.insert(n, stake);
}
// wait until we have at least 1 session
wait_for_session(&serai, i).await;
// wait for a session change
let current_session = wait_for_session_change(&serai).await;
// get distances to ec security
// get last block
let last_block = serai.latest_finalized_block().await.unwrap();
let serai_latest = serai.as_of(last_block.hash());
let change_block_number = last_block.number();
// get distances to ec security & block count of the previous session
let (distances, total_distance) = get_distances(&serai_latest, &current_stake).await;
let block_count = get_session_blocks(&serai_latest, current_session - 1).await;
// calculate how much reward in this session
let block_count = last_block.number() - last_epoch_start;
let reward_this_epoch = if i == 1 {
// last block number should be the block count since we are in the first block of session 1.
// TODO: genesis is complete at block 24 in current tests. Initial period is just double that.
// See the emissions pallet to further read on this. We also assume we are in pre-ec era.
let reward_this_epoch = if change_block_number < 24 * 3 {
block_count * INITIAL_REWARD_PER_BLOCK
} else {
let blocks_until = SECURE_BY - last_block.number();
let blocks_until = SECURE_BY - change_block_number;
let block_reward = total_distance / blocks_until;
block_count * block_reward
};
@@ -73,8 +101,14 @@ async fn test_emissions(serai: Serai) {
})
.collect::<HashMap<NetworkId, u64>>();
// retire the prev-set so that TotalAllocatedStake updated.
send_batches(&serai, &mut batch_ids).await;
for (n, reward) in reward_per_network {
let stake = serai_latest
let stake = serai
.as_of_latest_finalized_block()
.await
.unwrap()
.validator_sets()
.total_allocated_stake(n)
.await
@@ -85,10 +119,9 @@ async fn test_emissions(serai: Serai) {
// all reward should automatically staked for the network since we are in initial period.
assert_eq!(stake, *current_stake.get(&n).unwrap() + reward);
}
// TODO: check stake per address?
// TODO: check post ec security era
last_epoch_start = last_block.number();
}
}
@@ -99,7 +132,7 @@ async fn required_stake(serai: &TemporalSerai<'_>, balance: Balance) -> u64 {
// See dex-pallet for the reasoning on these
let coin_decimals = balance.coin.decimals().max(5);
let accuracy_increase = u128::from(u64::pow(10, coin_decimals));
let accuracy_increase = u128::from(10u64.pow(coin_decimals));
let total_coin_value =
u64::try_from(u128::from(balance.amount.0) * u128::from(sri_per_coin.0) / accuracy_increase)
@@ -110,9 +143,21 @@ async fn required_stake(serai: &TemporalSerai<'_>, balance: Balance) -> u64 {
required_stake.saturating_add(total_coin_value.saturating_div(5))
}
async fn wait_for_session(serai: &Serai, session: u32) {
// Epoch time is half an hour with the fast epoch feature, so lets wait double that.
tokio::time::timeout(tokio::time::Duration::from_secs(60 * 6), async {
async fn wait_for_session_change(serai: &Serai) -> u32 {
let current_session = serai
.as_of_latest_finalized_block()
.await
.unwrap()
.validator_sets()
.session(NetworkId::Serai)
.await
.unwrap()
.unwrap()
.0;
let next_session = current_session + 1;
// Epoch time is 2 mins with the fast epoch feature, so lets wait double that.
tokio::time::timeout(tokio::time::Duration::from_secs(60 * 4), async {
while serai
.as_of_latest_finalized_block()
.await
@@ -123,13 +168,15 @@ async fn wait_for_session(serai: &Serai, session: u32) {
.unwrap()
.unwrap()
.0 <
session
next_session
{
tokio::time::sleep(Duration::from_secs(6)).await;
}
})
.await
.unwrap();
next_session
}
async fn get_distances(
@@ -140,14 +187,18 @@ async fn get_distances(
// we can check the supply to see how much coin hence liability we have.
let mut distances: HashMap<NetworkId, u64> = HashMap::new();
let mut total_distance = 0;
for coin in COINS {
if coin == Coin::Serai {
for n in NETWORKS {
if n == NetworkId::Serai {
continue;
}
let amount = serai.coins().coin_supply(coin).await.unwrap();
let required = required_stake(serai, Balance { coin, amount }).await;
let mut current = *current_stake.get(&coin.network()).unwrap();
let mut required = 0;
for c in n.coins() {
let amount = serai.coins().coin_supply(*c).await.unwrap();
required += required_stake(serai, Balance { coin: *c, amount }).await;
}
let mut current = *current_stake.get(&n).unwrap();
if current > required {
current = required;
}
@@ -155,10 +206,7 @@ async fn get_distances(
let distance = required - current;
total_distance += distance;
distances.insert(
coin.network(),
distances.get(&coin.network()).unwrap_or(&0).saturating_add(distance),
);
distances.insert(n, distance);
}
// add serai network portion(20%)
@@ -168,3 +216,21 @@ async fn get_distances(
(distances, total_distance)
}
async fn get_session_blocks(serai: &TemporalSerai<'_>, session: u32) -> u64 {
let begin_block = serai
.validator_sets()
.session_begin_block(NetworkId::Serai, Session(session))
.await
.unwrap()
.unwrap();
let next_begin_block = serai
.validator_sets()
.session_begin_block(NetworkId::Serai, Session(session + 1))
.await
.unwrap()
.unwrap();
next_begin_block.saturating_sub(begin_block)
}

View File

@@ -326,8 +326,8 @@ async fn verify_session_and_active_validators(
session: u32,
participants: &[Public],
) {
// wait until the active session. This wait should be max 30 secs since the epoch time.
let block = tokio::time::timeout(core::time::Duration::from_secs(2 * 60), async move {
// wait until the active session. This wait should be max 2 mins since the epoch time.
let block = tokio::time::timeout(core::time::Duration::from_secs(5 * 60), async move {
loop {
let mut block = serai.latest_finalized_block_hash().await.unwrap();
if session_for_block(serai, block, network).await < session {