Skip to content

Commit

Permalink
builds and test passes
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Sep 27, 2023
1 parent 659ffac commit a74636a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 57 deletions.
23 changes: 12 additions & 11 deletions primitives/src/vouches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

use crate::common::BoundedIpfsCid;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{traits::Get, BoundedVec};
use frame_support::{
traits::{ConstU32, Get},
BoundedVec, RuntimeDebug,
};
use scale_info::TypeInfo;

#[cfg(feature = "serde_derive")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -55,13 +57,12 @@ pub enum VouchQuality {
Rating(Rating),
}

#[derive(Default, Encode, Decode, Clone, TypeInfo, MaxEncodedLen)]
pub struct Vouch<MaxQualities, Moment>
where
MaxQualities: Get<u32>,
{
protected: bool,
timestamp: Moment,
vouch_type: VouchType,
qualities: BoundedVec<MaxQualities, VouchQuality>,
pub type VouchQualityBoundedVec = BoundedVec<VouchQuality, ConstU32<32>>;

#[derive(Default, Encode, Decode, PartialEq, Eq, RuntimeDebug, Clone, TypeInfo, MaxEncodedLen)]
pub struct Vouch<Moment> {
pub protected: bool,
pub timestamp: Moment,
pub vouch_type: VouchType,
pub qualities: VouchQualityBoundedVec,
}
47 changes: 20 additions & 27 deletions vouches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
use core::marker::PhantomData;
use encointer_primitives::vouches::{Vouch, VouchQuality, VouchType};

use encointer_primitives::vouches::VouchQualityBoundedVec;
use frame_system::{self as frame_system, ensure_signed, pallet_prelude::OriginFor};
use log::info;
pub use pallet::*;
use sp_core::H256;
use sp_std::convert::TryInto;
pub use weights::WeightInfo;

// Logger target
const LOG: &str = "encointer";

Expand Down Expand Up @@ -58,39 +58,32 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((<T as Config>::WeightInfo::register_purpose(), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((<T as Config>::WeightInfo::vouch_for(), DispatchClass::Normal, Pays::Yes))]
pub fn vouch_for(
origin: OriginFor<T>,
attestee: T::AccountId,
vouch_type: VouchType,
qualities: BoundedVec<T::MaxQualitiesPerVouch, VouchQuality>,
) -> DispatchResultWithPostInfo
where
<T as pallet::Config>::MaxQualitiesPerVouch: Clone,
{
qualities: VouchQualityBoundedVec,
) -> DispatchResultWithPostInfo {
let attester = ensure_signed(origin)?;
let now = <pallet_timestamp::Pallet<T>>::get();
Self::do_vouch_for(attester, attestee, now, vouch_type, qualities)?;
Ok(().into())
}
}

impl<T: Config> Pallet<T> {
pub fn do_vouch_for(
attester: T::AccountId,
attestee: T::AccountId,
timestamp: T::Moment,
vouch_type: VouchType,
qualities: BoundedVec<T::MaxQualitiesPerVouch, VouchQuality>,
) -> Result<(), Error<T>> {
let vouch = Vouch { protected: false, timestamp, vouch_type, qualities };
<Vouches<T>>::try_mutate(&attestee, &attester, |vouches| {
*vouches.push(vouch).map_err(|| Error::<T>::TooManyVouchesForAttestee);
Ok(())
})?;
let vouch = Vouch {
protected: false,
timestamp: now,
vouch_type,
qualities: qualities.clone(),
};
<Vouches<T>>::try_mutate(
&attestee,
&attester,
|vouches| -> DispatchResultWithPostInfo {
vouches.try_push(vouch).map_err(|_| Error::<T>::TooManyVouchesForAttestee)?;
Ok(().into())
},
)?;
info!(target: LOG, "vouching: {:?} for {:?}, vouch type: {:?}, attached qualities: {:?}", attester, attestee, vouch_type, qualities.len());
Self::deposit_event(Event::VouchedFor { attestee, attester, vouch_type });
Ok(())
Ok(().into())
}
}

Expand All @@ -115,7 +108,7 @@ pub mod pallet {
T::AccountId,
Identity,
T::AccountId,
BoundedVec<T::MaxVouchesPerAttester, Vouch<T::MaxQualitiesPerVouch, T::Moment>>,
BoundedVec<Vouch<T::Moment>, T::MaxVouchesPerAttester>,
ValueQuery,
>;
}
3 changes: 2 additions & 1 deletion vouches/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ parameter_types! {}
impl dut::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
type MaxQualitiesPerCall = ConstU32<4>;
type MaxQualitiesPerVouch = ConstU32<4>;
type MaxVouchesPerAttester = ConstU32<4>;
}

// boilerplate
Expand Down
28 changes: 10 additions & 18 deletions vouches/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::*;
use crate::mock::{EncointerVouches, RuntimeOrigin, Timestamp};
use codec::Encode;
use encointer_primitives::{
common::BoundedIpfsCid,
common::{BoundedIpfsCid, FromStr},
vouches::{PresenceType, VouchType},
};
use frame_support::{
Expand All @@ -46,31 +46,23 @@ fn vouch_for_works() {
let charlie = AccountId::from(AccountKeyring::Charlie);

let vouch_type = VouchType::EncounteredHuman(PresenceType::Physical);
let qualities = vec![VouchQuality::Badge(BoundedIpfsCid::from(
"QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB",
))];
let mut qualities = VouchQualityBoundedVec::default();
qualities
.try_push(VouchQuality::Badge(
BoundedIpfsCid::from_str("QmPK1s3pNYLi9ERiq3BDxKa4XosgWwFRQUydHUtz4YgpqB").unwrap(),
))
.unwrap();

assert_ok!(EncointerVouches::do_vouch_for(
alice.clone(),
bob.clone(),
vouch_type,
qualities.clone()
));
assert_eq!(
EncointerVouches::vouches(bob, alice),
vec![Vouch { protected: false, timestamp: 42, vouch_type, qualities }]
);

// use extrinsic
assert_ok!(EncointerVouches::vouch_for(
RuntimeOrigin::signed(alice.clone()),
charlie.clone(),
vouch_type,
qualities.clone(),
));

assert_eq!(
EncointerVouches::vouches(charlie, alice),
vec![Vouch { protected: false, timestamp: 42, vouch_type, qualities }]
EncointerVouches::vouches(charlie, alice)[0],
Vouch { protected: false, timestamp: 42, vouch_type, qualities }
);
});
}

0 comments on commit a74636a

Please sign in to comment.