mirror of
https://github.com/serai-dex/serai.git
synced 2025-02-03 11:46:31 +00:00
Consolidate references to sr25519 in sc_tendermint
This commit is contained in:
parent
503adfee2f
commit
c4976ff97d
2 changed files with 37 additions and 29 deletions
|
@ -1,41 +1,49 @@
|
|||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use sp_core::{Decode, sr25519::Signature};
|
||||
use sp_core::Decode;
|
||||
use sp_runtime::traits::{Hash, Header, Block};
|
||||
|
||||
use sc_network::PeerId;
|
||||
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)]
|
||||
pub struct TendermintGossip<S: SignatureScheme<ValidatorId = u16, Signature = Signature>> {
|
||||
pub(crate) struct TendermintGossip<T: TendermintValidator> {
|
||||
number: Arc<RwLock<u64>>,
|
||||
signature_scheme: Arc<S>,
|
||||
signature_scheme: Arc<TendermintValidators<T>>,
|
||||
}
|
||||
|
||||
impl<S: SignatureScheme<ValidatorId = u16, Signature = Signature>> TendermintGossip<S> {
|
||||
pub(crate) fn new(number: Arc<RwLock<u64>>, signature_scheme: Arc<S>) -> TendermintGossip<S> {
|
||||
impl<T: TendermintValidator> TendermintGossip<T> {
|
||||
pub(crate) fn new(
|
||||
number: Arc<RwLock<u64>>,
|
||||
signature_scheme: Arc<TendermintValidators<T>>,
|
||||
) -> Self {
|
||||
TendermintGossip { number, signature_scheme }
|
||||
}
|
||||
|
||||
pub(crate) fn topic<B: Block>(number: u64) -> B::Hash {
|
||||
<<B::Header as Header>::Hashing as Hash>::hash(
|
||||
pub(crate) fn topic(number: u64) -> <T::Block as Block>::Hash {
|
||||
<<<T::Block as Block>::Header as Header>::Hashing as Hash>::hash(
|
||||
&[b"Tendermint Block Topic".as_ref(), &number.to_le_bytes()].concat(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B: Block, S: SignatureScheme<ValidatorId = u16, Signature = Signature>> Validator<B>
|
||||
for TendermintGossip<S>
|
||||
{
|
||||
impl<T: TendermintValidator> Validator<T::Block> for TendermintGossip<T> {
|
||||
fn validate(
|
||||
&self,
|
||||
_: &mut dyn ValidatorContext<B>,
|
||||
_: &mut dyn ValidatorContext<T::Block>,
|
||||
_: &PeerId,
|
||||
data: &[u8],
|
||||
) -> ValidationResult<B::Hash> {
|
||||
let msg = match SignedMessage::<u16, B, Signature>::decode(&mut &*data) {
|
||||
) -> ValidationResult<<T::Block as Block>::Hash> {
|
||||
let msg = match SignedMessage::<
|
||||
u16,
|
||||
T::Block,
|
||||
<TendermintValidators<T> as SignatureScheme>::Signature,
|
||||
>::decode(&mut &*data)
|
||||
{
|
||||
Ok(msg) => msg,
|
||||
Err(_) => return ValidationResult::Discard,
|
||||
};
|
||||
|
@ -48,6 +56,6 @@ impl<B: Block, S: SignatureScheme<ValidatorId = u16, Signature = Signature>> Val
|
|||
return ValidationResult::Discard;
|
||||
}
|
||||
|
||||
ValidationResult::ProcessAndKeep(Self::topic::<B>(msg.number().0))
|
||||
ValidationResult::ProcessAndKeep(Self::topic(msg.number().0))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ use log::warn;
|
|||
|
||||
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_runtime::{
|
||||
traits::{Header, Block},
|
||||
|
@ -29,7 +29,7 @@ use sc_network_gossip::GossipEngine;
|
|||
use substrate_prometheus_endpoint::Registry;
|
||||
|
||||
use tendermint_machine::{
|
||||
ext::{BlockError, BlockNumber, Commit, Network},
|
||||
ext::{BlockError, BlockNumber, Commit, SignatureScheme, Network},
|
||||
SignedMessage, TendermintMachine,
|
||||
};
|
||||
|
||||
|
@ -51,7 +51,11 @@ struct ActiveAuthority<T: TendermintValidator> {
|
|||
// Block whose gossip is being tracked
|
||||
number: Arc<RwLock<u64>>,
|
||||
// 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
|
||||
env: T::Environment,
|
||||
|
@ -188,18 +192,13 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
|
|||
};
|
||||
|
||||
// Start receiving messages about the Tendermint process for this block
|
||||
let mut recv = gossip
|
||||
.messages_for(TendermintGossip::<TendermintValidators<T>>::topic::<T::Block>(last_number));
|
||||
let mut recv = gossip.messages_for(TendermintGossip::<T>::topic(last_number));
|
||||
|
||||
'outer: loop {
|
||||
// Send out any queued messages
|
||||
let mut queue = gossip_queue.write().unwrap().drain(..).collect::<Vec<_>>();
|
||||
for msg in queue.drain(..) {
|
||||
gossip.gossip_message(
|
||||
TendermintGossip::<TendermintValidators<T>>::topic::<T::Block>(msg.number().0),
|
||||
msg.encode(),
|
||||
false,
|
||||
);
|
||||
gossip.gossip_message(TendermintGossip::<T>::topic(msg.number().0), msg.encode(), false);
|
||||
}
|
||||
|
||||
// Handle any received messages
|
||||
|
@ -232,9 +231,7 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
|
|||
last_number = curr;
|
||||
// TODO: Will this return existing messages on the new height? Or will those have
|
||||
// been ignored and are now gone?
|
||||
recv = gossip.messages_for(TendermintGossip::<TendermintValidators<T>>::topic::<
|
||||
T::Block,
|
||||
>(last_number));
|
||||
recv = gossip.messages_for(TendermintGossip::<T>::topic(last_number));
|
||||
}
|
||||
|
||||
// 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()
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue