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 2 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
11 changes: 5 additions & 6 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::{DensePullResponse, PullResponse, RuntimeDensePullResponse};
Garandor marked this conversation as resolved.
Show resolved Hide resolved
pub use weights::WeightInfo;

#[cfg(test)]
Expand Down Expand Up @@ -680,20 +680,19 @@ pub mod pallet {
checkpoint: Checkpoint,
max_receivers: u64,
max_senders: u64,
) -> DensePullResponse {
) -> 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;
DensePullResponse {
RuntimeDensePullResponse {
should_continue: more_receivers || more_senders,
receivers: base64::encode(receivers.encode()),
senders: base64::encode(senders.encode()),
receivers: receivers.encode(),
Garandor marked this conversation as resolved.
Show resolved Hide resolved
senders: senders.encode(),
senders_receivers_total: asset_value_encode(senders_receivers_total),
next_checkpoint: None,
}
}

Expand Down
1 change: 1 addition & 0 deletions pallets/manta-pay/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ where
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)
.map(Into::into)
.map_err(|err| {
CallError::Custom(ErrorObject::owned(
PULL_LEDGER_DIFF_ERROR,
Expand Down
4 changes: 2 additions & 2 deletions pallets/manta-pay/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

//! MantaPay Runtime APIs

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

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;
fn dense_pull_ledger_diff(checkpoint: RawCheckpoint, max_receivers: u64, max_senders: u64) -> RuntimeDensePullResponse;
}
}
21 changes: 15 additions & 6 deletions pallets/manta-pay/src/test/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,25 +592,34 @@ 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 =
MantaPayPallet::pull_ledger_diff(check_point, max_receivers, max_senders);
let dense_pull_response =
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.senders_receivers_total,
dense_pull_response.senders_receivers_total
pull_response.should_continue,
runtime_dense_pull_response.should_continue
);
assert_eq!(
pull_response.should_continue,
dense_pull_response.should_continue
runtime_dense_pull_response.should_continue
);

assert_eq!(
pull_response.should_continue,
dense_pull_response.should_continue
pull_response.senders_receivers_total,
runtime_dense_pull_response.senders_receivers_total
);

// convert runtime response into native response
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
let dense_pull_response: crate::DensePullResponse = runtime_dense_pull_response.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();
Expand Down
34 changes: 34 additions & 0 deletions pallets/manta-pay/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,40 @@ 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 {
#[inline]
fn from(resp: RuntimeDensePullResponse) -> DensePullResponse {
Self {
should_continue: resp.should_continue,
receivers: base64::encode(resp.receivers),
senders: base64::encode(resp.senders),
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
2 changes: 1 addition & 1 deletion runtime/calamari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ impl_runtime_apis! {
checkpoint: pallet_manta_pay::RawCheckpoint,
max_receiver: u64,
max_sender: u64
) -> pallet_manta_pay::DensePullResponse {
) -> pallet_manta_pay::RuntimeDensePullResponse {
MantaPay::dense_pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/dolphin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ impl_runtime_apis! {
checkpoint: pallet_manta_pay::RawCheckpoint,
max_receiver: u64,
max_sender: u64
) -> pallet_manta_pay::DensePullResponse {
) -> pallet_manta_pay::RuntimeDensePullResponse {
MantaPay::dense_pull_ledger_diff(checkpoint.into(), max_receiver, max_sender)
}
}
Expand Down