Consolidate references to sr25519 in sc_tendermint

This commit is contained in:
Luke Parker
2022-10-30 11:24:52 -04:00
parent 503adfee2f
commit c4976ff97d
2 changed files with 37 additions and 29 deletions

View File

@@ -1,41 +1,49 @@
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use sp_core::{Decode, sr25519::Signature}; use sp_core::Decode;
use sp_runtime::traits::{Hash, Header, Block}; use sp_runtime::traits::{Hash, Header, Block};
use sc_network::PeerId; use sc_network::PeerId;
use sc_network_gossip::{Validator, ValidatorContext, ValidationResult}; use sc_network_gossip::{Validator, ValidatorContext, ValidationResult};
use tendermint_machine::{SignedMessage, ext::SignatureScheme}; use tendermint_machine::{ext::SignatureScheme, SignedMessage};
use crate::{TendermintValidator, validators::TendermintValidators};
#[derive(Clone)] #[derive(Clone)]
pub struct TendermintGossip<S: SignatureScheme<ValidatorId = u16, Signature = Signature>> { pub(crate) struct TendermintGossip<T: TendermintValidator> {
number: Arc<RwLock<u64>>, number: Arc<RwLock<u64>>,
signature_scheme: Arc<S>, signature_scheme: Arc<TendermintValidators<T>>,
} }
impl<S: SignatureScheme<ValidatorId = u16, Signature = Signature>> TendermintGossip<S> { impl<T: TendermintValidator> TendermintGossip<T> {
pub(crate) fn new(number: Arc<RwLock<u64>>, signature_scheme: Arc<S>) -> TendermintGossip<S> { pub(crate) fn new(
number: Arc<RwLock<u64>>,
signature_scheme: Arc<TendermintValidators<T>>,
) -> Self {
TendermintGossip { number, signature_scheme } TendermintGossip { number, signature_scheme }
} }
pub(crate) fn topic<B: Block>(number: u64) -> B::Hash { pub(crate) fn topic(number: u64) -> <T::Block as Block>::Hash {
<<B::Header as Header>::Hashing as Hash>::hash( <<<T::Block as Block>::Header as Header>::Hashing as Hash>::hash(
&[b"Tendermint Block Topic".as_ref(), &number.to_le_bytes()].concat(), &[b"Tendermint Block Topic".as_ref(), &number.to_le_bytes()].concat(),
) )
} }
} }
impl<B: Block, S: SignatureScheme<ValidatorId = u16, Signature = Signature>> Validator<B> impl<T: TendermintValidator> Validator<T::Block> for TendermintGossip<T> {
for TendermintGossip<S>
{
fn validate( fn validate(
&self, &self,
_: &mut dyn ValidatorContext<B>, _: &mut dyn ValidatorContext<T::Block>,
_: &PeerId, _: &PeerId,
data: &[u8], data: &[u8],
) -> ValidationResult<B::Hash> { ) -> ValidationResult<<T::Block as Block>::Hash> {
let msg = match SignedMessage::<u16, B, Signature>::decode(&mut &*data) { let msg = match SignedMessage::<
u16,
T::Block,
<TendermintValidators<T> as SignatureScheme>::Signature,
>::decode(&mut &*data)
{
Ok(msg) => msg, Ok(msg) => msg,
Err(_) => return ValidationResult::Discard, Err(_) => return ValidationResult::Discard,
}; };
@@ -48,6 +56,6 @@ impl<B: Block, S: SignatureScheme<ValidatorId = u16, Signature = Signature>> Val
return ValidationResult::Discard; return ValidationResult::Discard;
} }
ValidationResult::ProcessAndKeep(Self::topic::<B>(msg.number().0)) ValidationResult::ProcessAndKeep(Self::topic(msg.number().0))
} }
} }

View File

@@ -9,7 +9,7 @@ use log::warn;
use tokio::task::yield_now; use tokio::task::yield_now;
use sp_core::{Encode, Decode, sr25519::Signature}; use sp_core::{Encode, Decode};
use sp_inherents::{InherentData, InherentDataProvider, CreateInherentDataProviders}; use sp_inherents::{InherentData, InherentDataProvider, CreateInherentDataProviders};
use sp_runtime::{ use sp_runtime::{
traits::{Header, Block}, traits::{Header, Block},
@@ -29,7 +29,7 @@ use sc_network_gossip::GossipEngine;
use substrate_prometheus_endpoint::Registry; use substrate_prometheus_endpoint::Registry;
use tendermint_machine::{ use tendermint_machine::{
ext::{BlockError, BlockNumber, Commit, Network}, ext::{BlockError, BlockNumber, Commit, SignatureScheme, Network},
SignedMessage, TendermintMachine, SignedMessage, TendermintMachine,
}; };
@@ -51,7 +51,11 @@ struct ActiveAuthority<T: TendermintValidator> {
// Block whose gossip is being tracked // Block whose gossip is being tracked
number: Arc<RwLock<u64>>, number: Arc<RwLock<u64>>,
// Outgoing message queue, placed here as the GossipEngine itself can't be // Outgoing message queue, placed here as the GossipEngine itself can't be
gossip_queue: Arc<RwLock<Vec<SignedMessage<u16, T::Block, Signature>>>>, gossip_queue: Arc<
RwLock<
Vec<SignedMessage<u16, T::Block, <TendermintValidators<T> as SignatureScheme>::Signature>>,
>,
>,
// Block producer // Block producer
env: T::Environment, env: T::Environment,
@@ -188,18 +192,13 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
}; };
// Start receiving messages about the Tendermint process for this block // Start receiving messages about the Tendermint process for this block
let mut recv = gossip let mut recv = gossip.messages_for(TendermintGossip::<T>::topic(last_number));
.messages_for(TendermintGossip::<TendermintValidators<T>>::topic::<T::Block>(last_number));
'outer: loop { 'outer: loop {
// Send out any queued messages // Send out any queued messages
let mut queue = gossip_queue.write().unwrap().drain(..).collect::<Vec<_>>(); let mut queue = gossip_queue.write().unwrap().drain(..).collect::<Vec<_>>();
for msg in queue.drain(..) { for msg in queue.drain(..) {
gossip.gossip_message( gossip.gossip_message(TendermintGossip::<T>::topic(msg.number().0), msg.encode(), false);
TendermintGossip::<TendermintValidators<T>>::topic::<T::Block>(msg.number().0),
msg.encode(),
false,
);
} }
// Handle any received messages // Handle any received messages
@@ -232,9 +231,7 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
last_number = curr; last_number = curr;
// TODO: Will this return existing messages on the new height? Or will those have // TODO: Will this return existing messages on the new height? Or will those have
// been ignored and are now gone? // been ignored and are now gone?
recv = gossip.messages_for(TendermintGossip::<TendermintValidators<T>>::topic::< recv = gossip.messages_for(TendermintGossip::<T>::topic(last_number));
T::Block,
>(last_number));
} }
// If there are no messages available, yield to not hog the thread, then return to the // If there are no messages available, yield to not hog the thread, then return to the
@@ -265,7 +262,10 @@ impl<T: TendermintValidator> Network for TendermintAuthority<T> {
self.import.validators.clone() self.import.validators.clone()
} }
async fn broadcast(&mut self, msg: SignedMessage<u16, Self::Block, Signature>) { async fn broadcast(
&mut self,
msg: SignedMessage<u16, Self::Block, <TendermintValidators<T> as SignatureScheme>::Signature>,
) {
self.active.as_mut().unwrap().gossip_queue.write().unwrap().push(msg); self.active.as_mut().unwrap().gossip_queue.write().unwrap().push(msg);
} }