Skip to content

Commit

Permalink
Cache scanned masp txs more effectively
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed May 29, 2024
1 parent 6165b65 commit ca44b7b
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 374 deletions.
2 changes: 1 addition & 1 deletion crates/apps_lib/src/cli/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl CliApi {
.map(|sk| sk.into())
.collect::<Vec<_>>();
crate::client::masp::syncing(
chain_ctx.shielded,
&chain_ctx.shielded.utils,
&client,
&io,
args.batch_size,
Expand Down
42 changes: 21 additions & 21 deletions crates/apps_lib/src/client/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ use masp_primitives::sapling::ViewingKey;
use masp_primitives::zip32::ExtendedSpendingKey;
use namada_sdk::error::Error;
use namada_sdk::io::Io;
use namada_sdk::masp::shielded_ctx::fetch_shielded_ctx;
use namada_sdk::masp::types::IndexedNoteEntry;
use namada_sdk::masp::utils::{
LedgerMaspClient, PeekableIter, ProgressTracker, ProgressType,
RetryStrategy,
};
use namada_sdk::masp::{ShieldedContext, ShieldedUtils};
use namada_sdk::masp::ShieldedUtils;
use namada_sdk::queries::Client;
use namada_sdk::storage::BlockHeight;
use namada_sdk::{display, display_line};
Expand All @@ -21,15 +22,15 @@ pub async fn syncing<
C: Client + Sync,
IO: Io + Sync + Send,
>(
mut shielded: ShieldedContext<U>,
shielded_utils: &U,
client: &C,
io: &IO,
batch_size: u64,
start_query_height: Option<BlockHeight>,
last_query_height: Option<BlockHeight>,
sks: &[ExtendedSpendingKey],
fvks: &[ViewingKey],
) -> Result<ShieldedContext<U>, Error> {
) -> Result<(), Error> {
let shutdown_signal = async {
let (tx, rx) = tokio::sync::oneshot::channel();
namada_sdk::control_flow::shutdown_send(tx).await;
Expand All @@ -38,31 +39,30 @@ pub async fn syncing<

display_line!(io, "\n\n");
let logger = CliProgressTracker::new(io);
let sync = async move {
shielded
.fetch::<_, _, _, LedgerMaspClient<'_, C>>(
client,
&logger,
RetryStrategy::Forever,
start_query_height,
last_query_height,
batch_size,
sks,
fvks,
)
.await
.map(|_| shielded)
let sync_result_fut = async move {
fetch_shielded_ctx::<_, _, _, LedgerMaspClient<'_, C>, _>(
shielded_utils,
client,
&logger,
RetryStrategy::Forever,
start_query_height,
last_query_height,
batch_size,
sks,
fvks,
)
.await
};
tokio::select! {
sync = sync => {
let shielded = sync?;
sync_result = sync_result_fut => {
sync_result?;
display!(io, "\nSyncing finished\n");
Ok(shielded)
Ok(())
},
sig = shutdown_signal => {
sig.map_err(|e| Error::Other(e.to_string()))?;
display!(io, "\n");
Ok(ShieldedContext::default())
Ok(())
},
}
}
Expand Down
11 changes: 10 additions & 1 deletion crates/core/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::str::FromStr;

use arse_merkle_tree::traits::Hasher;
use arse_merkle_tree::H256;
use borsh::{BorshDeserialize, BorshSchema, BorshSerialize};
use data_encoding::HEXUPPER;
use namada_macros::BorshDeserializer;
#[cfg(feature = "migrations")]
Expand All @@ -14,6 +13,10 @@ use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use thiserror::Error;

use crate::borsh::{
BorshDeserialize, BorshSchema, BorshSerialize, BorshSerializeExt,
};

/// The length of the transaction hash string
pub const HASH_LENGTH: usize = 32;

Expand Down Expand Up @@ -130,6 +133,12 @@ impl Hash {
Self(*digest.as_ref())
}

/// Compute sha256 of some borsh encodable data
#[inline]
pub fn sha256_borsh<T: BorshSerialize>(value: &T) -> Self {
Self::sha256(value.serialize_to_vec())
}

/// Return zeros
pub fn zero() -> Self {
Self([0u8; HASH_LENGTH])
Expand Down
19 changes: 14 additions & 5 deletions crates/node/src/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,18 +1033,27 @@ impl BenchShieldedCtx {
.wallet
.find_spending_key(ALBERT_SPENDING_KEY, None)
.unwrap();
self.shielded = async_runtime
.block_on(namada_apps_lib::client::masp::syncing(
self.shielded,
&self.shell,
let shielded_utils_ref = &self.shielded.utils;
let shell_ref = &self.shell;
self.shielded = async_runtime.block_on(async move {
namada_apps_lib::client::masp::syncing(
shielded_utils_ref,
shell_ref,
&StdIo,
1,
None,
None,
&[spending_key.into()],
&[],
))
)
.await
.unwrap();

shielded_utils_ref
.load(ContextSyncStatus::Confirmed, true)
.await
.unwrap()
});
let native_token = self.shell.state.in_mem().native_token.clone();
let namada = NamadaImpl::native_new(
self.shell,
Expand Down
Loading

0 comments on commit ca44b7b

Please sign in to comment.