Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix some territory bug #366

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions pallets/file-bank/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,16 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(now: BlockNumberFor<T>) -> Weight {
fn on_initialize(_now: BlockNumberFor<T>) -> Weight {
let days = T::OneDay::get();
let mut weight: Weight = Weight::zero();
// FOR TESTING
if now % days == 0u32.saturated_into() {
let (temp_weight, clear_list) = T::StorageHandle::frozen_task();
weight = weight.saturating_add(temp_weight);
let temp_acc_list: BoundedVec<(AccountOf<T>, TerrName), ConstU32<2000>> =
clear_list.try_into().unwrap_or_default();
ClearUserList::<T>::put(temp_acc_list);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}
let (temp_weight, clear_list) = T::StorageHandle::frozen_task();
weight = weight.saturating_add(temp_weight);
let temp_acc_list: BoundedVec<(AccountOf<T>, TerrName), ConstU32<2000>> =
clear_list.try_into().unwrap_or_default();
ClearUserList::<T>::put(temp_acc_list);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

let mut count: u32 = 0;
let clear_list = ClearUserList::<T>::get();
Expand Down
2 changes: 1 addition & 1 deletion pallets/storage-handler/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ benchmarks! {
assert_eq!(territory_info.deadline, (40u32 * 14400u32).saturated_into());
}

treeitory_consignment {
territory_consignment {
let caller: AccountOf<T> = account("user1", 100, SEED);
let terr_name: TerrName = "t1".as_bytes().to_vec().try_into().map_err(|_| "boundedvec error")?;
let free: BalanceOf<T> = 365_000_000_000_000_000_000_000u128.try_into().map_err(|_| "tryinto error!").expect("tryinto error!");
Expand Down
73 changes: 55 additions & 18 deletions pallets/storage-handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub const SPACE_DEAD: &str = "dead";
type AccountOf<T> = <T as frame_system::Config>::AccountId;
type BalanceOf<T> =
<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type TokenId = H256;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

Expand Down Expand Up @@ -134,7 +135,7 @@ pub mod pallet {
PaidOrder { order_hash: BoundedVec<u8, ConstU32<32>> },

MintTerritory {
token: H256,
token: TokenId,
name: TerrName,
storage_capacity: u128,
spend: BalanceOf<T>,
Expand All @@ -160,28 +161,28 @@ pub mod pallet {

Consignment {
name: TerrName,
token: H256,
token: TokenId,
price: BalanceOf<T>,
},

BuyConsignment {
name: TerrName,
token: H256,
token: TokenId,
price: BalanceOf<T>,
},

CancleConsignment {
token: H256,
token: TokenId,
},

CancelPurchaseAction {
token: H256,
token: TokenId,
},

ExecConsignment {
buyer: AccountOf<T>,
seller: AccountOf<T>,
token: H256,
token: TokenId,
},
}

Expand Down Expand Up @@ -249,12 +250,14 @@ pub mod pallet {
NotBuyer,
/// This is an invalid order, Because the price can't match
InvalidOrder,
/// Unable to purchase own consignment
OwnConsignment,
}

#[pallet::storage]
#[pallet::getter(fn territory_key)]
pub(super) type TerritoryKey<T: Config> =
StorageMap<_, Blake2_128Concat, H256, (AccountOf<T>, TerrName)>;
StorageMap<_, Blake2_128Concat, TokenId, (AccountOf<T>, TerrName)>;

#[pallet::storage]
#[pallet::getter(fn territory)]
Expand All @@ -271,7 +274,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn consignment)]
pub(super) type Consignment<T: Config> =
StorageMap<_, Blake2_128Concat, H256, ConsignmentInfo<T>>;
StorageMap<_, Blake2_128Concat, TokenId, ConsignmentInfo<T>>;

#[pallet::storage]
#[pallet::getter(fn territory_frozen)]
Expand All @@ -281,7 +284,7 @@ pub mod pallet {
Blake2_128Concat,
BlockNumberFor<T>,
Blake2_128Concat,
H256,
TokenId,
bool,
>;

Expand All @@ -298,7 +301,7 @@ pub mod pallet {
Blake2_128Concat,
BlockNumberFor<T>,
Blake2_128Concat,
H256,
TokenId,
bool,
>;

Expand Down Expand Up @@ -556,15 +559,15 @@ pub mod pallet {

#[pallet::call_index(102)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::treeitory_consignment())]
pub fn treeitory_consignment(
#[pallet::weight(<T as pallet::Config>::WeightInfo::territory_consignment())]
pub fn territory_consignment(
origin: OriginFor<T>,
territory_name: TerrName,
price: BalanceOf<T>
) -> DispatchResult {
let sender = ensure_signed(origin)?;

let token = <Territory<T>>::try_mutate(&sender, &territory_name, |t_opt| -> Result<H256, DispatchError> {
let token = <Territory<T>>::try_mutate(&sender, &territory_name, |t_opt| -> Result<TokenId, DispatchError> {
let t = t_opt.as_mut().ok_or(Error::<T>::NotHaveTerritory)?;

ensure!(t.state == TerritoryState::Active, Error::<T>::NotActive);
Expand Down Expand Up @@ -604,13 +607,14 @@ pub mod pallet {
#[pallet::weight(<T as pallet::Config>::WeightInfo::buy_consignment())]
pub fn buy_consignment(
origin: OriginFor<T>,
token: H256,
token: TokenId,
rename: TerrName,
) -> DispatchResult {
let sender = ensure_signed(origin)?;

let consignment = <Consignment<T>>::try_get(&token).map_err(|_| Error::<T>::NonExistentConsignment)?;
ensure!(!consignment.locked, Error::<T>::ConsignmentLocked);
ensure!(consignment.user != sender, Error::<T>::OwnConsignment);

<Consignment<T>>::try_mutate(&token, |c_opt| -> DispatchResult {
let c = c_opt.as_mut().ok_or(Error::<T>::NonExistentConsignment)?;
Expand Down Expand Up @@ -647,7 +651,7 @@ pub mod pallet {
#[pallet::call_index(104)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::exec_consignment())]
pub fn exec_consignment(origin: OriginFor<T>, token: H256, territory_name: TerrName) -> DispatchResult {
pub fn exec_consignment(origin: OriginFor<T>, token: TokenId, territory_name: TerrName) -> DispatchResult {
ensure_root(origin)?;

let consignment = <Consignment<T>>::try_get(&token).map_err(|_| Error::<T>::NonExistentConsignment)?;
Expand Down Expand Up @@ -703,7 +707,7 @@ pub mod pallet {
#[pallet::call_index(106)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::cancel_purchase_action())]
pub fn cancel_purchase_action(origin: OriginFor<T>, token: H256) -> DispatchResult {
pub fn cancel_purchase_action(origin: OriginFor<T>, token: TokenId) -> DispatchResult {
let sender = ensure_signed(origin)?;

<Consignment<T>>::try_mutate(&token, |c_opt| -> DispatchResult {
Expand Down Expand Up @@ -797,18 +801,51 @@ pub mod pallet {
#[pallet::call_index(5)]
#[transactional]
#[pallet::weight(Weight::zero())]
pub fn update_user_territory_life(origin: OriginFor<T>, user: AccountOf<T>, terr_name: TerrName, deadline: BlockNumberFor<T>) -> DispatchResult {
pub fn update_user_territory_life(
origin: OriginFor<T>,
user: AccountOf<T>,
terr_name: TerrName,
deadline: BlockNumberFor<T>
) -> DispatchResult {
let _ = ensure_root(origin)?;

<Territory<T>>::try_mutate(&user, &terr_name, |space_opt| -> DispatchResult {
let space_info = space_opt.as_mut().ok_or(Error::<T>::NotPurchasedSpace)?;

// TerritoryFrozenCounter::<T>::mutate(&space_info.deadline, |counter| -> DispatchResult {
// *counter = counter.checked_sub(1).ok_or(Error::<T>::Overflow)?;
// Ok(())
// })?;

TerritoryFrozen::<T>::remove(&space_info.deadline, &space_info.token);

space_info.deadline = deadline;

TerritoryFrozen::<T>::insert(&space_info.deadline, &space_info.token, true);

Ok(())
})
}

#[pallet::call_index(201)]
#[transactional]
#[pallet::weight(Weight::zero())]
pub fn update_expired_exec(
origin: OriginFor<T>,
old_block: BlockNumberFor<T>,
new_block: BlockNumberFor<T>,
token: TokenId
) -> DispatchResult {
let _ = ensure_root(origin)?;

TerritoryExpired::<T>::remove(&old_block, &token);

TerritoryExpired::<T>::insert(&new_block, &token, true);

Ok(())
}


#[pallet::call_index(6)]
#[transactional]
#[pallet::weight(<T as pallet::Config>::WeightInfo::create_order())]
Expand Down Expand Up @@ -978,7 +1015,7 @@ impl<T: Config> Pallet<T> {
}

fn storage_territory(
token: H256,
token: TokenId,
user: AccountOf<T>,
space: u128,
days: u32,
Expand Down
2 changes: 1 addition & 1 deletion pallets/storage-handler/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum OrderType {
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct TerritoryInfo<T: Config> {
pub(super) token: H256,
pub(super) token: TokenId,
pub(super) total_space: u128,
pub(super) used_space: u128,
pub(super) locked_space: u128,
Expand Down
6 changes: 3 additions & 3 deletions pallets/storage-handler/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait WeightInfo {
fn mint_territory() -> Weight;
fn expanding_territory() -> Weight;
fn renewal_territory() -> Weight;
fn treeitory_consignment() -> Weight;
fn territory_consignment() -> Weight;
fn buy_consignment() -> Weight;
fn exec_consignment() -> Weight;
fn cancel_consignment() -> Weight;
Expand Down Expand Up @@ -129,7 +129,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Proof: `StorageHandler::Territory` (`max_values`: None, `max_size`: Some(233), added: 2708, mode: `MaxEncodedLen`)
/// Storage: `StorageHandler::Consignment` (r:1 w:1)
/// Proof: `StorageHandler::Consignment` (`max_values`: None, `max_size`: Some(135), added: 2610, mode: `MaxEncodedLen`)
fn treeitory_consignment() -> Weight {
fn territory_consignment() -> Weight {
// Proof Size summary in bytes:
// Measured: `590`
// Estimated: `3698`
Expand Down Expand Up @@ -374,7 +374,7 @@ impl WeightInfo for () {
/// Proof: `StorageHandler::Territory` (`max_values`: None, `max_size`: Some(233), added: 2708, mode: `MaxEncodedLen`)
/// Storage: `StorageHandler::Consignment` (r:1 w:1)
/// Proof: `StorageHandler::Consignment` (`max_values`: None, `max_size`: Some(135), added: 2610, mode: `MaxEncodedLen`)
fn treeitory_consignment() -> Weight {
fn territory_consignment() -> Weight {
// Proof Size summary in bytes:
// Measured: `590`
// Estimated: `3698`
Expand Down
Loading