From ec19f60b4bce162eac9085a528ff9047151d9cc0 Mon Sep 17 00:00:00 2001 From: Joshy Orndorff Date: Wed, 4 Oct 2023 21:17:36 -0400 Subject: [PATCH] fix inherent identifier --- tuxedo-core/src/inherents.rs | 28 ++++++++++++++++++---------- tuxedo-template-runtime/src/lib.rs | 18 ++++++++++-------- wardrobe/timestamp/src/lib.rs | 5 +++-- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/tuxedo-core/src/inherents.rs b/tuxedo-core/src/inherents.rs index 2c3d63a23..ba2a68ee7 100644 --- a/tuxedo-core/src/inherents.rs +++ b/tuxedo-core/src/inherents.rs @@ -101,9 +101,11 @@ impl sp_inherents::InherentDataProvider for ParentBlockInherentDataPr pub trait TuxedoInherent>: Sized + TypeInfo { type Error: Encode + IsFatalError; + const INHERENT_IDENTIFIER: InherentIdentifier; + /// Create the inherent extrinsic to insert into a block that is being authored locally. /// The inherent data is supplied by the authoring node. - fn create( + fn create_inherent( authoring_inherent_data: &InherentData, previous_inherent: Transaction, ) -> Transaction; @@ -112,7 +114,7 @@ pub trait TuxedoInherent>: Sized + TypeInfo /// The inherent data is supplied by the importing node. /// The inherent data available here is not guaranteed to be the /// same as what is available at authoring time. - fn check( + fn check_inherent( importing_inherent_data: &InherentData, inherent: Transaction, ) -> Result<(), Self::Error>; @@ -120,12 +122,13 @@ pub trait TuxedoInherent>: Sized + TypeInfo impl> TuxedoInherent for () { type Error = MakeFatalError<()>; + const INHERENT_IDENTIFIER: InherentIdentifier = *b"no_inher"; - fn create(_: &InherentData, _: Transaction) -> Transaction { + fn create_inherent(_: &InherentData, _: Transaction) -> Transaction { panic!("Attempting to create an inherent for a constraint checker that does not support inherents.") } - fn check(_: &InherentData, _: Transaction) -> Result<(), Self::Error> { + fn check_inherent(_: &InherentData, _: Transaction) -> Result<(), Self::Error> { panic!("Attemping to perform inherent checks for a constraint checker that does not support inherents.") } } @@ -139,9 +142,11 @@ impl> TuxedoInherent for () { pub trait InherentInternal>: Sized + TypeInfo { type Error: Encode + IsFatalError; + const INHERENT_IDENTIFIER: InherentIdentifier; + /// Create the inherent extrinsic to insert into a block that is being authored locally. /// The inherent data is supplied by the authoring node. - fn create( + fn create_inherent( authoring_inherent_data: &InherentData, previous_inherent: Transaction, ) -> Vec>; @@ -150,7 +155,7 @@ pub trait InherentInternal>: Sized + TypeIn /// The inherent data is supplied by the importing node. /// The inherent data available here is not guaranteed to be the /// same as what is available at authoring time. - fn check( + fn check_inherent( importing_inherent_data: &InherentData, inherent: Transaction, ) -> Result<(), Self::Error>; @@ -159,22 +164,25 @@ pub trait InherentInternal>: Sized + TypeIn impl, T: TuxedoInherent> InherentInternal for T { type Error = >::Error; - fn create( + const INHERENT_IDENTIFIER: InherentIdentifier = + >::INHERENT_IDENTIFIER; + + fn create_inherent( authoring_inherent_data: &InherentData, previous_inherent: Transaction, ) -> Vec> { // This is the magic. We just take the single transaction from the individual piece // and put it into a vec so it can be aggregated. - vec![>::create( + vec![>::create_inherent( authoring_inherent_data, previous_inherent, )] } - fn check( + fn check_inherent( importing_inherent_data: &InherentData, inherent: Transaction, ) -> Result<(), Self::Error> { - >::check(importing_inherent_data, inherent) + >::check_inherent(importing_inherent_data, inherent) } } diff --git a/tuxedo-template-runtime/src/lib.rs b/tuxedo-template-runtime/src/lib.rs index a6e70d107..f6ae8db91 100644 --- a/tuxedo-template-runtime/src/lib.rs +++ b/tuxedo-template-runtime/src/lib.rs @@ -15,6 +15,7 @@ use sp_consensus_aura::sr25519::AuthorityId as AuraId; use sp_consensus_grandpa::AuthorityId as GrandpaId; use sp_api::impl_runtime_apis; +use sp_inherents::{InherentData, InherentIdentifier}; use sp_runtime::{ create_runtime_str, impl_opaque_keys, traits::{BlakeTwo256, Block as BlockT}, @@ -37,7 +38,7 @@ use serde::{Deserialize, Serialize}; use timestamp::SetTimestamp; use tuxedo_core::{ dynamic_typing::{DynamicallyTypedData, UtxoData}, - inherents::TuxedoInherent, + inherents::InherentInternal, tuxedo_constraint_checker, tuxedo_verifier, types::Transaction as TuxedoTransaction, verifier::{SigCheck, ThresholdMultiSignature, UpForGrabs}, @@ -389,20 +390,21 @@ impl_runtime_apis! { ); // Call into the timestamp helper, then map the checker - let timestamp_tx = timestamp::SetTimestamp::::create(&data, prev_set_timestamp); + let transactions = as InherentInternal>::create_inherent(&data, prev_set_timestamp); // Return just the timestamp extrinsic for now. // Later we will either handle Aura properly or switch to nimbus. // Soon we will add the parachain inherent in here. - vec![timestamp_tx] + transactions } fn check_inherents( block: Block, - data: sp_inherents::InherentData + data: InherentData ) -> sp_inherents::CheckInherentsResult { use sp_inherents::{IsFatalError, CheckInherentsResult}; + use tuxedo_core::ConstraintChecker; let mut results = CheckInherentsResult::new(); log::info!( @@ -422,17 +424,17 @@ impl_runtime_apis! { .cloned() .expect("SetTimestamp extrinsic should appear in every block."); - let result = timestamp::SetTimestamp::::check(&data, set_timestamp_ext); + let result = as InherentInternal>::check_inherent(&data, set_timestamp_ext); if let Err(e) = result { log::info!( target: LOG_TARGET, "🕰️🖴 Got an error when checking an inherent: {:?}", e, ); - //TODO I need a better way to access the inherent identifier. I guess it needs to be assiciated - // with the TuxedoInherent trait. For now I leave it here. + + const INHERENT_ID: InherentIdentifier = < as ConstraintChecker>::InherentHooks as InherentInternal>::INHERENT_IDENTIFIER; results - .put_error(sp_timestamp::INHERENT_IDENTIFIER, &e) + .put_error(INHERENT_ID, &e) .expect("Should be able to put some errors"); if e.is_fatal_error() { diff --git a/wardrobe/timestamp/src/lib.rs b/wardrobe/timestamp/src/lib.rs index 34d8814f3..37d37d932 100644 --- a/wardrobe/timestamp/src/lib.rs +++ b/wardrobe/timestamp/src/lib.rs @@ -274,8 +274,9 @@ impl, C: ConstraintChecker, T: TimestampConfig TuxedoInherent for SetTimestamp { type Error = sp_timestamp::InherentError; + const INHERENT_IDENTIFIER: sp_inherents::InherentIdentifier = sp_timestamp::INHERENT_IDENTIFIER; - fn create( + fn create_inherent( authoring_inherent_data: &InherentData, previous_inherent: tuxedo_core::types::Transaction, ) -> tuxedo_core::types::Transaction { @@ -354,7 +355,7 @@ impl, C: ConstraintChecker, T: TimestampConfig timestamp_tx } - fn check( + fn check_inherent( importing_inherent_data: &InherentData, inherent: tuxedo_core::types::Transaction, ) -> Result<(), Self::Error> {