Skip to content

Commit

Permalink
some minor additions
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 committed Dec 21, 2024
1 parent 586c401 commit ea52409
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 56 deletions.
21 changes: 12 additions & 9 deletions modules/pallets/ismp/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,9 @@ where
.client
.read_proof(at, &mut keys.iter().map(|key| key.as_slice()))
.map(|proof| proof.into_iter_nodes().collect())
.map_err(|_| runtime_error_into_rpc_error("Error generating state proof"))?;
.map_err(|e| {
runtime_error_into_rpc_error(format!("Error generating state proof: {e:?}"))
})?;
Ok(Proof { proof: proof.encode(), height })
}

Expand All @@ -284,11 +286,12 @@ where
let storage_proof = self
.client
.read_child_proof(at, &child_info, &mut keys.iter().map(|key| key.as_slice()))
.map_err(|_| runtime_error_into_rpc_error("Error generating child trie proof"))?;
let state = self
.backend
.state_at(at)
.map_err(|_| runtime_error_into_rpc_error("Error accessing state backend"))?;
.map_err(|e| {
runtime_error_into_rpc_error(format!("Error generating child trie proof: {e:?}"))
})?;
let state = self.backend.state_at(at).map_err(|e| {
runtime_error_into_rpc_error(format!("Error accessing state backend: {e:?}"))
})?;
let child_root = state
.storage(child_info.prefixed_storage_key().as_slice())
.map_err(|err| runtime_error_into_rpc_error(format!("Storage Read Error: {err:?}")))?
Expand All @@ -310,9 +313,9 @@ where
.with_recorder(&mut recorder)
.build();
for key in keys {
let _ = trie
.get(&key)
.map_err(|_| runtime_error_into_rpc_error("Error generating child trie proof"))?;
let _ = trie.get(&key).map_err(|e| {
runtime_error_into_rpc_error(format!("Error generating child trie proof: {e:?}"))
})?;
}

let proof_nodes = recorder.drain().into_iter().map(|f| f.data).collect::<Vec<_>>();
Expand Down
6 changes: 4 additions & 2 deletions modules/pallets/mmr/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ where
.ok_or_else(|| runtime_error_into_rpc_error("invalid block height provided"))?;
let (_, proof): (Vec<Leaf>, pallet_ismp::offchain::Proof<B::Hash>) = api
.generate_proof(at, keys)
.map_err(|_| runtime_error_into_rpc_error("Error calling runtime api"))?
.map_err(|_| runtime_error_into_rpc_error("Error generating mmr proof"))?;
.map_err(|e| runtime_error_into_rpc_error(format!("Error calling runtime api: {e:?}")))?
.map_err(|e| {
runtime_error_into_rpc_error(format!("Error generating mmr proof: {e:?}"))
})?;
Ok(Proof { proof: proof.encode(), height })
}
}
47 changes: 2 additions & 45 deletions modules/pallets/testsuite/src/tests/pallet_call_decompressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
runtime::{
new_test_ext, Ismp, RuntimeCall, RuntimeOrigin, Test, Timestamp, MOCK_CONSENSUS_STATE_ID,
},
tests::pallet_ismp_relayer::{encode_accumulate_fees_call, read_file_string},
tests::pallet_ismp_relayer::encode_accumulate_fees_call,
};
use codec::Encode;
use frame_support::traits::Time;
Expand All @@ -29,52 +29,9 @@ use ismp::{
messaging::{Message, Proof, RequestMessage, ResponseMessage, TimeoutMessage},
router::{PostResponse, Request, RequestResponse},
};
use ruzstd::StreamingDecoder;
use sp_core::{H256, H512};
use sp_runtime::{DispatchError, ModuleError};
use std::{
io::Read,
time::{Duration, Instant},
};
use zstd_safe::WriteBuf;

#[test]
fn compress_benchmark_with_zstd_safe() {
new_test_ext().execute_with(|| {
let start_time = Instant::now();
let mut buffer = [0u8; 256000];
let proof_str = read_file_string("src/tests/proofs/accumulate_fee_proof.txt");
let compressed_proof = zstd_safe::compress(&mut buffer, proof_str.as_bytes(), 3).unwrap();
let compressed_proof = &buffer[..compressed_proof];
let end_time = Instant::now();
let duration = end_time - start_time;
println!("time taken for compression with zstd_safe {:?}", duration);
assert!(proof_str.as_bytes().to_vec().len() > compressed_proof.len());

let start_time = Instant::now();
let mut buffer = vec![0u8; 25600000000];
let written = zstd_safe::decompress(&mut buffer[..], compressed_proof).unwrap();
let decompressed_data = &buffer[..written];
let end_time = Instant::now();

let duration = end_time - start_time;
println!("time taken for decompression with zstd_safe {:?}", duration);

let start_time = Instant::now();
let mut decoder = StreamingDecoder::new(compressed_proof.as_slice()).unwrap();
let mut result = vec![0u8; 2000000];
let read = decoder.read(&mut result).unwrap();
dbg!(read / 1000);

let end_time = Instant::now();
let duration = end_time - start_time;
println!("time taken for decompression with ruzstd {:?}", duration);

dbg!(decompressed_data.len() / 1000);
assert_eq!(proof_str.as_bytes().to_vec(), decompressed_data);
assert_eq!(proof_str.as_bytes(), &result[..read]);
});
}
use std::time::Duration;

#[test]
#[ignore]
Expand Down

0 comments on commit ea52409

Please sign in to comment.