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

feat: MPC Jobs pallet #285

Merged
merged 22 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
51 changes: 51 additions & 0 deletions Cargo.lock

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

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ members = [
"standalone/node",
"standalone/runtime",
"standalone/runtime/evm_tracer",
"pallets/claims",
"pallets/transaction-pause",
"pallets/*",
"pallets/jobs/rpc",
"pallets/jobs/rpc/runtime-api",
"precompiles/utils",
"precompiles/utils/macro",
"precompiles/utils/tests-external",
Expand Down Expand Up @@ -83,6 +84,7 @@ tangle-runtime = { package = "tangle-standalone-runtime", path = "standalone/run

# Tangle Dependencies
pallet-ecdsa-claims = { path = "pallets/claims", default-features = false }
pallet-jobs = { path = "pallets/jobs", default-features = false }

# Orml dependencies
orml-currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v1.0.0", default-features = false }
Expand Down Expand Up @@ -121,6 +123,8 @@ sc-consensus-grandpa = { git = "https://github.com/paritytech/substrate", branch
sc-network-sync = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
sp-weights = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
Expand Down
36 changes: 36 additions & 0 deletions pallets/jobs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "pallet-jobs"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }

[dependencies]
parity-scale-codec = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
scale-info = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
tangle-primitives = {workspace = true, default-features = false }
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
pallet-balances = { workspace = true }
smallvec = { workspace = true }
sp-io = { workspace = true }

[features]
default = ["std"]
std = ["scale-info/std", "sp-runtime/std", "frame-benchmarking?/std", "frame-support/std", "frame-system/std", "sp-core/std", "sp-std/std", "tangle-primitives/std", "pallet-balances/std"]
try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-balances/runtime-benchmarks"
1xstj marked this conversation as resolved.
Show resolved Hide resolved
1xstj marked this conversation as resolved.
Show resolved Hide resolved
]
23 changes: 23 additions & 0 deletions pallets/jobs/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "pallet-jobs-rpc"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true }
jsonrpsee = { version = "0.16.2", features = ["client-core", "server", "macros"] }
pallet-jobs-rpc-runtime-api = { path = "./runtime-api" }
sp-api = { workspace = true }
sp-blockchain = { workspace = true }
sp-core = { workspace = true }
sp-rpc = { workspace = true }
sp-runtime = { workspace = true }
sp-weights = { workspace = true }
tangle-primitives = { workspace = true }
30 changes: 30 additions & 0 deletions pallets/jobs/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "pallet-jobs-rpc-runtime-api"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true }
pallet-jobs = { default-features = false, path = "../../../jobs" }
sp-api = { workspace = true }
tangle-primitives = { workspace = true }
sp-runtime = { workspace = true }
sp-weights = { workspace = true }

[features]
default = [ "std" ]
std = [
"parity-scale-codec/std",
"pallet-jobs/std",
"sp-api/std",
"sp-runtime/std",
"sp-weights/std",
"tangle-primitives/std"
]
46 changes: 46 additions & 0 deletions pallets/jobs/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of Webb.
// Copyright (C) 2022 Webb Technologies Inc.
1xstj marked this conversation as resolved.
Show resolved Hide resolved
//
// Tangle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tangle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Tangle. If not, see <http://www.gnu.org/licenses/>.
//! Runtime API definition for jobs pallet.

#![cfg_attr(not(feature = "std"), no_std)]

use parity_scale_codec::Codec;
use sp_runtime::{traits::MaybeDisplay, Serialize};

pub use tangle_primitives::jobs::RpcResponseJobsData;

sp_api::decl_runtime_apis! {
pub trait JobsApi<AccountId> where
AccountId: Codec + MaybeDisplay + Serialize,
{
/// Query jobs associated with a specific validator.
///
/// This function takes a `validator` parameter of type `AccountId` and attempts
/// to retrieve a list of jobs associated with the provided validator. If successful,
/// it constructs a vector of `RpcResponseJobsData<AccountId>` containing information
/// about the jobs and returns it as a `Result`.
///
/// # Arguments
///
/// * `validator` - The account ID of the validator whose jobs are to be queried.
///
/// # Returns
///
/// Returns a `Result` containing a vector of `RpcResponseJobsData<AccountId>` if the
/// operation is successful
fn query_jobs_by_validator(validator: AccountId) -> Result<Vec<RpcResponseJobsData<AccountId>>, String>;
}
}
110 changes: 110 additions & 0 deletions pallets/jobs/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2022 Webb Technologies Inc.
1xstj marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#![allow(clippy::unnecessary_mut_passed)]

use jsonrpsee::{
core::{Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};
pub use pallet_jobs_rpc_runtime_api::JobsApi as JobsRuntimeApi;
use parity_scale_codec::Codec;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{
traits::{Block as BlockT, MaybeDisplay},
Serialize,
};
use std::sync::Arc;
use tangle_primitives::jobs::RpcResponseJobsData;

/// Merkle RPC methods.
1xstj marked this conversation as resolved.
Show resolved Hide resolved
#[rpc(client, server)]
pub trait JobsApi<BlockHash, AccountId> {
#[method(name = "jobs_queryJobsByValidator")]
fn query_jobs_by_validator(
&self,
at: Option<BlockHash>,
validator: AccountId,
) -> RpcResult<Vec<RpcResponseJobsData<AccountId>>>;
}

/// A struct that implements the `JobsApi`.
pub struct JobsClient<C, M, P> {
client: Arc<C>,
_marker: std::marker::PhantomData<(M, P)>,
}

impl<C, M, P> JobsClient<C, M, P> {
/// Create new `Merkle` instance with the given reference to the client.
1xstj marked this conversation as resolved.
Show resolved Hide resolved
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: Default::default() }
}
}

impl<C, Block, AccountId> JobsApiServer<<Block as BlockT>::Hash, AccountId>
for JobsClient<C, Block, AccountId>
where
Block: BlockT,
AccountId: Codec + MaybeDisplay + Send + Sync + 'static + Serialize,
C: HeaderBackend<Block> + ProvideRuntimeApi<Block> + Send + Sync + 'static,
C::Api: JobsRuntimeApi<Block, AccountId>,
{
fn query_jobs_by_validator(
&self,
at: Option<<Block as BlockT>::Hash>,
validator: AccountId,
) -> RpcResult<Vec<RpcResponseJobsData<AccountId>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

match api.query_jobs_by_validator(at, validator) {
Ok(res) => match res {
Ok(res) => Ok(res),
Err(e) => Err(runtime_error_into_rpc_err(e)),
},
Err(e) => Err(runtime_error_into_rpc_err(e)),
}
}
}

/// Error type of this RPC api.
pub enum Error {
/// The transaction was not decodable.
DecodeError,
/// The call to runtime failed.
RuntimeError,
}

impl From<Error> for i32 {
fn from(e: Error) -> i32 {
match e {
Error::RuntimeError => 1,
Error::DecodeError => 2,
}
}
}

/// Converts a runtime trap into an RPC error.
fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> JsonRpseeError {
CallError::Custom(ErrorObject::owned(
RUNTIME_ERROR,
"Runtime error",
Some(format!("{:?}", err)),
))
.into()
}

const RUNTIME_ERROR: i32 = 1;
Loading
Loading