Skip to content

Commit

Permalink
fix inherent identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshy Orndorff committed Oct 5, 2023
1 parent f7f31d1 commit ec19f60
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
28 changes: 18 additions & 10 deletions tuxedo-core/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ impl<B: BlockT> sp_inherents::InherentDataProvider for ParentBlockInherentDataPr
pub trait TuxedoInherent<V: Verifier, C: ConstraintChecker<V>>: 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<V, C>,
) -> Transaction<V, C>;
Expand All @@ -112,20 +114,21 @@ pub trait TuxedoInherent<V: Verifier, C: ConstraintChecker<V>>: 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<V, C>,
) -> Result<(), Self::Error>;
}

impl<V: Verifier, C: ConstraintChecker<V>> TuxedoInherent<V, C> for () {
type Error = MakeFatalError<()>;
const INHERENT_IDENTIFIER: InherentIdentifier = *b"no_inher";

fn create(_: &InherentData, _: Transaction<V, C>) -> Transaction<V, C> {
fn create_inherent(_: &InherentData, _: Transaction<V, C>) -> Transaction<V, C> {
panic!("Attempting to create an inherent for a constraint checker that does not support inherents.")
}

fn check(_: &InherentData, _: Transaction<V, C>) -> Result<(), Self::Error> {
fn check_inherent(_: &InherentData, _: Transaction<V, C>) -> Result<(), Self::Error> {
panic!("Attemping to perform inherent checks for a constraint checker that does not support inherents.")
}
}
Expand All @@ -139,9 +142,11 @@ impl<V: Verifier, C: ConstraintChecker<V>> TuxedoInherent<V, C> for () {
pub trait InherentInternal<V: Verifier, C: ConstraintChecker<V>>: 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<V, C>,
) -> Vec<Transaction<V, C>>;
Expand All @@ -150,7 +155,7 @@ pub trait InherentInternal<V: Verifier, C: ConstraintChecker<V>>: 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<V, C>,
) -> Result<(), Self::Error>;
Expand All @@ -159,22 +164,25 @@ pub trait InherentInternal<V: Verifier, C: ConstraintChecker<V>>: Sized + TypeIn
impl<V: Verifier, C: ConstraintChecker<V>, T: TuxedoInherent<V, C>> InherentInternal<V, C> for T {
type Error = <T as TuxedoInherent<V, C>>::Error;

fn create(
const INHERENT_IDENTIFIER: InherentIdentifier =
<T as TuxedoInherent<V, C>>::INHERENT_IDENTIFIER;

fn create_inherent(
authoring_inherent_data: &InherentData,
previous_inherent: Transaction<V, C>,
) -> Vec<Transaction<V, C>> {
// 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![<T as TuxedoInherent<V, C>>::create(
vec![<T as TuxedoInherent<V, C>>::create_inherent(
authoring_inherent_data,
previous_inherent,
)]
}

fn check(
fn check_inherent(
importing_inherent_data: &InherentData,
inherent: Transaction<V, C>,
) -> Result<(), Self::Error> {
<T as TuxedoInherent<V, C>>::check(importing_inherent_data, inherent)
<T as TuxedoInherent<V, C>>::check_inherent(importing_inherent_data, inherent)
}
}
18 changes: 10 additions & 8 deletions tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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},
Expand Down Expand Up @@ -389,20 +390,21 @@ impl_runtime_apis! {
);

// Call into the timestamp helper, then map the checker
let timestamp_tx = timestamp::SetTimestamp::<Runtime>::create(&data, prev_set_timestamp);
let transactions = <timestamp::SetTimestamp::<Runtime> as InherentInternal<OuterVerifier, OuterConstraintChecker>>::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!(
Expand All @@ -422,17 +424,17 @@ impl_runtime_apis! {
.cloned()
.expect("SetTimestamp extrinsic should appear in every block.");

let result = timestamp::SetTimestamp::<Runtime>::check(&data, set_timestamp_ext);
let result = <timestamp::SetTimestamp::<Runtime> as InherentInternal<OuterVerifier, OuterConstraintChecker>>::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 = <<timestamp::SetTimestamp<Runtime> as ConstraintChecker<OuterVerifier>>::InherentHooks as InherentInternal<OuterVerifier, OuterConstraintChecker>>::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() {
Expand Down
5 changes: 3 additions & 2 deletions wardrobe/timestamp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,9 @@ impl<V: Verifier + From<UpForGrabs>, C: ConstraintChecker<V>, T: TimestampConfig
TuxedoInherent<V, C> for SetTimestamp<T>
{
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<V, C>,
) -> tuxedo_core::types::Transaction<V, C> {
Expand Down Expand Up @@ -354,7 +355,7 @@ impl<V: Verifier + From<UpForGrabs>, C: ConstraintChecker<V>, T: TimestampConfig
timestamp_tx
}

fn check(
fn check_inherent(
importing_inherent_data: &InherentData,
inherent: tuxedo_core::types::Transaction<V, C>,
) -> Result<(), Self::Error> {
Expand Down

0 comments on commit ec19f60

Please sign in to comment.