Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Feat/reward system #95

Merged
merged 7 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 7 additions & 55 deletions pallets/acurast/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use acurast_common::{AttestationChain, JobRegistration, Script};
use frame_benchmarking::{account, benchmarks, whitelist_account};
use frame_support::{
assert_ok,
sp_runtime::traits::{AccountIdConversion, Get, StaticLookup},
traits::{Currency, OriginTrait},
sp_runtime::traits::{AccountIdConversion, Get},
traits::OriginTrait,
BoundedVec,
};
use frame_system::RawOrigin;
use hex_literal::hex;
use sp_std::prelude::*;

use acurast_common::{AttestationChain, JobRegistration, Script};
pub use pallet::Config;

use crate::utils::validate_and_extract_attestation;
Expand All @@ -30,15 +30,7 @@ const SCRIPT_BYTES: [u8; 53] = hex!("697066733A2F2F00000000000000000000000000000

pub trait BenchmarkHelper<T: Config> {
fn registration_extra() -> T::RegistrationExtra;
}

impl<T: Config> BenchmarkHelper<T> for ()
where
T::RegistrationExtra: Default,
{
fn registration_extra() -> T::RegistrationExtra {
Default::default()
}
fn funded_account(index: u32) -> T::AccountId;
}

pub fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
Expand Down Expand Up @@ -89,46 +81,8 @@ pub fn attestation_chain() -> AttestationChain {
}
}

fn token_22_funded_account<T: Config>() -> T::AccountId
where
T: pallet_assets::Config,
<T as pallet_assets::Config>::AssetId: From<u32>,
<T as pallet_assets::Config>::Balance: From<u128>,
{
use pallet_assets::Pallet as Assets;
let caller: T::AccountId = account("token_account", 0, SEED);
whitelist_account!(caller);
let pallet_account: T::AccountId = <T as Config>::PalletId::get().into_account_truncating();
let pallet_origin: T::RuntimeOrigin = RawOrigin::Signed(pallet_account.clone()).into();

T::Currency::make_free_balance_be(&caller, u32::MAX.into());

// might fail if asset is already created in genesis config. Fail doesn't affect later mint
let _create_token_call = Assets::<T>::create(
pallet_origin.clone(),
T::AssetId::from(22).into(),
T::Lookup::unlookup(pallet_account.clone()),
10u32.into(),
);

let mint_token_call = Assets::<T>::mint(
pallet_origin,
T::AssetId::from(22).into(),
T::Lookup::unlookup(caller.clone()),
INITIAL_BALANCE.into(),
);
assert_ok!(mint_token_call);

caller
}

fn register_job<T: Config>(submit: bool) -> (T::AccountId, JobRegistrationFor<T>)
where
T: pallet_assets::Config,
<T as pallet_assets::Config>::AssetId: From<u32>,
<T as pallet_assets::Config>::Balance: From<u128>,
{
let caller: T::AccountId = token_22_funded_account::<T>();
fn register_job<T: Config>(submit: bool) -> (T::AccountId, JobRegistrationFor<T>) {
let caller: T::AccountId = <T as Config>::BenchmarkHelper::funded_account(0);
whitelist_account!(caller);

let job = job_registration::<T>(<T as Config>::BenchmarkHelper::registration_extra());
Expand All @@ -144,9 +98,7 @@ where

benchmarks! {
where_clause { where
T: pallet_assets::Config + pallet_timestamp::Config,
<T as pallet_assets::Config>::AssetId: From<u32>,
<T as pallet_assets::Config>::Balance: From<u128>,
T: pallet_timestamp::Config,
<T as frame_system::Config>::AccountId: From<[u8; 32]>,
<T as pallet_timestamp::Config>::Moment: From<u64>,
}
Expand Down
63 changes: 29 additions & 34 deletions pallets/acurast/src/mock.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use acurast_common::{JobModules, Schedule};
use frame_support::{
pallet_prelude::GenesisBuild, parameter_types, traits::AsEnsureOriginWithArg,
traits::Everything, PalletId,
};
use frame_support::{pallet_prelude::GenesisBuild, parameter_types, traits::Everything, PalletId};
use hex_literal::hex;
use sp_io;
use sp_runtime::traits::{AccountIdConversion, AccountIdLookup, BlakeTwo256, ConstU128, ConstU32};
use sp_runtime::traits::{AccountIdConversion, AccountIdLookup, BlakeTwo256};
use sp_runtime::{generic, AccountId32};

use acurast_common::{JobModules, Schedule};

#[cfg(feature = "runtime-benchmarks")]
use crate::benchmarking::BenchmarkHelper;
use crate::{AttestationChain, JobRegistration, RevocationListUpdateBarrier, Script, SerialNumber};

pub const SEED: u32 = 1337;

type AccountId = AccountId32;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -77,7 +79,6 @@ frame_support::construct_runtime!(
System: frame_system::{Pallet, Call, Config, Storage, Event<T>} = 0,
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Assets: pallet_assets::{Pallet, Config<T>, Event<T>, Storage},
ParachainInfo: parachain_info::{Pallet, Storage, Config},
Acurast: crate::{Pallet, Call, Storage, Event<T>}
}
Expand Down Expand Up @@ -147,29 +148,6 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = [u8; 8];
}

impl pallet_assets::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type AssetId = parachains_common::AssetIdForTrustBackedAssets;
type AssetIdParameter = codec::Compact<parachains_common::AssetIdForTrustBackedAssets>;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<frame_system::EnsureSigned<AccountId>>;
type ForceOrigin = frame_system::EnsureRoot<Self::AccountId>;
type AssetDeposit = ConstU128<0>;
type AssetAccountDeposit = ConstU128<0>;
type MetadataDepositBase = ConstU128<{ UNIT }>;
type MetadataDepositPerByte = ConstU128<{ 10 * MICROUNIT }>;
type ApprovalDeposit = ConstU128<{ 10 * MICROUNIT }>;
type StringLimit = ConstU32<50>;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
type RemoveItemsLimit = ();
type CallbackHandle = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

impl parachain_info::Config for Test {}

impl crate::Config for Test {
Expand All @@ -187,6 +165,27 @@ impl crate::Config for Test {
type BenchmarkHelper = ();
}

#[cfg(feature = "runtime-benchmarks")]
impl<T: Config> BenchmarkHelper<T> for ()
where
T::RegistrationExtra: Default,
T::AccountId: Into<AccountId32>,
{
fn registration_extra() -> T::RegistrationExtra {
Default::default()
}

fn funded_account(index: u32) -> T::AccountId {
let caller: T::AccountId = frame_benchmarking::account("token_account", index, SEED);
<Balances as frame_support::traits::Currency<_>>::make_free_balance_be(
&caller.clone().into(),
u32::MAX.into(),
);

caller
}
}

pub fn events() -> Vec<RuntimeEvent> {
let evt = System::events()
.into_iter()
Expand Down Expand Up @@ -328,10 +327,6 @@ pub fn processor_account_id() -> AccountId {
hex!("b8bc25a2b4c0386b8892b43e435b71fe11fa50533935f027949caf04bcce4694").into()
}

pub fn pallet_assets_account() -> <Test as frame_system::Config>::AccountId {
<Test as crate::Config>::PalletId::get().into_account_truncating()
}

pub fn alice_account_id() -> AccountId {
[0; 32].into()
}
Expand Down
5 changes: 2 additions & 3 deletions pallets/assets-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
# Benchmarks
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", optional = true, default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", optional = true, default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", optional = true, default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.41" }

# Substrate
Expand All @@ -33,6 +32,7 @@ scale-info = { version = "2.2.0", default-features = false, features = [
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.41" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", default-features = false }
pallet-assets = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.41" }

# Polkadot
Expand All @@ -44,7 +44,6 @@ acurast-common = { path = "../acurast/common", default-features = false }

[dev-dependencies]
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41", default-features = false }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.41" }

[features]
Expand All @@ -57,6 +56,7 @@ std = [
"frame-benchmarking/std",
"sp-std/std",
"sp-runtime/std",
"sp-core/std",
"pallet-assets/std",
"xcm-builder/std",
"xcm-executor/std",
Expand All @@ -67,7 +67,6 @@ std = [
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"sp-io",
"sp-core",
"pallet-balances/runtime-benchmarks",
"pallet-assets/runtime-benchmarks",
]
Expand Down
46 changes: 12 additions & 34 deletions pallets/assets-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ mod stub;
#[cfg(test)]
mod tests;

mod migration;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;

pub mod weights;

pub mod traits;

use sp_runtime::traits::StaticLookup;

pub use pallet::*;
Expand All @@ -27,15 +27,17 @@ type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup
#[frame_support::pallet]
pub mod pallet {
use super::*;
use crate::traits::AssetValidator;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use xcm::latest::MultiLocation;
use xcm::prelude::{Abstract, AssetId, Concrete, GeneralIndex, PalletInstance, Parachain, X3};
use xcm_executor::traits::Convert;

pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

#[pallet::config]
Expand Down Expand Up @@ -115,6 +117,13 @@ pub mod pallet {
InvalidAssetIndex,
}

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_runtime_upgrade() -> Weight {
crate::migration::migrate_to_v2::<T, I>()
}
}

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I>
where
Expand Down Expand Up @@ -240,15 +249,6 @@ pub mod pallet {
}
}

impl<T: Config<I> + pallet_assets::Config<I>, I: 'static> AssetValidator<AssetId> for Pallet<T, I> {
type Error = Error<T, I>;

fn validate(asset: &AssetId) -> Result<(), Self::Error> {
Self::reverse_asset_index(asset).ok_or(Error::<T, I>::AssetNotIndexed)?;
Ok(())
}
}

impl<T: Config<I> + pallet_assets::Config<I>, I: 'static> Convert<MultiLocation, T::AssetId>
for Pallet<T, I>
{
Expand All @@ -262,26 +262,4 @@ pub mod pallet {
}
}
}

impl<T: Config<I> + pallet_assets::Config<I>, I: 'static> acurast_common::AssetTransfer
for Pallet<T, I>
{
type AssetId = AssetId;
type AccountId = T::AccountId;
type Balance = T::Balance;
type Error = DispatchError;

fn transfer(
asset: Self::AssetId,
source: &Self::AccountId,
dest: &Self::AccountId,
amount: Self::Balance,
) -> Result<(), Self::Error> {
let id =
<ReverseAssetIndex<T, I>>::get(&asset).ok_or(Error::<T, I>::AssetNotIndexed)?;
let _: Self::Balance =
<pallet_assets::Pallet::<T, I> as frame_support::traits::tokens::fungibles::Transfer<Self::AccountId>>::transfer(id, source, dest, amount, true)?;
Ok(())
}
}
}
18 changes: 18 additions & 0 deletions pallets/assets-manager/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use frame_support::{traits::GetStorageVersion, weights::Weight};
use sp_core::Get;

use super::*;

pub fn migrate_to_v2<T: Config<I>, I: 'static>() -> Weight {
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
if onchain_version < crate::STORAGE_VERSION {
let mut count = 0u32;
count += AssetIndex::<T, I>::clear(100, None).loops;
count += ReverseAssetIndex::<T, I>::clear(100, None).loops;

STORAGE_VERSION.put::<Pallet<T, I>>();
T::DbWeight::get().reads_writes((count + 1).into(), (count + 1).into())
} else {
Weight::zero()
}
}
15 changes: 0 additions & 15 deletions pallets/assets-manager/src/traits.rs

This file was deleted.

4 changes: 1 addition & 3 deletions pallets/hyperdrive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub mod pallet {
use sp_std::vec;

use pallet_acurast_marketplace::types::RegistrationExtra;
use pallet_acurast_marketplace::Reward;

use crate::weights::WeightInfo;

Expand Down Expand Up @@ -88,12 +87,11 @@ pub mod pallet {
+ Zero
+ From<u8>
+ CheckedRem;
type Reward: Parameter + Member + Reward;
type Balance: Member
+ Parameter
+ AtLeast32BitUnsigned
+ From<u128>
// required to translate Tezos Ints of unknown precision (Alternative: use Tezos SDK types in clients of this pallet)
+ From<u128>
+ Default
+ Copy
+ MaybeSerializeDeserialize
Expand Down
Loading