-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
connectors-gateway: Add gateway pallet (#1456)
* connectors-gateway: Add gateway pallet * benchmarks: Use correct frame-benchmarking feature * common: Add missing pallet-connectors features * connectors-gateway: Update error and weight info calls * test: Use hard-coded test account, remove extra checks * connectors-gateway: Update the router mock to use the mock-builder functionality * connectors-gateway: Use hard coded weight values * connectors-gateway: Rename LocalOrigin to LocalEVMOrigin
- Loading branch information
Showing
35 changed files
with
1,307 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use cfg_traits::connectors::Codec; | ||
use codec::{Error, Input}; | ||
|
||
#[derive(Debug, Eq, PartialEq, Clone)] | ||
pub enum MessageMock { | ||
First, | ||
Second, | ||
} | ||
|
||
impl MessageMock { | ||
fn call_type(&self) -> u8 { | ||
match self { | ||
MessageMock::First => 0, | ||
MessageMock::Second => 1, | ||
} | ||
} | ||
} | ||
|
||
impl Codec for MessageMock { | ||
fn serialize(&self) -> Vec<u8> { | ||
vec![self.call_type()] | ||
} | ||
|
||
fn deserialize<I: Input>(input: &mut I) -> Result<Self, Error> { | ||
let call_type = input.read_byte()?; | ||
|
||
match call_type { | ||
0 => Ok(MessageMock::First), | ||
1 => Ok(MessageMock::Second), | ||
_ => Err("unsupported message".into()), | ||
} | ||
} | ||
} | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use cfg_traits::connectors::InboundQueue; | ||
use cfg_types::domain_address::DomainAddress; | ||
use frame_support::pallet_prelude::*; | ||
use mock_builder::{execute_call, register_call}; | ||
|
||
use crate::connectors::MessageMock; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config { | ||
type DomainAddress; | ||
type Message; | ||
} | ||
|
||
#[pallet::pallet] | ||
#[pallet::generate_store(pub(super) trait Store)] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::storage] | ||
pub(super) type CallIds<T: Config> = StorageMap< | ||
_, | ||
Blake2_128Concat, | ||
<Blake2_128 as frame_support::StorageHasher>::Output, | ||
mock_builder::CallId, | ||
>; | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn mock_submit(f: impl Fn(DomainAddress, MessageMock) -> DispatchResult + 'static) { | ||
register_call!(move |(sender, msg)| f(sender, msg)); | ||
} | ||
} | ||
|
||
impl<T: Config> InboundQueue for Pallet<T> { | ||
type Message = T::Message; | ||
type Sender = T::DomainAddress; | ||
|
||
fn submit(sender: Self::Sender, msg: Self::Message) -> DispatchResult { | ||
execute_call!((sender, msg)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use cfg_traits::connectors::Router; | ||
use frame_support::{dispatch::DispatchResult, pallet_prelude::*}; | ||
use mock_builder::{execute_call, register_call}; | ||
use sp_std::default::Default; | ||
|
||
use crate::MessageMock; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use super::*; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
#[pallet::pallet] | ||
#[pallet::generate_store(pub(super) trait Store)] | ||
pub struct Pallet<T>(_); | ||
|
||
#[pallet::storage] | ||
pub(super) type CallIds<T: Config> = StorageMap< | ||
_, | ||
Blake2_128Concat, | ||
<Blake2_128 as frame_support::StorageHasher>::Output, | ||
mock_builder::CallId, | ||
>; | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn mock_init(f: impl Fn() -> DispatchResult + 'static) { | ||
register_call!(move |()| f()); | ||
} | ||
|
||
pub fn mock_send(f: impl Fn(T::AccountId, MessageMock) -> DispatchResult + 'static) { | ||
register_call!(move |(sender, message)| f(sender, message)); | ||
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn init() -> DispatchResult { | ||
execute_call!(()) | ||
} | ||
|
||
pub fn send(sender: T::AccountId, message: MessageMock) -> DispatchResult { | ||
execute_call!((sender, message)) | ||
} | ||
} | ||
} | ||
|
||
/// This wraps the mocking functionality of the pallet that we build here and is | ||
/// necessary since this will kept in storage, therefore it has to implement the | ||
/// below traits that make that possible. | ||
#[derive(Debug, Encode, Decode, Clone, PartialEq, Eq, TypeInfo, MaxEncodedLen)] | ||
pub struct RouterMock<T> { | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T: pallet::Config> Default for RouterMock<T> { | ||
fn default() -> Self { | ||
RouterMock::<T> { | ||
_marker: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl<T: pallet::Config> RouterMock<T> { | ||
pub fn mock_init(&self, f: impl Fn() -> DispatchResult + 'static) { | ||
pallet::Pallet::<T>::mock_init(f) | ||
} | ||
|
||
pub fn mock_send(&self, f: impl Fn(T::AccountId, MessageMock) -> DispatchResult + 'static) { | ||
pallet::Pallet::<T>::mock_send(f) | ||
} | ||
} | ||
|
||
impl<T: pallet::Config> Router for RouterMock<T> { | ||
type Message = MessageMock; | ||
type Sender = T::AccountId; | ||
|
||
fn init(&self) -> DispatchResult { | ||
pallet::Pallet::<T>::init() | ||
} | ||
|
||
fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResult { | ||
pallet::Pallet::<T>::send(sender, message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
[package] | ||
authors = ["Centrifuge <[email protected]>"] | ||
description = "Centrifuge Connectors Gateway Pallet" | ||
edition = "2021" | ||
license = "LGPL-3.0" | ||
name = "pallet-connectors-gateway" | ||
repository = "https://github.com/centrifuge/centrifuge-chain" | ||
version = "0.0.1" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.0.0", features = ["derive"], default-features = false } | ||
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" } | ||
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" } | ||
scale-info = { version = "2.3.0", default-features = false, features = ["derive"] } | ||
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" } | ||
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" } | ||
|
||
# Benchmarking | ||
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.38" } | ||
|
||
# Substrate crates | ||
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" } | ||
|
||
# Our custom pallets | ||
cfg-traits = { path = "../../libs/traits", default-features = false } | ||
cfg-types = { path = "../../libs/types", default-features = false } | ||
|
||
[dev-dependencies] | ||
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" } | ||
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" } | ||
|
||
cfg-mocks = { path = "../../libs/mocks" } | ||
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" } | ||
rand = "0.8.5" | ||
|
||
[features] | ||
default = ["std"] | ||
runtime-benchmarks = [ | ||
"frame-benchmarking/runtime-benchmarks", | ||
"cfg-traits/runtime-benchmarks", | ||
"cfg-types/runtime-benchmarks", | ||
"frame-support/runtime-benchmarks", | ||
"frame-system/runtime-benchmarks", | ||
"sp-runtime/runtime-benchmarks", | ||
] | ||
std = [ | ||
"codec/std", | ||
"cfg-types/std", | ||
"cfg-traits/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"frame-benchmarking/std", | ||
"sp-std/std", | ||
"sp-core/std", | ||
"sp-runtime/std", | ||
"scale-info/std", | ||
] | ||
try-runtime = [ | ||
"cfg-traits/try-runtime", | ||
"cfg-types/try-runtime", | ||
"frame-support/try-runtime", | ||
"frame-system/try-runtime", | ||
] |
Oops, something went wrong.