Skip to content

Commit

Permalink
Remove runtime api dense_pull_ledger_diff
Browse files Browse the repository at this point in the history
Signed-off-by: Dengjianping <djptux@gmail.com>
  • Loading branch information
Dengjianping committed Jan 12, 2023
1 parent 57130c1 commit f690dcb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 95 deletions.
26 changes: 1 addition & 25 deletions pallets/manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use manta_util::{

pub use crate::types::{Checkpoint, RawCheckpoint};
pub use pallet::*;
pub use types::{DensePullResponse, PullResponse, RuntimeDensePullResponse};
pub use types::PullResponse;
pub use weights::WeightInfo;

#[cfg(test)]
Expand Down Expand Up @@ -121,7 +121,6 @@ pub mod pallet {
use super::*;
use frame_support::{pallet_prelude::*, traits::StorageVersion};
use frame_system::pallet_prelude::*;
use scale_codec::Encode;
use sp_runtime::traits::AccountIdConversion;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
Expand Down Expand Up @@ -673,29 +672,6 @@ pub mod pallet {
}
}

/// Returns the diff of ledger state since the given `checkpoint`, `max_receivers`, and
/// `max_senders`.
#[inline]
pub fn dense_pull_ledger_diff(
checkpoint: Checkpoint,
max_receivers: u64,
max_senders: u64,
) -> RuntimeDensePullResponse {
let (more_receivers, receivers) =
Self::pull_receivers(*checkpoint.receiver_index, max_receivers);
let (more_senders, senders) = Self::pull_senders(checkpoint.sender_index, max_senders);
let senders_receivers_total = (0..=255)
.map(|i| ShardTrees::<T>::get(i).current_path.leaf_index as u128)
.sum::<u128>()
+ NullifierSetSize::<T>::get() as u128;
RuntimeDensePullResponse {
should_continue: more_receivers || more_senders,
receivers: receivers.encode(),
senders: senders.encode(),
senders_receivers_total: asset_value_encode(senders_receivers_total),
}
}

/// Returns the account ID of this pallet.
#[inline]
pub fn account_id() -> T::AccountId {
Expand Down
4 changes: 2 additions & 2 deletions pallets/manta-pay/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! MantaPay RPC Interfaces

use crate::{runtime::PullLedgerDiffApi, Checkpoint, DensePullResponse, PullResponse};
use crate::{runtime::PullLedgerDiffApi, types::DensePullResponse, Checkpoint, PullResponse};
use alloc::sync::Arc;
use core::marker::PhantomData;
use jsonrpsee::{
Expand Down Expand Up @@ -109,7 +109,7 @@ where
) -> RpcResult<DensePullResponse> {
let api = self.client.runtime_api();
let at = BlockId::hash(self.client.info().finalized_hash);
api.dense_pull_ledger_diff(&at, checkpoint.into(), max_receivers, max_senders)
api.pull_ledger_diff(&at, checkpoint.into(), max_receivers, max_senders)
.map(Into::into)
.map_err(|err| {
CallError::Custom(ErrorObject::owned(
Expand Down
4 changes: 1 addition & 3 deletions pallets/manta-pay/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

//! MantaPay Runtime APIs

use crate::{PullResponse, RawCheckpoint, RuntimeDensePullResponse};
use crate::{PullResponse, RawCheckpoint};

sp_api::decl_runtime_apis! {
pub trait PullLedgerDiffApi {
fn pull_ledger_diff(checkpoint: RawCheckpoint, max_receivers: u64, max_senders: u64) -> PullResponse;

fn dense_pull_ledger_diff(checkpoint: RawCheckpoint, max_receivers: u64, max_senders: u64) -> RuntimeDensePullResponse;
}
}
27 changes: 6 additions & 21 deletions pallets/manta-pay/src/test/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,39 +595,24 @@ fn pull_ledger_diff_should_work() {
// retrieving 128 receivers/senders can get all existing Utxos
let (max_receivers, max_senders) = (128, 128);
let check_point = crate::Checkpoint::default();
let pull_response =
let runtime_pull_response =
MantaPayPallet::pull_ledger_diff(check_point, max_receivers, max_senders);
let runtime_dense_pull_response =
MantaPayPallet::dense_pull_ledger_diff(check_point, max_receivers, max_senders);

// ensure all Utxos have been returned.
assert!(!pull_response.should_continue);
assert!(!runtime_dense_pull_response.should_continue);
assert_eq!(
pull_response.should_continue,
runtime_dense_pull_response.should_continue
);
assert_eq!(
pull_response.should_continue,
runtime_dense_pull_response.should_continue
);

assert_eq!(
pull_response.senders_receivers_total,
runtime_dense_pull_response.senders_receivers_total
);
assert!(!runtime_pull_response.should_continue);

// convert runtime response into native response
let dense_pull_response: crate::DensePullResponse = runtime_dense_pull_response.into();
let dense_pull_response: crate::types::DensePullResponse =
runtime_pull_response.clone().into();

let dense_receivers = base64::decode(dense_pull_response.receivers).unwrap();
let mut slice_of = dense_receivers.as_slice();
let decoded_receivers = <crate::ReceiverChunk as Decode>::decode(&mut slice_of).unwrap();
assert_eq!(pull_response.receivers, decoded_receivers);
assert_eq!(runtime_pull_response.receivers, decoded_receivers);

let dense_senders = base64::decode(dense_pull_response.senders).unwrap();
let mut slice_of = dense_senders.as_slice();
let decoded_senders = <crate::SenderChunk as Decode>::decode(&mut slice_of).unwrap();
assert_eq!(pull_response.senders, decoded_senders);
assert_eq!(runtime_pull_response.senders, decoded_senders);
});
}
37 changes: 9 additions & 28 deletions pallets/manta-pay/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Type Definitions for Manta Pay

use alloc::{boxed::Box, string::String, vec::Vec};
use alloc::{boxed::Box, vec::Vec};
use manta_crypto::merkle_tree;
use manta_pay::{
config::{
Expand Down Expand Up @@ -896,6 +896,7 @@ pub struct PullResponse {
}

/// Ledger Source Dense Pull Response
#[cfg_attr(feature = "rpc", test)]
#[cfg_attr(
feature = "serde",
derive(Deserialize, Serialize),
Expand All @@ -912,11 +913,11 @@ pub struct DensePullResponse {
/// Ledger Receiver Chunk
// we decode the receivers/senders with our own way
#[codec(skip)]
pub receivers: String,
pub receivers: alloc::string::String,

/// Ledger Sender Chunk
#[codec(skip)]
pub senders: String,
pub senders: alloc::string::String,

/// Total Number of Senders/Receivers in Ledger
pub senders_receivers_total: [u8; 16],
Expand All @@ -930,34 +931,14 @@ pub struct DensePullResponse {
pub next_checkpoint: Option<Checkpoint>,
}

// Runtime only type,
// for fixing the issue when receivers/senders(Vec<u8>, runtime) to receivers/senders(String, native client)
#[derive(Clone, Debug, Encode, Default, Eq, Hash, Decode, PartialEq, TypeInfo)]
pub struct RuntimeDensePullResponse {
/// Pull Continuation Flag
///
/// The `should_continue` flag is set to `true` if the client should request more data from the
/// ledger to finish the pull.
pub should_continue: bool,

/// Ledger Receiver Chunk
// we decode the receivers/senders with our own way
pub receivers: Vec<u8>,

/// Ledger Sender Chunk
pub senders: Vec<u8>,

/// Total Number of Senders/Receivers in Ledger
pub senders_receivers_total: [u8; 16],
}

impl From<RuntimeDensePullResponse> for DensePullResponse {
#[cfg_attr(feature = "rpc", test)]
impl From<PullResponse> for DensePullResponse {
#[inline]
fn from(resp: RuntimeDensePullResponse) -> DensePullResponse {
fn from(resp: PullResponse) -> DensePullResponse {
Self {
should_continue: resp.should_continue,
receivers: base64::encode(resp.receivers),
senders: base64::encode(resp.senders),
receivers: base64::encode(resp.receivers.encode()),
senders: base64::encode(resp.senders.encode()),
senders_receivers_total: resp.senders_receivers_total,
next_checkpoint: None,
}
Expand Down
8 changes: 0 additions & 8 deletions runtime/calamari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,14 +1049,6 @@ impl_runtime_apis! {
) -> pallet_manta_pay::PullResponse {
MantaPay::pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}

fn dense_pull_ledger_diff(
checkpoint: pallet_manta_pay::RawCheckpoint,
max_receiver: u64,
max_sender: u64
) -> pallet_manta_pay::RuntimeDensePullResponse {
MantaPay::dense_pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}
}

impl nimbus_primitives::NimbusApi<Block> for Runtime {
Expand Down
8 changes: 0 additions & 8 deletions runtime/dolphin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,6 @@ impl_runtime_apis! {
) -> pallet_manta_pay::PullResponse {
MantaPay::pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}

fn dense_pull_ledger_diff(
checkpoint: pallet_manta_pay::RawCheckpoint,
max_receiver: u64,
max_sender: u64
) -> pallet_manta_pay::RuntimeDensePullResponse {
MantaPay::dense_pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}
}

impl nimbus_primitives::NimbusApi<Block> for Runtime {
Expand Down

0 comments on commit f690dcb

Please sign in to comment.