Skip to content

Commit

Permalink
feat: support running with blockfrost and no node
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Nov 18, 2024
1 parent 8a0e0b4 commit 34757c6
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 54 deletions.
32 changes: 27 additions & 5 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions firefly-cardanoconnect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ repository = "https://github.com/SundaeSwap-finance/firefly-cardano"
aide = { version = "0.13", features = ["axum"] }
anyhow = "1"
async-trait = "0.1"
axum = { version = "0.7", features = ["ws"] }
blockfrost = { version = "1", default-features = false, features = ["rustls-tls"] }
axum = { version = "0.7", features = ["macros", "ws"] }
blockfrost = { git = "https://github.com/blockfrost/blockfrost-rust.git", rev = "14e22b5", default-features = false, features = ["rustls-tls"] }
blockfrost-openapi = "0.1.69"
clap = { version = "4", features = ["derive"] }
chrono = "0.4"
dashmap = "6"
Expand Down
44 changes: 28 additions & 16 deletions firefly-cardanoconnect/src/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ use crate::{
};
use anyhow::{bail, Result};
use async_trait::async_trait;
use blockfrost::Blockfrost;
use mocks::MockChain;
use n2c::NodeToClient;
use pallas_primitives::conway::Tx;
use pallas_traverse::wellknown::GenesisValues;
use serde::Deserialize;
use tokio::sync::RwLock;

mod blockfrost;
pub mod mocks;
mod n2c;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockchainConfig {
pub socket: PathBuf,
pub socket: Option<PathBuf>,
pub blockfrost_key: Option<Secret<String>>,
pub network: Option<Network>,
pub network_magic: Option<u64>,
Expand Down Expand Up @@ -66,6 +68,7 @@ impl BlockchainConfig {
}

enum ClientImpl {
Blockfrost(Blockfrost),
NodeToClient(RwLock<NodeToClient>),
Mock(MockChain),
}
Expand All @@ -77,26 +80,32 @@ pub struct BlockchainClient {
}

impl BlockchainClient {
pub async fn new(config: &CardanoConnectConfig) -> Self {
pub async fn new(config: &CardanoConnectConfig) -> Result<Self> {
let blockchain = &config.connector.blockchain;

let n2c = {
let client = NodeToClient::new(
&blockchain.socket,
blockchain.magic(),
blockchain.genesis_hash(),
blockchain.genesis_values(),
blockchain.blockfrost_key.as_ref(),
)
.await;
RwLock::new(client)
let client = match (&blockchain.socket, &blockchain.blockfrost_key) {
(Some(socket), key) => {
let client = NodeToClient::new(
socket,
blockchain.magic(),
blockchain.genesis_hash(),
blockchain.genesis_values(),
key.as_ref(),
)
.await;
ClientImpl::NodeToClient(RwLock::new(client))
}
(None, Some(key)) => {
let client = Blockfrost::new(&key.0, blockchain.genesis_hash());
ClientImpl::Blockfrost(client)
}
(None, None) => bail!("Missing blockchain configuration"),
};

Self {
client: ClientImpl::NodeToClient(n2c),
Ok(Self {
client,
genesis_hash: blockchain.genesis_hash().to_string(),
era: blockchain.era,
}
})
}

#[allow(unused)]
Expand All @@ -116,6 +125,7 @@ impl BlockchainClient {

pub async fn health(&self) -> Result<()> {
match &self.client {
ClientImpl::Blockfrost(_) => Ok(()),
ClientImpl::Mock(_) => Ok(()),
ClientImpl::NodeToClient(n2c) => {
let client = n2c.read().await;
Expand All @@ -126,6 +136,7 @@ impl BlockchainClient {

pub async fn submit(&self, transaction: Tx) -> Result<String> {
match &self.client {
ClientImpl::Blockfrost(bf) => bf.submit(transaction).await,
ClientImpl::Mock(_) => bail!("mock transaction submission not implemented"),
ClientImpl::NodeToClient(n2c) => {
let mut client = n2c.write().await;
Expand All @@ -136,6 +147,7 @@ impl BlockchainClient {

pub async fn sync(&self) -> Result<ChainSyncClientWrapper> {
let inner: Box<dyn ChainSyncClient + Send + Sync> = match &self.client {
ClientImpl::Blockfrost(bf) => Box::new(bf.open_chainsync().await?),
ClientImpl::Mock(mock) => Box::new(mock.sync()),
ClientImpl::NodeToClient(n2c) => {
let client = n2c.read().await;
Expand Down
Loading

0 comments on commit 34757c6

Please sign in to comment.