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

Order Processor Pallet #190

Merged
merged 36 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
aa60b88
outline
Szegoo Jul 6, 2024
e3a1853
add primitives
Szegoo Jul 6, 2024
4b70491
progress
Szegoo Jul 8, 2024
af73b15
fulfill order
cuteolaf Jul 10, 2024
346b42b
merge
Szegoo Jul 13, 2024
6ebc859
implement payment
Szegoo Jul 13, 2024
c36f45b
progress
Szegoo Jul 17, 2024
839631d
Cargo.lock
Szegoo Jul 18, 2024
819c70c
merge
Szegoo Jul 18, 2024
c31cfe7
progress
Szegoo Jul 19, 2024
a474ce4
mock.rs compiling
Szegoo Jul 19, 2024
a3ac7a1
outline login for the fulfill extrinsic
Szegoo Jul 19, 2024
88e3784
test progress
Szegoo Jul 19, 2024
1d8d91b
fulfill_order_works almost complete
Szegoo Jul 20, 2024
8b654dc
complete test
Szegoo Jul 20, 2024
bf7aa11
region assignment
Szegoo Jul 20, 2024
fd770a9
outline xcm region assigner
Szegoo Jul 22, 2024
9337a56
assignment call
Szegoo Jul 22, 2024
ba5a61d
mock.rs compiles
Szegoo Jul 23, 2024
519f68e
add some docs
Szegoo Jul 23, 2024
cb29be8
region assignment
Szegoo Jul 24, 2024
649058f
check assignments & lock
Szegoo Jul 24, 2024
3be428e
ensure_matching_requirements_works
Szegoo Jul 24, 2024
4132627
check events & assign works
Szegoo Jul 24, 2024
0e8b419
benchmark progress | not working
Szegoo Jul 24, 2024
6b046bd
remove benchmark helper
Szegoo Jul 24, 2024
eb5abdc
order creator
Szegoo Jul 24, 2024
452cb9b
benchmarking
Szegoo Jul 25, 2024
62fcf1b
nit fixes
Szegoo Jul 25, 2024
678f66b
runtime implementation
Szegoo Jul 25, 2024
bb56d58
fix bench build
Szegoo Jul 25, 2024
6d3d080
fixes
Szegoo Jul 25, 2024
6d2497a
add weight
Szegoo Jul 26, 2024
3660242
weight from gcloud instance
Szegoo Jul 26, 2024
d85dbbe
use weight
Szegoo Jul 26, 2024
add628e
make clippy happy
Szegoo Jul 26, 2024
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
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ ismp-testsuite = { git = "https://github.com/polytope-labs/hyperbridge.git", bra
# Local
region-primitives = { path = "./primitives/region", default-features = false }
nonfungible-primitives = { path = "./primitives/nonfungible", default-features = false }
order-primitives = { path = "./primitives/order", default-features = false }
regionx-runtime-common = { path = "./runtime/common", default-features = false }
cocos-runtime = { path = "./runtime/cocos", default-features = false }
pallet-market = { path = "./pallets/market", default-features = false }
pallet-orders = { path = "./pallets/orders", default-features = false }
pallet-processor = { path = "./pallets/processor", default-features = false }
pallet-regions = { path = "./pallets/regions", default-features = false }
8 changes: 4 additions & 4 deletions pallets/market/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod benchmarks {

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: caller.clone(), paid: None };
T::BenchmarkHelper::create_region(region_id, record, caller.clone())?;
T::Regions::create_region(region_id, record, caller.clone())?;

let timeslice_price: BalanceOf<T> = 1_000u32.into();
#[extrinsic_call]
Expand All @@ -65,7 +65,7 @@ mod benchmarks {

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: caller.clone(), paid: None };
T::BenchmarkHelper::create_region(region_id, record, caller.clone())?;
T::Regions::create_region(region_id, record, caller.clone())?;

let timeslice_price: BalanceOf<T> = 1_000u32.into();
crate::Pallet::<T>::list_region(
Expand All @@ -89,7 +89,7 @@ mod benchmarks {

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: caller.clone(), paid: None };
T::BenchmarkHelper::create_region(region_id, record, caller.clone())?;
T::Regions::create_region(region_id, record, caller.clone())?;

crate::Pallet::<T>::list_region(
RawOrigin::Signed(caller.clone()).into(),
Expand All @@ -116,7 +116,7 @@ mod benchmarks {
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: alice.clone(), paid: None };

<T as crate::Config>::Currency::set_balance(&alice.clone(), u32::MAX.into());
T::BenchmarkHelper::create_region(region_id, record, alice.clone())?;
T::Regions::create_region(region_id, record, alice.clone())?;
crate::Pallet::<T>::list_region(
RawOrigin::Signed(alice).into(),
region_id,
Expand Down
17 changes: 11 additions & 6 deletions pallets/market/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use frame_support::traits::{fungible::Inspect, tokens::Preservation};
use nonfungible_primitives::LockableNonFungible;
pub use pallet::*;
use pallet_broker::{RegionId, Timeslice};
use region_primitives::RegionInspect;
use region_primitives::{RegionFactory, RegionInspect};
use sp_runtime::{traits::BlockNumberProvider, SaturatedConversion, Saturating};

mod types;
Expand Down Expand Up @@ -67,7 +67,8 @@ pub mod pallet {
// The item id is `u128` encoded RegionId.
type Regions: Transfer<Self::AccountId, ItemId = u128>
+ LockableNonFungible<Self::AccountId, ItemId = u128>
+ RegionInspect<Self::AccountId, BalanceOf<Self>, ItemId = u128>;
+ RegionInspect<Self::AccountId, BalanceOf<Self>, ItemId = u128>
+ RegionFactory<Self::AccountId, RegionRecordOf<Self>>;

/// Type for getting the current relay chain block.
///
Expand All @@ -80,9 +81,6 @@ pub mod pallet {

/// Weight Info
type WeightInfo: WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: RegionFactory<Self>;
}

#[pallet::pallet]
Expand Down Expand Up @@ -143,6 +141,10 @@ pub mod pallet {
NotAllowed,
/// The price of the region is higher than what the buyer is willing to pay.
PriceTooHigh,
/// The region record is not available.
RecordUnavailable,
/// Locked regions cannot be listed on sale.
RegionLocked,
}

#[pallet::call]
Expand All @@ -165,7 +167,10 @@ pub mod pallet {
let who = ensure_signed(origin)?;

ensure!(Listings::<T>::get(region_id).is_none(), Error::<T>::AlreadyListed);
let record = T::Regions::record(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;

let region = T::Regions::region(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;
ensure!(!region.locked, Error::<T>::RegionLocked);
let record = region.record.get().ok_or(Error::<T>::RecordUnavailable)?;

// It doesn't make sense to list a region that expired.
let current_timeslice = Self::current_timeslice();
Expand Down
24 changes: 2 additions & 22 deletions pallets/market/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
// You should have received a copy of the GNU General Public License
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

use crate::{RegionId, RegionRecordOf};
use frame_support::{
pallet_prelude::*,
parameter_types,
traits::{nonfungible::Mutate, Everything},
};
use frame_support::{pallet_prelude::*, parameter_types, traits::Everything};
use ismp::{
consensus::StateMachineId,
dispatcher::{DispatchRequest, FeeMetadata, IsmpDispatcher},
Expand All @@ -31,7 +26,7 @@ use pallet_regions::primitives::StateMachineHeightProvider;
use sp_core::{ConstU64, H256};
use sp_runtime::{
traits::{BlakeTwo256, BlockNumberProvider, IdentityLookup},
BuildStorage, DispatchResult,
BuildStorage,
};
use std::sync::Arc;

Expand Down Expand Up @@ -158,28 +153,13 @@ impl BlockNumberProvider for RelayBlockNumberProvider {
}
}

pub struct RegionFactory;
impl crate::RegionFactory<Test> for RegionFactory {
fn create_region(
region_id: RegionId,
record: RegionRecordOf<Test>,
owner: <Test as frame_system::Config>::AccountId,
) -> DispatchResult {
Regions::mint_into(&region_id.into(), &owner)?;
Regions::set_record(region_id, record.clone())?;
Ok(())
}
}

impl crate::Config for Test {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type Regions = Regions;
type RCBlockNumberProvider = RelayBlockNumberProvider;
type TimeslicePeriod = ConstU64<80>;
type WeightInfo = ();
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = RegionFactory;
}

// Build genesis storage according to the mock runtime.
Expand Down
2 changes: 1 addition & 1 deletion pallets/market/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn list_region_works() {

assert_noop!(
Market::list_region(signer.clone(), region_id, price, None),
Error::<Test>::UnknownRegion
Error::<Test>::RecordUnavailable
);

assert_ok!(Regions::set_record(region_id, record.clone()));
Expand Down
12 changes: 1 addition & 11 deletions pallets/market/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
//
// You should have received a copy of the GNU General Public License
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.
use crate::{BalanceOf, RegionId};
use crate::BalanceOf;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::DispatchResult;

pub type RegionRecordOf<T> =
pallet_broker::RegionRecord<<T as frame_system::Config>::AccountId, BalanceOf<T>>;
Expand All @@ -32,12 +31,3 @@ pub struct Listing<AccountId, Balance> {
/// This will usually be the seller account.
pub sale_recipient: AccountId,
}

/// Trait for creating regions. Used for benchmarking.
pub trait RegionFactory<T: crate::Config> {
fn create_region(
region_id: RegionId,
record: RegionRecordOf<T>,
owner: T::AccountId,
) -> DispatchResult;
}
4 changes: 4 additions & 0 deletions pallets/orders/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ sp-core = { workspace = true, default-features = false }
sp-runtime = { workspace = true, default-features = false }
pallet-broker = { workspace = true, default-features = false }

# Local
order-primitives = { workspace = true, default-features = false }

[dev-dependencies]
serde = { workspace = true }
pallet-balances = { workspace = true, default-features = false }
Expand All @@ -46,5 +49,6 @@ std = [
"frame-system/std",
"pallet-broker/std",
"pallet-balances/std",
"order-primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
49 changes: 41 additions & 8 deletions pallets/orders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::traits::{Currency, ExistenceRequirement};
use order_primitives::{Order, OrderFactory, OrderId, OrderInspect, Requirements};
pub use pallet::*;
use pallet_broker::Timeslice;
use sp_runtime::{
Expand Down Expand Up @@ -167,14 +168,7 @@ pub mod pallet {
) -> DispatchResult {
let who = ensure_signed(origin)?;

T::OrderCreationFeeHandler::handle(&who, T::OrderCreationCost::get())?;

let order_id = NextOrderId::<T>::get();
Orders::<T>::insert(order_id, Order { creator: who.clone(), para_id, requirements });
NextOrderId::<T>::put(order_id.saturating_add(1));

Self::deposit_event(Event::OrderCreated { order_id, by: who });
Ok(())
Self::do_create_order(who, para_id, requirements)
}

/// Extrinsic for cancelling an order.
Expand Down Expand Up @@ -263,6 +257,24 @@ pub mod pallet {
}

impl<T: Config> Pallet<T> {
pub(crate) fn do_create_order(
creator: T::AccountId,
para_id: ParaId,
requirements: Requirements,
) -> DispatchResult {
T::OrderCreationFeeHandler::handle(&creator, T::OrderCreationCost::get())?;

let order_id = NextOrderId::<T>::get();
Orders::<T>::insert(
order_id,
Order { creator: creator.clone(), para_id, requirements },
);
NextOrderId::<T>::put(order_id.saturating_add(1));

Self::deposit_event(Event::OrderCreated { order_id, by: creator });
Ok(())
}

pub(crate) fn do_cancel_order(
order_id: OrderId,
current_timeslice: Timeslice,
Expand All @@ -286,4 +298,25 @@ pub mod pallet {
(latest_rc_block / timeslice_period).saturated_into()
}
}

impl<T: Config> OrderInspect<T::AccountId> for Pallet<T> {
fn order(order_id: &OrderId) -> Option<Order<T::AccountId>> {
Orders::<T>::get(order_id)
}

fn remove_order(order_id: &OrderId) {
Orders::<T>::remove(order_id)
}
}
}

impl<T: crate::Config> OrderFactory<T::AccountId> for Pallet<T> {
fn create_order(
creator: T::AccountId,
para_id: ParaId,
requirements: Requirements,
) -> sp_runtime::DispatchResult {
crate::Pallet::<T>::do_create_order(creator, para_id, requirements)?;
Ok(())
}
}
Loading
Loading