From 16a7851952f51b4a77ef5d58c18995130882e3e7 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Mon, 1 Mar 2021 10:47:07 +0100 Subject: [PATCH 01/27] refactor: Remove execution runtime module and merge into config module --- .github/workflows/test.yml | 3 +- Cargo.lock | 24 - custom_types.json | 18 +- pallets/mining/config/token/src/lib.rs | 272 +++++++++ pallets/mining/execution/token/Cargo.toml | 50 -- pallets/mining/execution/token/src/lib.rs | 562 ------------------ runtime/Cargo.toml | 2 - runtime/src/lib.rs | 6 - .../cli_integration_tests_mining_tokens.rs | 30 +- 9 files changed, 291 insertions(+), 676 deletions(-) delete mode 100644 pallets/mining/execution/token/Cargo.toml delete mode 100644 pallets/mining/execution/token/src/lib.rs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f6b3f778e..cb90a6373 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -92,8 +92,7 @@ jobs: cargo +nightly-2020-10-06 test -p mining-eligibility-token && cargo +nightly-2020-10-06 test -p mining-eligibility-hardware && cargo +nightly-2020-10-06 test -p mining-claims-token && - cargo +nightly-2020-10-06 test -p mining-claims-hardware && - cargo +nightly-2020-10-06 test -p mining-execution-token + cargo +nightly-2020-10-06 test -p mining-claims-hardware lints: name: Lints diff --git a/Cargo.lock b/Cargo.lock index 513d6a613..4b15fe86c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -999,7 +999,6 @@ dependencies = [ "mining-config-token", "mining-eligibility-hardware", "mining-eligibility-token", - "mining-execution-token", "mining-rates-hardware", "mining-rates-token", "mining-sampling-hardware", @@ -3274,29 +3273,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "mining-execution-token" -version = "0.1.0" -dependencies = [ - "frame-support", - "frame-system", - "mining-claims-token", - "mining-config-token", - "mining-eligibility-token", - "mining-rates-token", - "mining-sampling-token", - "pallet-balances", - "pallet-randomness-collective-flip", - "pallet-transaction-payment", - "parity-scale-codec", - "roaming-operators", - "safe-mix", - "sp-core", - "sp-io", - "sp-runtime", - "sp-std", -] - [[package]] name = "mining-rates-hardware" version = "0.1.0" diff --git a/custom_types.json b/custom_types.json index b5645f0d8..35f29ebea 100644 --- a/custom_types.json +++ b/custom_types.json @@ -124,8 +124,8 @@ "MiningConfigTokenConfig": { "token_type": "Text", "token_lock_amount": "u64", - "token_lock_start_block": "Moment", - "token_lock_interval_blocks": "Moment" + "token_lock_start_block": "u64", + "token_lock_interval_blocks": "u64" }, "MiningConfigTokenRequirementsConfig": { "token_type": "Text", @@ -146,6 +146,12 @@ "hardware_lock_start_block": "Moment", "hardware_lock_interval_blocks": "Moment" }, + "MiningConfigTokenExecutorAccountID": "u64", + "MiningConfigTokenExecutionResult": { + "token_execution_executor_account_id": "u64", + "token_execution_started_block": "u64", + "token_execution_interval_blocks": "u64" + }, "MiningSamplingToken": "[u8; 16]", "MiningSamplingTokenIndex": "u64", "MiningSamplingTokenSampleLockedAmount": "u64", @@ -195,13 +201,5 @@ "MiningClaimsHardwareClaimResult": { "hardware_claim_amount": "u64", "hardware_claim_block_redeemed": "u64" - }, - "MiningExecutionToken": "[u8; 16]", - "MiningExecutionTokenIndex": "u64", - "MiningExecutionTokenExecutorAccountID": "u64", - "MiningExecutionTokenExecutionResult": { - "token_execution_exector_account_id": "u64", - "token_execution_started_block": "Moment", - "token_execution_ended_block": "Moment" } } diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 5b67fc778..df3873245 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -73,6 +73,14 @@ pub struct MiningConfigTokenRequirementsConfig { pub token_lock_min_blocks: W, } +#[cfg_attr(feature = "std", derive(Debug))] +#[derive(Encode, Decode, Default, Clone, PartialEq)] +pub struct MiningConfigTokenExecutionResult { + pub token_execution_executor_account_id: U, + pub token_execution_started_block: V, + pub token_execution_interval_blocks: W, +} + decl_event!( pub enum Event where ::AccountId, @@ -92,6 +100,9 @@ decl_event!( AccountId, MiningConfigTokenIndex, MiningConfigTokenType, Balance, BlockNumber ), + MiningConfigTokenExecutionResultSet( + AccountId, MiningConfigTokenIndex,AccountId, BlockNumber, BlockNumber + ), } ); @@ -114,6 +125,14 @@ decl_storage! { /// Stores mining_config_token_token_cooldown_config pub MiningConfigTokenRequirementsConfigs get(fn mining_config_token_token_cooldown_configs): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option, T::BlockNumber>>; + + /// Stores mining_config_token_execution_result + pub MiningConfigTokenExecutionResults get(fn mining_config_token_execution_results): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => + Option>; } } @@ -341,6 +360,131 @@ decl_module! { token_lock_min_blocks, )); } + + /// Set mining_config_token_execution_result + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn set_mining_config_token_execution_result( + origin, + mining_config_token_id: T::MiningConfigTokenIndex, + ) { + let sender = ensure_signed(origin)?; + + // Ensure that the mining_config_token_id whose config we want to change actually exists + let is_mining_config_token = Self::exists_mining_config_token(mining_config_token_id).is_ok(); + ensure!(is_mining_config_token, "MiningConfigToken does not exist"); + + // Ensure that the caller is owner of the mining_config_token_token_config they are trying to change + ensure!(Self::mining_config_token_owner(mining_config_token_id) == Some(sender.clone()), "Only owner can set mining_config_token_token_config"); + + // Check that only allow the owner of the configuration that the execution belongs to call this extrinsic to set and execute + ensure!( + Self::is_mining_config_token_owner( + mining_config_token_id, sender.clone() + ).is_ok(), + "Only the configuration_token owner can execute their associated execution" + ); + + // Assign config values to the execution values + let token_execution_executor_account_id = sender.clone(); + let token_execution_started_block; + let token_execution_interval_blocks; + + if let Some(configuration_token_config) = + Self::mining_config_token_token_configs(mining_config_token_id) + { + if let _token_lock_start_block = configuration_token_config.token_lock_start_block { + token_execution_started_block = _token_lock_start_block.clone(); + } else { + return Err(DispatchError::Other("Cannot find token_lock_start_block associated with the config")); + } + + if let _token_lock_interval_blocks = configuration_token_config.token_lock_interval_blocks { + token_execution_interval_blocks = _token_lock_interval_blocks.clone(); + } else { + return Err(DispatchError::Other("Cannot find token_lock_interval_blocks associated with the config")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + + // TODO - we could just use the token_execution_started_block that we queried already instead of calling it again within this function + // Ensure that the associated token configuration has a token_execution_started_block > current_block + let is_token_execution_started_block_greater_than_current_block = Self::token_execution_started_block_greater_than_current_block(mining_config_token_id).is_ok(); + ensure!(is_token_execution_started_block_greater_than_current_block, "token execution does not have a token_execution_started_block > current_block"); + + // Ensure that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks + let is_token_lock_interval_blocks_greater_than_token_lock_min_blocks = Self::token_lock_interval_blocks_greater_than_token_lock_min_blocks(mining_config_token_id).is_ok(); + ensure!(is_token_lock_interval_blocks_greater_than_token_lock_min_blocks, "token configuration does not have a token_lock_interval_blocks > token_lock_min_blocks"); + + // Ensure that the associated token configuration has a token_lock_amount > token_lock_min_amount + let is_token_lock_amount_greater_than_token_lock_min_amount = Self::token_lock_amount_greater_than_token_lock_min_amount(mining_config_token_id).is_ok(); + ensure!(is_token_lock_amount_greater_than_token_lock_min_amount, "token configuration does not have a token_lock_amount > token_lock_min_amount"); + + // Check if a mining_config_token_execution_result already exists with the given mining_config_token_id + // to determine whether to insert new or mutate existing. + if Self::has_value_for_mining_config_token_execution_result_index(mining_config_token_id).is_ok() { + debug::info!("Mutating values"); + >::mutate((mining_config_token_id), |mining_config_token_execution_result| { + if let Some(_mining_config_token_execution_result) = mining_config_token_execution_result { + // Only update the value of a key in a KV pair if the corresponding parameter value has been provided + _mining_config_token_execution_result.token_execution_executor_account_id = token_execution_executor_account_id.clone(); + _mining_config_token_execution_result.token_execution_started_block = token_execution_started_block.clone(); + _mining_config_token_execution_result.token_execution_interval_blocks = token_execution_interval_blocks.clone(); + } + }); + debug::info!("Checking mutated values"); + let fetched_mining_config_token_execution_result = >::get((mining_config_token_id)); + if let Some(_mining_config_token_execution_result) = fetched_mining_config_token_execution_result { + debug::info!("Latest field token_execution_executor_account_id {:#?}", _mining_config_token_execution_result.token_execution_executor_account_id); + debug::info!("Latest field token_execution_started_block {:#?}", _mining_config_token_execution_result.token_execution_started_block); + debug::info!("Latest field token_execution_interval_blocks {:#?}", _mining_config_token_execution_result.token_execution_interval_blocks); + } + } else { + debug::info!("Inserting values"); + + // Create a new mining mining_config_token_execution_result instance with the input params + let mining_config_token_execution_result_instance = MiningConfigTokenExecutionResult { + // Since each parameter passed into the function is optional (i.e. `Option`) + // we will assign a default value if a parameter value is not provided. + token_execution_executor_account_id: token_execution_executor_account_id.clone(), + token_execution_started_block: token_execution_started_block.clone(), + token_execution_interval_blocks: token_execution_interval_blocks.clone(), + }; + + >::insert( + mining_config_token_id, + &mining_config_token_execution_result_instance + ); + + debug::info!("Checking inserted values"); + let fetched_mining_config_token_execution_result = >::get(mining_config_token_id); + if let Some(_mining_config_token_execution_result) = fetched_mining_config_token_execution_result { + debug::info!("Inserted field token_execution_executor_account_id {:#?}", _mining_config_token_execution_result.token_execution_executor_account_id); + debug::info!("Inserted field token_execution_started_block {:#?}", _mining_config_token_execution_result.token_execution_started_block); + debug::info!("Inserted field token_execution_interval_blocks {:#?}", _mining_config_token_execution_result.token_execution_interval_blocks); + } + } + + Self::deposit_event(RawEvent::MiningConfigTokenExecutionResultSet( + sender.clone(), + mining_config_token_id, + token_execution_executor_account_id.clone(), + token_execution_started_block, + token_execution_interval_blocks, + )); + + if Self::execution( + sender.clone(), + mining_config_token_id, + token_execution_executor_account_id.clone(), + token_execution_started_block, + token_execution_interval_blocks, + ).is_ok() { + debug::info!("Executed"); + } else { + debug::info!("Cannot execute"); + } + } } } @@ -374,6 +518,103 @@ impl Module { } } + // Check that the token execution has a token_execution_started_block > current_block + pub fn token_execution_started_block_greater_than_current_block( + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + // Check that the extrinsic call is made after the start date defined in the provided configuration + + let current_block = >::block_number(); + // Get the config associated with the given configuration_token + if let Some(configuration_token_config) = + Self::mining_config_token_token_configs(mining_config_token_id) + { + if let _token_lock_start_block = configuration_token_config.token_lock_start_block { + ensure!( + current_block > _token_lock_start_block, + "Execution may not be made until after the start block of the lock period in the configuration" + ); + Ok(()) + } else { + return Err(DispatchError::Other("Cannot find token_config start_date associated with the execution")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + } + + // Check that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks + pub fn token_lock_interval_blocks_greater_than_token_lock_min_blocks( + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + if let Some(configuration_token) = + Self::mining_config_token_token_configs((mining_config_token_id)) + { + if let Some(cooldown_configuration_token) = + Self::mining_config_token_token_cooldown_configs((mining_config_token_id)) + { + if let token_lock_interval_blocks = configuration_token.token_lock_interval_blocks { + if let token_lock_min_blocks = cooldown_configuration_token.token_lock_min_blocks { + ensure!( + token_lock_interval_blocks > token_lock_min_blocks, + "Lock period must be longer than the minimum lock period of the cooldown config. Cannot \ + execute." + ); + Ok(()) + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_min_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_interval_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + } + + // Check that the associated token configuration has a token_lock_amount > token_lock_min_amount + pub fn token_lock_amount_greater_than_token_lock_min_amount( + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + if let Some(configuration_token) = + Self::mining_config_token_token_configs((mining_config_token_id)) + { + if let Some(cooldown_configuration_token) = + Self::mining_config_token_token_cooldown_configs((mining_config_token_id)) + { + if let locked_amount = configuration_token.token_lock_amount { + if let lock_min_amount = cooldown_configuration_token.token_lock_min_amount { + ensure!( + locked_amount > lock_min_amount, + "Locked amount must be larger than the minimum locked amount of the cooldown config. \ + Cannot execute." + ); + Ok(()) + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_min_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_interval_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + } + pub fn has_value_for_mining_config_token_token_config_index( mining_config_token_id: T::MiningConfigTokenIndex, ) -> Result<(), DispatchError> { @@ -401,6 +642,37 @@ impl Module { Err(DispatchError::Other("No value for mining_config_token_token_cooldown_config")) } + pub fn has_value_for_mining_config_token_execution_result_index( + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + debug::info!("Checking if mining_config_token_execution_result has a value that is defined"); + let fetched_mining_config_token_execution_result = + >::get(mining_config_token_id); + if let Some(_value) = fetched_mining_config_token_execution_result { + debug::info!("Found value for mining_config_token_execution_result"); + return Ok(()); + } + debug::info!("No value for mining_config_token_execution_result"); + Err(DispatchError::Other("No value for mining_config_token_execution_result")) + } + + pub fn execution( + sender: T::AccountId, + mining_config_token_id: T::MiningConfigTokenIndex, + _token_execution_executor_account_id: T::AccountId, + _token_execution_started_block: T::BlockNumber, + _token_execution_interval_blocks: T::BlockNumber, + ) -> Result<(), DispatchError> { + return Ok(()); + + // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module + + // TODO - Setup a function in on_finalize that automatically checks through all the accounts that have + // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to + // determine their elegibility, and perform the claim for reward and unlock their tokens + // TODO - Update tests for the above + } + fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( T::Randomness::random(&[0]), diff --git a/pallets/mining/execution/token/Cargo.toml b/pallets/mining/execution/token/Cargo.toml deleted file mode 100644 index dc848a6f1..000000000 --- a/pallets/mining/execution/token/Cargo.toml +++ /dev/null @@ -1,50 +0,0 @@ -[package] -name = "mining-execution-token" -version = "0.1.0" -authors = ["Luke Schoen"] -edition = "2018" - -[features] -default = ['std'] -std = [ - 'codec/std', - 'frame-support/std', - 'frame-system/std', - 'safe-mix/std', - 'pallet-balances/std', - 'pallet-randomness-collective-flip/std', - 'pallet-transaction-payment/std', - 'sp-core/std', - 'sp-io/std', - 'sp-runtime/std', - 'sp-std/std', - - 'roaming-operators/std', - 'mining-config-token/std', - 'mining-eligibility-token/std', - 'mining-rates-token/std', - 'mining-sampling-token/std', - 'mining-claims-token/std', -] - -[dependencies] -codec = { version = '1.2.0', package = 'parity-scale-codec', default-features = false, features = ['derive'] } -safe-mix = { version = '1.0.0', default-features = false } -frame-support = { version = '2.0.0', default_features = false } -frame-system = { version = '2.0.0', default_features = false } -pallet-balances = { version = '2.0.0', default_features = false } -pallet-randomness-collective-flip = { version = '2.0.0', default_features = false } -pallet-transaction-payment = { version = '2.0.0', default_features = false } -sp-core = { version = '2.0.0', default_features = false } -sp-io = { version = '2.0.0', default_features = false } -sp-runtime = { version = '2.0.0', default_features = false } -sp-std = { version = '2.0.0', default_features = false } - -roaming-operators = { default_features = false, package = 'roaming-operators', path = '../../../roaming/roaming-operators' } -mining-config-token = { default_features = false, package = 'mining-config-token', path = '../../../mining/config/token' } -mining-eligibility-token = { default_features = false, package = 'mining-eligibility-token', path = '../../../mining/eligibility/token' } -mining-rates-token = { default_features = false, package = 'mining-rates-token', path = '../../../mining/rates/token' } -mining-sampling-token = { default_features = false, package = 'mining-sampling-token', path = '../../../mining/sampling/token' } -mining-claims-token = { default_features = false, package = 'mining-claims-token', path = '../../../mining/claims/token' } - -[dev-dependencies] diff --git a/pallets/mining/execution/token/src/lib.rs b/pallets/mining/execution/token/src/lib.rs deleted file mode 100644 index 7214ae56c..000000000 --- a/pallets/mining/execution/token/src/lib.rs +++ /dev/null @@ -1,562 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -use codec::{ - Decode, - Encode, -}; -use frame_support::{ - debug, - decl_event, - decl_module, - decl_storage, - ensure, - traits::{ - Get, - Randomness, - }, - Parameter, -}; -use frame_system::ensure_signed; -use sp_io::hashing::blake2_128; -use sp_runtime::{ - traits::{ - AtLeast32Bit, - Bounded, - Member, - One, - }, - DispatchError, -}; -use sp_std::prelude::*; // Imports Vec - -// FIXME - remove roaming_operators here, only use this approach since do not know how to use BalanceOf using only -// mining runtime module -use mining_claims_token; -use mining_config_token; -use mining_eligibility_token; -use mining_rates_token; -use mining_sampling_token; - -// #[cfg(test)] -// mod mock; - -// #[cfg(test)] -// mod tests; - -/// The module's configuration trait. -pub trait Trait: - frame_system::Trait - + roaming_operators::Trait - + mining_config_token::Trait - + mining_eligibility_token::Trait - + mining_rates_token::Trait - + mining_sampling_token::Trait - + mining_claims_token::Trait -{ - type Event: From> + Into<::Event>; - type MiningExecutionTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; - // type MiningExecutionTokenExecutorAccountID: Parameter - // + Member - // + AtLeast32Bit - // + Bounded - // + Default - // + Copy; -} - -// type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; - -#[derive(Encode, Decode, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "std", derive(Debug))] -pub struct MiningExecutionToken(pub [u8; 16]); - -#[cfg_attr(feature = "std", derive(Debug))] -#[derive(Encode, Decode, Default, Clone, PartialEq)] -pub struct MiningExecutionTokenExecutionResult { - pub token_execution_executor_account_id: U, - pub token_execution_started_block: V, - pub token_execution_ended_block: W, -} - -decl_event!( - pub enum Event where - ::AccountId, - ::MiningExecutionTokenIndex, - // ::MiningExecutionTokenExecutorAccountID, - ::MiningConfigTokenIndex, - ::BlockNumber, - // Balance = BalanceOf, - { - /// A mining_execution_token is created. (owner, mining_execution_token_id) - Created(AccountId, MiningExecutionTokenIndex), - /// A mining_execution_token is transferred. (from, to, mining_execution_token_id) - Transferred(AccountId, AccountId, MiningExecutionTokenIndex), - MiningExecutionTokenExecutionResultSet( - AccountId, MiningConfigTokenIndex, MiningExecutionTokenIndex, - AccountId, BlockNumber, BlockNumber - ), - /// A mining_execution_token is assigned to an mining_token. - /// (owner of mining_token, mining_execution_token_id, mining_config_token_id) - AssignedTokenExecutionToConfiguration(AccountId, MiningExecutionTokenIndex, MiningConfigTokenIndex), - } -); - -// This module's storage items. -decl_storage! { - trait Store for Module as MiningExecutionToken { - /// Stores all the mining_execution_tokens, key is the mining_execution_token id / index - pub MiningExecutionTokens get(fn mining_execution_token): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; - - /// Stores the total number of mining_execution_tokens. i.e. the next mining_execution_token index - pub MiningExecutionTokenCount get(fn mining_execution_token_count): T::MiningExecutionTokenIndex; - - /// Stores mining_execution_token owner - pub MiningExecutionTokenOwners get(fn mining_execution_token_owner): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; - - /// Stores mining_execution_token_execution_result - pub MiningExecutionTokenExecutionResults get(fn mining_execution_token_execution_results): map hasher(opaque_blake2_256) (T::MiningConfigTokenIndex, T::MiningExecutionTokenIndex) => - Option>; - - /// Get mining_config_token_id belonging to a mining_execution_token_id - pub TokenExecutionConfiguration get(fn token_execution_configuration): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; - - /// Get mining_execution_token_id's belonging to a mining_config_token_id - pub TokenConfigExecution get(fn token_config_execution): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option> - } -} - -// The module's dispatchable functions. -decl_module! { - /// The module declaration. - pub struct Module for enum Call where origin: T::Origin { - fn deposit_event() = default; - - /// Create a new mining mining_execution_token - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn create(origin) { - let sender = ensure_signed(origin)?; - let mining_execution_token_id = Self::next_mining_execution_token_id()?; - - // Generate a random 128bit value - let unique_id = Self::random_value(&sender); - - // Create and store mining_execution_token - let mining_execution_token = MiningExecutionToken(unique_id); - Self::insert_mining_execution_token(&sender, mining_execution_token_id, mining_execution_token); - - Self::deposit_event(RawEvent::Created(sender, mining_execution_token_id)); - } - - /// Transfer a mining_execution_token to new owner - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn transfer(origin, to: T::AccountId, mining_execution_token_id: T::MiningExecutionTokenIndex) { - let sender = ensure_signed(origin)?; - - ensure!(Self::mining_execution_token_owner(mining_execution_token_id) == Some(sender.clone()), "Only owner can transfer mining mining_execution_token"); - - Self::update_owner(&to, mining_execution_token_id); - - Self::deposit_event(RawEvent::Transferred(sender, to, mining_execution_token_id)); - } - - /// Set mining_execution_token_execution_result - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn set_mining_execution_token_execution_result( - origin, - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - _token_execution_started_block: Option, - _token_execution_ended_block: Option, - ) { - let sender = ensure_signed(origin)?; - - // Ensure that the mining_execution_token_id whose config we want to change actually exists - let is_mining_execution_token = Self::exists_mining_execution_token(mining_execution_token_id).is_ok(); - ensure!(is_mining_execution_token, "MiningExecutionToken does not exist"); - - // Ensure that the caller is owner of the mining_execution_token_execution_result they are trying to change - ensure!(Self::mining_execution_token_owner(mining_execution_token_id) == Some(sender.clone()), "Only owner can set mining_execution_token_execution_result"); - - // Check that only allow the owner of the configuration that the execution belongs to call this extrinsic to set and execute - ensure!( - >::is_mining_config_token_owner( - mining_config_token_id, sender.clone() - ).is_ok(), - "Only the configuration_token owner can execute their associated execution" - ); - - // TODO - adjust defaults - let token_execution_executor_account_id = sender.clone(); - let token_execution_started_block = match _token_execution_started_block.clone() { - Some(value) => value, - None => >::block_number() - }; - let token_execution_ended_block = match _token_execution_ended_block { - Some(value) => value, - None => >::block_number() + 1.into() // Default - }; - - // FIXME - // // Ensure that the associated token configuration has a token_execution_started_block > current_block - // let is_token_execution_started_block_greater_than_current_block = Self::token_execution_started_block_greater_than_current_block(mining_execution_token_id, mining_config_token_id).is_ok(); - // ensure!(is_token_execution_started_block_greater_than_current_block, "token execution does not have a token_execution_started_block > current_block"); - - // FIXME - // // Ensure that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks - // let is_token_lock_interval_blocks_greater_than_token_lock_min_blocks = Self::token_lock_interval_blocks_greater_than_token_lock_min_blocks(mining_execution_token_id, mining_config_token_id).is_ok(); - // ensure!(is_token_lock_interval_blocks_greater_than_token_lock_min_blocks, "token configuration does not have a token_lock_interval_blocks > token_lock_min_blocks"); - - // Ensure that the associated token configuration has a token_lock_amount > token_lock_min_amount - let is_token_lock_amount_greater_than_token_lock_min_amount = Self::token_lock_amount_greater_than_token_lock_min_amount(mining_execution_token_id, mining_config_token_id).is_ok(); - ensure!(is_token_lock_amount_greater_than_token_lock_min_amount, "token configuration does not have a token_lock_amount > token_lock_min_amount"); - - // Check if a mining_execution_token_execution_result already exists with the given mining_execution_token_id - // to determine whether to insert new or mutate existing. - if Self::has_value_for_mining_execution_token_execution_result_index(mining_config_token_id, mining_execution_token_id).is_ok() { - debug::info!("Mutating values"); - >::mutate((mining_config_token_id, mining_execution_token_id), |mining_execution_token_execution_result| { - if let Some(_mining_execution_token_execution_result) = mining_execution_token_execution_result { - // Only update the value of a key in a KV pair if the corresponding parameter value has been provided - _mining_execution_token_execution_result.token_execution_executor_account_id = token_execution_executor_account_id.clone(); - _mining_execution_token_execution_result.token_execution_started_block = token_execution_started_block.clone(); - _mining_execution_token_execution_result.token_execution_ended_block = token_execution_ended_block.clone(); - } - }); - debug::info!("Checking mutated values"); - let fetched_mining_execution_token_execution_result = >::get((mining_config_token_id, mining_execution_token_id)); - if let Some(_mining_execution_token_execution_result) = fetched_mining_execution_token_execution_result { - debug::info!("Latest field token_execution_executor_account_id {:#?}", _mining_execution_token_execution_result.token_execution_executor_account_id); - debug::info!("Latest field token_execution_started_block {:#?}", _mining_execution_token_execution_result.token_execution_started_block); - debug::info!("Latest field token_execution_ended_block {:#?}", _mining_execution_token_execution_result.token_execution_ended_block); - } - } else { - debug::info!("Inserting values"); - - // Create a new mining mining_execution_token_execution_result instance with the input params - let mining_execution_token_execution_result_instance = MiningExecutionTokenExecutionResult { - // Since each parameter passed into the function is optional (i.e. `Option`) - // we will assign a default value if a parameter value is not provided. - token_execution_executor_account_id: token_execution_executor_account_id.clone(), - token_execution_started_block: token_execution_started_block.clone(), - token_execution_ended_block: token_execution_ended_block.clone(), - }; - - >::insert( - (mining_config_token_id, mining_execution_token_id), - &mining_execution_token_execution_result_instance - ); - - debug::info!("Checking inserted values"); - let fetched_mining_execution_token_execution_result = >::get((mining_config_token_id, mining_execution_token_id)); - if let Some(_mining_execution_token_execution_result) = fetched_mining_execution_token_execution_result { - debug::info!("Inserted field token_execution_executor_account_id {:#?}", _mining_execution_token_execution_result.token_execution_executor_account_id); - debug::info!("Inserted field token_execution_started_block {:#?}", _mining_execution_token_execution_result.token_execution_started_block); - debug::info!("Inserted field token_execution_ended_block {:#?}", _mining_execution_token_execution_result.token_execution_ended_block); - } - } - - Self::deposit_event(RawEvent::MiningExecutionTokenExecutionResultSet( - sender.clone(), - mining_config_token_id, - mining_execution_token_id, - token_execution_executor_account_id.clone(), - token_execution_started_block, - token_execution_ended_block, - )); - - - - if Self::execution( - sender.clone(), - mining_config_token_id, - mining_execution_token_id, - token_execution_executor_account_id.clone(), - token_execution_started_block, - token_execution_ended_block, - ).is_ok() { - debug::info!("Executed"); - } else { - debug::info!("Cannot execute"); - } - } - - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn assign_execution_to_configuration( - origin, - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex - ) { - let sender = ensure_signed(origin)?; - - // Ensure that the given configuration id already exists - let is_configuration_token = > - ::exists_mining_config_token(mining_config_token_id).is_ok(); - ensure!(is_configuration_token, "configuration_token does not exist"); - - // Ensure that caller of the function is the owner of the configuration id to assign the execution to - ensure!( - >::is_mining_config_token_owner(mining_config_token_id, sender.clone()).is_ok(), - "Only the configuration_token owner can assign itself a execution" - ); - - Self::associate_token_execution_with_configuration(mining_execution_token_id, mining_config_token_id) - .expect("Unable to associate execution with configuration"); - - // Ensure that the given mining_execution_token_id already exists - let token_execution = Self::mining_execution_token(mining_execution_token_id); - ensure!(token_execution.is_some(), "Invalid mining_execution_token_id"); - - // // Ensure that the execution is not already owned by a different configuration - // // Unassign the execution from any existing configuration since it may only be owned by one configuration - // >::remove(mining_execution_token_id); - - // Assign the network owner to the given operator (even if already belongs to them) - >::insert(mining_execution_token_id, mining_config_token_id); - - Self::deposit_event(RawEvent::AssignedTokenExecutionToConfiguration(sender, mining_execution_token_id, mining_config_token_id)); - } - } -} - -impl Module { - pub fn is_mining_execution_token_owner( - mining_execution_token_id: T::MiningExecutionTokenIndex, - sender: T::AccountId, - ) -> Result<(), DispatchError> { - ensure!( - Self::mining_execution_token_owner(&mining_execution_token_id) - .map(|owner| owner == sender) - .unwrap_or(false), - "Sender is not owner of MiningExecutionToken" - ); - Ok(()) - } - - pub fn exists_mining_execution_token( - mining_execution_token_id: T::MiningExecutionTokenIndex, - ) -> Result { - match Self::mining_execution_token(mining_execution_token_id) { - Some(value) => Ok(value), - None => Err(DispatchError::Other("MiningExecutionToken does not exist")), - } - } - - pub fn exists_mining_execution_token_execution_result( - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - ) -> Result<(), DispatchError> { - match Self::mining_execution_token_execution_results((mining_config_token_id, mining_execution_token_id)) { - Some(_value) => Ok(()), - None => Err(DispatchError::Other("MiningExecutionTokenExecutionResult does not exist")), - } - } - - // Check that the token execution has a token_execution_started_block > current_block - pub fn token_execution_started_block_greater_than_current_block( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - // Check that the extrinsic call is made after the start date defined in the provided configuration - - let current_block = >::block_number(); - // Get the config associated with the given configuration_token - if let Some(configuration_token_config) = - >::mining_config_token_token_configs(mining_config_token_id) - { - if let _token_lock_start_block = configuration_token_config.token_lock_start_block { - ensure!( - current_block > _token_lock_start_block, - "Execution may not be made until after the start block of the lock period in the configuration" - ); - Ok(()) - } else { - return Err(DispatchError::Other("Cannot find token_config start_date associated with the execution")); - } - } else { - return Err(DispatchError::Other("Cannot find token_config associated with the execution")); - } - } - - // Check that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks - pub fn token_lock_interval_blocks_greater_than_token_lock_min_blocks( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - if let Some(configuration_token) = - >::mining_config_token_token_configs((mining_config_token_id)) - { - if let Some(cooldown_configuration_token) = - >::mining_config_token_token_cooldown_configs((mining_config_token_id)) - { - if let token_lock_interval_blocks = configuration_token.token_lock_interval_blocks { - if let token_lock_min_blocks = cooldown_configuration_token.token_lock_min_blocks { - ensure!( - token_lock_interval_blocks > token_lock_min_blocks, - "Lock period must be longer than the minimum lock period of the cooldown config. Cannot \ - execute." - ); - Ok(()) - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_min_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_interval_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); - } - } else { - return Err(DispatchError::Other("Cannot find token_config associated with the execution")); - } - } - - // Check that the associated token configuration has a token_lock_amount > token_lock_min_amount - pub fn token_lock_amount_greater_than_token_lock_min_amount( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - if let Some(configuration_token) = - >::mining_config_token_token_configs((mining_config_token_id)) - { - if let Some(cooldown_configuration_token) = - >::mining_config_token_token_cooldown_configs((mining_config_token_id)) - { - if let locked_amount = configuration_token.token_lock_amount { - if let lock_min_amount = cooldown_configuration_token.token_lock_min_amount { - ensure!( - locked_amount > lock_min_amount, - "Locked amount must be larger than the minimum locked amount of the cooldown config. \ - Cannot execute." - ); - Ok(()) - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_min_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_interval_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); - } - } else { - return Err(DispatchError::Other("Cannot find token_config associated with the execution")); - } - } - - pub fn execution( - sender: T::AccountId, - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - _token_execution_executor_account_id: T::AccountId, - _token_execution_started_block: T::BlockNumber, - _token_execution_ended_block: T::BlockNumber, - ) -> Result<(), DispatchError> { - return Ok(()); - - // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module - - // TODO - Setup a function in on_finalize that automatically checks through all the accounts that have - // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to - // determine their elegibility, and perform the claim for reward and unlock their tokens - // TODO - Update tests for the above - } - - pub fn has_value_for_mining_execution_token_execution_result_index( - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - ) -> Result<(), DispatchError> { - debug::info!("Checking if mining_execution_token_execution_result has a value that is defined"); - let fetched_mining_execution_token_execution_result = - >::get((mining_config_token_id, mining_execution_token_id)); - if let Some(_value) = fetched_mining_execution_token_execution_result { - debug::info!("Found value for mining_execution_token_execution_result"); - return Ok(()); - } - debug::info!("No value for mining_execution_token_execution_result"); - Err(DispatchError::Other("No value for mining_execution_token_execution_result")) - } - - /// Only push the execution id onto the end of the vector if it does not already exist - pub fn associate_token_execution_with_configuration( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - // Early exit with error since do not want to append if the given configuration id already exists as a key, - // and where its corresponding value is a vector that already contains the given execution id - if let Some(configuration_execution) = Self::token_config_execution(mining_config_token_id) { - debug::info!( - "Configuration id key {:?} exists with value {:?}", - mining_config_token_id, - configuration_execution - ); - let not_configuration_contains_execution = !configuration_execution.contains(&mining_execution_token_id); - ensure!(not_configuration_contains_execution, "Configuration already contains the given execution id"); - debug::info!("Configuration id key exists but its vector value does not contain the given execution id"); - >::mutate(mining_config_token_id, |v| { - if let Some(value) = v { - value.push(mining_execution_token_id); - } - }); - debug::info!( - "Associated execution {:?} with configuration {:?}", - mining_execution_token_id, - mining_config_token_id - ); - Ok(()) - } else { - debug::info!( - "Configuration id key does not yet exist. Creating the configuration key {:?} and appending the \ - execution id {:?} to its vector value", - mining_config_token_id, - mining_execution_token_id - ); - >::insert(mining_config_token_id, &vec![mining_execution_token_id]); - Ok(()) - } - } - - fn random_value(sender: &T::AccountId) -> [u8; 16] { - let payload = ( - T::Randomness::random(&[0]), - sender, - >::extrinsic_index(), - >::block_number(), - ); - payload.using_encoded(blake2_128) - } - - fn next_mining_execution_token_id() -> Result { - let mining_execution_token_id = Self::mining_execution_token_count(); - if mining_execution_token_id == ::max_value() { - return Err(DispatchError::Other("MiningExecutionToken count overflow")); - } - Ok(mining_execution_token_id) - } - - fn insert_mining_execution_token( - owner: &T::AccountId, - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_execution_token: MiningExecutionToken, - ) { - // Create and store mining mining_execution_token - >::insert(mining_execution_token_id, mining_execution_token); - >::put(mining_execution_token_id + One::one()); - >::insert(mining_execution_token_id, owner.clone()); - } - - fn update_owner(to: &T::AccountId, mining_execution_token_id: T::MiningExecutionTokenIndex) { - >::insert(mining_execution_token_id, to); - } -} diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index ac9b77a35..7f7fa54e7 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -64,7 +64,6 @@ std = [ 'mining-eligibility-hardware/std', 'mining-claims-token/std', 'mining-claims-hardware/std', - 'mining-execution-token/std', ] [dependencies] @@ -129,7 +128,6 @@ mining-eligibility-token = { default_features = false, package = 'mining-eligibi mining-eligibility-hardware = { default_features = false, package = 'mining-eligibility-hardware', path = '../pallets/mining/eligibility/hardware' } mining-claims-token = { default_features = false, package = 'mining-claims-token', path = '../pallets/mining/claims/token' } mining-claims-hardware = { default_features = false, package = 'mining-claims-hardware', path = '../pallets/mining/claims/hardware' } -mining-execution-token = { default_features = false, package = 'mining-execution-token', path = '../pallets/mining/execution/token' } module-primitives = { default-features = false, path = '../pallets/primitives' } [build-dependencies] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 86d67da6b..7d4131339 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -700,11 +700,6 @@ impl mining_claims_hardware::Trait for Runtime { type MiningClaimsHardwareIndex = u64; } -impl mining_execution_token::Trait for Runtime { - type Event = Event; - type MiningExecutionTokenIndex = u64; -} - // Create the runtime by composing the FRAME pallets that were previously configured. construct_runtime!( pub enum Runtime where @@ -751,7 +746,6 @@ construct_runtime!( MiningEligibilityHardware: mining_eligibility_hardware::{Module, Call, Storage, Event}, MiningClaimsToken: mining_claims_token::{Module, Call, Storage, Event}, MiningClaimsHardware: mining_claims_hardware::{Module, Call, Storage, Event}, - MiningExecutionToken: mining_execution_token::{Module, Call, Storage, Event}, } ); diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index bfee5786b..840a75516 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -2,7 +2,6 @@ extern crate mining_claims_token as mining_claims_token; extern crate mining_config_token as mining_config_token; extern crate mining_eligibility_token as mining_eligibility_token; -extern crate mining_execution_token as mining_execution_token; extern crate mining_rates_token as mining_rates_token; extern crate mining_sampling_token as mining_sampling_token; extern crate roaming_operators as roaming_operators; @@ -50,11 +49,6 @@ mod tests { Module as MiningEligibilityTokenModule, Trait as MiningEligibilityTokenTrait, }; - use mining_execution_token::{ - MiningExecutionTokenExecutionResult, - Module as MiningExecutionTokenModule, - Trait as MiningExecutionTokenTrait, - }; use mining_rates_token::{ MiningRatesTokenConfig, Module as MiningRatesTokenModule, @@ -428,33 +422,29 @@ mod tests { }) ); - // Create Mining Speed Boost Execution Token Mining - - // Call Functions - assert_ok!(MiningExecutionTokenTestModule::create(Origin::signed(0))); - assert_ok!(MiningExecutionTokenTestModule::assign_execution_to_configuration(Origin::signed(0), 0, 0)); + // Create Mining Speed Boost Config Token Mining // Override by DAO if necessary // // Execute is called to start the mining if all checks pass - assert_ok!(MiningExecutionTokenTestModule::set_mining_execution_token_execution_result( + assert_ok!(MiningConfigTokenTestModule::set_mining_config_token_execution_result( Origin::signed(0), 0, // mining_config_token_id - 0, // mining_execution_token_id + 0, // mining_config_token_id Some(12345), // token_execution_started_block - Some(34567) // token_execution_ended_block + Some(34567) // token_execution_interval_blocks )); // Verify Storage - assert_eq!(MiningExecutionTokenTestModule::mining_execution_token_count(), 1); - assert!(MiningExecutionTokenTestModule::mining_execution_token(0).is_some()); - assert_eq!(MiningExecutionTokenTestModule::mining_execution_token_owner(0), Some(0)); + assert_eq!(MiningConfigTokenTestModule::mining_config_token_count(), 1); + assert!(MiningConfigTokenTestModule::mining_config_token(0).is_some()); + assert_eq!(MiningConfigTokenTestModule::mining_config_token_owner(0), Some(0)); assert_eq!( - MiningExecutionTokenTestModule::mining_execution_token_execution_results((0, 0)), - Some(MiningExecutionTokenExecutionResult { + MiningConfigTokenTestModule::mining_config_token_execution_results((0, 0)), + Some(MiningConfigTokenExecutionResult { token_execution_executor_account_id: 0, token_execution_started_block: 12345, - token_execution_ended_block: 34567, + token_execution_interval_blocks: 34567, }) ); // TODO - check that the locked amount has actually been locked and check that a sampling, eligibility, and From 23cc7c5747bd5ef2666260a6324cc7506d8afe37 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Mon, 1 Mar 2021 10:52:21 +0100 Subject: [PATCH 02/27] refactor: Rename without duplicate _token --- EXAMPLES.md | 3 ++- pallets/mining/claims/token/src/lib.rs | 2 +- pallets/mining/config/token/src/lib.rs | 12 ++++++------ pallets/mining/eligibility/token/src/lib.rs | 2 +- runtime/tests/cli_integration_tests_mining_tokens.rs | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/EXAMPLES.md b/EXAMPLES.md index 204cb2b99..3edf66b8f 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -46,7 +46,8 @@ rm -rf /tmp/polkadot-chains/alice /tmp/polkadot-chains/bob /tmp/polkadot-chains/ --base-path /tmp/polkadot-chains/alice \ --name "Data Highway Development Chain" \ --dev \ - --telemetry-url "wss://telemetry.polkadot.io/submit/ 0" + --telemetry-url "wss://telemetry.polkadot.io/submit/ 0" \ + -lruntime=debug ``` ## Example "local" PoS testnet (with multiple nodes) diff --git a/pallets/mining/claims/token/src/lib.rs b/pallets/mining/claims/token/src/lib.rs index 15cc230c8..f56cc823b 100644 --- a/pallets/mining/claims/token/src/lib.rs +++ b/pallets/mining/claims/token/src/lib.rs @@ -184,7 +184,7 @@ decl_module! { // FIXME // let current_block = >::block_number(); // // Get the config associated with the given configuration_token - // if let Some(configuration_token_config) = >::mining_config_token_token_configs(mining_config_token_id) { + // if let Some(configuration_token_config) = >::mining_config_token_configs(mining_config_token_id) { // if let _token_lock_interval_blocks = configuration_token_config.token_lock_interval_blocks { // ensure!(current_block > _token_lock_interval_blocks, "Claim may not be made until after the end of the lock interval"); // } else { diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index df3873245..49835655d 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -119,7 +119,7 @@ decl_storage! { pub MiningConfigTokenOwners get(fn mining_config_token_owner): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option; /// Stores mining_config_token_token_config - pub MiningConfigTokenConfigs get(fn mining_config_token_token_configs): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => + pub MiningConfigTokenConfigs get(fn mining_config_token_configs): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option, T::BlockNumber, T::BlockNumber>>; /// Stores mining_config_token_token_cooldown_config @@ -390,7 +390,7 @@ decl_module! { let token_execution_interval_blocks; if let Some(configuration_token_config) = - Self::mining_config_token_token_configs(mining_config_token_id) + Self::mining_config_token_configs(mining_config_token_id) { if let _token_lock_start_block = configuration_token_config.token_lock_start_block { token_execution_started_block = _token_lock_start_block.clone(); @@ -512,7 +512,7 @@ impl Module { pub fn exists_mining_config_token_token_config( mining_config_token_id: T::MiningConfigTokenIndex, ) -> Result<(), DispatchError> { - match Self::mining_config_token_token_configs(mining_config_token_id) { + match Self::mining_config_token_configs(mining_config_token_id) { Some(_value) => Ok(()), None => Err(DispatchError::Other("MiningConfigTokenConfig does not exist")), } @@ -527,7 +527,7 @@ impl Module { let current_block = >::block_number(); // Get the config associated with the given configuration_token if let Some(configuration_token_config) = - Self::mining_config_token_token_configs(mining_config_token_id) + Self::mining_config_token_configs(mining_config_token_id) { if let _token_lock_start_block = configuration_token_config.token_lock_start_block { ensure!( @@ -548,7 +548,7 @@ impl Module { mining_config_token_id: T::MiningConfigTokenIndex, ) -> Result<(), DispatchError> { if let Some(configuration_token) = - Self::mining_config_token_token_configs((mining_config_token_id)) + Self::mining_config_token_configs((mining_config_token_id)) { if let Some(cooldown_configuration_token) = Self::mining_config_token_token_cooldown_configs((mining_config_token_id)) @@ -584,7 +584,7 @@ impl Module { mining_config_token_id: T::MiningConfigTokenIndex, ) -> Result<(), DispatchError> { if let Some(configuration_token) = - Self::mining_config_token_token_configs((mining_config_token_id)) + Self::mining_config_token_configs((mining_config_token_id)) { if let Some(cooldown_configuration_token) = Self::mining_config_token_token_cooldown_configs((mining_config_token_id)) diff --git a/pallets/mining/eligibility/token/src/lib.rs b/pallets/mining/eligibility/token/src/lib.rs index 7a9c1a8aa..75403ce08 100644 --- a/pallets/mining/eligibility/token/src/lib.rs +++ b/pallets/mining/eligibility/token/src/lib.rs @@ -192,7 +192,7 @@ decl_module! { // let mut current_token_type; // let mut current_token_lock_amount; // // Get the config associated with the given configuration_token - // if let Some(configuration_token_config) = >::mining_config_token_token_configs(mining_config_token_id) { + // if let Some(configuration_token_config) = >::mining_config_token_configs(mining_config_token_id) { // if let token_type = configuration_token_config.token_type { // if token_type != "".to_string() { // current_token_type = token_type.clone(); diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index 840a75516..910332722 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -279,7 +279,7 @@ mod tests { }) ); assert_eq!( - MiningConfigTokenTestModule::mining_config_token_token_configs(0), + MiningConfigTokenTestModule::mining_config_token_configs(0), Some(MiningConfigTokenConfig { token_type: b"MXC".to_vec(), // token_type token_lock_amount: 100, // token_lock_amount From 8e753b7d1563eef0671356bd18cfbb49de9325d8 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Mon, 1 Mar 2021 11:46:28 +0100 Subject: [PATCH 03/27] wip --- Cargo.lock | 1 + pallets/mining/config/token/Cargo.toml | 3 +++ pallets/mining/config/token/src/lib.rs | 22 +++++++++---------- runtime/src/lib.rs | 1 + .../cli_integration_tests_mining_tokens.rs | 9 +++++--- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4b15fe86c..8da1ec677 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3222,6 +3222,7 @@ dependencies = [ "pallet-balances", "pallet-randomness-collective-flip", "pallet-transaction-payment", + "pallet-treasury", "parity-scale-codec", "roaming-operators", "safe-mix", diff --git a/pallets/mining/config/token/Cargo.toml b/pallets/mining/config/token/Cargo.toml index 14d23431f..1c548ca8e 100644 --- a/pallets/mining/config/token/Cargo.toml +++ b/pallets/mining/config/token/Cargo.toml @@ -14,6 +14,8 @@ std = [ 'pallet-balances/std', 'pallet-randomness-collective-flip/std', 'pallet-transaction-payment/std', + 'pallet-treasury/std', + 'sp-core/std', 'sp-io/std', 'sp-runtime/std', @@ -30,6 +32,7 @@ frame-system = { version = '2.0.0', default_features = false } pallet-balances = { version = '2.0.0', default_features = false } pallet-randomness-collective-flip = { version = '2.0.0', default_features = false } pallet-transaction-payment = { version = '2.0.0', default_features = false } +pallet-treasury = { version = '2.0.0', default-features = false } sp-core = { version = '2.0.0', default_features = false } sp-io = { version = '2.0.0', default_features = false } sp-runtime = { version = '2.0.0', default_features = false } diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 49835655d..eb682a90a 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -46,10 +46,10 @@ pub trait Trait: frame_system::Trait + roaming_operators::Trait { // Mining Speed Boost Token Mining Config type MiningConfigTokenType: Parameter + Member + Default; type MiningConfigTokenLockAmount: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type Currency: Currency; } -type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; +type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; #[derive(Encode, Decode, Clone, PartialEq, Eq)] #[cfg_attr(feature = "std", derive(Debug))] @@ -83,10 +83,10 @@ pub struct MiningConfigTokenExecutionResult { decl_event!( pub enum Event where - ::AccountId, + AccountId = ::AccountId, ::MiningConfigTokenIndex, ::MiningConfigTokenType, - ::BlockNumber, + BlockNumber = ::BlockNumber, Balance = BalanceOf, { /// A mining_config_token is created. (owner, mining_config_token_id) @@ -123,7 +123,7 @@ decl_storage! { Option, T::BlockNumber, T::BlockNumber>>; /// Stores mining_config_token_token_cooldown_config - pub MiningConfigTokenRequirementsConfigs get(fn mining_config_token_token_cooldown_configs): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => + pub MiningConfigTokenRequirementsConfigs get(fn mining_config_token_cooldown_configs): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option, T::BlockNumber>>; /// Stores mining_config_token_execution_result @@ -172,7 +172,7 @@ decl_module! { /// Set mining_config_token_token_config #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn set_mining_config_token_token_config( + pub fn set_mining_config_token_config( origin, mining_config_token_id: T::MiningConfigTokenIndex, _token_type: Option, @@ -278,7 +278,7 @@ decl_module! { /// Set mining_config_token_token_cooldown_config #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn set_mining_config_token_token_cooldown_config( + pub fn set_mining_config_token_cooldown_config( origin, mining_config_token_id: T::MiningConfigTokenIndex, _token_type: Option, @@ -551,7 +551,7 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let Some(cooldown_configuration_token) = - Self::mining_config_token_token_cooldown_configs((mining_config_token_id)) + Self::mining_config_token_cooldown_configs((mining_config_token_id)) { if let token_lock_interval_blocks = configuration_token.token_lock_interval_blocks { if let token_lock_min_blocks = cooldown_configuration_token.token_lock_min_blocks { @@ -587,12 +587,12 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let Some(cooldown_configuration_token) = - Self::mining_config_token_token_cooldown_configs((mining_config_token_id)) + Self::mining_config_token_cooldown_configs((mining_config_token_id)) { - if let locked_amount = configuration_token.token_lock_amount { + if let lock_amount = configuration_token.token_lock_amount { if let lock_min_amount = cooldown_configuration_token.token_lock_min_amount { ensure!( - locked_amount > lock_min_amount, + lock_amount > lock_min_amount, "Locked amount must be larger than the minimum locked amount of the cooldown config. \ Cannot execute." ); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 7d4131339..43506dcc7 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -608,6 +608,7 @@ impl roaming_packet_bundles::Trait for Runtime { } impl mining_config_token::Trait for Runtime { + type Currency = Balances; type Event = Event; // FIXME - restore when stop temporarily using roaming-operators // type Currency = Balances; diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index 910332722..ce22c109f 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -250,14 +250,14 @@ mod tests { // Call Functions assert_ok!(MiningConfigTokenTestModule::create(Origin::signed(0))); - assert_ok!(MiningConfigTokenTestModule::set_mining_config_token_token_cooldown_config( + assert_ok!(MiningConfigTokenTestModule::set_mining_config_token_cooldown_config( Origin::signed(0), 0, // mining_token_id Some(b"DHX".to_vec()), // token_type Some(10), // token_lock_min_amount Some(7), // token_lock_min_blocks )); - assert_ok!(MiningConfigTokenTestModule::set_mining_config_token_token_config( + assert_ok!(MiningConfigTokenTestModule::set_mining_config_token_config( Origin::signed(0), 0, // mining_token_id Some(b"MXC".to_vec()), // token_type @@ -271,7 +271,7 @@ mod tests { assert!(MiningConfigTokenTestModule::mining_config_token(0).is_some()); assert_eq!(MiningConfigTokenTestModule::mining_config_token_owner(0), Some(0)); assert_eq!( - MiningConfigTokenTestModule::mining_config_token_token_cooldown_configs(0), + MiningConfigTokenTestModule::mining_config_token_cooldown_configs(0), Some(MiningConfigTokenRequirementsConfig { token_type: b"DHX".to_vec(), // token_type token_lock_min_amount: 10, // token_lock_min_amount @@ -449,6 +449,9 @@ mod tests { ); // TODO - check that the locked amount has actually been locked and check that a sampling, eligibility, and // claim were all run automatically afterwards assert!(false); + + // TODO - allow user to request to stop mining, and trigger the cooldown period until they can + // access their tokens that are locked for mining. }); } } From 1f594c256746e34ebfe1f509a09696c93d06fdf6 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Tue, 2 Mar 2021 10:07:27 +0100 Subject: [PATCH 04/27] wip --- Cargo.lock | 1 + pallets/mining/config/token/Cargo.toml | 2 + pallets/mining/config/token/src/lib.rs | 149 ++++++++++++++++++++++++- runtime/src/lib.rs | 1 + 4 files changed, 152 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 8da1ec677..04b796b74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3219,6 +3219,7 @@ version = "0.1.0" dependencies = [ "frame-support", "frame-system", + "mining-rates-token", "pallet-balances", "pallet-randomness-collective-flip", "pallet-transaction-payment", diff --git a/pallets/mining/config/token/Cargo.toml b/pallets/mining/config/token/Cargo.toml index 1c548ca8e..24f7260c1 100644 --- a/pallets/mining/config/token/Cargo.toml +++ b/pallets/mining/config/token/Cargo.toml @@ -22,6 +22,7 @@ std = [ 'sp-std/std', 'roaming-operators/std', + 'mining-rates-token/std', ] [dependencies] @@ -39,5 +40,6 @@ sp-runtime = { version = '2.0.0', default_features = false } sp-std = { version = '2.0.0', default_features = false } roaming-operators = { default_features = false, package = 'roaming-operators', path = '../../../roaming/roaming-operators' } +mining-rates-token = { default_features = false, package = 'mining-rates-token', path = '../../../mining/rates/token' } [dev-dependencies] diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index eb682a90a..7978b516a 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -25,6 +25,7 @@ use sp_runtime::{ Bounded, Member, One, + Zero, }, DispatchError, }; @@ -40,7 +41,7 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + roaming_operators::Trait { +pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait { type Event: From> + Into<::Event>; type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; // Mining Speed Boost Token Mining Config @@ -142,6 +143,109 @@ decl_module! { pub struct Module for enum Call where origin: T::Origin { fn deposit_event() = default; + // TODO - automatically checks through all the accounts that have + // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to + // determine their elegibility, and perform the lodgement for reward and unlock their tokens + fn on_finalize(current_block_number: T::BlockNumber) { + debug::info!("execution/token-mining - on_finalize"); + debug::info!("current block number {:#?}", current_block_number); + + let config_token_count = Self::mining_config_token_count(); + + // FIXME - is there an upper bound on the size of these sets and + // the computation of this nested loop? what max size or custom weight function? + // See https://substrate.dev/recipes/map-set.html + // + // Loop through all mining_config_token_id + for idx_c in 0..config_token_count.into() { + let fetched_mining_execution_token_result = >::get(idx_c); + + if let Some(_mining_execution_token_result) = fetched_mining_execution_token_result { + debug::info!("token_execution_executor_account_id {:#?}", _mining_execution_token_result.token_execution_executor_account_id); + debug::info!("token_execution_started_block {:#?}", _mining_execution_token_result.token_execution_started_block); + debug::info!("token_execution_interval_blocks {:#?}", _mining_execution_token_result.token_execution_interval_blocks); + + let fetched_mining_config_token_cooldown_config = Self::mining_config_token_cooldown_configs(idx_c); + if let Some(_mining_config_token_cooldown_config) = fetched_mining_config_token_cooldown_config { + // debug::info!("token_type {:#?}", _mining_config_token_cooldown_config.token_type); + debug::info!("token_lock_min_blocks {:#?}", _mining_config_token_cooldown_config.token_lock_min_blocks); + + if let token_lock_min_blocks = _mining_config_token_cooldown_config.token_lock_min_blocks { + if let Some(configuration_token) = Self::mining_config_token_configs((idx_c)) { + if let token_lock_amount = configuration_token.token_lock_amount { + + // FIXME - remove hard-coded and integrate + // const MINING_REQUESTED_END_BLOCK = One::one() * 10; + + // If the end of the mining period has been reached, then stop giving them rewards, + // and unlock their bonded tokens after waiting the cooldown period of _mining_config_token_cooldown_config.token_lock_min_blocks + // after the end of their mining period. + + if >::block_number() >= _mining_execution_token_result.token_execution_interval_blocks { + if >::block_number() > _mining_execution_token_result.token_execution_interval_blocks + token_lock_min_blocks { + // if >::block_number() > MINING_REQUESTED_END_BLOCK + token_lock_min_blocks { + // TODO - Unlock the funds. Store updated status + // We only want to unlock the rewards they have earned. + // The amount of tokens that they originally locked to mine the rewards will remain locked until the end of their mining period + // (FIXME - or until they request to stop mining, which hasn't been implemented yet), + // and then they cannot move those locked tokens for the cooldown period and receive no further rewards. + + if let Some(mining_config_token) = Self::mining_config_token(idx_c) { + // ::Currency::remove_lock( + // mining_config_token, // where idx_c is mining_config_token_id + // &_mining_execution_token_result.token_execution_executor_account_id, + // ); + } + } + } else if >::block_number() < _mining_execution_token_result.token_execution_interval_blocks { + // Check if cooldown period has been reached before start distributing rewards. + // If so then we unlock and transfer the reward tokens to the user from the treasury. + // Reference: https://github.com/hicommonwealth/edgeware-node/blob/master/modules/edge-treasury-reward/src/lib.rs#L42 + if >::block_number() % token_lock_min_blocks == Zero::zero() { + // FIXME - assumes there is only one rates config index so hard-coded 0, but we could have many + let fetched_mining_rates_token_rates_config = >::mining_rates_token_rates_configs(0.into()); + if let Some(_mining_rates_token_rates_config) = fetched_mining_rates_token_rates_config { + debug::info!("token_execution_interval_blocks {:#?}", _mining_execution_token_result.token_execution_interval_blocks); + + // TODO - choose the token rate that corresponds to the _mining_config_token_cooldown_config.token_type + // and use this to determine the reward ratio. + // in the meantime until this is fixed, we will just assume the user is mining MXC and choose the rate for that + + // Reward ratio + let reward_ratio = _mining_rates_token_rates_config.token_token_mxc; + + // Calculate the reward based on the reward ratio (i.e. 1 DHX per 10 DHX that was locked) + // e.g. (1.1 - 1) * 10 DHX, where 1.1 is ratio of mining reward for the MXC token + + // let reward = (reward_ratio - 1.into()) * token_lock_amount; + + // Distribute the reward to the account that has locked the funds + + // ::Currency::transfer( + // &>::account_id(), + // &_mining_execution_token_result.token_execution_executor_account_id, + // reward, + // ExistenceRequirement::KeepAlive + // ); + + // Emit event since treasury unlocked locked tokens and rewarded customer the reward ratio + + // Self::deposit_event(RawEvent::TreasuryRewardTokenMiningPostCooldown( + // >::free_balance(_mining_execution_token_result.token_execution_executor_account_id), + // >::block_number(), + // _mining_execution_token_result.token_execution_executor_account_id + // )); + } + } + } + } + } + } + } + } + } + } + /// Create a new mining mining_config_token #[weight = 10_000 + T::DbWeight::get().writes(1)] pub fn create(origin) { @@ -420,6 +524,23 @@ decl_module! { let is_token_lock_amount_greater_than_token_lock_min_amount = Self::token_lock_amount_greater_than_token_lock_min_amount(mining_config_token_id).is_ok(); ensure!(is_token_lock_amount_greater_than_token_lock_min_amount, "token configuration does not have a token_lock_amount > token_lock_min_amount"); + Self::execution( + sender.clone(), + mining_config_token_id, + token_execution_executor_account_id.clone(), + token_execution_started_block, + token_execution_interval_blocks, + ); + debug::info!("Executed"); + + ensure!(Self::execution( + sender.clone(), + mining_config_token_id, + token_execution_executor_account_id.clone(), + token_execution_started_block, + token_execution_interval_blocks, + ).is_ok(), "Cannot execute"); + // Check if a mining_config_token_execution_result already exists with the given mining_config_token_id // to determine whether to insert new or mutate existing. if Self::has_value_for_mining_config_token_execution_result_index(mining_config_token_id).is_ok() { @@ -664,6 +785,7 @@ impl Module { _token_execution_interval_blocks: T::BlockNumber, ) -> Result<(), DispatchError> { return Ok(()); + // const EXAMPLE_ID: LockIdentifier = *b"example "; // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module @@ -671,6 +793,31 @@ impl Module { // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to // determine their elegibility, and perform the claim for reward and unlock their tokens // TODO - Update tests for the above + if let Some(configuration_token) = + Self::mining_config_token_configs((mining_config_token_id)) + { + if let lock_amount = configuration_token.token_lock_amount { + if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { + // ::Currency::set_lock( + // execution_token, // EXAMPLE_ID, + // &_token_execution_executor_account_id, + // lock_amount, + // WithdrawReasons::all(), + // ); + return Ok(()); + } else { + return Err(DispatchError::Other( + "Cannot find mining_config_token_id associated with the execution", + )); + } + } else { + return Err(DispatchError::Other( + "Cannot find token_mining_config with token_lock_period associated with the execution", + )); + } + } else { + return Err(DispatchError::Other("Cannot find token_mining_config associated with the execution")); + } } fn random_value(sender: &T::AccountId) -> [u8; 16] { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 43506dcc7..cad4d3631 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -609,6 +609,7 @@ impl roaming_packet_bundles::Trait for Runtime { impl mining_config_token::Trait for Runtime { type Currency = Balances; + type Randomness = RandomnessCollectiveFlip; type Event = Event; // FIXME - restore when stop temporarily using roaming-operators // type Currency = Balances; From 2922d521b35abc66f804b49024091931d0cc1d75 Mon Sep 17 00:00:00 2001 From: Ilya Beregovskiy Date: Tue, 2 Mar 2021 11:53:25 +0200 Subject: [PATCH 05/27] fixes --- pallets/mining/config/token/src/lib.rs | 7 ++++--- runtime/src/lib.rs | 1 - 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 7978b516a..5ebe263d9 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] +#![feature(step_trait)] use codec::{ Decode, @@ -43,7 +44,7 @@ mod tests; /// The module's configuration trait. pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait { type Event: From> + Into<::Event>; - type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + sp_std::iter::Step; // Mining Speed Boost Token Mining Config type MiningConfigTokenType: Parameter + Member + Default; type MiningConfigTokenLockAmount: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; @@ -157,7 +158,7 @@ decl_module! { // See https://substrate.dev/recipes/map-set.html // // Loop through all mining_config_token_id - for idx_c in 0..config_token_count.into() { + for idx_c in 0u32.into()..config_token_count { let fetched_mining_execution_token_result = >::get(idx_c); if let Some(_mining_execution_token_result) = fetched_mining_execution_token_result { @@ -203,7 +204,7 @@ decl_module! { // Reference: https://github.com/hicommonwealth/edgeware-node/blob/master/modules/edge-treasury-reward/src/lib.rs#L42 if >::block_number() % token_lock_min_blocks == Zero::zero() { // FIXME - assumes there is only one rates config index so hard-coded 0, but we could have many - let fetched_mining_rates_token_rates_config = >::mining_rates_token_rates_configs(0.into()); + let fetched_mining_rates_token_rates_config = >::mining_rates_token_rates_configs(0u32.into()); if let Some(_mining_rates_token_rates_config) = fetched_mining_rates_token_rates_config { debug::info!("token_execution_interval_blocks {:#?}", _mining_execution_token_result.token_execution_interval_blocks); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index cad4d3631..43506dcc7 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -609,7 +609,6 @@ impl roaming_packet_bundles::Trait for Runtime { impl mining_config_token::Trait for Runtime { type Currency = Balances; - type Randomness = RandomnessCollectiveFlip; type Event = Event; // FIXME - restore when stop temporarily using roaming-operators // type Currency = Balances; From 7fdc96d266a4293dcde4a0e1efac0f2dbbe56d18 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 10:03:45 +0100 Subject: [PATCH 06/27] wip --- pallets/mining/config/token/src/lib.rs | 23 +++++++++++++++-------- pallets/mining/rates/token/src/lib.rs | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 5ebe263d9..195301999 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -176,15 +176,15 @@ decl_module! { if let token_lock_amount = configuration_token.token_lock_amount { // FIXME - remove hard-coded and integrate - // const MINING_REQUESTED_END_BLOCK = One::one() * 10; + let MINING_REQUESTED_END_BLOCK: T::BlockNumber = 10.into(); // If the end of the mining period has been reached, then stop giving them rewards, // and unlock their bonded tokens after waiting the cooldown period of _mining_config_token_cooldown_config.token_lock_min_blocks // after the end of their mining period. if >::block_number() >= _mining_execution_token_result.token_execution_interval_blocks { - if >::block_number() > _mining_execution_token_result.token_execution_interval_blocks + token_lock_min_blocks { - // if >::block_number() > MINING_REQUESTED_END_BLOCK + token_lock_min_blocks { + // if >::block_number() > _mining_execution_token_result.token_execution_interval_blocks + token_lock_min_blocks { + if >::block_number() > MINING_REQUESTED_END_BLOCK as T::BlockNumber + token_lock_min_blocks { // TODO - Unlock the funds. Store updated status // We only want to unlock the rewards they have earned. // The amount of tokens that they originally locked to mine the rewards will remain locked until the end of their mining period @@ -204,7 +204,8 @@ decl_module! { // Reference: https://github.com/hicommonwealth/edgeware-node/blob/master/modules/edge-treasury-reward/src/lib.rs#L42 if >::block_number() % token_lock_min_blocks == Zero::zero() { // FIXME - assumes there is only one rates config index so hard-coded 0, but we could have many - let fetched_mining_rates_token_rates_config = >::mining_rates_token_rates_configs(0u32.into()); + let RATE_HARD_CODED: T::MiningRatesTokenIndex = 0u32.into(); + let fetched_mining_rates_token_rates_config = >::mining_rates_token_rates_configs(RATE_HARD_CODED); if let Some(_mining_rates_token_rates_config) = fetched_mining_rates_token_rates_config { debug::info!("token_execution_interval_blocks {:#?}", _mining_execution_token_result.token_execution_interval_blocks); @@ -213,12 +214,14 @@ decl_module! { // in the meantime until this is fixed, we will just assume the user is mining MXC and choose the rate for that // Reward ratio - let reward_ratio = _mining_rates_token_rates_config.token_token_mxc; - + let reward_ratio: T::MiningRatesTokenTokenMXC = _mining_rates_token_rates_config.token_token_mxc; + // // Calculate the reward based on the reward ratio (i.e. 1 DHX per 10 DHX that was locked) // e.g. (1.1 - 1) * 10 DHX, where 1.1 is ratio of mining reward for the MXC token - - // let reward = (reward_ratio - 1.into()) * token_lock_amount; + let total_ratio: T::MiningRatesTokenTokenMXC = reward_ratio - 1u32.into(); + if let total = Some(balance_to_u32(total_ratio)) { + let reward: u32 = total * token_lock_amount.into(); + } // Distribute the reward to the account that has locked the funds @@ -611,6 +614,10 @@ decl_module! { } impl Module { + pub fn balance_to_u32(input: BalanceOf) -> Option { + >::into(input).is_some() + } + pub fn is_mining_config_token_owner( mining_config_token_id: T::MiningConfigTokenIndex, sender: T::AccountId, diff --git a/pallets/mining/rates/token/src/lib.rs b/pallets/mining/rates/token/src/lib.rs index 0d76c5151..4ffaf2f7d 100644 --- a/pallets/mining/rates/token/src/lib.rs +++ b/pallets/mining/rates/token/src/lib.rs @@ -41,7 +41,7 @@ mod tests; /// The module's configuration trait. pub trait Trait: frame_system::Trait + roaming_operators::Trait { type Event: From> + Into<::Event>; - type MiningRatesTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type MiningRatesTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + From + Into; type MiningRatesTokenTokenMXC: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; type MiningRatesTokenTokenIOTA: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; type MiningRatesTokenTokenDOT: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; From c6bcb1e60ab89d49eb309035c4f0b8bf7881a1e1 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 11:20:16 +0100 Subject: [PATCH 07/27] fix type conversion thanks to ilya --- pallets/mining/config/token/src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 195301999..d30c87bdb 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -30,6 +30,7 @@ use sp_runtime::{ }, DispatchError, }; +use sp_std::convert::TryInto; use sp_std::prelude::*; // Imports Vec // FIXME - remove roaming_operators here, only use this approach since do not know how to use BalanceOf using only @@ -219,9 +220,17 @@ decl_module! { // Calculate the reward based on the reward ratio (i.e. 1 DHX per 10 DHX that was locked) // e.g. (1.1 - 1) * 10 DHX, where 1.1 is ratio of mining reward for the MXC token let total_ratio: T::MiningRatesTokenTokenMXC = reward_ratio - 1u32.into(); - if let total = Some(balance_to_u32(total_ratio)) { - let reward: u32 = total * token_lock_amount.into(); - } + + // if let total = Some(balance_to_u32(total_ratio)) { + // let reward: u32 = total * token_lock_amount.into(); + // } + + let total = TryInto::::try_into(total_ratio).ok(); + let lock_amount = TryInto::::try_into(token_lock_amount).ok(); + let reward: Option = match (total, lock_amount) { + (Some(a), Some(b)) => Some(a * b), + _ => None, + }; // Distribute the reward to the account that has locked the funds @@ -614,9 +623,9 @@ decl_module! { } impl Module { - pub fn balance_to_u32(input: BalanceOf) -> Option { - >::into(input).is_some() - } + // pub fn balance_to_u32(input: BalanceOf) -> Option { + // >::into(input).is_some() + // } pub fn is_mining_config_token_owner( mining_config_token_id: T::MiningConfigTokenIndex, From 1409c6e352754a38e1cb5bda12a6c731842f1487 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 13:37:45 +0100 Subject: [PATCH 08/27] try move into balance_to_u32 fn --- pallets/mining/config/token/src/lib.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index d30c87bdb..1f2e71279 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -226,9 +226,11 @@ decl_module! { // } let total = TryInto::::try_into(total_ratio).ok(); - let lock_amount = TryInto::::try_into(token_lock_amount).ok(); + // let lock_amount = TryInto::::try_into(token_lock_amount).ok(); + let lock_amount = Self::balance_to_u32(token_lock_amount).ok(); + let reward: Option = match (total, lock_amount) { - (Some(a), Some(b)) => Some(a * b), + (Some(a), b) => Some(a) * b, _ => None, }; @@ -623,9 +625,13 @@ decl_module! { } impl Module { - // pub fn balance_to_u32(input: BalanceOf) -> Option { - // >::into(input).is_some() - // } + pub fn balance_to_u32(input: BalanceOf) -> Result<(), DispatchError> { + let output = match TryInto::::try_into(input) { + Ok(output) => return Ok(output), + Err(e) => Err(DispatchError::Other("Unable to convert Balance to u32")), + }; + Ok(output) + } pub fn is_mining_config_token_owner( mining_config_token_id: T::MiningConfigTokenIndex, From c5a3019e6a42d1c872c242c8b6a1a8dcf2b69aa6 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 13:45:42 +0100 Subject: [PATCH 09/27] revert back to ilya solution --- pallets/mining/config/token/src/lib.rs | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 1f2e71279..7cae12494 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -221,16 +221,10 @@ decl_module! { // e.g. (1.1 - 1) * 10 DHX, where 1.1 is ratio of mining reward for the MXC token let total_ratio: T::MiningRatesTokenTokenMXC = reward_ratio - 1u32.into(); - // if let total = Some(balance_to_u32(total_ratio)) { - // let reward: u32 = total * token_lock_amount.into(); - // } - let total = TryInto::::try_into(total_ratio).ok(); - // let lock_amount = TryInto::::try_into(token_lock_amount).ok(); - let lock_amount = Self::balance_to_u32(token_lock_amount).ok(); - + let lock_amount = TryInto::::try_into(token_lock_amount).ok(); let reward: Option = match (total, lock_amount) { - (Some(a), b) => Some(a) * b, + (Some(a), Some(b)) => Some(a * b), _ => None, }; @@ -625,14 +619,6 @@ decl_module! { } impl Module { - pub fn balance_to_u32(input: BalanceOf) -> Result<(), DispatchError> { - let output = match TryInto::::try_into(input) { - Ok(output) => return Ok(output), - Err(e) => Err(DispatchError::Other("Unable to convert Balance to u32")), - }; - Ok(output) - } - pub fn is_mining_config_token_owner( mining_config_token_id: T::MiningConfigTokenIndex, sender: T::AccountId, From 984f849819bb024e712d26d82f5b751d6608ab1b Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 14:58:24 +0100 Subject: [PATCH 10/27] feat: Got treasury transfer reward to compile --- pallets/mining/config/token/src/lib.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 7cae12494..ec3946685 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -13,6 +13,7 @@ use frame_support::{ ensure, traits::{ Currency, + ExistenceRequirement, Get, Randomness, }, @@ -43,7 +44,7 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait { +pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait + pallet_treasury::Trait { type Event: From> + Into<::Event>; type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + sp_std::iter::Step; // Mining Speed Boost Token Mining Config @@ -230,12 +231,20 @@ decl_module! { // Distribute the reward to the account that has locked the funds - // ::Currency::transfer( - // &>::account_id(), - // &_mining_execution_token_result.token_execution_executor_account_id, - // reward, - // ExistenceRequirement::KeepAlive - // ); + let treasury_account_id: T::AccountId = >::account_id(); + + if let Some(reward_to_pay_to_try) = reward { + let reward_to_pay_as_balance_to_try = TryInto::>::try_into(reward_to_pay_to_try).ok(); + if let Some(reward_to_pay) = reward_to_pay_as_balance_to_try { + ::Currency::transfer( + &treasury_account_id, + &_mining_execution_token_result.token_execution_executor_account_id, + reward_to_pay, + ExistenceRequirement::KeepAlive + ); + } + } + // Emit event since treasury unlocked locked tokens and rewarded customer the reward ratio From cf8dfc1c002642fb5ee998587b16897361c646be Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 15:43:47 +0100 Subject: [PATCH 11/27] feat: use BalanceOf and Balance types to get post-cooldown event to compile --- pallets/mining/config/token/src/lib.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index ec3946685..a3a239b63 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -44,7 +44,7 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait + pallet_treasury::Trait { +pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait + pallet_treasury::Trait + pallet_balances::Trait { type Event: From> + Into<::Event>; type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + sp_std::iter::Step; // Mining Speed Boost Token Mining Config @@ -91,22 +91,26 @@ decl_event!( ::MiningConfigTokenIndex, ::MiningConfigTokenType, BlockNumber = ::BlockNumber, - Balance = BalanceOf, + BalanceOfCurrency = BalanceOf, + Balance = ::Balance, { /// A mining_config_token is created. (owner, mining_config_token_id) Created(AccountId, MiningConfigTokenIndex), /// A mining_config_token is transferred. (from, to, mining_config_token_id) Transferred(AccountId, AccountId, MiningConfigTokenIndex), MiningConfigTokenConfigSet( - AccountId, MiningConfigTokenIndex, MiningConfigTokenType, Balance, BlockNumber, BlockNumber + AccountId, MiningConfigTokenIndex, MiningConfigTokenType, BalanceOfCurrency, BlockNumber, BlockNumber ), MiningConfigTokenRequirementsConfigSet( - AccountId, MiningConfigTokenIndex, MiningConfigTokenType, Balance, + AccountId, MiningConfigTokenIndex, MiningConfigTokenType, BalanceOfCurrency, BlockNumber ), MiningConfigTokenExecutionResultSet( AccountId, MiningConfigTokenIndex,AccountId, BlockNumber, BlockNumber ), + TreasuryRewardTokenMiningPostCooldown( + Balance, BlockNumber, AccountId + ), } ); @@ -230,7 +234,6 @@ decl_module! { }; // Distribute the reward to the account that has locked the funds - let treasury_account_id: T::AccountId = >::account_id(); if let Some(reward_to_pay_to_try) = reward { @@ -245,14 +248,12 @@ decl_module! { } } - // Emit event since treasury unlocked locked tokens and rewarded customer the reward ratio - - // Self::deposit_event(RawEvent::TreasuryRewardTokenMiningPostCooldown( - // >::free_balance(_mining_execution_token_result.token_execution_executor_account_id), - // >::block_number(), - // _mining_execution_token_result.token_execution_executor_account_id - // )); + Self::deposit_event(RawEvent::TreasuryRewardTokenMiningPostCooldown( + >::free_balance(&_mining_execution_token_result.token_execution_executor_account_id), + >::block_number(), + _mining_execution_token_result.token_execution_executor_account_id + )); } } } From 85c908d504c23363111bd55ac26c73d5434998f8 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 18:17:44 +0100 Subject: [PATCH 12/27] wip --- pallets/mining/config/token/src/lib.rs | 44 +++++++++++++++----------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index a3a239b63..e938762b9 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -15,7 +15,10 @@ use frame_support::{ Currency, ExistenceRequirement, Get, + LockIdentifier, + LockableCurrency, Randomness, + WithdrawReasons, }, Parameter, }; @@ -51,6 +54,7 @@ pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_t type MiningConfigTokenType: Parameter + Member + Default; type MiningConfigTokenLockAmount: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; type Currency: Currency; + type CurrencyLockable: LockableCurrency; } type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -198,10 +202,10 @@ decl_module! { // and then they cannot move those locked tokens for the cooldown period and receive no further rewards. if let Some(mining_config_token) = Self::mining_config_token(idx_c) { - // ::Currency::remove_lock( - // mining_config_token, // where idx_c is mining_config_token_id - // &_mining_execution_token_result.token_execution_executor_account_id, - // ); + T::CurrencyLockable::remove_lock( + mining_config_token, // where idx_c is mining_config_token_id + &_mining_execution_token_result.token_execution_executor_account_id, + ); } } } else if >::block_number() < _mining_execution_token_result.token_execution_interval_blocks { @@ -804,8 +808,6 @@ impl Module { _token_execution_interval_blocks: T::BlockNumber, ) -> Result<(), DispatchError> { return Ok(()); - // const EXAMPLE_ID: LockIdentifier = *b"example "; - // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module // TODO - Setup a function in on_finalize that automatically checks through all the accounts that have @@ -816,18 +818,24 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let lock_amount = configuration_token.token_lock_amount { - if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { - // ::Currency::set_lock( - // execution_token, // EXAMPLE_ID, - // &_token_execution_executor_account_id, - // lock_amount, - // WithdrawReasons::all(), - // ); - return Ok(()); - } else { - return Err(DispatchError::Other( - "Cannot find mining_config_token_id associated with the execution", - )); + let lock_amount_lockable_currency_try = TryInto::::try_into(lock_amount).ok(); + if let lock_amount_lockable_currency = Some(lock_amount_lockable_currency_try) { + if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { + const EXAMPLE_ID: LockIdentifier = *b"example "; + + T::CurrencyLockable::set_lock( + // execution_token, // EXAMPLE_ID, + EXAMPLE_ID, + &_token_execution_executor_account_id, + lock_amount_lockable_currency, + WithdrawReasons::all(), + ); + return Ok(()); + } else { + return Err(DispatchError::Other( + "Cannot find mining_config_token_id associated with the execution", + )); + } } } else { return Err(DispatchError::Other( From 60e8ce769adec9258d4f0a514a73eb39511bfda3 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 18:50:33 +0100 Subject: [PATCH 13/27] wip - remove use of roaming_operators from token_mining_config --- Cargo.lock | 1 - pallets/mining/config/token/Cargo.toml | 2 -- pallets/mining/config/token/src/lib.rs | 18 ++++++++++-------- runtime/src/lib.rs | 3 ++- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 04b796b74..c9935b5d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3225,7 +3225,6 @@ dependencies = [ "pallet-transaction-payment", "pallet-treasury", "parity-scale-codec", - "roaming-operators", "safe-mix", "sp-core", "sp-io", diff --git a/pallets/mining/config/token/Cargo.toml b/pallets/mining/config/token/Cargo.toml index 24f7260c1..650b34b67 100644 --- a/pallets/mining/config/token/Cargo.toml +++ b/pallets/mining/config/token/Cargo.toml @@ -21,7 +21,6 @@ std = [ 'sp-runtime/std', 'sp-std/std', - 'roaming-operators/std', 'mining-rates-token/std', ] @@ -39,7 +38,6 @@ sp-io = { version = '2.0.0', default_features = false } sp-runtime = { version = '2.0.0', default_features = false } sp-std = { version = '2.0.0', default_features = false } -roaming-operators = { default_features = false, package = 'roaming-operators', path = '../../../roaming/roaming-operators' } mining-rates-token = { default_features = false, package = 'mining-rates-token', path = '../../../mining/rates/token' } [dev-dependencies] diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index e938762b9..9f178490a 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -47,14 +47,13 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait + pallet_treasury::Trait + pallet_balances::Trait { +pub trait Trait: frame_system::Trait + mining_rates_token::Trait + pallet_treasury::Trait + pallet_balances::Trait { type Event: From> + Into<::Event>; type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + sp_std::iter::Step; // Mining Speed Boost Token Mining Config type MiningConfigTokenType: Parameter + Member + Default; type MiningConfigTokenLockAmount: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; - type Currency: Currency; - type CurrencyLockable: LockableCurrency; + type Currency: LockableCurrency; } type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -96,7 +95,6 @@ decl_event!( ::MiningConfigTokenType, BlockNumber = ::BlockNumber, BalanceOfCurrency = BalanceOf, - Balance = ::Balance, { /// A mining_config_token is created. (owner, mining_config_token_id) Created(AccountId, MiningConfigTokenIndex), @@ -113,7 +111,7 @@ decl_event!( AccountId, MiningConfigTokenIndex,AccountId, BlockNumber, BlockNumber ), TreasuryRewardTokenMiningPostCooldown( - Balance, BlockNumber, AccountId + BalanceOfCurrency, BlockNumber, AccountId ), } ); @@ -202,7 +200,7 @@ decl_module! { // and then they cannot move those locked tokens for the cooldown period and receive no further rewards. if let Some(mining_config_token) = Self::mining_config_token(idx_c) { - T::CurrencyLockable::remove_lock( + T::Currency::remove_lock( mining_config_token, // where idx_c is mining_config_token_id &_mining_execution_token_result.token_execution_executor_account_id, ); @@ -818,12 +816,12 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let lock_amount = configuration_token.token_lock_amount { - let lock_amount_lockable_currency_try = TryInto::::try_into(lock_amount).ok(); + let lock_amount_lockable_currency_try = TryInto::::try_into(lock_amount).ok(); if let lock_amount_lockable_currency = Some(lock_amount_lockable_currency_try) { if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { const EXAMPLE_ID: LockIdentifier = *b"example "; - T::CurrencyLockable::set_lock( + T::Currency::set_lock( // execution_token, // EXAMPLE_ID, EXAMPLE_ID, &_token_execution_executor_account_id, @@ -836,6 +834,10 @@ impl Module { "Cannot find mining_config_token_id associated with the execution", )); } + } else { + return Err(DispatchError::Other( + "Cannot find lock_amount_lockable_currency associated with the execution", + )); } } else { return Err(DispatchError::Other( diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 43506dcc7..ef6dd8ee2 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -515,7 +515,7 @@ impl pallet_staking::Trait for Runtime { } impl roaming_operators::Trait for Runtime { - type Currency = Balances; + // type Currency = Balances; type Event = Event; type Randomness = RandomnessCollectiveFlip; type RoamingOperatorIndex = u64; @@ -609,6 +609,7 @@ impl roaming_packet_bundles::Trait for Runtime { impl mining_config_token::Trait for Runtime { type Currency = Balances; + type Balance = Balance; type Event = Event; // FIXME - restore when stop temporarily using roaming-operators // type Currency = Balances; From 480f9fcee986e98fa8c050043ea963d6cfe67a3e Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Wed, 3 Mar 2021 20:26:05 +0100 Subject: [PATCH 14/27] remove roaming_operators dependency in mining_tokens_config --- Cargo.lock | 1 - pallets/mining/config/token/src/lib.rs | 13 +++++++------ pallets/mining/rates/token/Cargo.toml | 4 ---- pallets/mining/rates/token/src/lib.rs | 3 ++- runtime/src/lib.rs | 5 +++-- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9935b5d2..79c84de31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3302,7 +3302,6 @@ dependencies = [ "pallet-randomness-collective-flip", "pallet-transaction-payment", "parity-scale-codec", - "roaming-operators", "safe-mix", "sp-core", "sp-io", diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 9f178490a..a4d2538d6 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -95,6 +95,7 @@ decl_event!( ::MiningConfigTokenType, BlockNumber = ::BlockNumber, BalanceOfCurrency = BalanceOf, + Balance = ::Balance, { /// A mining_config_token is created. (owner, mining_config_token_id) Created(AccountId, MiningConfigTokenIndex), @@ -108,10 +109,10 @@ decl_event!( BlockNumber ), MiningConfigTokenExecutionResultSet( - AccountId, MiningConfigTokenIndex,AccountId, BlockNumber, BlockNumber + AccountId, MiningConfigTokenIndex, AccountId, BlockNumber, BlockNumber ), TreasuryRewardTokenMiningPostCooldown( - BalanceOfCurrency, BlockNumber, AccountId + Balance, BlockNumber, AccountId ), } ); @@ -200,7 +201,7 @@ decl_module! { // and then they cannot move those locked tokens for the cooldown period and receive no further rewards. if let Some(mining_config_token) = Self::mining_config_token(idx_c) { - T::Currency::remove_lock( + ::Currency::remove_lock( mining_config_token, // where idx_c is mining_config_token_id &_mining_execution_token_result.token_execution_executor_account_id, ); @@ -805,7 +806,7 @@ impl Module { _token_execution_started_block: T::BlockNumber, _token_execution_interval_blocks: T::BlockNumber, ) -> Result<(), DispatchError> { - return Ok(()); + // return Ok(()); // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module // TODO - Setup a function in on_finalize that automatically checks through all the accounts that have @@ -816,12 +817,12 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let lock_amount = configuration_token.token_lock_amount { - let lock_amount_lockable_currency_try = TryInto::::try_into(lock_amount).ok(); + let lock_amount_lockable_currency_try = TryInto::<::Currency>::try_into(lock_amount).ok(); if let lock_amount_lockable_currency = Some(lock_amount_lockable_currency_try) { if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { const EXAMPLE_ID: LockIdentifier = *b"example "; - T::Currency::set_lock( + ::Currency::set_lock( // execution_token, // EXAMPLE_ID, EXAMPLE_ID, &_token_execution_executor_account_id, diff --git a/pallets/mining/rates/token/Cargo.toml b/pallets/mining/rates/token/Cargo.toml index df990f355..49aca57cd 100644 --- a/pallets/mining/rates/token/Cargo.toml +++ b/pallets/mining/rates/token/Cargo.toml @@ -18,8 +18,6 @@ std = [ 'sp-io/std', 'sp-runtime/std', 'sp-std/std', - - 'roaming-operators/std', ] [dependencies] @@ -35,6 +33,4 @@ sp-io = { version = '2.0.0', default_features = false } sp-runtime = { version = '2.0.0', default_features = false } sp-std = { version = '2.0.0', default_features = false } -roaming-operators = { default_features = false, package = 'roaming-operators', path = '../../../roaming/roaming-operators' } - [dev-dependencies] diff --git a/pallets/mining/rates/token/src/lib.rs b/pallets/mining/rates/token/src/lib.rs index 4ffaf2f7d..2e2c61e6e 100644 --- a/pallets/mining/rates/token/src/lib.rs +++ b/pallets/mining/rates/token/src/lib.rs @@ -39,8 +39,9 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + roaming_operators::Trait { +pub trait Trait: frame_system::Trait { type Event: From> + Into<::Event>; + type Randomness: Randomness; type MiningRatesTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + From + Into; type MiningRatesTokenTokenMXC: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; type MiningRatesTokenTokenIOTA: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index ef6dd8ee2..062633f11 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -608,8 +608,8 @@ impl roaming_packet_bundles::Trait for Runtime { } impl mining_config_token::Trait for Runtime { - type Currency = Balances; - type Balance = Balance; + // type Currency = Balances; + // type Balance = Balance; type Event = Event; // FIXME - restore when stop temporarily using roaming-operators // type Currency = Balances; @@ -639,6 +639,7 @@ impl mining_config_hardware::Trait for Runtime { impl mining_rates_token::Trait for Runtime { type Event = Event; + type Randomness = RandomnessCollectiveFlip; type MiningRatesTokenIndex = u64; type MiningRatesTokenMaxLoyalty = u32; // Mining Speed Boost Max Rates From 3a5825cd2c9e5a53d723257e6e7e3ebec820a697 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 11:31:01 +0100 Subject: [PATCH 15/27] change to BalanceOf, add Currency to runtime instead of use balances --- pallets/mining/config/token/src/lib.rs | 37 +++++++++++++++----------- runtime/src/lib.rs | 2 +- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index a4d2538d6..67d15d2c7 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -201,7 +201,7 @@ decl_module! { // and then they cannot move those locked tokens for the cooldown period and receive no further rewards. if let Some(mining_config_token) = Self::mining_config_token(idx_c) { - ::Currency::remove_lock( + ::Currency::remove_lock( mining_config_token, // where idx_c is mining_config_token_id &_mining_execution_token_result.token_execution_executor_account_id, ); @@ -817,27 +817,32 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let lock_amount = configuration_token.token_lock_amount { - let lock_amount_lockable_currency_try = TryInto::<::Currency>::try_into(lock_amount).ok(); - if let lock_amount_lockable_currency = Some(lock_amount_lockable_currency_try) { - if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { - const EXAMPLE_ID: LockIdentifier = *b"example "; - - ::Currency::set_lock( - // execution_token, // EXAMPLE_ID, - EXAMPLE_ID, - &_token_execution_executor_account_id, - lock_amount_lockable_currency, - WithdrawReasons::all(), - ); - return Ok(()); + if let lock_amount_lockable_currency_try = TryInto::BalanceOf::try_into(lock_amount).ok() { + if let lock_amount_lockable_currency = lock_amount_lockable_currency_try { + if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { + const EXAMPLE_ID: LockIdentifier = *b"example "; + + ::Currency::set_lock( + // execution_token, // EXAMPLE_ID, + EXAMPLE_ID, + &_token_execution_executor_account_id, + lock_amount_lockable_currency, + WithdrawReasons::all(), + ); + return Ok(()); + } else { + return Err(DispatchError::Other( + "Cannot find mining_config_token_id associated with the execution", + )); + } } else { return Err(DispatchError::Other( - "Cannot find mining_config_token_id associated with the execution", + "Cannot find lock_amount_lockable_currency associated with the execution", )); } } else { return Err(DispatchError::Other( - "Cannot find lock_amount_lockable_currency associated with the execution", + "Cannot find lock_amount associated with the execution", )); } } else { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 062633f11..5f720976d 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -612,7 +612,7 @@ impl mining_config_token::Trait for Runtime { // type Balance = Balance; type Event = Event; // FIXME - restore when stop temporarily using roaming-operators - // type Currency = Balances; + type Currency = Balances; // type Randomness = RandomnessCollectiveFlip; type MiningConfigTokenIndex = u64; type MiningConfigTokenLockAmount = u64; From 0089aa6ed1556e129bee1c3fa0a787d0b9e429f0 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 12:34:10 +0100 Subject: [PATCH 16/27] wip --- pallets/exchange-rate/src/lib.rs | 291 +++++++++++ pallets/mining/config/token/src/lib.rs | 9 +- pallets/mining/execution/token/src/lib.rs | 562 ++++++++++++++++++++++ 3 files changed, 859 insertions(+), 3 deletions(-) create mode 100644 pallets/exchange-rate/src/lib.rs create mode 100644 pallets/mining/execution/token/src/lib.rs diff --git a/pallets/exchange-rate/src/lib.rs b/pallets/exchange-rate/src/lib.rs new file mode 100644 index 000000000..a339e2d08 --- /dev/null +++ b/pallets/exchange-rate/src/lib.rs @@ -0,0 +1,291 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use codec::{ + Decode, + Encode, +}; +use frame_support::{ + debug, + decl_event, + decl_module, + decl_storage, + ensure, + traits::{ + Get, + Randomness, + }, + Parameter, +}; +use frame_system::ensure_signed; +use sp_io::hashing::blake2_128; +use sp_runtime::{ + traits::{ + AtLeast32Bit, + Bounded, + Member, + One, + }, + DispatchError, +}; +use sp_std::prelude::*; + +#[cfg(test)] +mod mock; + +#[cfg(test)] +mod tests; + +#[derive(Encode, Decode, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "std", derive(Debug))] +pub struct ExchangeRate(pub [u8; 16]); + +#[cfg_attr(feature = "std", derive(Debug))] +#[derive(Encode, Decode, Default, Clone, PartialEq)] +pub struct ExchangeRateConfig { + pub hbtc: H, + pub dot: D, + pub iota: I, + pub fil: F, + pub decimals_after_point: P, +} + +pub trait Config: frame_system::Config + roaming_operators::Config { + type Event: From> + Into<::Event>; + type ExchangeRateIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type HBTCRate: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type DOTRate: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type IOTARate: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type FILRate: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + type DecimalsAfterPoint: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; +} + +decl_event!( + pub enum Event where + ::AccountId, + ::ExchangeRateIndex, + ::HBTCRate, + ::DOTRate, + ::IOTARate, + ::FILRate, + ::DecimalsAfterPoint, + { + /// A exchange_rate is created. (owner, exchange_rate_index) + Created(AccountId, ExchangeRateIndex), + /// A exchange_rate is transferred. (from, to, exchange_rate_index) + Transferred(AccountId, AccountId, ExchangeRateIndex), + ConfigSet( + AccountId, ExchangeRateIndex, HBTCRate, + DOTRate, IOTARate, + FILRate, DecimalsAfterPoint + ), + } +); + +decl_storage! { + trait Store for Module as ExchangeRate { + pub ExchangeRates get(fn exchange_rates): map hasher(opaque_blake2_256) T::ExchangeRateIndex => Option; + pub ExchangeRateOwners get(fn exchange_rate_owner): map hasher(opaque_blake2_256) T::ExchangeRateIndex => Option; + pub ExchangeRateCount get(fn exchange_rate_count): T::ExchangeRateIndex; + pub ExchangeRateConfigs get(fn exchange_rate_configs): map hasher(opaque_blake2_256) T::ExchangeRateIndex => + Option>; + } +} + +decl_module! { + pub struct Module for enum Call where origin: T::Origin { + fn deposit_event() = default; + + #[weight = 10_000 + T::DbWeight::get().writes(3)] + pub fn create(origin) { + let sender = ensure_signed(origin)?; + let exchange_rate_id = Self::next_exchange_rate_id()?; + + // Generate a random 128bit value + let unique_id = Self::random_value(&sender); + + let exchange_rate = ExchangeRate(unique_id); + + >::insert( + exchange_rate_id, + exchange_rate, + ); + >::put(exchange_rate_id + One::one()); + >::insert(exchange_rate_id, &sender.clone()); + + Self::deposit_event(RawEvent::Created(sender, exchange_rate_id)); + } + + /// Transfer a exchange_rate to new owner + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn transfer(origin, to: T::AccountId, exchange_rate_id: T::ExchangeRateIndex) { + let sender = ensure_signed(origin)?; + + ensure!(Self::exchange_rate_owner(exchange_rate_id) == Some(sender.clone()), "Only owner can transfer exchange_rate"); + + Self::update_owner(&to, exchange_rate_id); + + Self::deposit_event(RawEvent::Transferred(sender, to, exchange_rate_id)); + } + + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn set_config( + origin, + exchange_rate_id: T::ExchangeRateIndex, + hbtc_rate: Option, + dot_rate: Option, + iota_rate: Option, + fil_rate: Option, + decimals_after_point: Option + ) { + let sender = ensure_signed(origin)?; + + // Ensure that the exchange_rate_id whose config we want to change actually exists + let is_exchange_rate = Self::exists_exchange_rates(exchange_rate_id).is_ok(); + ensure!(is_exchange_rate, "ExchangeRates does not exist"); + + // Ensure that the caller is owner of the exchange_rate they are trying to change + ensure!(Self::exchange_rate_owner(exchange_rate_id) == Some(sender.clone()), "Only owner can set exchange_rate_config"); + + let out_hbtc_rate = match hbtc_rate.clone() { + Some(value) => value, + None => 200000u32.into() // Default + }; + + let out_dot_rate = match dot_rate.clone() { + Some(value) => value, + None => 100u32.into() // Default + }; + + let out_iota_rate = match iota_rate.clone() { + Some(value) => value, + None => 5u32.into() // Default + }; + + let out_fil_rate = match fil_rate.clone() { + Some(value) => value, + None => 200u32.into() // Default + }; + + let out_decimals_after_point = match decimals_after_point.clone() { + Some(value) => value, + None => 2u32.into() // Default + }; + + // Check if a exchange_rate_config already exists with the given exchange_rate_id + // to determine whether to insert new or mutate existing. + if Self::has_value_for_exchange_rates_index(exchange_rate_id).is_ok() { + debug::info!("Mutating values"); + >::mutate(exchange_rate_id, |exchange_rate_config| { + if let Some(_exchange_rate_config) = exchange_rate_config { + // Only update the value of a key in a KV pair if the corresponding parameter value has been provided + _exchange_rate_config.hbtc = out_hbtc_rate.clone(); + _exchange_rate_config.dot = out_dot_rate.clone(); + _exchange_rate_config.iota = out_iota_rate.clone(); + _exchange_rate_config.fil = out_fil_rate.clone(); + _exchange_rate_config.decimals_after_point = out_decimals_after_point.clone(); + } + }); + debug::info!("Checking mutated values"); + let fetched_exchange_rate_config = >::get(exchange_rate_id); + if let Some(_exchange_rate_config) = fetched_exchange_rate_config { + debug::info!("Latest field hbtc {:#?}", _exchange_rate_config.hbtc); + debug::info!("Latest field dot {:#?}", _exchange_rate_config.dot); + debug::info!("Latest field iota {:#?}", _exchange_rate_config.iota); + debug::info!("Latest field fil {:#?}", _exchange_rate_config.fil); + debug::info!("Latest field decimals_after_point {:#?}", _exchange_rate_config.decimals_after_point); + } + } else { + debug::info!("Inserting values"); + + // Create a new mining exchange_rate_config instance with the input params + let exchange_rate_config = ExchangeRateConfig { + // Since each parameter passed into the function is optional (i.e. `Option`) + // we will assign a default value if a parameter value is not provided. + hbtc: out_hbtc_rate.clone(), + dot: out_dot_rate.clone(), + iota: out_iota_rate.clone(), + fil: out_fil_rate.clone(), + decimals_after_point: out_decimals_after_point.clone(), + }; + + >::insert( + exchange_rate_id, + &exchange_rate_config + ); + + debug::info!("Checking inserted values"); + let fetched_exchange_rate_config = >::get(exchange_rate_id); + if let Some(_exchange_rate_config) = fetched_exchange_rate_config { + debug::info!("Latest field hbtc {:#?}", _exchange_rate_config.hbtc); + debug::info!("Latest field dot {:#?}", _exchange_rate_config.dot); + debug::info!("Latest field iota {:#?}", _exchange_rate_config.iota); + debug::info!("Latest field fil {:#?}", _exchange_rate_config.fil); + debug::info!("Latest field decimals_after_point {:#?}", _exchange_rate_config.decimals_after_point); + } + } + + Self::deposit_event(RawEvent::ConfigSet( + sender, + exchange_rate_id, + out_hbtc_rate, + out_dot_rate, + out_iota_rate, + out_fil_rate, + out_decimals_after_point, + )); + } + } +} + +impl Module { + pub fn is_exchange_rate_owner( + exchange_rate_id: T::ExchangeRateIndex, + sender: T::AccountId, + ) -> Result<(), DispatchError> { + ensure!( + Self::exchange_rate_owner(&exchange_rate_id).map(|owner| owner == sender).unwrap_or(false), + "Sender is not owner of ExchangeRate" + ); + Ok(()) + } + + pub fn exists_exchange_rates(exchange_rate_id: T::ExchangeRateIndex) -> Result { + match Self::exchange_rates(exchange_rate_id) { + Some(value) => Ok(value), + None => Err(DispatchError::Other("ExchangeRates does not exist")), + } + } + + pub fn has_value_for_exchange_rates_index(exchange_rate_id: T::ExchangeRateIndex) -> Result<(), DispatchError> { + debug::info!("Checking if exchange_rate_config has a value that is defined"); + let fetched_exchange_rate_config = >::get(exchange_rate_id); + if let Some(_value) = fetched_exchange_rate_config { + debug::info!("Found value for exchange_rate_config"); + return Ok(()); + } + debug::info!("No value for exchange_rate_config"); + Err(DispatchError::Other("No value for exchange_rate_config")) + } + + fn random_value(sender: &T::AccountId) -> [u8; 16] { + let payload = ( + ::Randomness::random(&[0]), + sender, + >::extrinsic_index(), + >::block_number(), + ); + payload.using_encoded(blake2_128) + } + + fn next_exchange_rate_id() -> Result { + let exchange_rate_id = Self::exchange_rate_count(); + if exchange_rate_id == ::max_value() { + return Err(DispatchError::Other("ExchangeRate count overflow")); + } + Ok(exchange_rate_id) + } + + fn update_owner(to: &T::AccountId, exchange_rate_id: T::ExchangeRateIndex) { + >::insert(exchange_rate_id, to); + } +} diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 67d15d2c7..5aeb0314c 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -54,6 +54,7 @@ pub trait Trait: frame_system::Trait + mining_rates_token::Trait + pallet_treasu type MiningConfigTokenType: Parameter + Member + Default; type MiningConfigTokenLockAmount: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; type Currency: LockableCurrency; + type Randomness: Randomness; } type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -201,8 +202,10 @@ decl_module! { // and then they cannot move those locked tokens for the cooldown period and receive no further rewards. if let Some(mining_config_token) = Self::mining_config_token(idx_c) { + const EXAMPLE_ID: LockIdentifier = *b"example "; + ::Currency::remove_lock( - mining_config_token, // where idx_c is mining_config_token_id + EXAMPLE_ID, // FIXME - somehow replace with: mining_config_token, // where idx_c is mining_config_token_id &_mining_execution_token_result.token_execution_executor_account_id, ); } @@ -817,7 +820,7 @@ impl Module { Self::mining_config_token_configs((mining_config_token_id)) { if let lock_amount = configuration_token.token_lock_amount { - if let lock_amount_lockable_currency_try = TryInto::BalanceOf::try_into(lock_amount).ok() { + if let Some(lock_amount_lockable_currency_try) = TryInto::>::try_into(lock_amount).ok() { if let lock_amount_lockable_currency = lock_amount_lockable_currency_try { if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { const EXAMPLE_ID: LockIdentifier = *b"example "; @@ -857,7 +860,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/execution/token/src/lib.rs b/pallets/mining/execution/token/src/lib.rs new file mode 100644 index 000000000..c56b48130 --- /dev/null +++ b/pallets/mining/execution/token/src/lib.rs @@ -0,0 +1,562 @@ +#![cfg_attr(not(feature = "std"), no_std)] + +use codec::{ + Decode, + Encode, +}; +use frame_support::{ + debug, + decl_event, + decl_module, + decl_storage, + ensure, + traits::{ + Get, + Randomness, + }, + Parameter, +}; +use frame_system::ensure_signed; +use sp_io::hashing::blake2_128; +use sp_runtime::{ + traits::{ + AtLeast32Bit, + Bounded, + Member, + One, + }, + DispatchError, +}; +use sp_std::prelude::*; // Imports Vec + +// FIXME - remove roaming_operators here, only use this approach since do not know how to use BalanceOf using only +// mining runtime module +use mining_claims_token; +use mining_config_token; +use mining_eligibility_token; +use mining_rates_token; +use mining_sampling_token; + +// #[cfg(test)] +// mod mock; + +// #[cfg(test)] +// mod tests; + +/// The module's configuration trait. +pub trait Config: + frame_system::Config + + roaming_operators::Config + + mining_config_token::Config + + mining_eligibility_token::Config + + mining_rates_token::Config + + mining_sampling_token::Config + + mining_claims_token::Config +{ + type Event: From> + Into<::Event>; + type MiningExecutionTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; + // type MiningExecutionTokenExecutorAccountID: Parameter + // + Member + // + AtLeast32Bit + // + Bounded + // + Default + // + Copy; +} + +// type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; + +#[derive(Encode, Decode, Clone, PartialEq, Eq)] +#[cfg_attr(feature = "std", derive(Debug))] +pub struct MiningExecutionToken(pub [u8; 16]); + +#[cfg_attr(feature = "std", derive(Debug))] +#[derive(Encode, Decode, Default, Clone, PartialEq)] +pub struct MiningExecutionTokenExecutionResult { + pub token_execution_executor_account_id: U, + pub token_execution_started_block: V, + pub token_execution_ended_block: W, +} + +decl_event!( + pub enum Event where + ::AccountId, + ::MiningExecutionTokenIndex, + // ::MiningExecutionTokenExecutorAccountID, + ::MiningConfigTokenIndex, + ::BlockNumber, + // Balance = BalanceOf, + { + /// A mining_execution_token is created. (owner, mining_execution_token_id) + Created(AccountId, MiningExecutionTokenIndex), + /// A mining_execution_token is transferred. (from, to, mining_execution_token_id) + Transferred(AccountId, AccountId, MiningExecutionTokenIndex), + MiningExecutionTokenExecutionResultSet( + AccountId, MiningConfigTokenIndex, MiningExecutionTokenIndex, + AccountId, BlockNumber, BlockNumber + ), + /// A mining_execution_token is assigned to an mining_token. + /// (owner of mining_token, mining_execution_token_id, mining_config_token_id) + AssignedTokenExecutionToConfiguration(AccountId, MiningExecutionTokenIndex, MiningConfigTokenIndex), + } +); + +// This module's storage items. +decl_storage! { + trait Store for Module as MiningExecutionToken { + /// Stores all the mining_execution_tokens, key is the mining_execution_token id / index + pub MiningExecutionTokens get(fn mining_execution_token): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; + + /// Stores the total number of mining_execution_tokens. i.e. the next mining_execution_token index + pub MiningExecutionTokenCount get(fn mining_execution_token_count): T::MiningExecutionTokenIndex; + + /// Stores mining_execution_token owner + pub MiningExecutionTokenOwners get(fn mining_execution_token_owner): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; + + /// Stores mining_execution_token_execution_result + pub MiningExecutionTokenExecutionResults get(fn mining_execution_token_execution_results): map hasher(opaque_blake2_256) (T::MiningConfigTokenIndex, T::MiningExecutionTokenIndex) => + Option>; + + /// Get mining_config_token_id belonging to a mining_execution_token_id + pub TokenExecutionConfiguration get(fn token_execution_configuration): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; + + /// Get mining_execution_token_id's belonging to a mining_config_token_id + pub TokenConfigExecution get(fn token_config_execution): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option> + } +} + +// The module's dispatchable functions. +decl_module! { + /// The module declaration. + pub struct Module for enum Call where origin: T::Origin { + fn deposit_event() = default; + + /// Create a new mining mining_execution_token + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn create(origin) { + let sender = ensure_signed(origin)?; + let mining_execution_token_id = Self::next_mining_execution_token_id()?; + + // Generate a random 128bit value + let unique_id = Self::random_value(&sender); + + // Create and store mining_execution_token + let mining_execution_token = MiningExecutionToken(unique_id); + Self::insert_mining_execution_token(&sender, mining_execution_token_id, mining_execution_token); + + Self::deposit_event(RawEvent::Created(sender, mining_execution_token_id)); + } + + /// Transfer a mining_execution_token to new owner + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn transfer(origin, to: T::AccountId, mining_execution_token_id: T::MiningExecutionTokenIndex) { + let sender = ensure_signed(origin)?; + + ensure!(Self::mining_execution_token_owner(mining_execution_token_id) == Some(sender.clone()), "Only owner can transfer mining mining_execution_token"); + + Self::update_owner(&to, mining_execution_token_id); + + Self::deposit_event(RawEvent::Transferred(sender, to, mining_execution_token_id)); + } + + /// Set mining_execution_token_execution_result + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn set_mining_execution_token_execution_result( + origin, + mining_config_token_id: T::MiningConfigTokenIndex, + mining_execution_token_id: T::MiningExecutionTokenIndex, + _token_execution_started_block: Option, + _token_execution_ended_block: Option, + ) { + let sender = ensure_signed(origin)?; + + // Ensure that the mining_execution_token_id whose config we want to change actually exists + let is_mining_execution_token = Self::exists_mining_execution_token(mining_execution_token_id).is_ok(); + ensure!(is_mining_execution_token, "MiningExecutionToken does not exist"); + + // Ensure that the caller is owner of the mining_execution_token_execution_result they are trying to change + ensure!(Self::mining_execution_token_owner(mining_execution_token_id) == Some(sender.clone()), "Only owner can set mining_execution_token_execution_result"); + + // Check that only allow the owner of the configuration that the execution belongs to call this extrinsic to set and execute + ensure!( + >::is_mining_config_token_owner( + mining_config_token_id, sender.clone() + ).is_ok(), + "Only the configuration_token owner can execute their associated execution" + ); + + // TODO - adjust defaults + let token_execution_executor_account_id = sender.clone(); + let token_execution_started_block = match _token_execution_started_block.clone() { + Some(value) => value, + None => >::block_number() + }; + let token_execution_ended_block = match _token_execution_ended_block { + Some(value) => value, + None => >::block_number() + 1u32.into() // Default + }; + + // FIXME + // // Ensure that the associated token configuration has a token_execution_started_block > current_block + // let is_token_execution_started_block_greater_than_current_block = Self::token_execution_started_block_greater_than_current_block(mining_execution_token_id, mining_config_token_id).is_ok(); + // ensure!(is_token_execution_started_block_greater_than_current_block, "token execution does not have a token_execution_started_block > current_block"); + + // FIXME + // // Ensure that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks + // let is_token_lock_interval_blocks_greater_than_token_lock_min_blocks = Self::token_lock_interval_blocks_greater_than_token_lock_min_blocks(mining_execution_token_id, mining_config_token_id).is_ok(); + // ensure!(is_token_lock_interval_blocks_greater_than_token_lock_min_blocks, "token configuration does not have a token_lock_interval_blocks > token_lock_min_blocks"); + + // Ensure that the associated token configuration has a token_lock_amount > token_lock_min_amount + let is_token_lock_amount_greater_than_token_lock_min_amount = Self::token_lock_amount_greater_than_token_lock_min_amount(mining_execution_token_id, mining_config_token_id).is_ok(); + ensure!(is_token_lock_amount_greater_than_token_lock_min_amount, "token configuration does not have a token_lock_amount > token_lock_min_amount"); + + // Check if a mining_execution_token_execution_result already exists with the given mining_execution_token_id + // to determine whether to insert new or mutate existing. + if Self::has_value_for_mining_execution_token_execution_result_index(mining_config_token_id, mining_execution_token_id).is_ok() { + debug::info!("Mutating values"); + >::mutate((mining_config_token_id, mining_execution_token_id), |mining_execution_token_execution_result| { + if let Some(_mining_execution_token_execution_result) = mining_execution_token_execution_result { + // Only update the value of a key in a KV pair if the corresponding parameter value has been provided + _mining_execution_token_execution_result.token_execution_executor_account_id = token_execution_executor_account_id.clone(); + _mining_execution_token_execution_result.token_execution_started_block = token_execution_started_block.clone(); + _mining_execution_token_execution_result.token_execution_ended_block = token_execution_ended_block.clone(); + } + }); + debug::info!("Checking mutated values"); + let fetched_mining_execution_token_execution_result = >::get((mining_config_token_id, mining_execution_token_id)); + if let Some(_mining_execution_token_execution_result) = fetched_mining_execution_token_execution_result { + debug::info!("Latest field token_execution_executor_account_id {:#?}", _mining_execution_token_execution_result.token_execution_executor_account_id); + debug::info!("Latest field token_execution_started_block {:#?}", _mining_execution_token_execution_result.token_execution_started_block); + debug::info!("Latest field token_execution_ended_block {:#?}", _mining_execution_token_execution_result.token_execution_ended_block); + } + } else { + debug::info!("Inserting values"); + + // Create a new mining mining_execution_token_execution_result instance with the input params + let mining_execution_token_execution_result_instance = MiningExecutionTokenExecutionResult { + // Since each parameter passed into the function is optional (i.e. `Option`) + // we will assign a default value if a parameter value is not provided. + token_execution_executor_account_id: token_execution_executor_account_id.clone(), + token_execution_started_block: token_execution_started_block.clone(), + token_execution_ended_block: token_execution_ended_block.clone(), + }; + + >::insert( + (mining_config_token_id, mining_execution_token_id), + &mining_execution_token_execution_result_instance + ); + + debug::info!("Checking inserted values"); + let fetched_mining_execution_token_execution_result = >::get((mining_config_token_id, mining_execution_token_id)); + if let Some(_mining_execution_token_execution_result) = fetched_mining_execution_token_execution_result { + debug::info!("Inserted field token_execution_executor_account_id {:#?}", _mining_execution_token_execution_result.token_execution_executor_account_id); + debug::info!("Inserted field token_execution_started_block {:#?}", _mining_execution_token_execution_result.token_execution_started_block); + debug::info!("Inserted field token_execution_ended_block {:#?}", _mining_execution_token_execution_result.token_execution_ended_block); + } + } + + Self::deposit_event(RawEvent::MiningExecutionTokenExecutionResultSet( + sender.clone(), + mining_config_token_id, + mining_execution_token_id, + token_execution_executor_account_id.clone(), + token_execution_started_block, + token_execution_ended_block, + )); + + + + if Self::execution( + sender.clone(), + mining_config_token_id, + mining_execution_token_id, + token_execution_executor_account_id.clone(), + token_execution_started_block, + token_execution_ended_block, + ).is_ok() { + debug::info!("Executed"); + } else { + debug::info!("Cannot execute"); + } + } + + #[weight = 10_000 + T::DbWeight::get().writes(1)] + pub fn assign_execution_to_configuration( + origin, + mining_execution_token_id: T::MiningExecutionTokenIndex, + mining_config_token_id: T::MiningConfigTokenIndex + ) { + let sender = ensure_signed(origin)?; + + // Ensure that the given configuration id already exists + let is_configuration_token = > + ::exists_mining_config_token(mining_config_token_id).is_ok(); + ensure!(is_configuration_token, "configuration_token does not exist"); + + // Ensure that caller of the function is the owner of the configuration id to assign the execution to + ensure!( + >::is_mining_config_token_owner(mining_config_token_id, sender.clone()).is_ok(), + "Only the configuration_token owner can assign itself a execution" + ); + + Self::associate_token_execution_with_configuration(mining_execution_token_id, mining_config_token_id) + .expect("Unable to associate execution with configuration"); + + // Ensure that the given mining_execution_token_id already exists + let token_execution = Self::mining_execution_token(mining_execution_token_id); + ensure!(token_execution.is_some(), "Invalid mining_execution_token_id"); + + // // Ensure that the execution is not already owned by a different configuration + // // Unassign the execution from any existing configuration since it may only be owned by one configuration + // >::remove(mining_execution_token_id); + + // Assign the network owner to the given operator (even if already belongs to them) + >::insert(mining_execution_token_id, mining_config_token_id); + + Self::deposit_event(RawEvent::AssignedTokenExecutionToConfiguration(sender, mining_execution_token_id, mining_config_token_id)); + } + } +} + +impl Module { + pub fn is_mining_execution_token_owner( + mining_execution_token_id: T::MiningExecutionTokenIndex, + sender: T::AccountId, + ) -> Result<(), DispatchError> { + ensure!( + Self::mining_execution_token_owner(&mining_execution_token_id) + .map(|owner| owner == sender) + .unwrap_or(false), + "Sender is not owner of MiningExecutionToken" + ); + Ok(()) + } + + pub fn exists_mining_execution_token( + mining_execution_token_id: T::MiningExecutionTokenIndex, + ) -> Result { + match Self::mining_execution_token(mining_execution_token_id) { + Some(value) => Ok(value), + None => Err(DispatchError::Other("MiningExecutionToken does not exist")), + } + } + + pub fn exists_mining_execution_token_execution_result( + mining_config_token_id: T::MiningConfigTokenIndex, + mining_execution_token_id: T::MiningExecutionTokenIndex, + ) -> Result<(), DispatchError> { + match Self::mining_execution_token_execution_results((mining_config_token_id, mining_execution_token_id)) { + Some(_value) => Ok(()), + None => Err(DispatchError::Other("MiningExecutionTokenExecutionResult does not exist")), + } + } + + // Check that the token execution has a token_execution_started_block > current_block + pub fn token_execution_started_block_greater_than_current_block( + mining_execution_token_id: T::MiningExecutionTokenIndex, + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + // Check that the extrinsic call is made after the start date defined in the provided configuration + + let current_block = >::block_number(); + // Get the config associated with the given configuration_token + if let Some(configuration_token_config) = + >::mining_config_token_token_configs(mining_config_token_id) + { + if let _token_lock_start_block = configuration_token_config.token_lock_start_block { + ensure!( + current_block > _token_lock_start_block, + "Execution may not be made until after the start block of the lock period in the configuration" + ); + Ok(()) + } else { + return Err(DispatchError::Other("Cannot find token_config start_date associated with the execution")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + } + + // Check that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks + pub fn token_lock_interval_blocks_greater_than_token_lock_min_blocks( + mining_execution_token_id: T::MiningExecutionTokenIndex, + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + if let Some(configuration_token) = + >::mining_config_token_token_configs((mining_config_token_id)) + { + if let Some(cooldown_configuration_token) = + >::mining_config_token_token_cooldown_configs((mining_config_token_id)) + { + if let token_lock_interval_blocks = configuration_token.token_lock_interval_blocks { + if let token_lock_min_blocks = cooldown_configuration_token.token_lock_min_blocks { + ensure!( + token_lock_interval_blocks > token_lock_min_blocks, + "Lock period must be longer than the minimum lock period of the cooldown config. Cannot \ + execute." + ); + Ok(()) + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_min_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_interval_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + } + + // Check that the associated token configuration has a token_lock_amount > token_lock_min_amount + pub fn token_lock_amount_greater_than_token_lock_min_amount( + mining_execution_token_id: T::MiningExecutionTokenIndex, + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + if let Some(configuration_token) = + >::mining_config_token_token_configs((mining_config_token_id)) + { + if let Some(cooldown_configuration_token) = + >::mining_config_token_token_cooldown_configs((mining_config_token_id)) + { + if let locked_amount = configuration_token.token_lock_amount { + if let lock_min_amount = cooldown_configuration_token.token_lock_min_amount { + ensure!( + locked_amount > lock_min_amount, + "Locked amount must be larger than the minimum locked amount of the cooldown config. \ + Cannot execute." + ); + Ok(()) + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_min_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other( + "Cannot find token_config with token_lock_interval_blocks associated with the execution", + )); + } + } else { + return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); + } + } else { + return Err(DispatchError::Other("Cannot find token_config associated with the execution")); + } + } + + pub fn execution( + sender: T::AccountId, + mining_config_token_id: T::MiningConfigTokenIndex, + mining_execution_token_id: T::MiningExecutionTokenIndex, + _token_execution_executor_account_id: T::AccountId, + _token_execution_started_block: T::BlockNumber, + _token_execution_ended_block: T::BlockNumber, + ) -> Result<(), DispatchError> { + return Ok(()); + + // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module + + // TODO - Setup a function in on_finalize that automatically checks through all the accounts that have + // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to + // determine their elegibility, and perform the claim for reward and unlock their tokens + // TODO - Update tests for the above + } + + pub fn has_value_for_mining_execution_token_execution_result_index( + mining_config_token_id: T::MiningConfigTokenIndex, + mining_execution_token_id: T::MiningExecutionTokenIndex, + ) -> Result<(), DispatchError> { + debug::info!("Checking if mining_execution_token_execution_result has a value that is defined"); + let fetched_mining_execution_token_execution_result = + >::get((mining_config_token_id, mining_execution_token_id)); + if let Some(_value) = fetched_mining_execution_token_execution_result { + debug::info!("Found value for mining_execution_token_execution_result"); + return Ok(()); + } + debug::info!("No value for mining_execution_token_execution_result"); + Err(DispatchError::Other("No value for mining_execution_token_execution_result")) + } + + /// Only push the execution id onto the end of the vector if it does not already exist + pub fn associate_token_execution_with_configuration( + mining_execution_token_id: T::MiningExecutionTokenIndex, + mining_config_token_id: T::MiningConfigTokenIndex, + ) -> Result<(), DispatchError> { + // Early exit with error since do not want to append if the given configuration id already exists as a key, + // and where its corresponding value is a vector that already contains the given execution id + if let Some(configuration_execution) = Self::token_config_execution(mining_config_token_id) { + debug::info!( + "Configuration id key {:?} exists with value {:?}", + mining_config_token_id, + configuration_execution + ); + let not_configuration_contains_execution = !configuration_execution.contains(&mining_execution_token_id); + ensure!(not_configuration_contains_execution, "Configuration already contains the given execution id"); + debug::info!("Configuration id key exists but its vector value does not contain the given execution id"); + >::mutate(mining_config_token_id, |v| { + if let Some(value) = v { + value.push(mining_execution_token_id); + } + }); + debug::info!( + "Associated execution {:?} with configuration {:?}", + mining_execution_token_id, + mining_config_token_id + ); + Ok(()) + } else { + debug::info!( + "Configuration id key does not yet exist. Creating the configuration key {:?} and appending the \ + execution id {:?} to its vector value", + mining_config_token_id, + mining_execution_token_id + ); + >::insert(mining_config_token_id, &vec![mining_execution_token_id]); + Ok(()) + } + } + + fn random_value(sender: &T::AccountId) -> [u8; 16] { + let payload = ( + ::Randomness::random(&[0]), + sender, + >::extrinsic_index(), + >::block_number(), + ); + payload.using_encoded(blake2_128) + } + + fn next_mining_execution_token_id() -> Result { + let mining_execution_token_id = Self::mining_execution_token_count(); + if mining_execution_token_id == ::max_value() { + return Err(DispatchError::Other("MiningExecutionToken count overflow")); + } + Ok(mining_execution_token_id) + } + + fn insert_mining_execution_token( + owner: &T::AccountId, + mining_execution_token_id: T::MiningExecutionTokenIndex, + mining_execution_token: MiningExecutionToken, + ) { + // Create and store mining mining_execution_token + >::insert(mining_execution_token_id, mining_execution_token); + >::put(mining_execution_token_id + One::one()); + >::insert(mining_execution_token_id, owner.clone()); + } + + fn update_owner(to: &T::AccountId, mining_execution_token_id: T::MiningExecutionTokenIndex) { + >::insert(mining_execution_token_id, to); + } +} From af6d6f894b7f50747ab340c0d88d8c50fd3829b0 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 13:33:32 +0100 Subject: [PATCH 17/27] wip --- Cargo.lock | 2 ++ pallets/mining/claims/hardware/src/lib.rs | 2 +- pallets/mining/claims/token/src/lib.rs | 2 +- pallets/mining/config/hardware/src/lib.rs | 2 +- pallets/mining/config/token/Cargo.toml | 2 ++ pallets/mining/config/token/src/lib.rs | 7 +++---- pallets/mining/eligibility/hardware/src/lib.rs | 2 +- pallets/mining/eligibility/token/src/lib.rs | 2 +- pallets/mining/lodgements/hardware/src/lib.rs | 2 +- pallets/mining/rates/hardware/src/lib.rs | 2 +- pallets/mining/rates/token/Cargo.toml | 4 ++++ pallets/mining/rates/token/src/lib.rs | 5 ++--- pallets/mining/sampling/hardware/src/lib.rs | 2 +- pallets/mining/sampling/token-mining/src/lib.rs | 2 +- pallets/mining/sampling/token/src/lib.rs | 2 +- pallets/roaming/roaming-accounting-policies/src/lib.rs | 2 +- pallets/roaming/roaming-agreement-policies/src/lib.rs | 2 +- pallets/roaming/roaming-billing-policies/src/lib.rs | 2 +- pallets/roaming/roaming-charging-policies/src/lib.rs | 2 +- pallets/roaming/roaming-device-profiles/src/lib.rs | 2 +- pallets/roaming/roaming-devices/src/lib.rs | 2 +- pallets/roaming/roaming-network-profiles/src/lib.rs | 2 +- pallets/roaming/roaming-network-servers/src/lib.rs | 2 +- pallets/roaming/roaming-networks/src/lib.rs | 2 +- pallets/roaming/roaming-operators/src/lib.rs | 2 +- pallets/roaming/roaming-organizations/src/lib.rs | 2 +- pallets/roaming/roaming-packet-bundles/src/lib.rs | 2 +- pallets/roaming/roaming-routing-profiles/src/lib.rs | 2 +- pallets/roaming/roaming-service-profiles/src/lib.rs | 2 +- pallets/roaming/roaming-sessions/src/lib.rs | 2 +- runtime/src/lib.rs | 1 - 31 files changed, 38 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79c84de31..04b796b74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3225,6 +3225,7 @@ dependencies = [ "pallet-transaction-payment", "pallet-treasury", "parity-scale-codec", + "roaming-operators", "safe-mix", "sp-core", "sp-io", @@ -3302,6 +3303,7 @@ dependencies = [ "pallet-randomness-collective-flip", "pallet-transaction-payment", "parity-scale-codec", + "roaming-operators", "safe-mix", "sp-core", "sp-io", diff --git a/pallets/mining/claims/hardware/src/lib.rs b/pallets/mining/claims/hardware/src/lib.rs index 5cf86d40b..ca72eaf4c 100644 --- a/pallets/mining/claims/hardware/src/lib.rs +++ b/pallets/mining/claims/hardware/src/lib.rs @@ -374,7 +374,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/claims/token/src/lib.rs b/pallets/mining/claims/token/src/lib.rs index f56cc823b..dae85fcc4 100644 --- a/pallets/mining/claims/token/src/lib.rs +++ b/pallets/mining/claims/token/src/lib.rs @@ -475,7 +475,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/config/hardware/src/lib.rs b/pallets/mining/config/hardware/src/lib.rs index 7a50b561f..0280552d3 100644 --- a/pallets/mining/config/hardware/src/lib.rs +++ b/pallets/mining/config/hardware/src/lib.rs @@ -315,7 +315,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/config/token/Cargo.toml b/pallets/mining/config/token/Cargo.toml index 650b34b67..24f7260c1 100644 --- a/pallets/mining/config/token/Cargo.toml +++ b/pallets/mining/config/token/Cargo.toml @@ -21,6 +21,7 @@ std = [ 'sp-runtime/std', 'sp-std/std', + 'roaming-operators/std', 'mining-rates-token/std', ] @@ -38,6 +39,7 @@ sp-io = { version = '2.0.0', default_features = false } sp-runtime = { version = '2.0.0', default_features = false } sp-std = { version = '2.0.0', default_features = false } +roaming-operators = { default_features = false, package = 'roaming-operators', path = '../../../roaming/roaming-operators' } mining-rates-token = { default_features = false, package = 'mining-rates-token', path = '../../../mining/rates/token' } [dev-dependencies] diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 5aeb0314c..7a4b124fd 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -47,14 +47,13 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + mining_rates_token::Trait + pallet_treasury::Trait + pallet_balances::Trait { +pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait + pallet_treasury::Trait + pallet_balances::Trait { type Event: From> + Into<::Event>; + type Currency: LockableCurrency; type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + sp_std::iter::Step; // Mining Speed Boost Token Mining Config type MiningConfigTokenType: Parameter + Member + Default; type MiningConfigTokenLockAmount: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; - type Currency: LockableCurrency; - type Randomness: Randomness; } type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -860,7 +859,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - ::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/eligibility/hardware/src/lib.rs b/pallets/mining/eligibility/hardware/src/lib.rs index 2ac927e27..0963422e6 100644 --- a/pallets/mining/eligibility/hardware/src/lib.rs +++ b/pallets/mining/eligibility/hardware/src/lib.rs @@ -546,7 +546,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/eligibility/token/src/lib.rs b/pallets/mining/eligibility/token/src/lib.rs index 75403ce08..523cc2f7e 100644 --- a/pallets/mining/eligibility/token/src/lib.rs +++ b/pallets/mining/eligibility/token/src/lib.rs @@ -544,7 +544,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/lodgements/hardware/src/lib.rs b/pallets/mining/lodgements/hardware/src/lib.rs index b82f5f0fb..dca98dd74 100644 --- a/pallets/mining/lodgements/hardware/src/lib.rs +++ b/pallets/mining/lodgements/hardware/src/lib.rs @@ -398,7 +398,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/rates/hardware/src/lib.rs b/pallets/mining/rates/hardware/src/lib.rs index cf2a6003d..66fec584b 100644 --- a/pallets/mining/rates/hardware/src/lib.rs +++ b/pallets/mining/rates/hardware/src/lib.rs @@ -328,7 +328,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/rates/token/Cargo.toml b/pallets/mining/rates/token/Cargo.toml index 49aca57cd..df990f355 100644 --- a/pallets/mining/rates/token/Cargo.toml +++ b/pallets/mining/rates/token/Cargo.toml @@ -18,6 +18,8 @@ std = [ 'sp-io/std', 'sp-runtime/std', 'sp-std/std', + + 'roaming-operators/std', ] [dependencies] @@ -33,4 +35,6 @@ sp-io = { version = '2.0.0', default_features = false } sp-runtime = { version = '2.0.0', default_features = false } sp-std = { version = '2.0.0', default_features = false } +roaming-operators = { default_features = false, package = 'roaming-operators', path = '../../../roaming/roaming-operators' } + [dev-dependencies] diff --git a/pallets/mining/rates/token/src/lib.rs b/pallets/mining/rates/token/src/lib.rs index 2e2c61e6e..38b5ecf80 100644 --- a/pallets/mining/rates/token/src/lib.rs +++ b/pallets/mining/rates/token/src/lib.rs @@ -39,9 +39,8 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait { +pub trait Trait: frame_system::Trait + roaming_operators::Trait { type Event: From> + Into<::Event>; - type Randomness: Randomness; type MiningRatesTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + From + Into; type MiningRatesTokenTokenMXC: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; type MiningRatesTokenTokenIOTA: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; @@ -304,7 +303,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/sampling/hardware/src/lib.rs b/pallets/mining/sampling/hardware/src/lib.rs index 586bf0827..8bc11da00 100644 --- a/pallets/mining/sampling/hardware/src/lib.rs +++ b/pallets/mining/sampling/hardware/src/lib.rs @@ -352,7 +352,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/sampling/token-mining/src/lib.rs b/pallets/mining/sampling/token-mining/src/lib.rs index 4108857c9..23eb9a68c 100644 --- a/pallets/mining/sampling/token-mining/src/lib.rs +++ b/pallets/mining/sampling/token-mining/src/lib.rs @@ -370,7 +370,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/mining/sampling/token/src/lib.rs b/pallets/mining/sampling/token/src/lib.rs index 59fef00fe..ddc7e44de 100644 --- a/pallets/mining/sampling/token/src/lib.rs +++ b/pallets/mining/sampling/token/src/lib.rs @@ -349,7 +349,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-accounting-policies/src/lib.rs b/pallets/roaming/roaming-accounting-policies/src/lib.rs index 516da1750..1a2194069 100644 --- a/pallets/roaming/roaming-accounting-policies/src/lib.rs +++ b/pallets/roaming/roaming-accounting-policies/src/lib.rs @@ -379,7 +379,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-agreement-policies/src/lib.rs b/pallets/roaming/roaming-agreement-policies/src/lib.rs index 99603f5b6..000c19d64 100644 --- a/pallets/roaming/roaming-agreement-policies/src/lib.rs +++ b/pallets/roaming/roaming-agreement-policies/src/lib.rs @@ -452,7 +452,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-billing-policies/src/lib.rs b/pallets/roaming/roaming-billing-policies/src/lib.rs index 26701da5e..8755eb379 100644 --- a/pallets/roaming/roaming-billing-policies/src/lib.rs +++ b/pallets/roaming/roaming-billing-policies/src/lib.rs @@ -427,7 +427,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-charging-policies/src/lib.rs b/pallets/roaming/roaming-charging-policies/src/lib.rs index a1abae433..db4040ecb 100644 --- a/pallets/roaming/roaming-charging-policies/src/lib.rs +++ b/pallets/roaming/roaming-charging-policies/src/lib.rs @@ -440,7 +440,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-device-profiles/src/lib.rs b/pallets/roaming/roaming-device-profiles/src/lib.rs index f2e1adf13..aec3dbe93 100644 --- a/pallets/roaming/roaming-device-profiles/src/lib.rs +++ b/pallets/roaming/roaming-device-profiles/src/lib.rs @@ -367,7 +367,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-devices/src/lib.rs b/pallets/roaming/roaming-devices/src/lib.rs index 348d12a19..f842f99ab 100644 --- a/pallets/roaming/roaming-devices/src/lib.rs +++ b/pallets/roaming/roaming-devices/src/lib.rs @@ -348,7 +348,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-network-profiles/src/lib.rs b/pallets/roaming/roaming-network-profiles/src/lib.rs index 6d09cab90..67f67a3c0 100644 --- a/pallets/roaming/roaming-network-profiles/src/lib.rs +++ b/pallets/roaming/roaming-network-profiles/src/lib.rs @@ -714,7 +714,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-network-servers/src/lib.rs b/pallets/roaming/roaming-network-servers/src/lib.rs index b9a7f5bed..360e9765f 100644 --- a/pallets/roaming/roaming-network-servers/src/lib.rs +++ b/pallets/roaming/roaming-network-servers/src/lib.rs @@ -346,7 +346,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-networks/src/lib.rs b/pallets/roaming/roaming-networks/src/lib.rs index 13a665d92..3cbbd0a16 100644 --- a/pallets/roaming/roaming-networks/src/lib.rs +++ b/pallets/roaming/roaming-networks/src/lib.rs @@ -261,7 +261,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-operators/src/lib.rs b/pallets/roaming/roaming-operators/src/lib.rs index 331afa8d2..e94e5c94e 100644 --- a/pallets/roaming/roaming-operators/src/lib.rs +++ b/pallets/roaming/roaming-operators/src/lib.rs @@ -184,7 +184,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-organizations/src/lib.rs b/pallets/roaming/roaming-organizations/src/lib.rs index f8e70f9eb..93fe93b31 100644 --- a/pallets/roaming/roaming-organizations/src/lib.rs +++ b/pallets/roaming/roaming-organizations/src/lib.rs @@ -276,7 +276,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-packet-bundles/src/lib.rs b/pallets/roaming/roaming-packet-bundles/src/lib.rs index 1c42c99e4..f3c34247e 100644 --- a/pallets/roaming/roaming-packet-bundles/src/lib.rs +++ b/pallets/roaming/roaming-packet-bundles/src/lib.rs @@ -606,7 +606,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-routing-profiles/src/lib.rs b/pallets/roaming/roaming-routing-profiles/src/lib.rs index 54a459017..1995ba4e7 100644 --- a/pallets/roaming/roaming-routing-profiles/src/lib.rs +++ b/pallets/roaming/roaming-routing-profiles/src/lib.rs @@ -249,7 +249,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-service-profiles/src/lib.rs b/pallets/roaming/roaming-service-profiles/src/lib.rs index 4331de75e..e073efdd1 100644 --- a/pallets/roaming/roaming-service-profiles/src/lib.rs +++ b/pallets/roaming/roaming-service-profiles/src/lib.rs @@ -287,7 +287,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/pallets/roaming/roaming-sessions/src/lib.rs b/pallets/roaming/roaming-sessions/src/lib.rs index a2ec70f3e..8616fefa0 100644 --- a/pallets/roaming/roaming-sessions/src/lib.rs +++ b/pallets/roaming/roaming-sessions/src/lib.rs @@ -456,7 +456,7 @@ impl Module { fn random_value(sender: &T::AccountId) -> [u8; 16] { let payload = ( - T::Randomness::random(&[0]), + ::Randomness::random(&[0]), sender, >::extrinsic_index(), >::block_number(), diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 5f720976d..ed9d6e755 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -639,7 +639,6 @@ impl mining_config_hardware::Trait for Runtime { impl mining_rates_token::Trait for Runtime { type Event = Event; - type Randomness = RandomnessCollectiveFlip; type MiningRatesTokenIndex = u64; type MiningRatesTokenMaxLoyalty = u32; // Mining Speed Boost Max Rates From e841565f6be07c9d130eedc21c9af58af1b92974 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 13:45:24 +0100 Subject: [PATCH 18/27] compiles now --- runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index ed9d6e755..7f565d4cb 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -515,7 +515,7 @@ impl pallet_staking::Trait for Runtime { } impl roaming_operators::Trait for Runtime { - // type Currency = Balances; + type Currency = Balances; type Event = Event; type Randomness = RandomnessCollectiveFlip; type RoamingOperatorIndex = u64; From acd9a75db4abfcb990e29cc1f60c0a29f70eb1dd Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 15:33:39 +0100 Subject: [PATCH 19/27] clean up' --- runtime/src/lib.rs | 15 +-------------- .../tests/cli_integration_tests_mining_tokens.rs | 2 +- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 7f565d4cb..79a2c52f6 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -543,7 +543,6 @@ impl roaming_devices::Trait for Runtime { impl roaming_routing_profiles::Trait for Runtime { type Event = Event; - // https://polkadot.js.org/api/types/#primitive-types type RoamingRoutingProfileAppServer = Vec; type RoamingRoutingProfileIndex = u64; } @@ -566,7 +565,7 @@ impl roaming_accounting_policies::Trait for Runtime { impl roaming_agreement_policies::Trait for Runtime { type Event = Event; type RoamingAgreementPolicyActivationType = Vec; - type RoamingAgreementPolicyIndex = u64; // as Trait>::Moment` timestamp::Module::Moment; + type RoamingAgreementPolicyIndex = u64; } impl roaming_network_profiles::Trait for Runtime { @@ -608,30 +607,18 @@ impl roaming_packet_bundles::Trait for Runtime { } impl mining_config_token::Trait for Runtime { - // type Currency = Balances; - // type Balance = Balance; type Event = Event; - // FIXME - restore when stop temporarily using roaming-operators type Currency = Balances; - // type Randomness = RandomnessCollectiveFlip; type MiningConfigTokenIndex = u64; type MiningConfigTokenLockAmount = u64; - // Mining Speed Boost Token Mining Config - // FIXME - how to use this enum from std? (including importing `use std::str::FromStr;`) type MiningConfigTokenType = Vec; } impl mining_config_hardware::Trait for Runtime { type Event = Event; type MiningConfigHardwareDevEUI = u64; - // type MiningConfigHardwareType = - // MiningConfigHardwareTypes; type MiningConfigHardwareID = u64; - // FIXME - restore when stop temporarily using roaming-operators - // type Currency = Balances; - // type Randomness = RandomnessCollectiveFlip; type MiningConfigHardwareIndex = u64; - // Mining Speed Boost Hardware Mining Config type MiningConfigHardwareSecure = bool; // FIXME - how to use this enum from std? (including importing `use std::str::FromStr;`) type MiningConfigHardwareType = Vec; diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index ce22c109f..e5b69628a 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -447,9 +447,9 @@ mod tests { token_execution_interval_blocks: 34567, }) ); + // TODO - check that the locked amount has actually been locked and check that a sampling, eligibility, and // claim were all run automatically afterwards assert!(false); - // TODO - allow user to request to stop mining, and trigger the cooldown period until they can // access their tokens that are locked for mining. }); From 7693a14f0065670ede49e2e3c6866c3a8ed12439 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 15:34:13 +0100 Subject: [PATCH 20/27] add question on how to do tests --- runtime/tests/cli_integration_tests_mining_tokens.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index e5b69628a..dd9d58bef 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -450,6 +450,13 @@ mod tests { // TODO - check that the locked amount has actually been locked and check that a sampling, eligibility, and // claim were all run automatically afterwards assert!(false); + // + // how to get the lock info for a given lock identifier using frame_support's methods? + // https://substrate.dev/rustdocs/v3.0.0/frame_support/traits/trait.LockableCurrency.html#required-methods + // + // Or is it going to be necessary to implement our own custom lock pallet that adds this? + // https://github.com/kulupu/kulupu/blob/master/frame/lockdrop/src/lib.rs#L238 + // TODO - allow user to request to stop mining, and trigger the cooldown period until they can // access their tokens that are locked for mining. }); From e84fe277dd3aba376f695b4b53824987792fc8c3 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 15:36:06 +0100 Subject: [PATCH 21/27] add fixme comment --- runtime/tests/cli_integration_tests_mining_tokens.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index dd9d58bef..be17cf8c6 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -456,6 +456,8 @@ mod tests { // // Or is it going to be necessary to implement our own custom lock pallet that adds this? // https://github.com/kulupu/kulupu/blob/master/frame/lockdrop/src/lib.rs#L238 + // + // See question posted here https://matrix.to/#/!HzySYSaIhtyWrwiwEV:matrix.org/$161495341266906GzpcB:matrix.org?via=matrix.parity.io&via=matrix.org&via=corepaper.org // TODO - allow user to request to stop mining, and trigger the cooldown period until they can // access their tokens that are locked for mining. From 083daf264a785a5f02ed742193a5b98fe1b31159 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 16:19:46 +0100 Subject: [PATCH 22/27] remove newline --- pallets/mining/config/token/Cargo.toml | 1 - pallets/mining/execution/token/src/lib.rs | 562 ---------------------- 2 files changed, 563 deletions(-) delete mode 100644 pallets/mining/execution/token/src/lib.rs diff --git a/pallets/mining/config/token/Cargo.toml b/pallets/mining/config/token/Cargo.toml index 24f7260c1..89b2f43d6 100644 --- a/pallets/mining/config/token/Cargo.toml +++ b/pallets/mining/config/token/Cargo.toml @@ -15,7 +15,6 @@ std = [ 'pallet-randomness-collective-flip/std', 'pallet-transaction-payment/std', 'pallet-treasury/std', - 'sp-core/std', 'sp-io/std', 'sp-runtime/std', diff --git a/pallets/mining/execution/token/src/lib.rs b/pallets/mining/execution/token/src/lib.rs deleted file mode 100644 index c56b48130..000000000 --- a/pallets/mining/execution/token/src/lib.rs +++ /dev/null @@ -1,562 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -use codec::{ - Decode, - Encode, -}; -use frame_support::{ - debug, - decl_event, - decl_module, - decl_storage, - ensure, - traits::{ - Get, - Randomness, - }, - Parameter, -}; -use frame_system::ensure_signed; -use sp_io::hashing::blake2_128; -use sp_runtime::{ - traits::{ - AtLeast32Bit, - Bounded, - Member, - One, - }, - DispatchError, -}; -use sp_std::prelude::*; // Imports Vec - -// FIXME - remove roaming_operators here, only use this approach since do not know how to use BalanceOf using only -// mining runtime module -use mining_claims_token; -use mining_config_token; -use mining_eligibility_token; -use mining_rates_token; -use mining_sampling_token; - -// #[cfg(test)] -// mod mock; - -// #[cfg(test)] -// mod tests; - -/// The module's configuration trait. -pub trait Config: - frame_system::Config - + roaming_operators::Config - + mining_config_token::Config - + mining_eligibility_token::Config - + mining_rates_token::Config - + mining_sampling_token::Config - + mining_claims_token::Config -{ - type Event: From> + Into<::Event>; - type MiningExecutionTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy; - // type MiningExecutionTokenExecutorAccountID: Parameter - // + Member - // + AtLeast32Bit - // + Bounded - // + Default - // + Copy; -} - -// type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; - -#[derive(Encode, Decode, Clone, PartialEq, Eq)] -#[cfg_attr(feature = "std", derive(Debug))] -pub struct MiningExecutionToken(pub [u8; 16]); - -#[cfg_attr(feature = "std", derive(Debug))] -#[derive(Encode, Decode, Default, Clone, PartialEq)] -pub struct MiningExecutionTokenExecutionResult { - pub token_execution_executor_account_id: U, - pub token_execution_started_block: V, - pub token_execution_ended_block: W, -} - -decl_event!( - pub enum Event where - ::AccountId, - ::MiningExecutionTokenIndex, - // ::MiningExecutionTokenExecutorAccountID, - ::MiningConfigTokenIndex, - ::BlockNumber, - // Balance = BalanceOf, - { - /// A mining_execution_token is created. (owner, mining_execution_token_id) - Created(AccountId, MiningExecutionTokenIndex), - /// A mining_execution_token is transferred. (from, to, mining_execution_token_id) - Transferred(AccountId, AccountId, MiningExecutionTokenIndex), - MiningExecutionTokenExecutionResultSet( - AccountId, MiningConfigTokenIndex, MiningExecutionTokenIndex, - AccountId, BlockNumber, BlockNumber - ), - /// A mining_execution_token is assigned to an mining_token. - /// (owner of mining_token, mining_execution_token_id, mining_config_token_id) - AssignedTokenExecutionToConfiguration(AccountId, MiningExecutionTokenIndex, MiningConfigTokenIndex), - } -); - -// This module's storage items. -decl_storage! { - trait Store for Module as MiningExecutionToken { - /// Stores all the mining_execution_tokens, key is the mining_execution_token id / index - pub MiningExecutionTokens get(fn mining_execution_token): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; - - /// Stores the total number of mining_execution_tokens. i.e. the next mining_execution_token index - pub MiningExecutionTokenCount get(fn mining_execution_token_count): T::MiningExecutionTokenIndex; - - /// Stores mining_execution_token owner - pub MiningExecutionTokenOwners get(fn mining_execution_token_owner): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; - - /// Stores mining_execution_token_execution_result - pub MiningExecutionTokenExecutionResults get(fn mining_execution_token_execution_results): map hasher(opaque_blake2_256) (T::MiningConfigTokenIndex, T::MiningExecutionTokenIndex) => - Option>; - - /// Get mining_config_token_id belonging to a mining_execution_token_id - pub TokenExecutionConfiguration get(fn token_execution_configuration): map hasher(opaque_blake2_256) T::MiningExecutionTokenIndex => Option; - - /// Get mining_execution_token_id's belonging to a mining_config_token_id - pub TokenConfigExecution get(fn token_config_execution): map hasher(opaque_blake2_256) T::MiningConfigTokenIndex => Option> - } -} - -// The module's dispatchable functions. -decl_module! { - /// The module declaration. - pub struct Module for enum Call where origin: T::Origin { - fn deposit_event() = default; - - /// Create a new mining mining_execution_token - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn create(origin) { - let sender = ensure_signed(origin)?; - let mining_execution_token_id = Self::next_mining_execution_token_id()?; - - // Generate a random 128bit value - let unique_id = Self::random_value(&sender); - - // Create and store mining_execution_token - let mining_execution_token = MiningExecutionToken(unique_id); - Self::insert_mining_execution_token(&sender, mining_execution_token_id, mining_execution_token); - - Self::deposit_event(RawEvent::Created(sender, mining_execution_token_id)); - } - - /// Transfer a mining_execution_token to new owner - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn transfer(origin, to: T::AccountId, mining_execution_token_id: T::MiningExecutionTokenIndex) { - let sender = ensure_signed(origin)?; - - ensure!(Self::mining_execution_token_owner(mining_execution_token_id) == Some(sender.clone()), "Only owner can transfer mining mining_execution_token"); - - Self::update_owner(&to, mining_execution_token_id); - - Self::deposit_event(RawEvent::Transferred(sender, to, mining_execution_token_id)); - } - - /// Set mining_execution_token_execution_result - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn set_mining_execution_token_execution_result( - origin, - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - _token_execution_started_block: Option, - _token_execution_ended_block: Option, - ) { - let sender = ensure_signed(origin)?; - - // Ensure that the mining_execution_token_id whose config we want to change actually exists - let is_mining_execution_token = Self::exists_mining_execution_token(mining_execution_token_id).is_ok(); - ensure!(is_mining_execution_token, "MiningExecutionToken does not exist"); - - // Ensure that the caller is owner of the mining_execution_token_execution_result they are trying to change - ensure!(Self::mining_execution_token_owner(mining_execution_token_id) == Some(sender.clone()), "Only owner can set mining_execution_token_execution_result"); - - // Check that only allow the owner of the configuration that the execution belongs to call this extrinsic to set and execute - ensure!( - >::is_mining_config_token_owner( - mining_config_token_id, sender.clone() - ).is_ok(), - "Only the configuration_token owner can execute their associated execution" - ); - - // TODO - adjust defaults - let token_execution_executor_account_id = sender.clone(); - let token_execution_started_block = match _token_execution_started_block.clone() { - Some(value) => value, - None => >::block_number() - }; - let token_execution_ended_block = match _token_execution_ended_block { - Some(value) => value, - None => >::block_number() + 1u32.into() // Default - }; - - // FIXME - // // Ensure that the associated token configuration has a token_execution_started_block > current_block - // let is_token_execution_started_block_greater_than_current_block = Self::token_execution_started_block_greater_than_current_block(mining_execution_token_id, mining_config_token_id).is_ok(); - // ensure!(is_token_execution_started_block_greater_than_current_block, "token execution does not have a token_execution_started_block > current_block"); - - // FIXME - // // Ensure that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks - // let is_token_lock_interval_blocks_greater_than_token_lock_min_blocks = Self::token_lock_interval_blocks_greater_than_token_lock_min_blocks(mining_execution_token_id, mining_config_token_id).is_ok(); - // ensure!(is_token_lock_interval_blocks_greater_than_token_lock_min_blocks, "token configuration does not have a token_lock_interval_blocks > token_lock_min_blocks"); - - // Ensure that the associated token configuration has a token_lock_amount > token_lock_min_amount - let is_token_lock_amount_greater_than_token_lock_min_amount = Self::token_lock_amount_greater_than_token_lock_min_amount(mining_execution_token_id, mining_config_token_id).is_ok(); - ensure!(is_token_lock_amount_greater_than_token_lock_min_amount, "token configuration does not have a token_lock_amount > token_lock_min_amount"); - - // Check if a mining_execution_token_execution_result already exists with the given mining_execution_token_id - // to determine whether to insert new or mutate existing. - if Self::has_value_for_mining_execution_token_execution_result_index(mining_config_token_id, mining_execution_token_id).is_ok() { - debug::info!("Mutating values"); - >::mutate((mining_config_token_id, mining_execution_token_id), |mining_execution_token_execution_result| { - if let Some(_mining_execution_token_execution_result) = mining_execution_token_execution_result { - // Only update the value of a key in a KV pair if the corresponding parameter value has been provided - _mining_execution_token_execution_result.token_execution_executor_account_id = token_execution_executor_account_id.clone(); - _mining_execution_token_execution_result.token_execution_started_block = token_execution_started_block.clone(); - _mining_execution_token_execution_result.token_execution_ended_block = token_execution_ended_block.clone(); - } - }); - debug::info!("Checking mutated values"); - let fetched_mining_execution_token_execution_result = >::get((mining_config_token_id, mining_execution_token_id)); - if let Some(_mining_execution_token_execution_result) = fetched_mining_execution_token_execution_result { - debug::info!("Latest field token_execution_executor_account_id {:#?}", _mining_execution_token_execution_result.token_execution_executor_account_id); - debug::info!("Latest field token_execution_started_block {:#?}", _mining_execution_token_execution_result.token_execution_started_block); - debug::info!("Latest field token_execution_ended_block {:#?}", _mining_execution_token_execution_result.token_execution_ended_block); - } - } else { - debug::info!("Inserting values"); - - // Create a new mining mining_execution_token_execution_result instance with the input params - let mining_execution_token_execution_result_instance = MiningExecutionTokenExecutionResult { - // Since each parameter passed into the function is optional (i.e. `Option`) - // we will assign a default value if a parameter value is not provided. - token_execution_executor_account_id: token_execution_executor_account_id.clone(), - token_execution_started_block: token_execution_started_block.clone(), - token_execution_ended_block: token_execution_ended_block.clone(), - }; - - >::insert( - (mining_config_token_id, mining_execution_token_id), - &mining_execution_token_execution_result_instance - ); - - debug::info!("Checking inserted values"); - let fetched_mining_execution_token_execution_result = >::get((mining_config_token_id, mining_execution_token_id)); - if let Some(_mining_execution_token_execution_result) = fetched_mining_execution_token_execution_result { - debug::info!("Inserted field token_execution_executor_account_id {:#?}", _mining_execution_token_execution_result.token_execution_executor_account_id); - debug::info!("Inserted field token_execution_started_block {:#?}", _mining_execution_token_execution_result.token_execution_started_block); - debug::info!("Inserted field token_execution_ended_block {:#?}", _mining_execution_token_execution_result.token_execution_ended_block); - } - } - - Self::deposit_event(RawEvent::MiningExecutionTokenExecutionResultSet( - sender.clone(), - mining_config_token_id, - mining_execution_token_id, - token_execution_executor_account_id.clone(), - token_execution_started_block, - token_execution_ended_block, - )); - - - - if Self::execution( - sender.clone(), - mining_config_token_id, - mining_execution_token_id, - token_execution_executor_account_id.clone(), - token_execution_started_block, - token_execution_ended_block, - ).is_ok() { - debug::info!("Executed"); - } else { - debug::info!("Cannot execute"); - } - } - - #[weight = 10_000 + T::DbWeight::get().writes(1)] - pub fn assign_execution_to_configuration( - origin, - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex - ) { - let sender = ensure_signed(origin)?; - - // Ensure that the given configuration id already exists - let is_configuration_token = > - ::exists_mining_config_token(mining_config_token_id).is_ok(); - ensure!(is_configuration_token, "configuration_token does not exist"); - - // Ensure that caller of the function is the owner of the configuration id to assign the execution to - ensure!( - >::is_mining_config_token_owner(mining_config_token_id, sender.clone()).is_ok(), - "Only the configuration_token owner can assign itself a execution" - ); - - Self::associate_token_execution_with_configuration(mining_execution_token_id, mining_config_token_id) - .expect("Unable to associate execution with configuration"); - - // Ensure that the given mining_execution_token_id already exists - let token_execution = Self::mining_execution_token(mining_execution_token_id); - ensure!(token_execution.is_some(), "Invalid mining_execution_token_id"); - - // // Ensure that the execution is not already owned by a different configuration - // // Unassign the execution from any existing configuration since it may only be owned by one configuration - // >::remove(mining_execution_token_id); - - // Assign the network owner to the given operator (even if already belongs to them) - >::insert(mining_execution_token_id, mining_config_token_id); - - Self::deposit_event(RawEvent::AssignedTokenExecutionToConfiguration(sender, mining_execution_token_id, mining_config_token_id)); - } - } -} - -impl Module { - pub fn is_mining_execution_token_owner( - mining_execution_token_id: T::MiningExecutionTokenIndex, - sender: T::AccountId, - ) -> Result<(), DispatchError> { - ensure!( - Self::mining_execution_token_owner(&mining_execution_token_id) - .map(|owner| owner == sender) - .unwrap_or(false), - "Sender is not owner of MiningExecutionToken" - ); - Ok(()) - } - - pub fn exists_mining_execution_token( - mining_execution_token_id: T::MiningExecutionTokenIndex, - ) -> Result { - match Self::mining_execution_token(mining_execution_token_id) { - Some(value) => Ok(value), - None => Err(DispatchError::Other("MiningExecutionToken does not exist")), - } - } - - pub fn exists_mining_execution_token_execution_result( - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - ) -> Result<(), DispatchError> { - match Self::mining_execution_token_execution_results((mining_config_token_id, mining_execution_token_id)) { - Some(_value) => Ok(()), - None => Err(DispatchError::Other("MiningExecutionTokenExecutionResult does not exist")), - } - } - - // Check that the token execution has a token_execution_started_block > current_block - pub fn token_execution_started_block_greater_than_current_block( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - // Check that the extrinsic call is made after the start date defined in the provided configuration - - let current_block = >::block_number(); - // Get the config associated with the given configuration_token - if let Some(configuration_token_config) = - >::mining_config_token_token_configs(mining_config_token_id) - { - if let _token_lock_start_block = configuration_token_config.token_lock_start_block { - ensure!( - current_block > _token_lock_start_block, - "Execution may not be made until after the start block of the lock period in the configuration" - ); - Ok(()) - } else { - return Err(DispatchError::Other("Cannot find token_config start_date associated with the execution")); - } - } else { - return Err(DispatchError::Other("Cannot find token_config associated with the execution")); - } - } - - // Check that the associated token configuration has a token_lock_interval_blocks > token_lock_min_blocks - pub fn token_lock_interval_blocks_greater_than_token_lock_min_blocks( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - if let Some(configuration_token) = - >::mining_config_token_token_configs((mining_config_token_id)) - { - if let Some(cooldown_configuration_token) = - >::mining_config_token_token_cooldown_configs((mining_config_token_id)) - { - if let token_lock_interval_blocks = configuration_token.token_lock_interval_blocks { - if let token_lock_min_blocks = cooldown_configuration_token.token_lock_min_blocks { - ensure!( - token_lock_interval_blocks > token_lock_min_blocks, - "Lock period must be longer than the minimum lock period of the cooldown config. Cannot \ - execute." - ); - Ok(()) - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_min_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_interval_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); - } - } else { - return Err(DispatchError::Other("Cannot find token_config associated with the execution")); - } - } - - // Check that the associated token configuration has a token_lock_amount > token_lock_min_amount - pub fn token_lock_amount_greater_than_token_lock_min_amount( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - if let Some(configuration_token) = - >::mining_config_token_token_configs((mining_config_token_id)) - { - if let Some(cooldown_configuration_token) = - >::mining_config_token_token_cooldown_configs((mining_config_token_id)) - { - if let locked_amount = configuration_token.token_lock_amount { - if let lock_min_amount = cooldown_configuration_token.token_lock_min_amount { - ensure!( - locked_amount > lock_min_amount, - "Locked amount must be larger than the minimum locked amount of the cooldown config. \ - Cannot execute." - ); - Ok(()) - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_min_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other( - "Cannot find token_config with token_lock_interval_blocks associated with the execution", - )); - } - } else { - return Err(DispatchError::Other("Cannot find token_cooldown_config associated with the execution")); - } - } else { - return Err(DispatchError::Other("Cannot find token_config associated with the execution")); - } - } - - pub fn execution( - sender: T::AccountId, - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - _token_execution_executor_account_id: T::AccountId, - _token_execution_started_block: T::BlockNumber, - _token_execution_ended_block: T::BlockNumber, - ) -> Result<(), DispatchError> { - return Ok(()); - - // TODO - Lock the token_lock_amount for the token_lock_interval_blocks using the Balances module - - // TODO - Setup a function in on_finalize that automatically checks through all the accounts that have - // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to - // determine their elegibility, and perform the claim for reward and unlock their tokens - // TODO - Update tests for the above - } - - pub fn has_value_for_mining_execution_token_execution_result_index( - mining_config_token_id: T::MiningConfigTokenIndex, - mining_execution_token_id: T::MiningExecutionTokenIndex, - ) -> Result<(), DispatchError> { - debug::info!("Checking if mining_execution_token_execution_result has a value that is defined"); - let fetched_mining_execution_token_execution_result = - >::get((mining_config_token_id, mining_execution_token_id)); - if let Some(_value) = fetched_mining_execution_token_execution_result { - debug::info!("Found value for mining_execution_token_execution_result"); - return Ok(()); - } - debug::info!("No value for mining_execution_token_execution_result"); - Err(DispatchError::Other("No value for mining_execution_token_execution_result")) - } - - /// Only push the execution id onto the end of the vector if it does not already exist - pub fn associate_token_execution_with_configuration( - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_config_token_id: T::MiningConfigTokenIndex, - ) -> Result<(), DispatchError> { - // Early exit with error since do not want to append if the given configuration id already exists as a key, - // and where its corresponding value is a vector that already contains the given execution id - if let Some(configuration_execution) = Self::token_config_execution(mining_config_token_id) { - debug::info!( - "Configuration id key {:?} exists with value {:?}", - mining_config_token_id, - configuration_execution - ); - let not_configuration_contains_execution = !configuration_execution.contains(&mining_execution_token_id); - ensure!(not_configuration_contains_execution, "Configuration already contains the given execution id"); - debug::info!("Configuration id key exists but its vector value does not contain the given execution id"); - >::mutate(mining_config_token_id, |v| { - if let Some(value) = v { - value.push(mining_execution_token_id); - } - }); - debug::info!( - "Associated execution {:?} with configuration {:?}", - mining_execution_token_id, - mining_config_token_id - ); - Ok(()) - } else { - debug::info!( - "Configuration id key does not yet exist. Creating the configuration key {:?} and appending the \ - execution id {:?} to its vector value", - mining_config_token_id, - mining_execution_token_id - ); - >::insert(mining_config_token_id, &vec![mining_execution_token_id]); - Ok(()) - } - } - - fn random_value(sender: &T::AccountId) -> [u8; 16] { - let payload = ( - ::Randomness::random(&[0]), - sender, - >::extrinsic_index(), - >::block_number(), - ); - payload.using_encoded(blake2_128) - } - - fn next_mining_execution_token_id() -> Result { - let mining_execution_token_id = Self::mining_execution_token_count(); - if mining_execution_token_id == ::max_value() { - return Err(DispatchError::Other("MiningExecutionToken count overflow")); - } - Ok(mining_execution_token_id) - } - - fn insert_mining_execution_token( - owner: &T::AccountId, - mining_execution_token_id: T::MiningExecutionTokenIndex, - mining_execution_token: MiningExecutionToken, - ) { - // Create and store mining mining_execution_token - >::insert(mining_execution_token_id, mining_execution_token); - >::put(mining_execution_token_id + One::one()); - >::insert(mining_execution_token_id, owner.clone()); - } - - fn update_owner(to: &T::AccountId, mining_execution_token_id: T::MiningExecutionTokenIndex) { - >::insert(mining_execution_token_id, to); - } -} From 878374101569fc193bff5d35b6430095eb29b5a1 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Fri, 5 Mar 2021 16:27:29 +0100 Subject: [PATCH 23/27] lint --- pallets/mining/config/token/src/lib.rs | 40 +++++++++++++------------- runtime/src/lib.rs | 2 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/pallets/mining/config/token/src/lib.rs b/pallets/mining/config/token/src/lib.rs index 7a4b124fd..2dbdc453a 100644 --- a/pallets/mining/config/token/src/lib.rs +++ b/pallets/mining/config/token/src/lib.rs @@ -34,8 +34,10 @@ use sp_runtime::{ }, DispatchError, }; -use sp_std::convert::TryInto; -use sp_std::prelude::*; // Imports Vec +use sp_std::{ + convert::TryInto, + prelude::*, +}; // Imports Vec // FIXME - remove roaming_operators here, only use this approach since do not know how to use BalanceOf using only // mining runtime module @@ -47,9 +49,15 @@ mod mock; mod tests; /// The module's configuration trait. -pub trait Trait: frame_system::Trait + roaming_operators::Trait + mining_rates_token::Trait + pallet_treasury::Trait + pallet_balances::Trait { +pub trait Trait: + frame_system::Trait + + roaming_operators::Trait + + mining_rates_token::Trait + + pallet_treasury::Trait + + pallet_balances::Trait +{ type Event: From> + Into<::Event>; - type Currency: LockableCurrency; + type Currency: LockableCurrency; type MiningConfigTokenIndex: Parameter + Member + AtLeast32Bit + Bounded + Default + Copy + sp_std::iter::Step; // Mining Speed Boost Token Mining Config type MiningConfigTokenType: Parameter + Member + Default; @@ -671,9 +679,7 @@ impl Module { let current_block = >::block_number(); // Get the config associated with the given configuration_token - if let Some(configuration_token_config) = - Self::mining_config_token_configs(mining_config_token_id) - { + if let Some(configuration_token_config) = Self::mining_config_token_configs(mining_config_token_id) { if let _token_lock_start_block = configuration_token_config.token_lock_start_block { ensure!( current_block > _token_lock_start_block, @@ -692,9 +698,7 @@ impl Module { pub fn token_lock_interval_blocks_greater_than_token_lock_min_blocks( mining_config_token_id: T::MiningConfigTokenIndex, ) -> Result<(), DispatchError> { - if let Some(configuration_token) = - Self::mining_config_token_configs((mining_config_token_id)) - { + if let Some(configuration_token) = Self::mining_config_token_configs((mining_config_token_id)) { if let Some(cooldown_configuration_token) = Self::mining_config_token_cooldown_configs((mining_config_token_id)) { @@ -728,9 +732,7 @@ impl Module { pub fn token_lock_amount_greater_than_token_lock_min_amount( mining_config_token_id: T::MiningConfigTokenIndex, ) -> Result<(), DispatchError> { - if let Some(configuration_token) = - Self::mining_config_token_configs((mining_config_token_id)) - { + if let Some(configuration_token) = Self::mining_config_token_configs((mining_config_token_id)) { if let Some(cooldown_configuration_token) = Self::mining_config_token_cooldown_configs((mining_config_token_id)) { @@ -815,13 +817,13 @@ impl Module { // successfully been locked, whether it is the end of their cooldown period and if so sample the balance, to // determine their elegibility, and perform the claim for reward and unlock their tokens // TODO - Update tests for the above - if let Some(configuration_token) = - Self::mining_config_token_configs((mining_config_token_id)) - { + if let Some(configuration_token) = Self::mining_config_token_configs((mining_config_token_id)) { if let lock_amount = configuration_token.token_lock_amount { if let Some(lock_amount_lockable_currency_try) = TryInto::>::try_into(lock_amount).ok() { if let lock_amount_lockable_currency = lock_amount_lockable_currency_try { - if let Some(execution_results) = Self::mining_config_token_execution_results(mining_config_token_id) { + if let Some(execution_results) = + Self::mining_config_token_execution_results(mining_config_token_id) + { const EXAMPLE_ID: LockIdentifier = *b"example "; ::Currency::set_lock( @@ -843,9 +845,7 @@ impl Module { )); } } else { - return Err(DispatchError::Other( - "Cannot find lock_amount associated with the execution", - )); + return Err(DispatchError::Other("Cannot find lock_amount associated with the execution")); } } else { return Err(DispatchError::Other( diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 828ead490..48d776c98 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -607,8 +607,8 @@ impl roaming_packet_bundles::Trait for Runtime { } impl mining_config_token::Trait for Runtime { - type Event = Event; type Currency = Balances; + type Event = Event; type MiningConfigTokenIndex = u64; type MiningConfigTokenLockAmount = u64; type MiningConfigTokenType = Vec; From bd0be46bce8633b45f3bac67c342591f283dceea Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Sat, 6 Mar 2021 01:24:59 +0100 Subject: [PATCH 24/27] try get tests to pass --- runtime/src/lib.rs | 2 +- .../cli_integration_tests_mining_tokens.rs | 112 ++++++++++++++++-- 2 files changed, 101 insertions(+), 13 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 48d776c98..ef5135c0e 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -320,7 +320,7 @@ parameter_types! { pub const CouncilMaxMembers: u32 = 100; } -type GeneralCouncilInstance = pallet_collective::Instance1; +pub type GeneralCouncilInstance = pallet_collective::Instance1; impl pallet_collective::Trait for Runtime { type DefaultVote = pallet_collective::PrimeDefaultVote; type Event = Event; diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index be17cf8c6..c8b5787ab 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -14,12 +14,23 @@ mod tests { assert_ok, impl_outer_origin, parameter_types, + traits::{ + Contains, + ContainsLengthBound, + }, weights::{ IdentityFee, Weight, }, }; - + use std::cell::RefCell; + use sp_core::{ + u32_trait::{ + _2, + _3, + _4, + }, + }; use sp_core::H256; use sp_runtime::{ testing::Header, @@ -29,7 +40,9 @@ mod tests { Zero, }, DispatchResult, + ModuleId, Perbill, + Percent, Permill, }; // Import Trait for each runtime module being tested @@ -41,6 +54,7 @@ mod tests { use mining_config_token::{ MiningConfigTokenConfig, MiningConfigTokenRequirementsConfig, + MiningConfigTokenExecutionResult, Module as MiningConfigTokenModule, Trait as MiningConfigTokenTrait, }; @@ -59,6 +73,10 @@ mod tests { Module as MiningSamplingTokenModule, Trait as MiningSamplingTokenTrait, }; + use datahighway_runtime::{ + AccountId, + Balance, + }; use roaming_operators; // pub fn origin_of(who: &AccountId) -> ::Origin { @@ -124,6 +142,86 @@ mod tests { type TransactionByteFee = (); type WeightToFee = IdentityFee; } + + thread_local! { + static TEN_TO_FOURTEEN: RefCell> = RefCell::new(vec![10,11,12,13,14]); + } + pub struct TenToFourteen; + impl Contains for TenToFourteen { + fn sorted_members() -> Vec { + TEN_TO_FOURTEEN.with(|v| v.borrow().clone()) + } + + #[cfg(feature = "runtime-benchmarks")] + fn add(new: &u64) { + TEN_TO_FOURTEEN.with(|v| { + let mut members = v.borrow_mut(); + members.push(*new); + members.sort(); + }) + } + } + impl ContainsLengthBound for TenToFourteen { + fn max_len() -> usize { + TEN_TO_FOURTEEN.with(|v| v.borrow().len()) + } + + fn min_len() -> usize { + 0 + } + } + + // Copied from ./runtime/src/constants.rs + const MILLISECS_PER_BLOCK: u64 = 4320; + const MINUTES: u64 = 60_000 / (MILLISECS_PER_BLOCK as u64); + const HOURS: u64 = MINUTES * 60; + const DAYS: u64 = HOURS * 24; + parameter_types! { + pub const ProposalBond: Permill = Permill::from_percent(5); + pub const ProposalBondMinimum: u64 = 1; + pub const SpendPeriod: u64 = 1 * DAYS; + pub const Burn: Permill = Permill::from_percent(0); + pub const TipCountdown: u64 = 1 * DAYS; + pub const TipFindersFee: Percent = Percent::from_percent(20); + pub const TipReportDepositBase: u64 = 1; + pub const MaximumReasonLength: u32 = 16384; + pub const BountyValueMinimum: u64 = 1; + pub const BountyCuratorDeposit: Permill = Permill::from_percent(50); + pub const BountyDepositBase: u64 = 80; + pub const BountyDepositPayoutDelay: u32 = 3; + pub const BountyUpdatePeriod: u32 = 20; + pub const DataDepositPerByte: u64 = 1; + pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry"); + } + impl pallet_treasury::Trait for Test { + // type ApproveOrigin = pallet_collective::EnsureMembers<_4, AccountId, GeneralCouncilInstance>; + type ApproveOrigin = frame_system::EnsureRoot; + type BountyCuratorDeposit = BountyCuratorDeposit; + type BountyDepositBase = BountyDepositBase; + type BountyDepositPayoutDelay = BountyDepositPayoutDelay; + type BountyUpdatePeriod = BountyUpdatePeriod; + type BountyValueMinimum = BountyValueMinimum; + type Burn = Burn; + type BurnDestination = (); + type Currency = Balances; + type DataDepositPerByte = DataDepositPerByte; + type Event = (); + type MaximumReasonLength = MaximumReasonLength; + type ModuleId = TreasuryModuleId; + type OnSlash = (); + type ProposalBond = ProposalBond; + type ProposalBondMinimum = ProposalBondMinimum; + // type RejectOrigin = pallet_collective::EnsureMembers<_2, AccountId, GeneralCouncilInstance>; + type RejectOrigin = frame_system::EnsureRoot; + type SpendPeriod = SpendPeriod; + type TipCountdown = TipCountdown; + type TipFindersFee = TipFindersFee; + type TipReportDepositBase = TipReportDepositBase; + // type Tippers = GeneralCouncilProvider; + type Tippers = TenToFourteen; + // Just gets burned. + type WeightInfo = (); + } // FIXME - remove this when figure out how to use these types within mining-speed-boost runtime module itself impl roaming_operators::Trait for Test { type Currency = Balances; @@ -132,24 +230,19 @@ mod tests { type RoamingOperatorIndex = u64; } impl MiningConfigTokenTrait for Test { + type Currency = Balances; type Event = (); - // type Currency = Balances; - // type Randomness = Randomness; type MiningConfigTokenIndex = u64; type MiningConfigTokenLockAmount = u64; - // Mining Speed Boost Token Mining Config - // FIXME - how to use this enum from std? (including importing `use std::str::FromStr;`) type MiningConfigTokenType = Vec; } impl MiningRatesTokenTrait for Test { type Event = (); type MiningRatesTokenIndex = u64; type MiningRatesTokenMaxLoyalty = u32; - // Mining Speed Boost Max Rates type MiningRatesTokenMaxToken = u32; type MiningRatesTokenTokenDOT = u32; type MiningRatesTokenTokenIOTA = u32; - // Mining Speed Boost Rate type MiningRatesTokenTokenMXC = u32; } impl MiningSamplingTokenTrait for Test { @@ -169,10 +262,6 @@ mod tests { type MiningClaimsTokenClaimAmount = u64; type MiningClaimsTokenIndex = u64; } - impl MiningExecutionTokenTrait for Test { - type Event = (); - type MiningExecutionTokenIndex = u64; - } type System = frame_system::Module; pub type Balances = pallet_balances::Module; @@ -181,7 +270,6 @@ mod tests { pub type MiningSamplingTokenTestModule = MiningSamplingTokenModule; pub type MiningEligibilityTokenTestModule = MiningEligibilityTokenModule; pub type MiningClaimsTokenTestModule = MiningClaimsTokenModule; - pub type MiningExecutionTokenTestModule = MiningExecutionTokenModule; type Randomness = pallet_randomness_collective_flip::Module; // This function basically just builds a genesis storage key/value store according to From 173b843dda86feac2f0410c662ca57cd527f8d25 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Sat, 6 Mar 2021 03:16:43 +0100 Subject: [PATCH 25/27] fix errors so compiles --- runtime/tests/cli_integration_tests_mining_tokens.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index c8b5787ab..4218e4dad 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -144,7 +144,7 @@ mod tests { } thread_local! { - static TEN_TO_FOURTEEN: RefCell> = RefCell::new(vec![10,11,12,13,14]); + static TEN_TO_FOURTEEN: RefCell> = RefCell::new(vec![10,11,12,13,14]); } pub struct TenToFourteen; impl Contains for TenToFourteen { @@ -194,8 +194,7 @@ mod tests { pub const TreasuryModuleId: ModuleId = ModuleId(*b"py/trsry"); } impl pallet_treasury::Trait for Test { - // type ApproveOrigin = pallet_collective::EnsureMembers<_4, AccountId, GeneralCouncilInstance>; - type ApproveOrigin = frame_system::EnsureRoot; + type ApproveOrigin = frame_system::EnsureRoot; type BountyCuratorDeposit = BountyCuratorDeposit; type BountyDepositBase = BountyDepositBase; type BountyDepositPayoutDelay = BountyDepositPayoutDelay; @@ -212,7 +211,7 @@ mod tests { type ProposalBond = ProposalBond; type ProposalBondMinimum = ProposalBondMinimum; // type RejectOrigin = pallet_collective::EnsureMembers<_2, AccountId, GeneralCouncilInstance>; - type RejectOrigin = frame_system::EnsureRoot; + type RejectOrigin = frame_system::EnsureRoot; type SpendPeriod = SpendPeriod; type TipCountdown = TipCountdown; type TipFindersFee = TipFindersFee; @@ -518,9 +517,6 @@ mod tests { assert_ok!(MiningConfigTokenTestModule::set_mining_config_token_execution_result( Origin::signed(0), 0, // mining_config_token_id - 0, // mining_config_token_id - Some(12345), // token_execution_started_block - Some(34567) // token_execution_interval_blocks )); // Verify Storage @@ -528,7 +524,7 @@ mod tests { assert!(MiningConfigTokenTestModule::mining_config_token(0).is_some()); assert_eq!(MiningConfigTokenTestModule::mining_config_token_owner(0), Some(0)); assert_eq!( - MiningConfigTokenTestModule::mining_config_token_execution_results((0, 0)), + MiningConfigTokenTestModule::mining_config_token_execution_results(0u64), Some(MiningConfigTokenExecutionResult { token_execution_executor_account_id: 0, token_execution_started_block: 12345, From ec6462b629fac06fd28f63c3c45e027282ce4053 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Sat, 6 Mar 2021 03:18:54 +0100 Subject: [PATCH 26/27] remove comments --- runtime/tests/cli_integration_tests_mining_tokens.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index 4218e4dad..7efea779e 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -210,13 +210,11 @@ mod tests { type OnSlash = (); type ProposalBond = ProposalBond; type ProposalBondMinimum = ProposalBondMinimum; - // type RejectOrigin = pallet_collective::EnsureMembers<_2, AccountId, GeneralCouncilInstance>; type RejectOrigin = frame_system::EnsureRoot; type SpendPeriod = SpendPeriod; type TipCountdown = TipCountdown; type TipFindersFee = TipFindersFee; type TipReportDepositBase = TipReportDepositBase; - // type Tippers = GeneralCouncilProvider; type Tippers = TenToFourteen; // Just gets burned. type WeightInfo = (); From eb6e7cbf069e8d9813ad17ca7b408feb36756d69 Mon Sep 17 00:00:00 2001 From: Luke Schoen Date: Sat, 6 Mar 2021 03:43:21 +0100 Subject: [PATCH 27/27] configure start block to be 0 --- runtime/tests/cli_integration_tests_mining_tokens.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/runtime/tests/cli_integration_tests_mining_tokens.rs b/runtime/tests/cli_integration_tests_mining_tokens.rs index 7efea779e..41008431f 100644 --- a/runtime/tests/cli_integration_tests_mining_tokens.rs +++ b/runtime/tests/cli_integration_tests_mining_tokens.rs @@ -521,6 +521,8 @@ mod tests { assert_eq!(MiningConfigTokenTestModule::mining_config_token_count(), 1); assert!(MiningConfigTokenTestModule::mining_config_token(0).is_some()); assert_eq!(MiningConfigTokenTestModule::mining_config_token_owner(0), Some(0)); + // Note: Refer to Edgeware's tests that use on_finalize https://github.com/hicommonwealth/edgeware-node/blob/master/modules/edge-treasury-reward/src/tests.rs#L177 + System::set_block_number(0); assert_eq!( MiningConfigTokenTestModule::mining_config_token_execution_results(0u64), Some(MiningConfigTokenExecutionResult {