Skip to content

Commit

Permalink
feat(overlay-client): impl BlockProvider for BlockchainClient
Browse files Browse the repository at this point in the history
  • Loading branch information
pashinov committed Apr 20, 2024
1 parent 254cb96 commit 0b6ddbe
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
66 changes: 63 additions & 3 deletions core/src/blockchain_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::sync::Arc;

use anyhow::Result;
use everscale_types::models::BlockId;
use futures_util::future::BoxFuture;
use tycho_block_util::block::{BlockStuff, BlockStuffAug};

use crate::overlay_client::public_overlay_client::{
OverlayClient, PublicOverlayClient, QueryResponse,
};
use crate::block_strider::provider::*;
use crate::overlay_client::public_overlay_client::*;
use crate::proto::overlay::rpc::*;
use crate::proto::overlay::*;

Expand Down Expand Up @@ -96,3 +97,62 @@ impl BlockchainClient {
Ok(data)
}
}

impl BlockProvider for BlockchainClient {
type GetNextBlockFut<'a> = BoxFuture<'a, OptionalBlockStuff>;
type GetBlockFut<'a> = BoxFuture<'a, OptionalBlockStuff>;

fn get_next_block<'a>(&'a self, prev_block_id: &'a BlockId) -> Self::GetNextBlockFut<'a> {
Box::pin(async {
let get_block = || async {
let res = self.get_next_block_full(*prev_block_id).await?;
let block = match res.data() {
BlockFull::Found {
block_id,
block: data,
..
} => {
let block = BlockStuff::deserialize_checked(*block_id, data)?;
Some(BlockStuffAug::new(block, data.to_vec()))
}
BlockFull::Empty => None,
};

Ok::<_, anyhow::Error>(block)
};

match get_block().await {
Ok(Some(block)) => Some(Ok(block)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
})
}

fn get_block<'a>(&'a self, block_id: &'a BlockId) -> Self::GetBlockFut<'a> {
Box::pin(async {
let get_block = || async {
let res = self.get_block_full(*block_id).await?;
let block = match res.data() {
BlockFull::Found {
block_id,
block: data,
..
} => {
let block = BlockStuff::deserialize_checked(*block_id, data)?;
Some(BlockStuffAug::new(block, data.to_vec()))
}
BlockFull::Empty => None,
};

Ok::<_, anyhow::Error>(block)
};

match get_block().await {
Ok(Some(block)) => Some(Ok(block)),
Ok(None) => None,
Err(e) => Some(Err(e)),
}
})
}
}
7 changes: 7 additions & 0 deletions core/tests/overlay_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use everscale_types::models::BlockId;
use futures_util::stream::FuturesUnordered;
use futures_util::StreamExt;
use tl_proto::{TlRead, TlWrite};
use tycho_core::block_strider::provider::BlockProvider;
use tycho_core::blockchain_client::BlockchainClient;
use tycho_core::overlay_client::public_overlay_client::PublicOverlayClient;
use tycho_core::overlay_client::settings::OverlayClientSettings;
Expand Down Expand Up @@ -354,6 +355,12 @@ async fn overlay_server_with_empty_storage() -> Result<()> {
);
}

let block = client.get_block(&BlockId::default()).await;
assert!(block.is_none());

let block = client.get_next_block(&BlockId::default()).await;
assert!(block.is_none());

break;
}

Expand Down

0 comments on commit 0b6ddbe

Please sign in to comment.