Use GrandpaJustification instead of Vec<u8> in Pallet API (#847)

* Stop passing raw encoded justifications to pallet API

By having the API accept a struct-ified justification we are able to
better utilize the justifications fields for weight calculations.

* Update relayer code to use decoded justifications

* Add justification to `expect()` statement

* Fix some imports

* Make justification wrapper contain decoded justification

* Rename some fields

* Get rid of warnings

* Appease Clippy

* Only decode justification once at init time

* Remove unnecessary method

* Remove justification wrapper

This became kinda unnecessary since we could implement the FinalityProof
trait on GrandpaJustification directly.
This commit is contained in:
Hernando Castano
2021-04-01 07:08:28 -04:00
committed by Bastian Köcher
parent 904b9f4da5
commit 67cdca8aa4
14 changed files with 92 additions and 117 deletions
+15 -16
View File
@@ -36,6 +36,7 @@
// Runtime-generated enums
#![allow(clippy::large_enum_variant)]
use bp_header_chain::justification::GrandpaJustification;
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf};
use codec::{Decode, Encode};
use finality_grandpa::voter_set::VoterSet;
@@ -46,7 +47,6 @@ use serde::{Deserialize, Serialize};
use sp_finality_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID};
use sp_runtime::traits::{BadOrigin, Header as HeaderT, Zero};
use sp_runtime::RuntimeDebug;
use sp_std::vec::Vec;
#[cfg(test)]
mod mock;
@@ -111,7 +111,7 @@ pub mod pallet {
pub fn submit_finality_proof(
origin: OriginFor<T>,
finality_target: BridgedHeader<T, I>,
justification: Vec<u8>,
justification: GrandpaJustification<BridgedHeader<T, I>>,
) -> DispatchResultWithPostInfo {
ensure_operational::<T, I>()?;
let _ = ensure_signed(origin)?;
@@ -359,7 +359,7 @@ pub mod pallet {
///
/// Will use the GRANDPA current authorities known to the pallet.
pub(crate) fn verify_justification<T: Config<I>, I: 'static>(
justification: &[u8],
justification: &GrandpaJustification<BridgedHeader<T, I>>,
hash: BridgedBlockHash<T, I>,
number: BridgedBlockNumber<T, I>,
) -> Result<(), sp_runtime::DispatchError> {
@@ -515,7 +515,7 @@ pub(crate) fn find_forced_change<H: HeaderT>(
pub fn initialize_for_benchmarks<T: Config<I>, I: 'static>(header: BridgedHeader<T, I>) {
initialize_bridge::<T, I>(InitializationData {
header,
authority_list: Vec::new(), // we don't verify any proofs in external benchmarks
authority_list: sp_std::vec::Vec::new(), // we don't verify any proofs in external benchmarks
set_id: 0,
is_halted: false,
});
@@ -555,7 +555,7 @@ mod tests {
fn submit_finality_proof(header: u8) -> frame_support::dispatch::DispatchResultWithPostInfo {
let header = test_header(header.into());
let justification = make_default_justification(&header).encode();
let justification = make_default_justification(&header);
Module::<TestRuntime>::submit_finality_proof(Origin::signed(1), header, justification)
}
@@ -701,10 +701,7 @@ mod tests {
run_test(|| {
<IsHalted<TestRuntime>>::put(true);
assert_noop!(
Module::<TestRuntime>::submit_finality_proof(Origin::signed(1), test_header(1), vec![]),
Error::<TestRuntime>::Halted,
);
assert_noop!(submit_finality_proof(1), Error::<TestRuntime>::Halted,);
})
}
@@ -731,7 +728,7 @@ mod tests {
set_id: 2,
..Default::default()
};
let justification = make_justification_for_header(params).encode();
let justification = make_justification_for_header(params);
assert_err!(
Module::<TestRuntime>::submit_finality_proof(Origin::signed(1), header, justification,),
@@ -746,7 +743,8 @@ mod tests {
initialize_substrate_bridge();
let header = test_header(1);
let justification = [1u8; 32].encode();
let mut justification = make_default_justification(&header);
justification.round = 42;
assert_err!(
Module::<TestRuntime>::submit_finality_proof(Origin::signed(1), header, justification,),
@@ -771,7 +769,7 @@ mod tests {
assert_ok!(Module::<TestRuntime>::initialize(Origin::root(), init_data));
let header = test_header(1);
let justification = [1u8; 32].encode();
let justification = make_default_justification(&header);
assert_err!(
Module::<TestRuntime>::submit_finality_proof(Origin::signed(1), header, justification,),
@@ -805,7 +803,7 @@ mod tests {
header.digest = change_log(0);
// Create a valid justification for the header
let justification = make_default_justification(&header).encode();
let justification = make_default_justification(&header);
// Let's import our test header
assert_ok!(Module::<TestRuntime>::submit_finality_proof(
@@ -837,7 +835,7 @@ mod tests {
header.digest = change_log(1);
// Create a valid justification for the header
let justification = make_default_justification(&header).encode();
let justification = make_default_justification(&header);
// Should not be allowed to import this header
assert_err!(
@@ -858,7 +856,7 @@ mod tests {
header.digest = forced_change_log(0);
// Create a valid justification for the header
let justification = make_default_justification(&header).encode();
let justification = make_default_justification(&header);
// Should not be allowed to import this header
assert_err!(
@@ -917,7 +915,8 @@ mod tests {
run_test(|| {
let submit_invalid_request = || {
let header = test_header(1);
let invalid_justification = vec![4, 2, 4, 2].encode();
let mut invalid_justification = make_default_justification(&header);
invalid_justification.round = 42;
Module::<TestRuntime>::submit_finality_proof(Origin::signed(1), header, invalid_justification)
};