Skip to content

Commit

Permalink
wip: add rpc to node
Browse files Browse the repository at this point in the history
  • Loading branch information
bhgomes committed May 5, 2022
1 parent 296b2cc commit fcfbb3c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pallets/manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ manta-accounting = { git = "https://github.com/manta-network/manta-rs.git", bran
manta-crypto = { git = "https://github.com/manta-network/manta-rs.git", branch = "feat/flexible-ledger-api", default-features = false }
manta-pay = { git = "https://github.com/manta-network/manta-rs.git", branch = "feat/flexible-ledger-api", default-features = false, features = ["groth16", "scale", "serde"] }
manta-sdk = { git = "https://github.com/manta-network/sdk.git", default-features = false }
manta-util = { git = "https://github.com/manta-network/manta-rs.git", branch = "feat/flexible-ledger-api", default-features = false, features = ["serde"] }
manta-util = { git = "https://github.com/manta-network/manta-rs.git", branch = "feat/flexible-ledger-api", default-features = false, features = ["serde", "serde_with"] }
manta-primitives = { path = "../../primitives", default-features = false}

[dev-dependencies]
Expand Down
74 changes: 30 additions & 44 deletions pallets/manta-pay/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
//! MantaPay RPC Interfaces

use crate::{
types::{decode, EncryptedNote, Utxo, VoidNumber},
types::{EncryptedNote, Utxo, VoidNumber},
Config, Pallet, Shards, VoidNumberSet,
};
use alloc::sync::Arc;
use core::marker::PhantomData;
use jsonrpc_core::{Error as RpcError, ErrorCode};
use jsonrpc_core::{Error, ErrorCode, Result};
use jsonrpc_derive::rpc;
use manta_accounting::wallet::ledger;
use manta_pay::{
config,
signer::{Checkpoint, RawCheckpoint},
};
use manta_pay::signer::{Checkpoint, RawCheckpoint};
use manta_util::serde::{Deserialize, Serialize};
use scale_codec::{Decode, Encode, Error as CodecError};
use scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
Expand All @@ -42,14 +38,14 @@ pub trait PullApi {
/// Returns the update required to be synchronized with the ledger starting from
/// `checkpoint`.
#[rpc(name = "manta_pay_pull")]
fn pull(&self, checkpoint: Checkpoint) -> jsonrpc_core::Result<PullResponse>;
fn pull(&self, checkpoint: Checkpoint) -> Result<PullResponse>;
}

/// Receiver Chunk Data Type
pub type ReceiverChunk = Vec<(config::Utxo, config::EncryptedNote)>;
pub type ReceiverChunk = Vec<(Utxo, EncryptedNote)>;

/// Sender Chunk Data Type
pub type SenderChunk = Vec<config::VoidNumber>;
pub type SenderChunk = Vec<VoidNumber>;

/// Ledger Source Pull Response
#[derive(
Expand Down Expand Up @@ -88,15 +84,13 @@ where

/// Pulls receiver data from the ledger starting at the `receiver_index`.
#[inline]
fn pull_receivers(
receiver_index: &mut [usize; 256],
) -> Result<(bool, ReceiverChunk), CodecError> {
fn pull_receivers(receiver_index: &mut [usize; 256]) -> (bool, ReceiverChunk) {
let mut more_receivers = false;
let mut receivers = Vec::new();
for (i, index) in receiver_index.iter_mut().enumerate() {
more_receivers |= Self::pull_receivers_for_shard(i as u8, index, &mut receivers)?;
more_receivers |= Self::pull_receivers_for_shard(i as u8, index, &mut receivers);
}
Ok((more_receivers, receivers))
(more_receivers, receivers)
}

/// Pulls receiver data from the shard at `shard_index` starting at the `receiver_index`,
Expand All @@ -106,7 +100,7 @@ where
shard_index: u8,
receiver_index: &mut usize,
receivers: &mut ReceiverChunk,
) -> Result<bool, CodecError> {
) -> bool {
let mut iter = if *receiver_index == 0 {
Shards::<T>::iter_prefix(shard_index)
} else {
Expand All @@ -117,49 +111,49 @@ where
match iter.next() {
Some((_, (utxo, encrypted_note))) => {
*receiver_index += 1;
receivers.push((decode(utxo)?, encrypted_note));
receivers.push((utxo, encrypted_note));
}
_ => return Ok(false),
_ => return false,
}
}
Ok(iter.next().is_some())
iter.next().is_some()
}

/// Pulls sender data from the ledger starting at the `sender_index`.
#[inline]
fn pull_senders(sender_index: &mut usize) -> Result<(bool, SenderChunk), CodecError> {
fn pull_senders(sender_index: &mut usize) -> (bool, SenderChunk) {
let mut senders = Vec::new();
let mut iter = VoidNumberSet::<T>::iter().skip(*sender_index);
for _ in 0..Self::PULL_MAX_SENDER_UPDATE_SIZE {
match iter.next() {
Some((sender, _)) => {
*sender_index += 1;
senders.push(decode(sender)?);
senders.push(sender);
}
_ => return Ok((false, senders)),
_ => return (false, senders),
}
}
Ok((iter.next().is_some(), senders))
(iter.next().is_some(), senders)
}

/// Returns the update required to be synchronized with the ledger starting from
/// `checkpoint`.
#[inline]
pub fn pull(mut checkpoint: Checkpoint) -> Result<PullResponse, CodecError> {
let (more_receivers, receivers) = Self::pull_receivers(&mut checkpoint.receiver_index)?;
let (more_senders, senders) = Self::pull_senders(&mut checkpoint.sender_index)?;
Ok(PullResponse {
pub fn pull(mut checkpoint: Checkpoint) -> PullResponse {
let (more_receivers, receivers) = Self::pull_receivers(&mut checkpoint.receiver_index);
let (more_senders, senders) = Self::pull_senders(&mut checkpoint.sender_index);
PullResponse {
should_continue: more_receivers || more_senders,
checkpoint,
receivers,
senders,
})
}
}
}

sp_api::decl_runtime_apis! {
pub trait MantaPayPullRuntimeApi {
fn pull(checkpoint: RawCheckpoint) -> Result<PullResponse, CodecError>;
fn pull(checkpoint: RawCheckpoint) -> PullResponse;
}
}

Expand Down Expand Up @@ -190,21 +184,13 @@ where
C::Api: MantaPayPullRuntimeApi<B>,
{
#[inline]
fn pull(&self, checkpoint: Checkpoint) -> jsonrpc_core::Result<PullResponse> {
fn pull(&self, checkpoint: Checkpoint) -> Result<PullResponse> {
let api = self.client.runtime_api();
let at = BlockId::hash(self.client.info().best_hash);
match api.pull(&at, checkpoint.into()) {
Ok(Ok(response)) => Ok(response),
Ok(Err(err)) => Err(RpcError {
code: ErrorCode::ServerError(1),
message: "Unable to compute state diff for pull".into(),
data: Some(err.to_string().into()),
}),
Err(err) => Err(RpcError {
code: ErrorCode::ServerError(2),
message: "Error during runtime API".into(),
data: Some(err.to_string().into()),
}),
}
api.pull(&at, checkpoint.into()).map_err(|err| Error {
code: ErrorCode::ServerError(1),
message: "Unable to compute state diff for pull".into(),
data: Some(err.to_string().into()),
})
}
}
1 change: 1 addition & 0 deletions pallets/manta-pay/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ pub struct EncryptedNote {
pub ephemeral_public_key: Group,

/// Ciphertext
#[serde(with = "manta_util::serde_with::As::<[manta_util::serde_with::Same; 68]>")]
pub ciphertext: Ciphertext,
}

Expand Down
17 changes: 9 additions & 8 deletions runtime/dolphin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_core::{
crypto::KeyTypeId,
u32_trait::{_1, _2, _3, _4, _5},
OpaqueMetadata,
};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{AccountIdLookup, BlakeTwo256, Block as BlockT},
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult, Perbill, Permill,
};

use sp_core::u32_trait::{_1, _2, _3, _4, _5};
use sp_std::{cmp::Ordering, prelude::*};
use sp_version::RuntimeVersion;

#[cfg(feature = "std")]
use sp_version::NativeVersion;
use sp_version::RuntimeVersion;

use frame_support::{
construct_runtime, match_type,
Expand Down Expand Up @@ -1276,13 +1279,11 @@ impl_runtime_apis! {
}
}

/* TODO:
impl pallet_manta_pay::rpc::MantaPayPullRuntimeApi for Runtime {
fn pull(checkpoint: Checkpoint) -> Result<PullResponse, scale_codec::Error> {
todo!()
fn pull(checkpoint: Checkpoint) -> PullResponse {
MantaPay::pull(checkpoint)
}
}
*/

#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> for Runtime {
Expand Down

0 comments on commit fcfbb3c

Please sign in to comment.