Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Block Height Stream #776

Merged
merged 20 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions block-streamer/Cargo.lock

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

4 changes: 3 additions & 1 deletion block-streamer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ edition = "2021"
[dependencies]
actix-web = "4.5.1"
anyhow = "1.0.57"
async-stream = "0.3.5"
async-trait = "0.1.74"
aws-config = { version = "1.1.3", features = ["behavior-version-latest"] }
aws-sdk-s3 = "1.13.0"
base64 = "0.22.1"
borsh = "0.10.2"
cached = "0.49.3"
chrono = "0.4.25"
Expand All @@ -20,6 +22,7 @@ near-lake-framework = "0.7.8"
prometheus = "0.13.3"
prost = "0.12.3"
redis = { version = "0.21.5", features = ["tokio-comp", "connection-manager"] }
regex = "1.10.4"
reqwest = { version = "^0.11.0", features = ["json"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0.55"
Expand All @@ -33,7 +36,6 @@ 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
48 changes: 38 additions & 10 deletions block-streamer/src/bitmap.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
use anyhow::anyhow;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work on this, the separated structs makes this a lot easier to understand :)

use base64::{engine::general_purpose, Engine as _};
use std::convert::TryFrom;

use crate::graphql::client::{get_bitmaps_exact, get_bitmaps_wildcard};

#[derive(Debug, Default, PartialEq)]
pub struct Base64Bitmap {
pub start_block_height: usize,
pub base64: String,
}

impl From<&get_bitmaps_exact::GetBitmapsExactDarunrsNearBitmapV5ActionsIndex> for Base64Bitmap {
fn from(
query_item: &get_bitmaps_exact::GetBitmapsExactDarunrsNearBitmapV5ActionsIndex,
) -> Self {
Self {
base64: query_item.bitmap.clone(),
start_block_height: usize::try_from(query_item.first_block_height).unwrap(),
darunrs marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl From<&get_bitmaps_wildcard::GetBitmapsWildcardDarunrsNearBitmapV5ActionsIndex>
for Base64Bitmap
{
fn from(
query_item: &get_bitmaps_wildcard::GetBitmapsWildcardDarunrsNearBitmapV5ActionsIndex,
) -> Self {
Self {
base64: query_item.bitmap.clone(),
start_block_height: usize::try_from(query_item.first_block_height).unwrap(),
}
}
}

#[derive(Debug, Default, PartialEq)]
pub struct Bitmap {
pub start_block_height: usize,
pub bitmap: Vec<u8>,
Expand All @@ -19,7 +48,6 @@ struct EliasGammaDecoded {

pub struct BitmapOperator {}

#[cfg_attr(test, mockall::automock)]
darunrs marked this conversation as resolved.
Show resolved Hide resolved
impl BitmapOperator {
pub fn new() -> Self {
Self {}
Expand Down Expand Up @@ -199,7 +227,7 @@ mod tests {

#[test]
fn get_bit_from_bytes() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let bytes: &[u8; 3] = &[0b00000001, 0b00000000, 0b00001001];
let results: Vec<bool> = [7, 8, 9, 15, 19, 20, 22, 23]
.iter()
Expand All @@ -215,7 +243,7 @@ mod tests {

#[test]
fn set_bit_in_bytes() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let correct_bytes: &[u8; 3] = &[0b00000001, 0b00000000, 0b00001001];
let test_bytes: &mut [u8; 3] = &mut [0b10000000, 0b10000000, 0b00001001];
operator.set_bit(test_bytes, 0, false, true);
Expand All @@ -227,14 +255,14 @@ mod tests {

#[test]
fn get_unsigned_integer_from_binary_sequence() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let bytes: &[u8; 3] = &[0b11111110, 0b10010100, 0b10001101];
assert_eq!(operator.read_integer_from_binary(bytes, 6, 16), 1321);
}

#[test]
fn get_index_of_first_set_bit() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let bytes: &[u8; 4] = &[0b00000001, 0b10000000, 0b00000001, 0b00000000];
assert_eq!(
operator.index_of_first_set_bit(bytes, 4).unwrap(),
Expand Down Expand Up @@ -264,7 +292,7 @@ mod tests {

#[test]
fn decode_elias_gamma() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let bytes: &[u8; 2] = &[0b00000000, 0b00110110];
let decoded_eg: EliasGammaDecoded = operator.decode_elias_gamma_entry(bytes, 6);
assert_eq!(decoded_eg.value, 27);
Expand All @@ -273,7 +301,7 @@ mod tests {

#[test]
fn decode_empty_elias_gamma() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let bytes: &[u8; 2] = &[0b00000000, 0b00000000];
let decoded_eg: EliasGammaDecoded = operator.decode_elias_gamma_entry(bytes, 0);
assert_eq!(decoded_eg.value, 0);
Expand All @@ -282,7 +310,7 @@ mod tests {

#[test]
fn decode_compressed_bitmap() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
assert_eq!(operator.decompress_bitmap(&[0b10100000]), &[0b11000000]);
assert_eq!(operator.decompress_bitmap(&[0b00100100]), &[0b00110000]);
assert_eq!(operator.decompress_bitmap(&[0b10010000]), &[0b11110000]);
Expand Down Expand Up @@ -317,7 +345,7 @@ mod tests {

#[test]
fn merge_two_decompressed_bitmaps() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let mut base_bitmap: Bitmap = Bitmap {
bitmap: vec![0b11001010, 0b10001111],
start_block_height: 10,
Expand All @@ -335,7 +363,7 @@ mod tests {

#[test]
fn merge_multiple_bitmaps_together() {
let operator: BitmapOperator = BitmapOperator::new();
let operator = BitmapOperator::new();
let test_bitmaps_to_merge: Vec<Base64Bitmap> = vec![
Base64Bitmap {
base64: "oA==".to_string(), // Decompresses to 11000000
Expand Down
Loading
Loading