diff --git a/Cargo.lock b/Cargo.lock index 6661c7979..6059b927b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4943,7 +4943,6 @@ dependencies = [ "frame-support", "frame-system", "log", - "pallet-ddc-staking", "parity-scale-codec", "scale-info", "sp-io", diff --git a/pallets/ddc-customers/Cargo.toml b/pallets/ddc-customers/Cargo.toml index 15e19a71f..48cdf287f 100644 --- a/pallets/ddc-customers/Cargo.toml +++ b/pallets/ddc-customers/Cargo.toml @@ -9,7 +9,6 @@ ddc-primitives = { version = "0.1.0", default-features = false, path = "../../pr ddc-traits = { version = "0.1.0", default-features = false, path = "../../traits" } frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" } frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" } -pallet-ddc-staking = { version = "4.7.0", default-features = false, path = "../ddc-staking" } scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } sp-io = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" } sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.30" } diff --git a/pallets/ddc-customers/src/lib.rs b/pallets/ddc-customers/src/lib.rs index e3efdc5b1..d544ced22 100644 --- a/pallets/ddc-customers/src/lib.rs +++ b/pallets/ddc-customers/src/lib.rs @@ -10,13 +10,11 @@ use frame_support::{ traits::{Currency, DefensiveSaturating, ExistenceRequirement}, BoundedVec, PalletId, }; -pub use pallet_ddc_staking::{self as ddc_staking}; use scale_info::TypeInfo; use sp_runtime::{ traits::{AccountIdConversion, AtLeast32BitUnsigned, Saturating, Zero}, RuntimeDebug, SaturatedConversion, }; -use sp_staking::EraIndex; use sp_std::prelude::*; pub use pallet::*; @@ -32,13 +30,14 @@ parameter_types! { /// Just a Balance/BlockNumber tuple to encode when a chunk of funds will be unlocked. #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct UnlockChunk { +#[scale_info(skip_type_params(T))] +pub struct UnlockChunk { /// Amount of funds to be unlocked. #[codec(compact)] value: Balance, - /// Era number at which point it'll be unlocked. + /// Block number at which point it'll be unlocked. #[codec(compact)] - era: EraIndex, + block: T::BlockNumber, } #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] @@ -55,7 +54,8 @@ pub struct BucketsDetails { } #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo)] -pub struct AccountsLedger { +#[scale_info(skip_type_params(T))] +pub struct AccountsLedger { /// The owner account whose balance is actually locked and can be used for CDN usage. pub owner: AccountId, /// The total amount of the owner's balance that we are currently accounting for. @@ -70,11 +70,14 @@ pub struct AccountsLedger { /// (assuming that the content owner has to pay for network usage). It is assumed that this /// will be treated as a first in, first out queue where the new (higher value) eras get pushed /// on the back. - pub unlocking: BoundedVec, MaxUnlockingChunks>, + pub unlocking: BoundedVec, MaxUnlockingChunks>, } -impl - AccountsLedger +impl< + AccountId, + Balance: HasCompact + Copy + Saturating + AtLeast32BitUnsigned + Zero, + T: Config, + > AccountsLedger { /// Initializes the default object using the given owner. pub fn default_from(owner: AccountId) -> Self { @@ -83,14 +86,14 @@ impl Self { + fn consolidate_unlocked(self, current_block: T::BlockNumber) -> Self { let mut total = self.total; let unlocking: BoundedVec<_, _> = self .unlocking .into_iter() .filter(|chunk| { - log::debug!("Chunk era: {:?}", chunk.era); - if chunk.era > current_era { + log::debug!("Chunk era: {:?}", chunk.block); + if chunk.block > current_block { true } else { total = total.saturating_sub(chunk.value); @@ -146,7 +149,7 @@ pub mod pallet { pub struct Pallet(_); #[pallet::config] - pub trait Config: frame_system::Config + ddc_staking::Config { + pub trait Config: frame_system::Config { /// The accounts's pallet id, used for deriving its sovereign account ID. #[pallet::constant] type PalletId: Get; @@ -154,7 +157,7 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// Number of eras that staked funds must remain locked for. #[pallet::constant] - type LockingDuration: Get; + type UnlockingDelay: Get<::BlockNumber>; type ClusterVisitor: ClusterVisitor; } @@ -162,7 +165,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn ledger)] pub type Ledger = - StorageMap<_, Identity, T::AccountId, AccountsLedger>>; + StorageMap<_, Identity, T::AccountId, AccountsLedger, T>>; #[pallet::type_value] pub fn DefaultBucketCount() -> BucketId { @@ -214,8 +217,6 @@ pub mod pallet { NoBucketWithId, /// Internal state has become somehow corrupted and the operation cannot continue. BadState, - /// Current era not set during runtime - DDCEraNotSet, /// Bucket with specified id doesn't exist BucketDoesNotExist, /// DDC Cluster with provided id doesn't exist @@ -376,12 +377,14 @@ pub mod pallet { ledger.active = Zero::zero(); } - let current_era = 0; - // Note: locking for extra era to allow for accounting - let era = current_era + ::LockingDuration::get(); - log::debug!("Era for the unlock: {:?}", era); + let current_block = >::block_number(); + // Note: locking for extra block to allow for accounting + let block = current_block + ::UnlockingDelay::get(); + log::debug!("Block for the unlock: {:?}", block); - if let Some(chunk) = ledger.unlocking.last_mut().filter(|chunk| chunk.era == era) { + if let Some(chunk) = + ledger.unlocking.last_mut().filter(|chunk| chunk.block == block) + { // To keep the chunk count down, we only keep one chunk per era. Since // `unlocking` is a FiFo queue, if a chunk exists for `era` we know that it will // be the last one. @@ -389,7 +392,7 @@ pub mod pallet { } else { ledger .unlocking - .try_push(UnlockChunk { value, era }) + .try_push(UnlockChunk { value, block }) .map_err(|_| Error::::NoMoreChunks)?; }; @@ -415,9 +418,8 @@ pub mod pallet { let owner = ensure_signed(origin)?; let mut ledger = Self::ledger(&owner).ok_or(Error::::NotOwner)?; let (owner, old_total) = (ledger.owner.clone(), ledger.total); - let current_era = 0; - ledger = ledger.consolidate_unlocked(current_era); - log::debug!("Current era: {:?}", current_era); + let current_block = >::block_number(); + ledger = ledger.consolidate_unlocked(current_block); if ledger.unlocking.is_empty() && ledger.active < ::Currency::minimum_balance() @@ -468,7 +470,7 @@ pub mod pallet { /// This will also deposit the funds to pallet. fn update_ledger_and_deposit( owner: &T::AccountId, - ledger: &AccountsLedger>, + ledger: &AccountsLedger, T>, ) -> DispatchResult { let account_id = Self::account_id(); @@ -486,7 +488,7 @@ pub mod pallet { /// Update the ledger for a owner. fn update_ledger( owner: &T::AccountId, - ledger: &AccountsLedger>, + ledger: &AccountsLedger, T>, ) { >::insert(owner, ledger); } @@ -523,7 +525,6 @@ pub mod pallet { ledger.total -= amount; ledger.active -= amount; total_charged += amount; - log::debug!("Ledger updated state: {:?}", &ledger); Self::update_ledger(&content_owner, &ledger); } else { let diff = amount - ledger.active; @@ -531,7 +532,6 @@ pub mod pallet { ledger.total -= ledger.active; ledger.active = BalanceOf::::zero(); let (ledger, charged) = ledger.charge_unlocking(diff); - log::debug!("Ledger updated state: {:?}", &ledger); Self::update_ledger(&content_owner, &ledger); total_charged += charged; } diff --git a/pallets/ddc-staking/src/lib.rs b/pallets/ddc-staking/src/lib.rs index 26fac2f84..fbc82d69b 100644 --- a/pallets/ddc-staking/src/lib.rs +++ b/pallets/ddc-staking/src/lib.rs @@ -39,19 +39,16 @@ use frame_support::{ assert_ok, pallet_prelude::*, parameter_types, - traits::{ - Currency, DefensiveSaturating, ExistenceRequirement, LockIdentifier, LockableCurrency, - WithdrawReasons, - }, - BoundedVec, PalletId, + traits::{Currency, DefensiveSaturating, LockIdentifier, LockableCurrency, WithdrawReasons}, + BoundedVec, }; use frame_system::pallet_prelude::*; use scale_info::TypeInfo; use sp_runtime::{ - traits::{AccountIdConversion, AtLeast32BitUnsigned, Saturating, StaticLookup, Zero}, + traits::{AtLeast32BitUnsigned, Saturating, StaticLookup, Zero}, RuntimeDebug, SaturatedConversion, }; -use sp_std::{collections::btree_map::BTreeMap, prelude::*}; +use sp_std::prelude::*; pub use pallet::*; diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index b8b8b52d9..6b2e35137 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -1332,11 +1332,11 @@ impl pallet_ddc_staking::Config for Runtime { parameter_types! { pub const DdcCustomersPalletId: PalletId = PalletId(*b"accounts"); // DDC maintainer's stake - pub const LockingDuration: sp_staking::EraIndex = 30 * 24; // 1 hour * 24 = 1 day; (1 era is 2 mins) + pub const UnlockingDelay: BlockNumber = 5256000u32; // 1 hour * 24 * 365 = 1 day; (1 hour is 600 blocks) } impl pallet_ddc_customers::Config for Runtime { - type LockingDuration = LockingDuration; + type UnlockingDelay = UnlockingDelay; type Currency = Balances; type PalletId = DdcCustomersPalletId; type RuntimeEvent = RuntimeEvent;