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

Fix codec issue for dense-pull-ledger-diff #982

Merged
merged 5 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions pallets/manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default = ["std"]
# RPC Interface
rpc = [
"jsonrpsee",
"runtime",
"serde",
"sp-api",
"sp-blockchain",
Expand Down
27 changes: 1 addition & 26 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};
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,30 +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,
) -> DensePullResponse {
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;
DensePullResponse {
should_continue: more_receivers || more_senders,
receivers: base64::encode(receivers.encode()),
senders: base64::encode(senders.encode()),
senders_receivers_total: asset_value_encode(senders_receivers_total),
next_checkpoint: None,
}
}

/// Returns the account ID of this pallet.
#[inline]
pub fn account_id() -> T::AccountId {
Expand Down
5 changes: 3 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
Garandor marked this conversation as resolved.
Show resolved Hide resolved

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,8 @@ 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(
PULL_LEDGER_DIFF_ERROR,
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::{DensePullResponse, PullResponse, RawCheckpoint};
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;
Garandor marked this conversation as resolved.
Show resolved Hide resolved

fn dense_pull_ledger_diff(checkpoint: RawCheckpoint, max_receivers: u64, max_senders: u64) -> DensePullResponse;
}
}
28 changes: 11 additions & 17 deletions pallets/manta-pay/src/test/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,33 +592,27 @@ 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 dense_pull_response =
MantaPayPallet::dense_pull_ledger_diff(check_point, max_receivers, max_senders);
assert_eq!(
pull_response.senders_receivers_total,
dense_pull_response.senders_receivers_total
);
assert_eq!(
pull_response.should_continue,
dense_pull_response.should_continue
);
assert_eq!(
pull_response.should_continue,
dense_pull_response.should_continue
);

// ensure all Utxos have been returned.
assert!(!runtime_pull_response.should_continue);

// convert runtime response into native response
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
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);
});
}
19 changes: 16 additions & 3 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 @@ -912,11 +912,11 @@ pub struct DensePullResponse {
/// Ledger Receiver Chunk
// we decode the receivers/senders with our own way
#[codec(skip)]
Garandor marked this conversation as resolved.
Show resolved Hide resolved
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,6 +930,19 @@ pub struct DensePullResponse {
pub next_checkpoint: Option<Checkpoint>,
}

impl From<PullResponse> for DensePullResponse {
#[inline]
fn from(resp: PullResponse) -> DensePullResponse {
Self {
should_continue: resp.should_continue,
receivers: base64::encode(resp.receivers.encode()),
senders: base64::encode(resp.senders.encode()),
senders_receivers_total: resp.senders_receivers_total,
next_checkpoint: None,
}
}
}

/// Raw Checkpoint for Encoding and Decoding
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode)]
pub struct RawCheckpoint {
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::DensePullResponse {
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::DensePullResponse {
MantaPay::dense_pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}
}

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