mirror of
https://github.com/serai-dex/serai.git
synced 2025-01-12 13:55:28 +00:00
98190b7b83
* initial staking pallet * add staking pallet to runtime * support session rotation for serai * optimizations & cleaning * fix deny * add serai network to initial networks * a few tweaks & comments * fix some pr comments * Rewrite validator-sets with logarithmic algorithms Uses the fact the underlying DB is sorted to achieve sorting of potential validators by stake. Removes release of deallocated stake for now. --------- Co-authored-by: Luke Parker <lukeparker5132@gmail.com>
61 lines
1.5 KiB
Rust
61 lines
1.5 KiB
Rust
use blake2::{
|
|
digest::{consts::U32, Digest},
|
|
Blake2b,
|
|
};
|
|
|
|
use scale::Encode;
|
|
|
|
use sp_core::Pair;
|
|
|
|
use serai_client::{
|
|
primitives::insecure_pair_from_name,
|
|
validator_sets::primitives::{Session, ValidatorSet},
|
|
in_instructions::{
|
|
primitives::{Batch, SignedBatch, batch_message},
|
|
InInstructionsEvent,
|
|
},
|
|
Serai,
|
|
};
|
|
|
|
use crate::common::{serai, tx::publish_tx, validator_sets::set_validator_set_keys};
|
|
|
|
#[allow(dead_code)]
|
|
pub async fn provide_batch(batch: Batch) -> [u8; 32] {
|
|
let serai = serai().await;
|
|
|
|
// TODO: Get the latest session
|
|
let set = ValidatorSet { session: Session(0), network: batch.network };
|
|
let pair = insecure_pair_from_name(&format!("ValidatorSet {:?}", set));
|
|
let keys = if let Some(keys) =
|
|
serai.get_keys(set, serai.get_latest_block_hash().await.unwrap()).await.unwrap()
|
|
{
|
|
keys
|
|
} else {
|
|
let keys = (pair.public(), vec![].try_into().unwrap());
|
|
set_validator_set_keys(set, keys.clone()).await;
|
|
keys
|
|
};
|
|
assert_eq!(keys.0, pair.public());
|
|
|
|
let block = publish_tx(&Serai::execute_batch(SignedBatch {
|
|
batch: batch.clone(),
|
|
signature: pair.sign(&batch_message(&batch)),
|
|
}))
|
|
.await;
|
|
|
|
let batches = serai.get_batch_events(block).await.unwrap();
|
|
// TODO: impl From<Batch> for BatchEvent?
|
|
assert_eq!(
|
|
batches,
|
|
vec![InInstructionsEvent::Batch {
|
|
network: batch.network,
|
|
id: batch.id,
|
|
block: batch.block,
|
|
instructions_hash: Blake2b::<U32>::digest(batch.instructions.encode()).into(),
|
|
}],
|
|
);
|
|
|
|
// TODO: Check the tokens events
|
|
|
|
block
|
|
}
|