Skip to content

Commit

Permalink
feat: remove request_block call (not supported in n2c)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Gellis <simongellis@gmail.com>
  • Loading branch information
SupernaviX committed Feb 14, 2025
1 parent 4e38327 commit 0226555
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 177 deletions.
17 changes: 4 additions & 13 deletions firefly-cardanoconnect/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,10 @@ impl BlockchainClient {
let blockchain = &config.connector.blockchain;

let client = match (&blockchain.socket, blockfrost) {
(Some(socket), blockfrost) => {
let client = NodeToClient::new(
socket,
blockchain.magic(),
blockchain.genesis_hash(),
blockchain.genesis_values(),
blockfrost,
)
.await;
(Some(socket), _) => {
let client =
NodeToClient::new(socket, blockchain.magic(), blockchain.genesis_values())
.await;
ClientImpl::NodeToClient(RwLock::new(client))
}
(None, Some(blockfrost)) => {
Expand Down Expand Up @@ -169,7 +164,6 @@ pub trait ChainSyncClient {
&mut self,
points: &[BlockReference],
) -> Result<(Option<BlockReference>, BlockReference)>;
async fn request_block(&mut self, block_ref: &BlockReference) -> Result<Option<BlockInfo>>;
}

pub struct ChainSyncClientWrapper {
Expand All @@ -187,9 +181,6 @@ impl ChainSyncClient for ChainSyncClientWrapper {
) -> Result<(Option<BlockReference>, BlockReference)> {
self.inner.find_intersect(points).await
}
async fn request_block(&mut self, block_ref: &BlockReference) -> Result<Option<BlockInfo>> {
self.inner.request_block(block_ref).await
}
}

pub enum RequestNextResponse {
Expand Down
24 changes: 0 additions & 24 deletions firefly-cardanoconnect/src/blockchain/blockfrost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,6 @@ impl ChainSyncClient for BlockfrostChainSync {
};
Ok((None, parse_reference(&self.tip)))
}

async fn request_block(&mut self, block_ref: &BlockReference) -> Result<Option<BlockInfo>> {
let (requested_slot, requested_hash) = match block_ref {
BlockReference::Origin => (None, &self.genesis_hash),
BlockReference::Point(number, hash) => (*number, hash),
};
request_block(&self.client, requested_hash, requested_slot).await
}
}

impl BlockfrostChainSync {
Expand Down Expand Up @@ -237,22 +229,6 @@ impl Point {
}
}

pub async fn request_block(
client: &BlockfrostClient,
hash: &str,
slot: Option<u64>,
) -> Result<Option<BlockInfo>> {
let Some(block) = client.try_blocks_by_id(hash).await? else {
return Ok(None);
};

if slot.is_some_and(|s| block.slot.is_some_and(|b| b as u64 != s)) {
bail!("requested_block returned a block in the wrong slot");
}

Ok(Some(parse_block(client, block).await?))
}

fn parse_point(block: &BlockContent) -> Point {
Point {
slot: block.slot.map(|s| s as u64),
Expand Down
20 changes: 0 additions & 20 deletions firefly-cardanoconnect/src/blockchain/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ impl ChainSyncClient for MockChainSync {
let tip = chain.last().map(|b| b.as_reference()).unwrap_or_default();
Ok((intersect.map(|i| i.as_reference()), tip))
}

async fn request_block(&mut self, block_ref: &BlockReference) -> Result<Option<BlockInfo>> {
self.chain.request_block(block_ref).await
}
}

// Mock implementation of something which can query the chain
Expand Down Expand Up @@ -134,22 +130,6 @@ impl MockChain {
Some(final_rollback_target)
}

// this is a simpler version of request_range from the block fetch protocol.
pub async fn request_block(&self, block_ref: &BlockReference) -> Result<Option<BlockInfo>> {
match block_ref {
BlockReference::Origin => Ok(None),
BlockReference::Point(slot, hash) => {
let chain = self.chain.read().await;
Ok(chain
.iter()
.rev()
.find(|b| b.block_hash == *hash)
.filter(|b| b.block_slot == *slot)
.cloned())
}
}
}

// TODO: roll back sometimes
async fn generate(
chain: Arc<RwLock<Vec<BlockInfo>>>,
Expand Down
28 changes: 1 addition & 27 deletions firefly-cardanoconnect/src/blockchain/n2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use tokio::time;
use tracing::warn;

use crate::{
blockfrost::BlockfrostClient,
streams::{BlockInfo, BlockReference},
utils::LazyInit,
};
Expand All @@ -30,27 +29,17 @@ use super::{ChainSyncClient, RequestNextResponse};
pub struct NodeToClient {
socket: PathBuf,
magic: u64,
genesis_hash: String,
genesis_values: GenesisValues,
blockfrost: Option<BlockfrostClient>,
client: LazyInit<NodeClient>,
}

impl NodeToClient {
pub async fn new(
socket: &Path,
magic: u64,
genesis_hash: &str,
genesis_values: GenesisValues,
blockfrost: Option<BlockfrostClient>,
) -> Self {
pub async fn new(socket: &Path, magic: u64, genesis_values: GenesisValues) -> Self {
let client = Self::connect(socket, magic);
let mut result = Self {
socket: socket.to_path_buf(),
magic,
genesis_hash: genesis_hash.to_string(),
genesis_values,
blockfrost,
client,
};
if let Err(error) = result.get_client().await {
Expand Down Expand Up @@ -93,16 +82,10 @@ impl NodeToClient {
}

pub fn open_chainsync(&self) -> Result<N2cChainSync> {
let Some(blockfrost) = self.blockfrost.clone() else {
bail!("Cannot use node-to-client without a blockfrost key")
};
let client = Self::connect(&self.socket, self.magic);
let genesis_hash = self.genesis_hash.clone();
let genesis_values = self.genesis_values.clone();
Ok(N2cChainSync {
client,
blockfrost,
genesis_hash,
genesis_values,
})
}
Expand Down Expand Up @@ -137,8 +120,6 @@ impl NodeToClient {

pub struct N2cChainSync {
client: LazyInit<NodeClient>,
blockfrost: BlockfrostClient,
genesis_hash: String,
genesis_values: GenesisValues,
}

Expand Down Expand Up @@ -188,13 +169,6 @@ impl ChainSyncClient for N2cChainSync {

Ok((intersect, tip))
}
async fn request_block(&mut self, block_ref: &BlockReference) -> Result<Option<BlockInfo>> {
let (requested_slot, requested_hash) = match block_ref {
BlockReference::Origin => (None, &self.genesis_hash),
BlockReference::Point(number, hash) => (*number, hash),
};
super::blockfrost::request_block(&self.blockfrost, requested_hash, requested_slot).await
}
}

impl N2cChainSync {
Expand Down
11 changes: 4 additions & 7 deletions firefly-cardanoconnect/src/routes/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use firefly_server::apitypes::{ApiError, ApiResult};
use schemars::JsonSchema;
use serde::Serialize;

use crate::{blockchain::ChainSyncClient, AppState};
use crate::{blockchain::ChainSyncClient, streams::BlockReference, AppState};

#[derive(Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ChainTip {
pub block_height: Option<u64>,
pub block_slot: Option<u64>,
pub block_hash: String,
}
Expand All @@ -17,13 +16,11 @@ pub async fn get_chain_tip(
State(AppState { blockchain, .. }): State<AppState>,
) -> ApiResult<Json<ChainTip>> {
let mut sync = blockchain.sync().await?;
let (_, tip) = sync.find_intersect(&[]).await?;
let Some(block) = sync.request_block(&tip).await? else {
let (_, BlockReference::Point(slot, hash)) = sync.find_intersect(&[]).await? else {
return Err(ApiError::not_found("tip of chain not found"));
};
Ok(Json(ChainTip {
block_height: block.block_height,
block_slot: block.block_slot,
block_hash: block.block_hash,
block_slot: slot,
block_hash: hash,
}))
}
Loading

0 comments on commit 0226555

Please sign in to comment.