mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-01 02:11:01 +00:00
grandpa: minor cleanups in communication module (#6371)
* grandpa: replace Result<(), ()> with Option<()> * grandpa: replace &Option<T> with Option<&T> * grandpa: cleanup local id and keystore usages * grandpa: return bool on check_message_signature * grandpa: fix erroneous log message on startup * grandpa: fix test
This commit is contained in:
@@ -750,7 +750,11 @@ impl<Block: BlockT> Inner<Block> {
|
||||
Round(1),
|
||||
)),
|
||||
Some(ref mut v) => if v.set_id == set_id {
|
||||
if self.authorities != authorities {
|
||||
let diff_authorities =
|
||||
self.authorities.iter().collect::<HashSet<_>>() !=
|
||||
authorities.iter().collect();
|
||||
|
||||
if diff_authorities {
|
||||
debug!(target: "afg",
|
||||
"Gossip validator noted set {:?} twice with different authorities. \
|
||||
Was the authority set hard forked?",
|
||||
@@ -829,7 +833,7 @@ impl<Block: BlockT> Inner<Block> {
|
||||
return Action::Discard(cost::UNKNOWN_VOTER);
|
||||
}
|
||||
|
||||
if let Err(()) = sp_finality_grandpa::check_message_signature(
|
||||
if !sp_finality_grandpa::check_message_signature(
|
||||
&full.message.message,
|
||||
&full.message.id,
|
||||
&full.message.signature,
|
||||
@@ -2620,12 +2624,12 @@ mod tests {
|
||||
fn allow_noting_different_authorities_for_same_set() {
|
||||
let (val, _) = GossipValidator::<Block>::new(config(), voter_set_state(), None);
|
||||
|
||||
let a1 = vec![AuthorityId::default()];
|
||||
let a1 = vec![AuthorityId::from_slice(&[0; 32])];
|
||||
val.note_set(SetId(1), a1.clone(), |_, _| {});
|
||||
|
||||
assert_eq!(val.inner().read().authorities, a1);
|
||||
|
||||
let a2 = vec![AuthorityId::default(), AuthorityId::default()];
|
||||
let a2 = vec![AuthorityId::from_slice(&[1; 32]), AuthorityId::from_slice(&[2; 32])];
|
||||
val.note_set(SetId(1), a2.clone(), |_, _| {});
|
||||
|
||||
assert_eq!(val.inner().read().authorities, a2);
|
||||
|
||||
@@ -105,6 +105,34 @@ mod benefit {
|
||||
pub(super) const PER_EQUIVOCATION: i32 = 10;
|
||||
}
|
||||
|
||||
/// A type that ties together our local authority id and a keystore where it is
|
||||
/// available for signing.
|
||||
pub struct LocalIdKeystore((AuthorityId, BareCryptoStorePtr));
|
||||
|
||||
impl LocalIdKeystore {
|
||||
/// Returns a reference to our local authority id.
|
||||
fn local_id(&self) -> &AuthorityId {
|
||||
&(self.0).0
|
||||
}
|
||||
|
||||
/// Returns a reference to the keystore.
|
||||
fn keystore(&self) -> &BareCryptoStorePtr {
|
||||
&(self.0).1
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<BareCryptoStorePtr> for LocalIdKeystore {
|
||||
fn as_ref(&self) -> &BareCryptoStorePtr {
|
||||
self.keystore()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(AuthorityId, BareCryptoStorePtr)> for LocalIdKeystore {
|
||||
fn from(inner: (AuthorityId, BareCryptoStorePtr)) -> LocalIdKeystore {
|
||||
LocalIdKeystore(inner)
|
||||
}
|
||||
}
|
||||
|
||||
/// If the voter set is larger than this value some telemetry events are not
|
||||
/// sent to avoid increasing usage resource on the node and flooding the
|
||||
/// telemetry server (e.g. received votes, received commits.)
|
||||
@@ -272,11 +300,10 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
|
||||
/// network all within the current set.
|
||||
pub(crate) fn round_communication(
|
||||
&self,
|
||||
keystore: Option<BareCryptoStorePtr>,
|
||||
keystore: Option<LocalIdKeystore>,
|
||||
round: Round,
|
||||
set_id: SetId,
|
||||
voters: Arc<VoterSet<AuthorityId>>,
|
||||
local_key: Option<AuthorityId>,
|
||||
has_voted: HasVoted<B>,
|
||||
) -> (
|
||||
impl Stream<Item = SignedMessage<B>> + Unpin,
|
||||
@@ -288,9 +315,10 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
|
||||
&*voters,
|
||||
);
|
||||
|
||||
let local_id = local_key.and_then(|id| {
|
||||
if voters.contains(&id) {
|
||||
Some(id)
|
||||
let keystore = keystore.and_then(|ks| {
|
||||
let id = ks.local_id();
|
||||
if voters.contains(id) {
|
||||
Some(ks)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -350,11 +378,10 @@ impl<B: BlockT, N: Network<B>> NetworkBridge<B, N> {
|
||||
|
||||
let (tx, out_rx) = mpsc::channel(0);
|
||||
let outgoing = OutgoingMessages::<B> {
|
||||
keystore: keystore.clone(),
|
||||
keystore,
|
||||
round: round.0,
|
||||
set_id: set_id.0,
|
||||
network: self.gossip_engine.clone(),
|
||||
local_id,
|
||||
sender: tx,
|
||||
has_voted,
|
||||
};
|
||||
@@ -629,11 +656,10 @@ pub struct SetId(pub SetIdNumber);
|
||||
pub(crate) struct OutgoingMessages<Block: BlockT> {
|
||||
round: RoundNumber,
|
||||
set_id: SetIdNumber,
|
||||
local_id: Option<AuthorityId>,
|
||||
keystore: Option<LocalIdKeystore>,
|
||||
sender: mpsc::Sender<SignedMessage<Block>>,
|
||||
network: Arc<Mutex<GossipEngine<Block>>>,
|
||||
has_voted: HasVoted<Block>,
|
||||
keystore: Option<BareCryptoStorePtr>,
|
||||
}
|
||||
|
||||
impl<B: BlockT> Unpin for OutgoingMessages<B> {}
|
||||
@@ -667,19 +693,12 @@ impl<Block: BlockT> Sink<Message<Block>> for OutgoingMessages<Block>
|
||||
}
|
||||
|
||||
// when locals exist, sign messages on import
|
||||
if let Some(ref public) = self.local_id {
|
||||
let keystore = match &self.keystore {
|
||||
Some(keystore) => keystore.clone(),
|
||||
None => {
|
||||
return Err(Error::Signing("Cannot sign without a keystore".to_string()))
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(ref keystore) = self.keystore {
|
||||
let target_hash = *(msg.target().0);
|
||||
let signed = sp_finality_grandpa::sign_message(
|
||||
keystore,
|
||||
keystore.as_ref(),
|
||||
msg,
|
||||
public.clone(),
|
||||
keystore.local_id().clone(),
|
||||
self.round,
|
||||
self.set_id,
|
||||
).ok_or(
|
||||
@@ -774,7 +793,7 @@ fn check_compact_commit<Block: BlockT>(
|
||||
use crate::communication::gossip::Misbehavior;
|
||||
use finality_grandpa::Message as GrandpaMessage;
|
||||
|
||||
if let Err(()) = sp_finality_grandpa::check_message_signature_with_buffer(
|
||||
if !sp_finality_grandpa::check_message_signature_with_buffer(
|
||||
&GrandpaMessage::Precommit(precommit.clone()),
|
||||
id,
|
||||
sig,
|
||||
@@ -862,7 +881,7 @@ fn check_catch_up<Block: BlockT>(
|
||||
for (msg, id, sig) in messages {
|
||||
signatures_checked += 1;
|
||||
|
||||
if let Err(()) = sp_finality_grandpa::check_message_signature_with_buffer(
|
||||
if !sp_finality_grandpa::check_message_signature_with_buffer(
|
||||
&msg,
|
||||
id,
|
||||
sig,
|
||||
|
||||
Reference in New Issue
Block a user