Basic Gossip Validator

This commit is contained in:
Luke Parker
2022-10-30 01:21:10 -04:00
parent f31c457c2c
commit 9a54317743
5 changed files with 81 additions and 1 deletions

View File

@@ -36,6 +36,7 @@ sc-transaction-pool = { git = "https://github.com/serai-dex/substrate" }
sc-basic-authorship = { git = "https://github.com/serai-dex/substrate" }
sc-executor = { git = "https://github.com/serai-dex/substrate" }
sc-network = { git = "https://github.com/serai-dex/substrate" }
sc-network-gossip = { git = "https://github.com/serai-dex/substrate" }
sc-service = { git = "https://github.com/serai-dex/substrate" }
sc-client-api = { git = "https://github.com/serai-dex/substrate" }
sc-consensus = { git = "https://github.com/serai-dex/substrate" }

View File

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

View File

@@ -20,6 +20,8 @@ mod verifier;
mod import_queue;
use import_queue::TendermintImportQueue;
mod gossip;
mod select_chain;
pub use select_chain::TendermintSelectChain;