Skip to content

Commit

Permalink
Add decoding of base 64 strings
Browse files Browse the repository at this point in the history
  • Loading branch information
darunrs committed May 24, 2024
1 parent 3b4708a commit 4e44707
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
7 changes: 7 additions & 0 deletions block-streamer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions block-streamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tonic = "0.10.2"
wildmatch = "2.1.1"

registry-types = { path = "../registry/types" }
base64 = "0.22.1"

[build-dependencies]
tonic-build = "0.10"
Expand Down
32 changes: 22 additions & 10 deletions block-streamer/src/bitmap.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use anyhow::anyhow;
use base64::{engine::general_purpose, Engine as _};

// const BLOCK_HEIGHTS_IN_DAY: usize = 86000;

pub struct Base64Bitmap {
pub start_block_height: usize,
pub base64: String,
}

pub struct Bitmap {
pub start_block_height: usize,
pub bitmap: Vec<u8>,
Expand Down Expand Up @@ -187,16 +193,22 @@ impl BitmapOperator {

pub fn get_merged_bitmap(
&self,
bitmaps_to_merge: &Vec<Bitmap>,
bitmaps_to_merge: &Vec<Base64Bitmap>,
smallest_start_block_height: usize,
) -> anyhow::Result<Bitmap> {
let mut merged_bitmap: Bitmap = Bitmap {
bitmap: Vec::new(),
start_block_height: smallest_start_block_height,
};

for compressed_bitmap in bitmaps_to_merge {
self.merge_compressed_bitmap_into_base_bitmap(&mut merged_bitmap, compressed_bitmap)?;
for compressed_base64_bitmap in bitmaps_to_merge {
let decoded_bitmap: Vec<u8> =
general_purpose::STANDARD.decode(compressed_base64_bitmap.base64.clone())?;
let compressed_bitmap: Bitmap = Bitmap {
bitmap: decoded_bitmap,
start_block_height: compressed_base64_bitmap.start_block_height,
};
self.merge_compressed_bitmap_into_base_bitmap(&mut merged_bitmap, &compressed_bitmap)?;
}

Ok(merged_bitmap)
Expand Down Expand Up @@ -333,17 +345,17 @@ mod tests {
#[test]
fn test_get_merged_bitmap() {
let operator: BitmapOperator = BitmapOperator::new();
let test_bitmaps_to_merge: Vec<Bitmap> = vec![
Bitmap {
bitmap: vec![0b10100000], // Decompresses to 11000000
let test_bitmaps_to_merge: Vec<Base64Bitmap> = vec![
Base64Bitmap {
base64: "oA==".to_string(), // Decompresses to 11000000
start_block_height: 10,
},
Bitmap {
bitmap: vec![0b10100000],
Base64Bitmap {
base64: "oA==".to_string(),
start_block_height: 14,
},
Bitmap {
bitmap: vec![0b10100000],
Base64Bitmap {
base64: "oA==".to_string(),
start_block_height: 18,
},
];
Expand Down
7 changes: 7 additions & 0 deletions coordinator/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e44707

Please sign in to comment.