Skip to content

Commit

Permalink
Got rid of the extra constraint... but at what cost?!??!
Browse files Browse the repository at this point in the history
dun dun duuuun
  • Loading branch information
expede committed Jan 29, 2024
1 parent 2355b79 commit 21af73e
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/ability/crud/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Command for AnyBuilder {
}

impl Checkable for AnyBuilder {
type CheckAs = Parentless<AnyBuilder>;
type Heirarchy = Parentless<AnyBuilder>;
}

impl CheckSame for AnyBuilder {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/crud/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl TryFrom<Ipld> for Create {
}

impl Checkable for Create {
type CheckAs = Parentful<Create>;
type Heirarchy = Parentful<Create>;
}

impl CheckSame for Create {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/crud/destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl TryFrom<Ipld> for Destroy {
}

impl Checkable for Destroy {
type CheckAs = Parentful<Destroy>;
type Heirarchy = Parentful<Destroy>;
}

impl CheckSame for Destroy {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/crud/mutate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl TryFrom<Ipld> for MutateBuilder {
}

impl Checkable for MutateBuilder {
type CheckAs = Parentful<MutateBuilder>;
type Heirarchy = Parentful<MutateBuilder>;
}

impl CheckSame for MutateBuilder {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/crud/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl TryFrom<Ipld> for Read {
}

impl Checkable for Read {
type CheckAs = Parentful<Read>;
type Heirarchy = Parentful<Read>;
}

impl CheckSame for Read {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/crud/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl TryFrom<Ipld> for UpdateBuilder {
}

impl Checkable for UpdateBuilder {
type CheckAs = Parentful<UpdateBuilder>;
type Heirarchy = Parentful<UpdateBuilder>;
}

impl CheckSame for UpdateBuilder {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/msg/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TryFrom<Ipld> for Any {
}

impl Checkable for Any {
type CheckAs = Parentless<Any>;
type Heirarchy = Parentless<Any>;
}

impl CheckSame for Any {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/msg/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TryFrom<Ipld> for Receive {
}

impl Checkable for Receive {
type CheckAs = Parentful<Receive>;
type Heirarchy = Parentful<Receive>;
}

impl CheckSame for Receive {
Expand Down
2 changes: 1 addition & 1 deletion src/ability/msg/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl TryFrom<Ipld> for SendBuilder {
}

impl Checkable for SendBuilder {
type CheckAs = Parentful<SendBuilder>;
type Heirarchy = Parentful<SendBuilder>;
}

impl CheckSame for SendBuilder {
Expand Down
78 changes: 41 additions & 37 deletions src/delegation/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,47 +81,52 @@ where
}
}

impl<T: Delegatable + Debug, C: Condition> From<Payload<T, C>> for Ipld {
impl<T: Delegatable, C: Condition> From<Payload<T, C>> for Ipld {
fn from(payload: Payload<T, C>) -> Self {
payload.into()
}
}

impl<'a, T: Delegatable + Resolvable + Checkable + Clone, C: Condition> Payload<T, C> {
pub fn check<U: Delegatable + Clone>(
invoked: invocation::Payload<T>, // FIXME promisory version
invoked: &'a invocation::Payload<T>, // FIXME promisory version
proofs: Vec<Payload<U, C>>,
now: SystemTime,
) -> Result<(), ()>
where
// FIXME so so so broken
invocation::Payload<T>: Clone,
T::CheckAs: From<invocation::Payload<T>> + From<U::Builder> + Prove<T::CheckAs> + CheckSame,
U::Builder: Clone,
T::Heirarchy: From<invocation::Payload<T>> + From<U::Builder> + CheckSame,
{
let check_chain: T::CheckAs = invoked.clone().into();
let start: Acc<T> = Acc {
issuer: invoked.issuer.clone(),
subject: invoked.subject.clone(),
check_chain,
let start: Acc<'a, T> = Acc {
issuer: &invoked.issuer,
subject: &invoked.subject,
to_check: invoked.clone().into(), // FIXME surely we can eliminate this clone
};

let ipld: Ipld = invoked.into();
let ipld: Ipld = invoked.clone().into();

let result = proofs.iter().fold(Ok(start), |prev, proof| {
if let Ok(to_check) = prev {
match step1(&to_check, proof, &ipld, now) {
let result = proofs.iter().fold(Ok(start), |acc, proof| {
if let Ok(prev) = acc {
match step(&prev, proof, &ipld, now) {
Outcome::ArgumentEscelation(_) => Err(()),
Outcome::InvalidProofChain(_) => Err(()),
Outcome::ProvenByAny => Ok(to_check), // NOTE this case!
Outcome::InvalidParents(_) => Err(()),
Outcome::CommandEscelation => Err(()),
// NOTE this case!
Outcome::ProvenByAny => Ok(Acc {
issuer: &proof.issuer,
subject: &proof.subject,
to_check: prev.to_check,
}),
Outcome::Proven => Ok(Acc {
issuer: proof.issuer.clone(),
subject: proof.subject.clone(),
check_chain: proof.ability_builder.clone().into(), // FIXME double check
issuer: &proof.issuer,
subject: &proof.subject,
to_check: proof.ability_builder.clone().into(), // FIXME double check
}),
}
} else {
prev
acc
}
});

Expand All @@ -133,25 +138,24 @@ impl<'a, T: Delegatable + Resolvable + Checkable + Clone, C: Condition> Payload<
}
}

#[derive(Clone)]
struct Acc<T: Checkable> {
issuer: Did,
subject: Did,
check_chain: T::CheckAs,
#[derive(Debug, Clone)]
struct Acc<'a, T: Checkable> {
issuer: &'a Did,
subject: &'a Did,
to_check: T::Heirarchy,
}

// FIXME replace with check_parents?
// FIXME this needs to move to Delegatable
fn step1<'a, T: Checkable, U: Delegatable, C: Condition>(
prev: &'a Acc<T>,
proof: &'a Payload<U, C>,
invoked_ipld: &'a Ipld,
// FIXME this should move to Delegatable
fn step<'a, T: Checkable, U: Delegatable, C: Condition>(
prev: &Acc<'a, T>,
proof: &Payload<U, C>,
invoked_ipld: &Ipld,
now: SystemTime,
) -> Outcome<(), ()>
// FIXME Outcome types
) -> Outcome<(), (), ()>
// FIXME ^^^^^^^^^^^^ Outcome types
where
T::CheckAs: From<U::Builder> + Prove<T::CheckAs>,
U::Builder: Clone,
T::Heirarchy: From<U::Builder>,
{
if let Err(_) = prev.issuer.check_same(&proof.issuer) {
return Outcome::InvalidProofChain(());
Expand Down Expand Up @@ -189,16 +193,16 @@ where
return Outcome::InvalidProofChain(());
}

// FIXME pricey clone
let foo = prev
.check_chain
.check(&proof.ability_builder.clone().into());
// FIXME pretty sure we can avoid this clone
let outcome = prev.to_check.check(&proof.ability_builder.clone().into());

match foo {
match outcome {
Outcome::Proven => Outcome::Proven,
Outcome::ProvenByAny => Outcome::ProvenByAny,
Outcome::CommandEscelation => Outcome::CommandEscelation,
Outcome::ArgumentEscelation(_) => Outcome::ArgumentEscelation(()),
Outcome::InvalidProofChain(_) => Outcome::InvalidProofChain(()),
Outcome::InvalidParents(_) => Outcome::InvalidParents(()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/proof/checkable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{internal::Checker, same::CheckSame};
use super::{internal::Checker, prove::Prove, same::CheckSame};

pub trait Checkable: CheckSame {
type CheckAs: Checker;
type Heirarchy: Checker + Prove<Self::Heirarchy>;
}
94 changes: 86 additions & 8 deletions src/proof/parentful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub enum Parentful<T: CheckParents> {
This(T),
}

pub enum ParentfulError<ArgErr, PrfErr, ParErr> {
CommandEscelation,
ArgumentEscelation(ArgErr),
InvalidProofChain(PrfErr),
InvalidParents(ParErr), // FIXME seems kinda broken -- better naming at least
}

impl<T: CheckParents> From<Parentful<T>> for Ipld
where
Ipld: From<T>,
Expand All @@ -34,25 +41,96 @@ where
}
}

impl<T: CheckSame + CheckParents> CheckSame for Parentful<T>
where
T::Parents: CheckSame,
{
type Error = ParentfulError<T::Error, T::ParentError, <T::Parents as CheckSame>::Error>; // FIXME

fn check_same(&self, proof: &Self) -> Result<(), Self::Error> {
match proof {
Parentful::Any => Ok(()),
Parentful::Parents(their_parents) => match self {
Parentful::Any => Err(ParentfulError::CommandEscelation),
Parentful::Parents(parents) => parents
.check_same(their_parents)
.map_err(ParentfulError::InvalidParents),
Parentful::This(this) => this
.check_parents(their_parents)
.map_err(ParentfulError::InvalidProofChain),
},
Parentful::This(that) => match self {
Parentful::Any => Err(ParentfulError::CommandEscelation),
Parentful::Parents(_) => Err(ParentfulError::CommandEscelation),
Parentful::This(this) => this
.check_same(that)
.map_err(ParentfulError::ArgumentEscelation),
},
}
}
}

impl<T: CheckSame + CheckParents> CheckParents for Parentful<T>
where
T::Parents: CheckSame,
{
type Parents = Parentful<T>;
type ParentError = ParentfulError<T::Error, T::ParentError, <T::Parents as CheckSame>::Error>;

fn check_parents(&self, proof: &Parentful<T>) -> Result<(), Self::ParentError> {
match proof {
Parentful::Any => Ok(()),
Parentful::Parents(their_parents) => match self {
Parentful::Any => Err(ParentfulError::CommandEscelation),
Parentful::Parents(parents) => parents
.check_same(their_parents)
.map_err(ParentfulError::InvalidParents),
Parentful::This(this) => this
.check_parents(their_parents)
.map_err(ParentfulError::InvalidProofChain),
},
Parentful::This(that) => match self {
Parentful::Any => Err(ParentfulError::CommandEscelation),
Parentful::Parents(_) => Err(ParentfulError::CommandEscelation),
Parentful::This(this) => this
.check_same(that)
.map_err(ParentfulError::ArgumentEscelation),
},
}
}
}

impl<T: CheckParents> Checker for Parentful<T> {}

impl<T: CheckParents> Prove<Parentful<T>> for T
impl<T: CheckParents> Prove<Parentful<T>> for Parentful<T>
where
T::Parents: CheckSame,
{
type ArgumentError = T::Error;
type ProofChainError = T::ParentError;
type ParentsError = <T::Parents as CheckSame>::Error; // FIXME better name

fn check<'a>(&'a self, proof: &'a Parentful<T>) -> Outcome<T::Error, T::ParentError> {
fn check(&self, proof: &Parentful<T>) -> Outcome<T::Error, T::ParentError, Self::ParentsError> {
match proof {
Parentful::Any => Outcome::ProvenByAny,
Parentful::Parents(parents) => match self.check_parents(parents) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::InvalidProofChain(e),
Parentful::Parents(their_parents) => match self {
Parentful::Any => Outcome::CommandEscelation,
Parentful::Parents(parents) => match parents.check_same(their_parents) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::InvalidParents(e),
},
Parentful::This(this) => match this.check_parents(their_parents) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::InvalidProofChain(e),
},
},
Parentful::This(that) => match self.check_same(&that) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::ArgumentEscelation(e),
Parentful::This(that) => match self {
Parentful::Any => Outcome::CommandEscelation,
Parentful::Parents(_) => Outcome::CommandEscelation,
Parentful::This(this) => match this.check_same(that) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::ArgumentEscelation(e),
},
},
}
}
Expand Down
36 changes: 31 additions & 5 deletions src/proof/parentless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub enum Parentless<T> {
This(T),
}

#[derive(Debug, Clone, PartialEq)]
pub enum ParentlessError<T: CheckSame> {
CommandEscelation,
ArgumentEscelation(T::Error),
}

impl<T> From<Parentless<T>> for Ipld
where
Ipld: From<T>,
Expand All @@ -30,18 +36,38 @@ impl<T: TryFrom<Ipld> + DeserializeOwned> TryFrom<Ipld> for Parentless<T> {
}
}

impl<T: CheckSame> CheckSame for Parentless<T> {
type Error = ParentlessError<T>;

fn check_same(&self, other: &Self) -> Result<(), Self::Error> {
match other {
Parentless::Any => Ok(()),
Parentless::This(that) => match self {
Parentless::Any => Err(ParentlessError::CommandEscelation),
Parentless::This(this) => this
.check_same(that)
.map_err(ParentlessError::ArgumentEscelation),
},
}
}
}

impl<T: CheckSame> Checker for Parentless<T> {}

impl<T: CheckSame> Prove<Parentless<T>> for T {
impl<T: CheckSame> Prove<Parentless<T>> for Parentless<T> {
type ArgumentError = T::Error;
type ProofChainError = Infallible;
type ParentsError = Infallible;

fn check<'a>(&'a self, proof: &'a Parentless<T>) -> Outcome<T::Error, Infallible> {
fn check(&self, proof: &Parentless<T>) -> Outcome<T::Error, Infallible, Infallible> {
match proof {
Parentless::Any => Outcome::Proven,
Parentless::This(this) => match self.check_same(&this) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::ArgumentEscelation(e),
Parentless::This(that) => match self {
Parentless::Any => Outcome::Proven,
Parentless::This(this) => match this.check_same(that) {
Ok(()) => Outcome::Proven,
Err(e) => Outcome::ArgumentEscelation(e),
},
},
}
}
Expand Down
Loading

0 comments on commit 21af73e

Please sign in to comment.