Merge weights and signing scheme into validators, documenting needed changes

This commit is contained in:
Luke Parker
2022-10-25 02:51:33 -04:00
parent 5839f44290
commit 285152b6e2
5 changed files with 39 additions and 45 deletions

View File

@@ -26,7 +26,7 @@ use tendermint_machine::{
use crate::{ use crate::{
CONSENSUS_ID, CONSENSUS_ID,
signature_scheme::TendermintSigner, validators::TendermintValidators,
tendermint::{TendermintClient, TendermintImport}, tendermint::{TendermintClient, TendermintImport},
Announce, Announce,
}; };
@@ -116,7 +116,7 @@ where
Ok(best) => BlockNumber(best), Ok(best) => BlockNumber(best),
Err(_) => panic!("BlockNumber exceeded u64"), Err(_) => panic!("BlockNumber exceeded u64"),
}, },
Commit::<TendermintSigner>::decode( Commit::<TendermintValidators>::decode(
&mut import_clone &mut import_clone
.client .client
.justifications(&BlockId::Number(best)) .justifications(&BlockId::Number(best))

View File

@@ -11,8 +11,7 @@ use substrate_prometheus_endpoint::Registry;
use serai_runtime::{self, opaque::Block, RuntimeApi}; use serai_runtime::{self, opaque::Block, RuntimeApi};
mod signature_scheme; mod validators;
mod weights;
mod tendermint; mod tendermint;
mod block_import; mod block_import;

View File

@@ -33,8 +33,7 @@ use tendermint_machine::{
use crate::{ use crate::{
CONSENSUS_ID, CONSENSUS_ID,
signature_scheme::TendermintSigner, validators::TendermintValidators,
weights::TendermintWeights,
import_queue::{ImportFuture, TendermintImportQueue}, import_queue::{ImportFuture, TendermintImportQueue},
Announce, Announce,
}; };
@@ -197,7 +196,7 @@ where
Err(Error::InvalidJustification)?; Err(Error::InvalidJustification)?;
} }
let commit: Commit<TendermintSigner> = let commit: Commit<TendermintValidators> =
Commit::decode(&mut justification.1.as_ref()).map_err(|_| Error::InvalidJustification)?; Commit::decode(&mut justification.1.as_ref()).map_err(|_| Error::InvalidJustification)?;
if !self.verify_commit(hash, &commit) { if !self.verify_commit(hash, &commit) {
Err(Error::InvalidJustification)?; Err(Error::InvalidJustification)?;
@@ -312,18 +311,18 @@ where
TransactionFor<C, B>: Send + Sync + 'static, TransactionFor<C, B>: Send + Sync + 'static,
{ {
type ValidatorId = u16; type ValidatorId = u16;
type SignatureScheme = TendermintSigner; type SignatureScheme = TendermintValidators;
type Weights = TendermintWeights; type Weights = TendermintValidators;
type Block = B; type Block = B;
const BLOCK_TIME: u32 = { (serai_runtime::MILLISECS_PER_BLOCK / 1000) as u32 }; const BLOCK_TIME: u32 = { (serai_runtime::MILLISECS_PER_BLOCK / 1000) as u32 };
fn signature_scheme(&self) -> Arc<TendermintSigner> { fn signature_scheme(&self) -> Arc<TendermintValidators> {
Arc::new(TendermintSigner::new()) Arc::new(TendermintValidators::new())
} }
fn weights(&self) -> Arc<TendermintWeights> { fn weights(&self) -> Arc<TendermintValidators> {
Arc::new(TendermintWeights) Arc::new(TendermintValidators::new())
} }
async fn broadcast(&mut self, msg: SignedMessage<u16, Self::Block, Signature>) { async fn broadcast(&mut self, msg: SignedMessage<u16, Self::Block, Signature>) {
@@ -391,7 +390,7 @@ where
Ok(()) 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 hash = block.hash();
let justification = (CONSENSUS_ID, commit.encode()); let justification = (CONSENSUS_ID, commit.encode());
debug_assert!(self.verify_justification(hash, &justification).is_ok()); debug_assert!(self.verify_justification(hash, &justification).is_ok());

View File

@@ -1,24 +1,27 @@
// TODO: This should be built around pallet_sessions (and pallet_staking?).
use sp_application_crypto::{ use sp_application_crypto::{
RuntimePublic as PublicTrait, Pair as PairTrait, RuntimePublic as PublicTrait, Pair as PairTrait,
sr25519::{Public, Pair, Signature}, sr25519::{Public, Pair, Signature},
}; };
use tendermint_machine::ext::SignatureScheme; use tendermint_machine::ext::{BlockNumber, Round, Weights, SignatureScheme};
pub(crate) struct TendermintSigner { const VALIDATORS: usize = 1;
keys: Pair,
lookup: Vec<Public>, pub(crate) struct TendermintValidators {
keys: Pair, // sp_keystore
lookup: Vec<Public>, // sessions
} }
impl TendermintSigner { impl TendermintValidators {
pub(crate) fn new() -> TendermintSigner { pub(crate) fn new() -> TendermintValidators {
// TODO
let keys = Pair::from_string("//Alice", None).unwrap(); 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 ValidatorId = u16;
type Signature = Signature; type Signature = Signature;
type AggregateSignature = Vec<Signature>; type AggregateSignature = Vec<Signature>;
@@ -47,3 +50,18 @@ impl SignatureScheme for TendermintSigner {
true 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()
}
}

View File

@@ -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()
}
}