From 4e44707ef0efc2578c715be5c60facc414237449 Mon Sep 17 00:00:00 2001 From: Darun Seethammagari Date: Fri, 24 May 2024 12:47:44 -0700 Subject: [PATCH] Add decoding of base 64 strings --- block-streamer/Cargo.lock | 7 +++++++ block-streamer/Cargo.toml | 1 + block-streamer/src/bitmap.rs | 32 ++++++++++++++++++++++---------- coordinator/Cargo.lock | 7 +++++++ 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/block-streamer/Cargo.lock b/block-streamer/Cargo.lock index e0e82e1ee..1a68afaa5 100644 --- a/block-streamer/Cargo.lock +++ b/block-streamer/Cargo.lock @@ -904,6 +904,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "base64-simd" version = "0.8.0" @@ -963,6 +969,7 @@ dependencies = [ "aws-sdk-s3", "aws-smithy-runtime", "aws-smithy-types", + "base64 0.22.1", "borsh 0.10.3", "cached", "chrono", diff --git a/block-streamer/Cargo.toml b/block-streamer/Cargo.toml index c8bc5f499..d44903786 100644 --- a/block-streamer/Cargo.toml +++ b/block-streamer/Cargo.toml @@ -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" diff --git a/block-streamer/src/bitmap.rs b/block-streamer/src/bitmap.rs index ff9779afc..36354dc11 100644 --- a/block-streamer/src/bitmap.rs +++ b/block-streamer/src/bitmap.rs @@ -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, @@ -187,7 +193,7 @@ impl BitmapOperator { pub fn get_merged_bitmap( &self, - bitmaps_to_merge: &Vec, + bitmaps_to_merge: &Vec, smallest_start_block_height: usize, ) -> anyhow::Result { let mut merged_bitmap: Bitmap = Bitmap { @@ -195,8 +201,14 @@ impl BitmapOperator { 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 = + 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) @@ -333,17 +345,17 @@ mod tests { #[test] fn test_get_merged_bitmap() { let operator: BitmapOperator = BitmapOperator::new(); - let test_bitmaps_to_merge: Vec = vec![ - Bitmap { - bitmap: vec![0b10100000], // Decompresses to 11000000 + let test_bitmaps_to_merge: Vec = 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, }, ]; diff --git a/coordinator/Cargo.lock b/coordinator/Cargo.lock index 7e5f2258e..032dae9cf 100644 --- a/coordinator/Cargo.lock +++ b/coordinator/Cargo.lock @@ -866,6 +866,12 @@ version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "base64-simd" version = "0.8.0" @@ -923,6 +929,7 @@ dependencies = [ "async-trait", "aws-config", "aws-sdk-s3", + "base64 0.22.1", "borsh 0.10.3", "cached", "chrono",