Skip to content

Commit

Permalink
Add equivocation detector crate and implement clients (#2348) (#2353)
Browse files Browse the repository at this point in the history
* Split FinalitySyncPipeline and SourceClient

* Move some logic to finality_base

* Add empty equivocation detection clients

* Add equivocation reporting logic to the source client

* Use convenience trait for SubstrateFinalitySyncPipeline

* Define JustificationVerificationContext for GRANDPA

* Equivocation source client: finality_verification_context()

* Equivocation source client: synced_headers_finality_info()

* reuse HeaderFinalityInfo

* Define EquivocationsFinder

* Fix spellcheck

* Address review comments

* Avoid equivocations lookup errors
  • Loading branch information
serban300 authored Aug 16, 2023
1 parent 3fe4b13 commit 620db2b
Show file tree
Hide file tree
Showing 37 changed files with 942 additions and 374 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ members = [
"relays/client-substrate",
"relays/client-westend",
"relays/client-wococo",
"relays/equivocation",
"relays/finality",
"relays/lib-substrate-relay",
"relays/messages",
Expand Down
4 changes: 2 additions & 2 deletions bin/millau/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ impl_runtime_apis! {
}

fn synced_headers_grandpa_info(
) -> Vec<bp_header_chain::HeaderGrandpaInfo<bp_rialto::Header>> {
) -> Vec<bp_header_chain::StoredHeaderGrandpaInfo<bp_rialto::Header>> {
BridgeRialtoGrandpa::synced_headers_grandpa_info()
}
}
Expand All @@ -895,7 +895,7 @@ impl_runtime_apis! {
}

fn synced_headers_grandpa_info(
) -> Vec<bp_header_chain::HeaderGrandpaInfo<bp_westend::Header>> {
) -> Vec<bp_header_chain::StoredHeaderGrandpaInfo<bp_westend::Header>> {
BridgeWestendGrandpa::synced_headers_grandpa_info()
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/rialto-parachain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ impl_runtime_apis! {
}

fn synced_headers_grandpa_info(
) -> Vec<bp_header_chain::HeaderGrandpaInfo<bp_millau::Header>> {
) -> Vec<bp_header_chain::StoredHeaderGrandpaInfo<bp_millau::Header>> {
BridgeMillauGrandpa::synced_headers_grandpa_info()
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin/rialto/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ impl_runtime_apis! {
}

fn synced_headers_grandpa_info(
) -> Vec<bp_header_chain::HeaderGrandpaInfo<bp_millau::Header>> {
) -> Vec<bp_header_chain::StoredHeaderGrandpaInfo<bp_millau::Header>> {
BridgeMillauGrandpa::synced_headers_grandpa_info()
}
}
Expand Down
48 changes: 25 additions & 23 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ pub use storage_types::StoredAuthoritySet;

use bp_header_chain::{
justification::GrandpaJustification, AuthoritySet, ChainWithGrandpa, GrandpaConsensusLogReader,
HeaderChain, HeaderGrandpaInfo, InitializationData, StoredHeaderData, StoredHeaderDataBuilder,
HeaderChain, InitializationData, StoredHeaderData, StoredHeaderDataBuilder,
StoredHeaderGrandpaInfo,
};
use bp_runtime::{BlockNumberOf, HashOf, HasherOf, HeaderId, HeaderOf, OwnedBridgeModule};
use finality_grandpa::voter_set::VoterSet;
use frame_support::{dispatch::PostDispatchInfo, ensure, DefaultNoBound};
use sp_runtime::{
traits::{Header as HeaderT, Zero},
Expand Down Expand Up @@ -241,9 +241,9 @@ pub mod pallet {
Self::deposit_event(Event::UpdatedBestFinalizedHeader {
number,
hash,
grandpa_info: HeaderGrandpaInfo {
justification,
authority_set: maybe_new_authority_set,
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification,
new_verification_context: maybe_new_authority_set,
},
});

Expand Down Expand Up @@ -411,7 +411,7 @@ pub mod pallet {
number: BridgedBlockNumber<T, I>,
hash: BridgedBlockHash<T, I>,
/// The Grandpa info associated to the new best finalized header.
grandpa_info: HeaderGrandpaInfo<BridgedHeader<T, I>>,
grandpa_info: StoredHeaderGrandpaInfo<BridgedHeader<T, I>>,
},
}

Expand Down Expand Up @@ -505,14 +505,9 @@ pub mod pallet {
) -> Result<(), sp_runtime::DispatchError> {
use bp_header_chain::justification::verify_justification;

let voter_set =
VoterSet::new(authority_set.authorities).ok_or(<Error<T, I>>::InvalidAuthoritySet)?;
let set_id = authority_set.set_id;

Ok(verify_justification::<BridgedHeader<T, I>>(
(hash, number),
set_id,
&voter_set,
&authority_set.try_into().map_err(|_| <Error<T, I>>::InvalidAuthoritySet)?,
justification,
)
.map_err(|e| {
Expand Down Expand Up @@ -617,7 +612,7 @@ where
<T as frame_system::Config>::RuntimeEvent: TryInto<Event<T, I>>,
{
/// Get the GRANDPA justifications accepted in the current block.
pub fn synced_headers_grandpa_info() -> Vec<HeaderGrandpaInfo<BridgedHeader<T, I>>> {
pub fn synced_headers_grandpa_info() -> Vec<StoredHeaderGrandpaInfo<BridgedHeader<T, I>>> {
frame_system::Pallet::<T>::read_events_no_consensus()
.filter_map(|event| {
if let Event::<T, I>::UpdatedBestFinalizedHeader { grandpa_info, .. } =
Expand Down Expand Up @@ -934,17 +929,20 @@ mod tests {
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
number: *header.number(),
hash: header.hash(),
grandpa_info: HeaderGrandpaInfo {
justification: justification.clone(),
authority_set: None,
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification.clone(),
new_verification_context: None,
},
}),
topics: vec![],
}],
);
assert_eq!(
Pallet::<TestRuntime>::synced_headers_grandpa_info(),
vec![HeaderGrandpaInfo { justification, authority_set: None }]
vec![StoredHeaderGrandpaInfo {
finality_proof: justification,
new_verification_context: None
}]
);
})
}
Expand Down Expand Up @@ -1075,19 +1073,23 @@ mod tests {
event: TestEvent::Grandpa(Event::UpdatedBestFinalizedHeader {
number: *header.number(),
hash: header.hash(),
grandpa_info: HeaderGrandpaInfo {
justification: justification.clone(),
authority_set: Some(<CurrentAuthoritySet<TestRuntime>>::get().into()),
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification.clone(),
new_verification_context: Some(
<CurrentAuthoritySet<TestRuntime>>::get().into()
),
},
}),
topics: vec![],
}],
);
assert_eq!(
Pallet::<TestRuntime>::synced_headers_grandpa_info(),
vec![HeaderGrandpaInfo {
justification,
authority_set: Some(<CurrentAuthoritySet<TestRuntime>>::get().into()),
vec![StoredHeaderGrandpaInfo {
finality_proof: justification,
new_verification_context: Some(
<CurrentAuthoritySet<TestRuntime>>::get().into()
),
}]
);
})
Expand Down
26 changes: 13 additions & 13 deletions modules/parachains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ pub(crate) mod tests {
use bp_test_utils::prepare_parachain_heads_proof;
use codec::Encode;

use bp_header_chain::{justification::GrandpaJustification, HeaderGrandpaInfo};
use bp_header_chain::{justification::GrandpaJustification, StoredHeaderGrandpaInfo};
use bp_parachains::{
BestParaHeadHash, BridgeParachainCall, ImportedParaHeadsKeyProvider, ParasInfoKeyProvider,
};
Expand Down Expand Up @@ -1036,9 +1036,9 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
grandpa_info: HeaderGrandpaInfo {
justification,
authority_set: None,
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification,
new_verification_context: None,
},
}
),
Expand Down Expand Up @@ -1177,9 +1177,9 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
grandpa_info: HeaderGrandpaInfo {
justification,
authority_set: None,
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification,
new_verification_context: None,
}
}
),
Expand Down Expand Up @@ -1230,9 +1230,9 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
grandpa_info: HeaderGrandpaInfo {
justification: justification.clone(),
authority_set: None,
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification.clone(),
new_verification_context: None,
}
}
),
Expand Down Expand Up @@ -1271,9 +1271,9 @@ pub(crate) mod tests {
pallet_bridge_grandpa::Event::UpdatedBestFinalizedHeader {
number: 1,
hash: relay_1_hash,
grandpa_info: HeaderGrandpaInfo {
justification,
authority_set: None,
grandpa_info: StoredHeaderGrandpaInfo {
finality_proof: justification,
new_verification_context: None,
}
}
),
Expand Down
5 changes: 3 additions & 2 deletions primitives/header-chain/src/justification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ mod verification;

use crate::ChainWithGrandpa;
pub use verification::{
equivocation::{EquivocationsCollector, Error as EquivocationsCollectorError},
equivocation::{EquivocationsCollector, GrandpaEquivocationsFinder},
optimizer::verify_and_optimize_justification,
strict::verify_justification,
AncestryChain, Error as JustificationVerificationError, PrecommitError,
AncestryChain, Error as JustificationVerificationError, JustificationVerificationContext,
PrecommitError,
};

use bp_runtime::{BlockNumberOf, Chain, HashOf, HeaderId};
Expand Down
Loading

0 comments on commit 620db2b

Please sign in to comment.