Skip to content

Commit

Permalink
Create agent (#895)
Browse files Browse the repository at this point in the history
* base

* removed location conversion

* completed implementation

* remove xcm-builder

* update cumulus

* update cumulus

* use contains_key

* Fix openzeppelin-contracts submodule

---------

Co-authored-by: David Dunn <[email protected]>
  • Loading branch information
alistair-singh and doubledup authored Jul 20, 2023
1 parent 8350a9a commit 7b7a037
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
[submodule "core/packages/contracts/lib/canonical-weth"]
path = core/packages/contracts/lib/canonical-weth
url = https://github.com/snowfork/canonical-weth
[submodule "lib/openzeppelin-contracts"]
[submodule "core/packages/contracts/lib/openzeppelin-contracts"]
path = core/packages/contracts/lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
1 change: 0 additions & 1 deletion lib/openzeppelin-contracts
Submodule openzeppelin-contracts deleted from e50c24
10 changes: 10 additions & 0 deletions parachain/pallets/control/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,15 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn create_agent() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

#[extrinsic_call]
_(RawOrigin::Signed(caller));

Ok(())
}

impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);
}
42 changes: 36 additions & 6 deletions parachain/pallets/control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ pub mod pallet {
type OutboundQueue: OutboundQueueTrait;
type OwnParaId: Get<ParaId>;
type WeightInfo: WeightInfo;

type MaxUpgradeDataSize: Get<u32>;

type EnsureCreateAgentOrigin: EnsureOrigin<
<Self as frame_system::Config>::RuntimeOrigin,
Success = MultiLocation,
>;
type CreateAgentOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = MultiLocation>;
}

#[pallet::event]
Expand All @@ -63,6 +58,7 @@ pub mod pallet {
pub enum Error<T> {
UpgradeDataTooLarge,
SubmissionFailed,
LocationConversionFailed,
}

#[pallet::storage]
Expand Down Expand Up @@ -101,5 +97,39 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::create_agent())]
pub fn create_agent(origin: OriginFor<T>) -> DispatchResult {
let agent_location: MultiLocation = T::CreateAgentOrigin::ensure_origin(origin)?;

let agent_id = match agent_location {
MultiLocation { parents: 0, interior: X1(AccountId32 { id, .. }) } => Ok(H256(id)),
_ => Err(Error::<T>::LocationConversionFailed),
}?;

if Agents::<T>::contains_key(agent_id) {
return Ok(());
}

let (command, params) = Command::CreateAgent { agent_id }.encode();

let message = OutboundMessage {
id: T::MessageHasher::hash(&agent_id.encode()),
origin: T::OwnParaId::get(),
command,
params,
};

let ticket =
T::OutboundQueue::validate(&message).map_err(|_| Error::<T>::SubmissionFailed)?;

T::OutboundQueue::submit(ticket).map_err(|_| Error::<T>::SubmissionFailed)?;

Agents::<T>::insert(agent_id, ());
Self::deposit_event(Event::<T>::CreateAgent { agent_id });

Ok(())
}
}
}
2 changes: 1 addition & 1 deletion parachain/pallets/control/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl snowbridge_control::Config for Test {
type OutboundQueue = ();
type MessageHasher = BlakeTwo256;
type MaxUpgradeDataSize = MaxUpgradeDataSize;
type EnsureCreateAgentOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type CreateAgentOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type WeightInfo = ();
}

Expand Down
9 changes: 9 additions & 0 deletions parachain/pallets/control/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use core::marker::PhantomData;
/// Weight functions needed for pallet_template.
pub trait WeightInfo {
fn upgrade(data_size: u32) -> Weight;
fn create_agent() -> Weight;
}

/// Weights for pallet_template using the Substrate node and recommended hardware.
Expand All @@ -45,6 +46,10 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
Weight::from_parts(9_000_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
fn create_agent() -> Weight {
Weight::from_parts(9_000_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests
Expand All @@ -53,4 +58,8 @@ impl WeightInfo for () {
Weight::from_parts(9_000_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
fn create_agent() -> Weight {
Weight::from_parts(9_000_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}

0 comments on commit 7b7a037

Please sign in to comment.