Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Jul 5, 2024
1 parent 4864020 commit f95cf8f
Show file tree
Hide file tree
Showing 19 changed files with 699 additions and 403 deletions.
5 changes: 4 additions & 1 deletion block-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ edition = "2021"
name = "block_parser"
path = "src/lib.rs"

[dependencies.regex]
version = "1.10.5"

[dependencies.serde_json]
version = "1.0.117"

[dependencies.snarkvm]
workspace = true
[dependencies]
anyhow = "1.0.86"
serde = { version = "1.0.203", features = ["derive"] }
serde = { version = "1.0.203", features = ["derive"] }
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::hash::Hash;

#[derive(Clone)]
pub struct AddressBytes<N: Network> {
// The little-endian bytes of the address.
// This is approximately 32 bytes, which implies that clones are not prohibitively expensive.
// The little-endian archive of the address.
// This is approximately 32 archive, which implies that clones are not prohibitively expensive.
// For further performance improvements, consider using `smallvec`.
bytes_le: Vec<u8>,
address: OnceCell<Option<Address<N>>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ pub type BondedMapping<N> = HashMap<AddressBytes<N>, (AddressBytes<N>, u64)>;
pub type UnbondedMapping<N> = HashMap<AddressBytes<N>, (u64, u32)>;
pub type WithdrawMapping<N> = HashMap<AddressBytes<N>, AddressBytes<N>>;

// Decodes the block from little-endian bytes.
// Decodes the block from little-endian archive.
pub fn decode_block<N: Network, R: Read>(mut reader: R) -> Result<Block<N>> {
Ok(Block::read_le(&mut reader)?)
}

// Decodes the bonded mapping from little-endian bytes.
// Decodes the bonded mapping from little-endian archive.
pub fn decode_bonded_mapping<N: Network, R: Read>(mut reader: R) -> Result<BondedMapping<N>> {
// Get the length of the bonded mapping.
let length = u32::read_le(&mut reader)?;
Expand All @@ -20,14 +20,14 @@ pub fn decode_bonded_mapping<N: Network, R: Read>(mut reader: R) -> Result<Bonde
println!("Entry: {}", i);
let key = get_address_bytes_from_plaintext(&mut reader)?;
let value = {
// Read the struct member bytes from the value bytes.
// Read the struct member archive from the value archive.
let mut members = get_struct_member_bytes_from_value::<N, _>(&mut reader, 2)?;
// Check that there are 2 members.
ensure!(members.len() == 2, "Expected 2 members");
// Get the amount.
// Note that this unwrap is safe because we have checked that there are 2 members.
let amount = u64::read_le(members.pop().unwrap().as_slice())?;
// Get the address bytes.
// Get the address archive.
// Note that this unwrap is safe because we have checked that there are 2 members.
let address_bytes = AddressBytes::new(members.pop().unwrap());

Expand All @@ -38,7 +38,7 @@ pub fn decode_bonded_mapping<N: Network, R: Read>(mut reader: R) -> Result<Bonde
Ok(bonded)
}

// Decodes the unbonding mapping from little-endian bytes.
// Decodes the unbonding mapping from little-endian archive.
pub fn decode_unbonding_mapping<N: Network, R: Read>(mut reader: R) -> Result<UnbondedMapping<N>> {
// Get the length of the unbonding mapping.
let length = u32::read_le(&mut reader)?;
Expand All @@ -48,7 +48,7 @@ pub fn decode_unbonding_mapping<N: Network, R: Read>(mut reader: R) -> Result<Un
for _ in 0..length {
let key = get_address_bytes_from_plaintext(&mut reader)?;
let value = {
// Read the struct member bytes from the value bytes.
// Read the struct member archive from the value archive.
let members = get_struct_member_bytes_from_value::<N, _>(&mut reader, 2)?;
// Get the amount.
let amount = u64::read_le(members[0].as_slice())?;
Expand All @@ -61,7 +61,7 @@ pub fn decode_unbonding_mapping<N: Network, R: Read>(mut reader: R) -> Result<Un
Ok(unbonding)
}

// Decodes the withdraw mapping from little-endian bytes.
// Decodes the withdraw mapping from little-endian archive.
pub fn decode_withdraw_mapping<N: Network, R: Read>(mut reader: R) -> Result<WithdrawMapping<N>> {
// Get the length of the withdraw mapping.
let length = u32::read_le(&mut reader)?;
Expand All @@ -76,12 +76,12 @@ pub fn decode_withdraw_mapping<N: Network, R: Read>(mut reader: R) -> Result<Wit
Ok(withdraw)
}

// A helper function to check and get the struct member plaintext bytes from value bytes.
// A helper function to check and get the struct member plaintext archive from value archive.
fn get_struct_member_bytes_from_value<N: Network, R: Read>(
mut reader: R,
num_members: u8,
) -> Result<Vec<Vec<u8>>> {
// Initialize the struct member bytes.
// Initialize the struct member archive.
let mut members = Vec::with_capacity(num_members as usize);
// Get the variant of the value.
let variant = u8::read_le(&mut reader).unwrap();
Expand All @@ -102,27 +102,27 @@ fn get_struct_member_bytes_from_value<N: Network, R: Read>(
// Read the plaintext value (in 2 steps to prevent infinite recursion).
let num_bytes = u16::read_le(&mut reader)?;
println!("num_bytes: {:?}", num_bytes);
// Read the plaintext bytes.
// Read the plaintext archive.
let mut bytes = Vec::new();
(&mut reader).take(num_bytes as u64).read_to_end(&mut bytes)?;
println!("bytes: {:?}", bytes);
println!("archive: {:?}", bytes);
// Add the member.
members.push(bytes);
}
Ok(members)
}

// A helper function to check and get the address bytes from value bytes.
// A helper function to check and get the address archive from value archive.
fn get_address_bytes_from_value<N: Network, R: Read>(mut reader: R) -> Result<AddressBytes<N>> {
// Get the variant of the address.
let variant = u8::read_le(&mut reader)?;
// Check that it is a `Plaintext` variant.
ensure!(variant == 0, "Expected a `Plaintext` variant");
// Check and get the address bytes.
// Check and get the address archive.
get_address_bytes_from_plaintext(&mut reader)
}

// A helper function to check and get the address bytes from plaintext bytes.
// A helper function to check and get the address archive from plaintext archive.
fn get_address_bytes_from_plaintext<N: Network, R: Read>(mut reader: R) -> Result<AddressBytes<N>> {
// Get the variant of the address.
let variant = u8::read_le(&mut reader)?;
Expand All @@ -132,7 +132,7 @@ fn get_address_bytes_from_plaintext<N: Network, R: Read>(mut reader: R) -> Resul
let literal_variant = u16::read_le(&mut reader).unwrap();
// Check that it is a `Address` variant.
ensure!(literal_variant == 0, "Expected a `Address` variant");
// Get the address bytes.
// Get the address archive.
let address_bytes = AddressBytes::read_le(&mut reader).unwrap();
Ok(address_bytes)
}
File renamed without changes.
73 changes: 0 additions & 73 deletions block-parser/src/json/address.rs

This file was deleted.

Empty file removed block-parser/src/json/block.rs
Empty file.
138 changes: 0 additions & 138 deletions block-parser/src/json/decoders.rs

This file was deleted.

Empty file removed block-parser/src/json/mod.rs
Empty file.
Loading

0 comments on commit f95cf8f

Please sign in to comment.