Skip to content

Commit

Permalink
start working on the gagregator. This is getting complicated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshy Orndorff committed Oct 9, 2023
1 parent fc7728c commit d4bf5c4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 24 deletions.
Empty file added out.rs
Empty file.
74 changes: 68 additions & 6 deletions tuxedo-core/aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,25 @@ pub fn tuxedo_constraint_checker(attrs: TokenStream, body: TokenStream) -> Token
let variants = variant_type_pairs.clone().map(|(v, _t)| v);
let inner_types = variant_type_pairs.map(|(_v, t)| t);

let vis = ast.vis;
//
let mut error_type_name = outer_type.to_string();
error_type_name.push_str("Error");
let error_type = Ident::new(&error_type_name, outer_type.span());
let inner_types = inner_types.clone();

let mut inherent_hooks_name = outer_type.to_string();
inherent_hooks_name.push_str("InherentHooks");
let inherent_hooks = Ident::new(&inherent_hooks_name, outer_type.span());

let vis = ast.vis;
let inner_types2 = inner_types.clone();
let inner_types3 = inner_types.clone();
let inner_types4 = inner_types.clone();
// let inner_types5 = inner_types.clone();
let variants2 = variants.clone();
let variants3 = variants.clone();
let variants4 = variants.clone();
let variants5 = variants.clone();

let output = quote! {
// Preserve the original enum, and write the From impls
#[tuxedo_core::aggregate]
Expand All @@ -138,12 +151,61 @@ pub fn tuxedo_constraint_checker(attrs: TokenStream, body: TokenStream) -> Token
)*
}

/// This type is generated by the `#[tuxedo_constraint_checker]` macro.
/// It is a combined set of inherent hooks for the inherent hooks of each individual checker.
///
/// This type is accessible downstream as `<OuterConstraintChecker as ConstraintChecker>::InherentHooks`
#[derive(Debug, scale_info::TypeInfo)]
#vis enum #inherent_hooks {
#(
#variants2(<#inner_types2 as tuxedo_core::ConstraintChecker<#verifier>>::InherentHooks),
)*
}

impl InherentInternal<#verifier, #outer_type> for #inherent_hooks {

// TODO I guess I'll want some aggregate error type here.
type Error = sp_inherents::MakeFatalError<()>;

// I guess this will never be used
const INHERENT_IDENTIFIER: InherentIdentifier = *b"aggregat";

fn create_inherents(
authoring_inherent_data: &InherentData,
previous_inherents: Vec<tuxedo_core::types::Transaction<#verifier, #outer_type>>,
) -> Vec<tuxedo_core::types::Transaction<#verifier, #outer_type>> {
todo!("This may be tricky")
}

fn check_inherent(
importing_inherent_data: &sp_inherents::InherentData,
inherent: tuxedo_core::types::Transaction<#verifier, #outer_type>,
) -> Result<(), Self::Error> {
match inherent.checker {
#(
#outer_type::#variants4(inner_checker) => {

// Tried a transform method here, but was missing some into impl. That could probably be fixed.
let unwrapped_inherent = tuxedo_core::types::Transaction::<#verifier, #inner_types4> {
inputs: inherent.inputs,
peeks: inherent.peeks,
outputs: inherent.outputs,
checker: inner_checker,
};

<#inner_types4 as tuxedo_core::ConstraintChecker<#verifier>>::InherentHooks::check_inherent(importing_inherent_data, unwrapped_inherent)
.map_err(|_| sp_inherents::MakeFatalError::from(()))
}
)*
}
}

}

impl tuxedo_core::ConstraintChecker<#verifier> for #outer_type {
type Error = #error_type;

//TODO We for sure need to fix this, and do some carefully-considered aggregating.
// But for now we can make it compile this way.
type InherentHooks = ();
type InherentHooks = #inherent_hooks;

fn check (
&self,
Expand All @@ -153,7 +215,7 @@ pub fn tuxedo_constraint_checker(attrs: TokenStream, body: TokenStream) -> Token
) -> Result<TransactionPriority, Self::Error> {
match self {
#(
Self::#variants2(inner) => inner.check(inputs, peeks, outputs).map_err(|e| Self::Error::#variants2(e)),
Self::#variants5(inner) => inner.check(inputs, peeks, outputs).map_err(|e| Self::Error::#variants5(e)),
)*
}
}
Expand Down
22 changes: 5 additions & 17 deletions tuxedo-core/src/inherents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ 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_inherents(
fn check_inherent(
importing_inherent_data: &InherentData,
inherents: Vec<Transaction<V, C>>,
inherent: Transaction<V, C>,
) -> Result<(), Self::Error>;
}

Expand All @@ -182,22 +182,10 @@ impl<V: Verifier, C: ConstraintChecker<V>, T: TuxedoInherent<V, C>> InherentInte
)]
}

fn check_inherents(
fn check_inherent(
importing_inherent_data: &InherentData,
inherents: Vec<Transaction<V, C>>,
inherent: Transaction<V, C>,
) -> Result<(), Self::Error> {
if inherents.len() > 1 {
panic!("Checking a leaf inherent constraint checker, but multiple inherents were supplied.")
}

<T as TuxedoInherent<V, C>>::check_inherent(
importing_inherent_data,
inherents
.get(0)
.expect(
"Checking a leaf inherent constraint checker, but no inherent was supplied.",
)
.clone(),
)
<T as TuxedoInherent<V, C>>::check_inherent(importing_inherent_data, inherent)
}
}
14 changes: 14 additions & 0 deletions tuxedo-core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ pub struct Transaction<V: TypeInfo, C: TypeInfo> {
pub checker: C,
}

impl<V: TypeInfo, C: TypeInfo> Transaction<V, C> {
/// A helper function for transforming a transaction generic over one
/// kind of constraint checker into a transaction generic over another type
/// of constraint checker. This is useful when moving up and down the aggregation tree.
pub fn transform<D: TypeInfo + From<C>>(self) -> Transaction<V, D> {
Transaction {
inputs: self.inputs,
peeks: self.peeks,
outputs: self.outputs,
checker: self.checker.into(),
}
}
}

// Manually implement Encode and Decode for the Transaction type
// so that its encoding is the same as an opaque Vec<u8>.
impl<V: Encode + TypeInfo, C: Encode + TypeInfo> Encode for Transaction<V, C> {
Expand Down
2 changes: 1 addition & 1 deletion tuxedo-template-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl_runtime_apis! {
);

// Call into the timestamp helper, then map the checker
let transactions = <timestamp::SetTimestamp::<Runtime> as InherentInternal<OuterVerifier, OuterConstraintChecker>>::create_inherent(&data, prev_set_timestamp);
let transactions = <timestamp::SetTimestamp::<Runtime> as InherentInternal<OuterVerifier, OuterConstraintChecker>>::create_inherents(&data, vec![prev_set_timestamp]);

// Return just the timestamp extrinsic for now.
// Later we will either handle Aura properly or switch to nimbus.
Expand Down

0 comments on commit d4bf5c4

Please sign in to comment.