Skip to content

Commit

Permalink
feat: chainhook-sdk config niteties
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Jun 12, 2023
1 parent c7672f9 commit 7d9e179
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 11 deletions.
8 changes: 8 additions & 0 deletions components/chainhook-sdk/src/lib.rs
Expand Up @@ -12,6 +12,7 @@ extern crate serde_json;
pub extern crate bitcoincore_rpc;
pub extern crate dashmap;
pub extern crate fxhash;
pub extern crate stacks_rpc_client;

pub use chainhook_types;

Expand All @@ -25,3 +26,10 @@ pub extern crate rocksdb;

#[cfg(feature = "ordinals")]
pub mod hord;

// TODO
// pub mod clarity {
// pub use stacks_rpc_client::clarity::stacks_common::*;
// pub use stacks_rpc_client::clarity::vm::*;
// pub use stacks_rpc_client::clarity::*;
// }
77 changes: 72 additions & 5 deletions components/chainhook-sdk/src/observer/mod.rs
Expand Up @@ -18,9 +18,7 @@ use crate::hord::{
revert_hord_db_with_augmented_bitcoin_block, update_hord_db_and_augment_bitcoin_block,
HordConfig,
};
use crate::indexer::bitcoin::{
standardize_bitcoin_block, BitcoinBlockFullBreakdown,
};
use crate::indexer::bitcoin::{standardize_bitcoin_block, BitcoinBlockFullBreakdown};
use crate::indexer::{Indexer, IndexerConfig};
use crate::utils::{send_request, Context};

Expand Down Expand Up @@ -68,7 +66,7 @@ pub enum Event {
}

// TODO(lgalabru): Support for GRPC?
#[derive(Clone, Debug)]
#[derive(Deserialize, Debug, Clone)]
pub enum EventHandler {
WebHook(String),
}
Expand Down Expand Up @@ -120,7 +118,7 @@ impl EventHandler {
async fn notify_bitcoin_transaction_proxied(&self) {}
}

#[derive(Clone, Debug)]
#[derive(Debug, Clone)]
pub struct EventObserverConfig {
pub chainhook_config: Option<ChainhookConfig>,
pub bitcoin_rpc_proxy_enabled: bool,
Expand All @@ -139,6 +137,20 @@ pub struct EventObserverConfig {
pub hord_config: Option<HordConfig>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct EventObserverConfigOverrides {
pub ingestion_port: Option<u16>,
pub bitcoind_rpc_username: Option<String>,
pub bitcoind_rpc_password: Option<String>,
pub bitcoind_rpc_url: Option<String>,
pub bitcoind_zmq_url: Option<String>,
pub stacks_node_rpc_url: Option<String>,
pub display_logs: Option<bool>,
pub cache_path: Option<String>,
pub bitcoin_network: Option<String>,
pub stacks_network: Option<String>,
}

impl EventObserverConfig {
pub fn get_cache_path_buf(&self) -> PathBuf {
let mut path_buf = PathBuf::new();
Expand All @@ -156,6 +168,61 @@ impl EventObserverConfig {
};
bitcoin_config
}

pub fn new_using_overrides(
overrides: Option<&EventObserverConfigOverrides>,
) -> Result<EventObserverConfig, String> {
let stacks_node_rpc_url = overrides
.and_then(|c| c.stacks_node_rpc_url.clone())
.unwrap_or("http://localhost:20443".to_string());

let bitcoin_network =
if let Some(network) = overrides.and_then(|c| c.bitcoin_network.as_ref()) {
BitcoinNetwork::from_str(network)?
} else {
BitcoinNetwork::Regtest
};

let stacks_network =
if let Some(network) = overrides.and_then(|c| c.stacks_network.as_ref()) {
StacksNetwork::from_str(network)?
} else {
StacksNetwork::Devnet
};

let config = EventObserverConfig {
bitcoin_rpc_proxy_enabled: false,
event_handlers: vec![],
chainhook_config: None,
ingestion_port: overrides
.and_then(|c| c.ingestion_port)
.unwrap_or(DEFAULT_INGESTION_PORT),
bitcoind_rpc_username: overrides
.and_then(|c| c.bitcoind_rpc_username.clone())
.unwrap_or("devnet".to_string()),
bitcoind_rpc_password: overrides
.and_then(|c| c.bitcoind_rpc_password.clone())
.unwrap_or("devnet".to_string()),
bitcoind_rpc_url: overrides
.and_then(|c| c.bitcoind_rpc_url.clone())
.unwrap_or("http://localhost:18443".to_string()),
bitcoin_block_signaling: overrides
.and_then(|c| match c.bitcoind_zmq_url.as_ref() {
Some(url) => Some(BitcoinBlockSignaling::ZeroMQ(url.clone())),
None => Some(BitcoinBlockSignaling::Stacks(stacks_node_rpc_url.clone())),
})
.unwrap_or(BitcoinBlockSignaling::Stacks(stacks_node_rpc_url.clone())),
stacks_node_rpc_url,
display_logs: overrides.and_then(|c| c.display_logs).unwrap_or(false),
cache_path: overrides
.and_then(|c| c.cache_path.clone())
.unwrap_or("cache".to_string()),
bitcoin_network,
stacks_network,
hord_config: None,
};
Ok(config)
}
}

#[derive(Deserialize, Debug)]
Expand Down
56 changes: 50 additions & 6 deletions components/chainhook-types-rs/src/rosetta.rs
Expand Up @@ -701,12 +701,24 @@ impl StacksChainEvent {
}
}

pub fn new_block(&self) -> Option<&StacksBlockData> {
pub fn get_latest_block_identifier(&self) -> Option<&BlockIdentifier> {
match self {
StacksChainEvent::ChainUpdatedWithBlocks(event) => {
event.new_blocks.first().and_then(|b| Some(&b.block))
}
_ => None,
StacksChainEvent::ChainUpdatedWithBlocks(event) => event
.new_blocks
.last()
.and_then(|b| Some(&b.block.block_identifier)),
StacksChainEvent::ChainUpdatedWithReorg(event) => event
.blocks_to_apply
.last()
.and_then(|b| Some(&b.block.block_identifier)),
StacksChainEvent::ChainUpdatedWithMicroblocks(event) => event
.new_microblocks
.first()
.and_then(|b| Some(&b.metadata.anchor_block_identifier)),
StacksChainEvent::ChainUpdatedWithMicroblocksReorg(event) => event
.microblocks_to_apply
.first()
.and_then(|b| Some(&b.metadata.anchor_block_identifier)),
}
}
}
Expand Down Expand Up @@ -764,6 +776,21 @@ pub enum StacksNetwork {
}

impl StacksNetwork {
pub fn from_str(network: &str) -> Result<StacksNetwork, String> {
let value = match network {
"devnet" => StacksNetwork::Devnet,
"testnet" => StacksNetwork::Testnet,
"mainnet" => StacksNetwork::Mainnet,
_ => {
return Err(format!(
"network '{}' unsupported (mainnet, testnet, devnet, simnet)",
network
))
}
};
Ok(value)
}

pub fn is_simnet(&self) -> bool {
match self {
StacksNetwork::Simnet => true,
Expand Down Expand Up @@ -827,7 +854,24 @@ pub enum BitcoinNetwork {
Mainnet,
}

#[derive(Debug, Clone)]
impl BitcoinNetwork {
pub fn from_str(network: &str) -> Result<BitcoinNetwork, String> {
let value = match network {
"regtest" => BitcoinNetwork::Regtest,
"testnet" => BitcoinNetwork::Testnet,
"mainnet" => BitcoinNetwork::Mainnet,
_ => {
return Err(format!(
"network '{}' unsupported (mainnet, testnet, regtest)",
network
))
}
};
Ok(value)
}
}

#[derive(Deserialize, Debug, Clone)]
pub enum BitcoinBlockSignaling {
Stacks(String),
ZeroMQ(String),
Expand Down

0 comments on commit 7d9e179

Please sign in to comment.