Skip to content

Commit

Permalink
proper impl for attests
Browse files Browse the repository at this point in the history
  • Loading branch information
gui1117 committed Sep 20, 2024
1 parent 5e8b5bf commit b392ec2
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions polkadot/runtime/common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,9 +636,14 @@ where
type Pre = ();
type Val = ();

// <weight>
// The weight of this logic is included in the `attest` dispatchable.
// </weight>
fn weight(&self, call: &T::RuntimeCall) -> Weight {
if let Some(Call::attest { .. }) = call.is_sub_type() {
T::WeightInfo::prevalidate_attests()
} else {
Weight::zero()
}
}

fn validate(
&self,
origin: <T::RuntimeCall as Dispatchable>::RuntimeOrigin,
Expand All @@ -651,19 +656,18 @@ where
(ValidTransaction, Self::Val, <T::RuntimeCall as Dispatchable>::RuntimeOrigin),
TransactionValidityError,
> {
if let Some(local_call) = call.is_sub_type() {
if let Call::attest { statement: attested_statement } = local_call {
let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
let signer = Preclaims::<T>::get(who)
.ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
if let Some(s) = Signing::<T>::get(signer) {
let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
ensure!(&attested_statement[..] == s.to_text(), e);
}
if let Some(Call::attest { statement: attested_statement }) = call.is_sub_type() {
let who = origin.as_system_origin_signer().ok_or(InvalidTransaction::BadSigner)?;
let signer = Preclaims::<T>::get(who)
.ok_or(InvalidTransaction::Custom(ValidityError::SignerHasNoClaim.into()))?;
if let Some(s) = Signing::<T>::get(signer) {
let e = InvalidTransaction::Custom(ValidityError::InvalidStatement.into());
ensure!(&attested_statement[..] == s.to_text(), e);
}
}
Ok((ValidTransaction::default(), (), origin))
}

impl_tx_ext_default!(T::RuntimeCall; prepare);
}

Expand Down Expand Up @@ -1591,24 +1595,9 @@ mod benchmarking {
Preclaims::<T>::insert(&account, eth_address);
assert_eq!(Claims::<T>::get(eth_address), Some(VALUE.into()));

let call = super::Call::<T>::attest { statement: StatementKind::Regular.to_text().to_vec() };
// We have to copy the validate statement here because of trait issues... :(
let validate = |who: &T::AccountId, call: &super::Call<T>| -> DispatchResult {
if let Call::attest{ statement: attested_statement } = call {
let signer = Preclaims::<T>::get(who).ok_or("signer has no claim")?;
if let Some(s) = Signing::<T>::get(signer) {
ensure!(&attested_statement[..] == s.to_text(), "invalid statement");
}
}
Ok(())
};
let call_enc = call.encode();
}: {
let call = <Call<T> as Decode>::decode(&mut &*call_enc)
.expect("call is encoded above, encoding must be correct");
validate(&account, &call)?;
call.dispatch_bypass_filter(RawOrigin::Signed(account).into())?;
}
let stmt = StatementKind::Regular.to_text().to_vec();
}:
_(RawOrigin::Signed(account), stmt)
verify {
assert_eq!(Claims::<T>::get(eth_address), None);
}
Expand Down

0 comments on commit b392ec2

Please sign in to comment.