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

connectors-gateway: Add gateway pallet #1456

Merged
merged 8 commits into from
Jul 18, 2023
Merged
22 changes: 22 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ members = [
"pallets/bridge",
"pallets/block-rewards",
"pallets/connectors",
"pallets/connectors-gateway",
"pallets/claims",
"pallets/collator-allowlist",
"pallets/crowdloan-claim",
Expand Down
76 changes: 76 additions & 0 deletions libs/mocks/src/connectors.rs
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))
}
}
}
85 changes: 85 additions & 0 deletions libs/mocks/src/connectors_gateway_routers.rs
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> {
lemunozm marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
4 changes: 4 additions & 0 deletions libs/mocks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod change_guard;
mod connectors;
mod connectors_gateway_routers;
mod data;
mod fees;
mod permissions;
Expand All @@ -7,6 +9,8 @@ mod rewards;
mod time;

pub use change_guard::pallet_mock_change_guard;
pub use connectors::{pallet as pallet_mock_connectors, MessageMock};
pub use connectors_gateway_routers::{pallet as pallet_mock_routers, RouterMock};
pub use data::pallet as pallet_mock_data;
pub use fees::pallet as pallet_mock_fees;
pub use permissions::pallet as pallet_mock_permissions;
Expand Down
15 changes: 15 additions & 0 deletions libs/traits/src/connectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ pub trait Codec: Sized {
fn deserialize<I: Input>(input: &mut I) -> Result<Self, codec::Error>;
}

/// The trait required for sending outbound messages.
pub trait Router {
/// The sender type of the outbound message.
type Sender;

/// The outbound message type.
type Message;

/// Initialize the router.
fn init(&self) -> DispatchResult;

/// Send the message to the router's destination.
fn send(&self, sender: Self::Sender, message: Self::Message) -> DispatchResult;
}

/// The trait required for processing outbound connectors messages.
pub trait OutboundQueue {
/// The sender type of the outgoing message.
Expand Down
2 changes: 1 addition & 1 deletion pallets/block-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ try-runtime = [
"sp-runtime/try-runtime",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"cfg-traits/runtime-benchmarks",
"cfg-types/runtime-benchmarks",
"frame-support/runtime-benchmarks",
Expand Down
2 changes: 1 addition & 1 deletion pallets/collator-allowlist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sp-io = { git = "https://github.com/paritytech/substrate", default-features = tr
[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
Expand Down
66 changes: 66 additions & 0 deletions pallets/connectors-gateway/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[package]
authors = ["Centrifuge <admin@centrifuge.io>"]
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",
]
Loading
Loading