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
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
5 changed files with 39 additions and 45 deletions

View file

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

View file

@ -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;

View file

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

View file

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

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