Skip to content

Commit

Permalink
Implement Peeks (#49)
Browse files Browse the repository at this point in the history
* Update the `Transaction` type

* first pass at executive

* `ConstraintChekcerTrait`

* Sketch new executive tests

* runtime compiles

* Amoeba tests compile

* Money tests compile

* cargo fmt

* `TestTransaction` builder pattern

* Slightly improve test externality builder pattern

* refactor executive tests and include new tests for peek

* update extrinsics root now that extrinsic format has changed

* trivial updates to wallet

* amoeba stuff leftover from merging main

* Update custom encode stuff

* Fix type

* update aggregate constraint checker macro

* Improve money tests

* Fix ordering: inputs, peeks, outputs

* Update kitties tests

* fix name ordering in macro

This only makes  the variable names match the contents. It does not change behavior in any way.

* Update extrinsics root in test

The transactions encode differently now becuase there is a Vec of peeks in it. Even if there are no peeks (like all of the pre-existing tests) the encoding still chenges because a Vec's encoding contains it's length, so even an empty (length-of-zero) Vec still inserts a byte into the encoding.

* Remove inherited hack in clone no bound.

I'm not sure what "some reason" was, but `cargo fmt` seems to be working fine for me.

---------

Co-authored-by: Joshy Orndorff <git-user-email.h0ly5@simplelogin.com>
  • Loading branch information
JoshOrndorff and Joshy Orndorff authored Sep 27, 2023
1 parent 561c5ff commit 4c9f9c7
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 155 deletions.
3 changes: 2 additions & 1 deletion tuxedo-core/aggregator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ pub fn tuxedo_constraint_checker(attrs: TokenStream, body: TokenStream) -> Token
fn check (
&self,
inputs: &[tuxedo_core::types::Output<#verifier>],
peeks: &[tuxedo_core::types::Output<#verifier>],
outputs: &[tuxedo_core::types::Output<#verifier>],
) -> Result<TransactionPriority, Self::Error> {
match self {
#(
Self::#variants2(inner) => inner.check(inputs, outputs).map_err(|e| Self::Error::#variants2(e)),
Self::#variants2(inner) => inner.check(inputs, peeks, outputs).map_err(|e| Self::Error::#variants2(e)),
)*
}
}
Expand Down
3 changes: 1 addition & 2 deletions tuxedo-core/no_bound/src/default_no_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ pub fn derive_default_no_bound(input: proc_macro::TokenStream) -> proc_macro::To
[] => {
return syn::Error::new(
name.clone().span(),
// writing this as a regular string breaks rustfmt for some reason
r#"no default declared, make a variant default by placing `#[default]` above it"#,
"no default declared, make a variant default by placing `#[default]` above it",
)
.into_compile_error()
.into();
Expand Down
14 changes: 11 additions & 3 deletions tuxedo-core/src/constraint_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub trait SimpleConstraintChecker: Debug + Encode + Decode + Clone {
fn check(
&self,
input_data: &[DynamicallyTypedData],
peek_data: &[DynamicallyTypedData],
output_data: &[DynamicallyTypedData],
) -> Result<TransactionPriority, Self::Error>;
}
Expand All @@ -47,6 +48,7 @@ pub trait ConstraintChecker<V: Verifier>: Debug + Encode + Decode + Clone {
fn check(
&self,
inputs: &[Output<V>],
peeks: &[Output<V>],
outputs: &[Output<V>],
) -> Result<TransactionPriority, Self::Error>;
}
Expand All @@ -61,18 +63,23 @@ impl<T: SimpleConstraintChecker, V: Verifier> ConstraintChecker<V> for T {
fn check(
&self,
inputs: &[Output<V>],
peeks: &[Output<V>],
outputs: &[Output<V>],
) -> Result<TransactionPriority, Self::Error> {
// Extract the input data
let input_data: Vec<DynamicallyTypedData> =
inputs.iter().map(|o| o.payload.clone()).collect();

// Extract the peek data
let peek_data: Vec<DynamicallyTypedData> =
peeks.iter().map(|o| o.payload.clone()).collect();

// Extract the output data
let output_data: Vec<DynamicallyTypedData> =
outputs.iter().map(|o| o.payload.clone()).collect();

// Call the simple constraint checker
SimpleConstraintChecker::check(self, &input_data, &output_data)
SimpleConstraintChecker::check(self, &input_data, &peek_data, &output_data)
}
}

Expand All @@ -95,6 +102,7 @@ pub mod testing {
fn check(
&self,
_input_data: &[DynamicallyTypedData],
_peek_data: &[DynamicallyTypedData],
_output_data: &[DynamicallyTypedData],
) -> Result<TransactionPriority, ()> {
if self.checks {
Expand All @@ -108,14 +116,14 @@ pub mod testing {
#[test]
fn test_checker_passes() {
let result =
SimpleConstraintChecker::check(&TestConstraintChecker { checks: true }, &[], &[]);
SimpleConstraintChecker::check(&TestConstraintChecker { checks: true }, &[], &[], &[]);
assert_eq!(result, Ok(0));
}

#[test]
fn test_checker_fails() {
let result =
SimpleConstraintChecker::check(&TestConstraintChecker { checks: false }, &[], &[]);
SimpleConstraintChecker::check(&TestConstraintChecker { checks: false }, &[], &[], &[]);
assert_eq!(result, Err(()));
}
}
Loading

0 comments on commit 4c9f9c7

Please sign in to comment.