mirror of
https://github.com/serai-dex/serai.git
synced 2025-02-03 11:46:31 +00:00
Merge weights and signing scheme into validators, documenting needed changes
This commit is contained in:
parent
5839f44290
commit
285152b6e2
5 changed files with 39 additions and 45 deletions
|
@ -26,7 +26,7 @@ use tendermint_machine::{
|
|||
|
||||
use crate::{
|
||||
CONSENSUS_ID,
|
||||
signature_scheme::TendermintSigner,
|
||||
validators::TendermintValidators,
|
||||
tendermint::{TendermintClient, TendermintImport},
|
||||
Announce,
|
||||
};
|
||||
|
@ -116,7 +116,7 @@ where
|
|||
Ok(best) => BlockNumber(best),
|
||||
Err(_) => panic!("BlockNumber exceeded u64"),
|
||||
},
|
||||
Commit::<TendermintSigner>::decode(
|
||||
Commit::<TendermintValidators>::decode(
|
||||
&mut import_clone
|
||||
.client
|
||||
.justifications(&BlockId::Number(best))
|
||||
|
|
|
@ -11,8 +11,7 @@ use substrate_prometheus_endpoint::Registry;
|
|||
|
||||
use serai_runtime::{self, opaque::Block, RuntimeApi};
|
||||
|
||||
mod signature_scheme;
|
||||
mod weights;
|
||||
mod validators;
|
||||
|
||||
mod tendermint;
|
||||
mod block_import;
|
||||
|
|
|
@ -33,8 +33,7 @@ use tendermint_machine::{
|
|||
|
||||
use crate::{
|
||||
CONSENSUS_ID,
|
||||
signature_scheme::TendermintSigner,
|
||||
weights::TendermintWeights,
|
||||
validators::TendermintValidators,
|
||||
import_queue::{ImportFuture, TendermintImportQueue},
|
||||
Announce,
|
||||
};
|
||||
|
@ -197,7 +196,7 @@ where
|
|||
Err(Error::InvalidJustification)?;
|
||||
}
|
||||
|
||||
let commit: Commit<TendermintSigner> =
|
||||
let commit: Commit<TendermintValidators> =
|
||||
Commit::decode(&mut justification.1.as_ref()).map_err(|_| Error::InvalidJustification)?;
|
||||
if !self.verify_commit(hash, &commit) {
|
||||
Err(Error::InvalidJustification)?;
|
||||
|
@ -312,18 +311,18 @@ where
|
|||
TransactionFor<C, B>: Send + Sync + 'static,
|
||||
{
|
||||
type ValidatorId = u16;
|
||||
type SignatureScheme = TendermintSigner;
|
||||
type Weights = TendermintWeights;
|
||||
type SignatureScheme = TendermintValidators;
|
||||
type Weights = TendermintValidators;
|
||||
type Block = B;
|
||||
|
||||
const BLOCK_TIME: u32 = { (serai_runtime::MILLISECS_PER_BLOCK / 1000) as u32 };
|
||||
|
||||
fn signature_scheme(&self) -> Arc<TendermintSigner> {
|
||||
Arc::new(TendermintSigner::new())
|
||||
fn signature_scheme(&self) -> Arc<TendermintValidators> {
|
||||
Arc::new(TendermintValidators::new())
|
||||
}
|
||||
|
||||
fn weights(&self) -> Arc<TendermintWeights> {
|
||||
Arc::new(TendermintWeights)
|
||||
fn weights(&self) -> Arc<TendermintValidators> {
|
||||
Arc::new(TendermintValidators::new())
|
||||
}
|
||||
|
||||
async fn broadcast(&mut self, msg: SignedMessage<u16, Self::Block, Signature>) {
|
||||
|
@ -391,7 +390,7 @@ where
|
|||
Ok(())
|
||||
}
|
||||
|
||||
async fn add_block(&mut self, block: B, commit: Commit<TendermintSigner>) -> B {
|
||||
async fn add_block(&mut self, block: B, commit: Commit<TendermintValidators>) -> B {
|
||||
let hash = block.hash();
|
||||
let justification = (CONSENSUS_ID, commit.encode());
|
||||
debug_assert!(self.verify_justification(hash, &justification).is_ok());
|
||||
|
|
|
@ -1,24 +1,27 @@
|
|||
// TODO: This should be built around pallet_sessions (and pallet_staking?).
|
||||
|
||||
use sp_application_crypto::{
|
||||
RuntimePublic as PublicTrait, Pair as PairTrait,
|
||||
sr25519::{Public, Pair, Signature},
|
||||
};
|
||||
|
||||
use tendermint_machine::ext::SignatureScheme;
|
||||
use tendermint_machine::ext::{BlockNumber, Round, Weights, SignatureScheme};
|
||||
|
||||
pub(crate) struct TendermintSigner {
|
||||
keys: Pair,
|
||||
lookup: Vec<Public>,
|
||||
const VALIDATORS: usize = 1;
|
||||
|
||||
pub(crate) struct TendermintValidators {
|
||||
keys: Pair, // sp_keystore
|
||||
lookup: Vec<Public>, // sessions
|
||||
}
|
||||
|
||||
impl TendermintSigner {
|
||||
pub(crate) fn new() -> TendermintSigner {
|
||||
// TODO
|
||||
impl TendermintValidators {
|
||||
pub(crate) fn new() -> TendermintValidators {
|
||||
let keys = Pair::from_string("//Alice", None).unwrap();
|
||||
TendermintSigner { lookup: vec![keys.public()], keys }
|
||||
TendermintValidators { lookup: vec![keys.public()], keys }
|
||||
}
|
||||
}
|
||||
|
||||
impl SignatureScheme for TendermintSigner {
|
||||
impl SignatureScheme for TendermintValidators {
|
||||
type ValidatorId = u16;
|
||||
type Signature = Signature;
|
||||
type AggregateSignature = Vec<Signature>;
|
||||
|
@ -47,3 +50,18 @@ impl SignatureScheme for TendermintSigner {
|
|||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl Weights for TendermintValidators {
|
||||
type ValidatorId = u16;
|
||||
|
||||
fn total_weight(&self) -> u64 {
|
||||
VALIDATORS.try_into().unwrap()
|
||||
}
|
||||
fn weight(&self, id: u16) -> u64 {
|
||||
[1; VALIDATORS][usize::try_from(id).unwrap()]
|
||||
}
|
||||
|
||||
fn proposer(&self, number: BlockNumber, round: Round) -> u16 {
|
||||
u16::try_from((number.0 + u64::from(round.0)) % u64::try_from(VALIDATORS).unwrap()).unwrap()
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
// TODO
|
||||
|
||||
use tendermint_machine::ext::{BlockNumber, Round, Weights};
|
||||
|
||||
const VALIDATORS: usize = 1;
|
||||
|
||||
// TODO: Move to sp_session
|
||||
pub(crate) struct TendermintWeights;
|
||||
impl Weights for TendermintWeights {
|
||||
type ValidatorId = u16;
|
||||
|
||||
fn total_weight(&self) -> u64 {
|
||||
VALIDATORS.try_into().unwrap()
|
||||
}
|
||||
fn weight(&self, id: u16) -> u64 {
|
||||
[1; VALIDATORS][usize::try_from(id).unwrap()]
|
||||
}
|
||||
|
||||
fn proposer(&self, number: BlockNumber, round: Round) -> u16 {
|
||||
u16::try_from((number.0 + u64::from(round.0)) % u64::try_from(VALIDATORS).unwrap()).unwrap()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue