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: improve chainhook-sdk interface #608

Merged
merged 29 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
694fb4d
chore: remove or rename event observer config fields
MicaiahReid Apr 24, 2024
0947acc
add prometheus monitoring port to config overrides
MicaiahReid Jun 20, 2024
4e997fc
rename ingestion_port in config overrides
MicaiahReid Jun 20, 2024
3f6ef10
create `default` fn for `EventObserverConfig`
MicaiahReid Jun 20, 2024
3b0f7a3
create `EventObserverConfig` helper to register a predicate
MicaiahReid Jun 20, 2024
fc67dff
feat: add `BitcoinEventObserverBuilder` struct
MicaiahReid Jun 20, 2024
6d6f9fe
add impl fns to `EventObserverConfigOverrides` to allow using as builder
MicaiahReid Jun 20, 2024
9da1178
rename EventObserverConfigOverrides to EventObserverConfigBuilder
MicaiahReid Jun 20, 2024
bc7c735
rsdoc comments
MicaiahReid Jun 20, 2024
5a4cb39
chore: rename structs
MicaiahReid Jun 20, 2024
f992880
fix tests
MicaiahReid Jun 20, 2024
39986d5
fix openapi.json
MicaiahReid Jun 21, 2024
473a8da
fix doc comments
MicaiahReid Jun 21, 2024
83e8336
move stacks/bitcoin specific chainhook types
MicaiahReid Jun 21, 2024
4debc28
add helpers for registering chainhooks
MicaiahReid Jun 24, 2024
c54b6e7
rename ChainhookConfig -> ChainhookStore
MicaiahReid Jun 24, 2024
08c15ba
rs doc comment
MicaiahReid Jun 24, 2024
cee7be6
fix helpers to return self
MicaiahReid Jun 24, 2024
dddc69a
rename `BitcoinEventObserverBuilder` -> `BitcoinEventObserverConfigBu…
MicaiahReid Jun 24, 2024
fe04dd9
add builder for starting an event observer
MicaiahReid Jun 24, 2024
1d1b1f2
rs doc for ChainhookSpecificationNetworkMap
MicaiahReid Jun 24, 2024
8731e3c
fix rsdoc
MicaiahReid Jun 24, 2024
7e99a3c
clean up
MicaiahReid Jun 24, 2024
ea66e72
update openapi spec
MicaiahReid Jun 24, 2024
2812b21
`uwrap_or_else` instead of `unwrap_or`
MicaiahReid Jun 24, 2024
1d5106f
remove dead code
MicaiahReid Jun 27, 2024
f0eb836
Merge branch 'main' into 'feat/improve-sdk-interface'
MicaiahReid Jun 27, 2024
99e745e
Merge branch 'main' into feat/improve-sdk-interface
MicaiahReid Jun 27, 2024
59c8e1a
apply suggestion from review
MicaiahReid Jun 27, 2024
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
79 changes: 43 additions & 36 deletions components/chainhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ use crate::storage::{
is_stacks_block_present, open_readonly_stacks_db_conn, open_readwrite_stacks_db_conn,
set_last_confirmed_insert_key,
};

use chainhook_sdk::chainhooks::types::{
BitcoinChainhookFullSpecification, BitcoinChainhookNetworkSpecification, BitcoinPredicateType,
ChainhookFullSpecification, FileHook, HookAction, InscriptionFeedData, OrdinalOperations,
StacksChainhookFullSpecification, StacksChainhookNetworkSpecification, StacksPredicate,
StacksPrintEventBasedPredicate,
};
use chainhook_sdk::chainhooks::bitcoin::BitcoinChainhookSpecification;
use chainhook_sdk::chainhooks::bitcoin::BitcoinChainhookSpecificationNetworkMap;
use chainhook_sdk::chainhooks::bitcoin::BitcoinPredicateType;
use chainhook_sdk::chainhooks::bitcoin::InscriptionFeedData;
use chainhook_sdk::chainhooks::bitcoin::OrdinalOperations;
use chainhook_sdk::chainhooks::stacks::StacksChainhookSpecification;
use chainhook_sdk::chainhooks::stacks::StacksChainhookSpecificationNetworkMap;
use chainhook_sdk::chainhooks::stacks::StacksPredicate;
use chainhook_sdk::chainhooks::stacks::StacksPrintEventBasedPredicate;
use chainhook_sdk::chainhooks::types::{ChainhookSpecificationNetworkMap, FileHook, HookAction};
use chainhook_sdk::types::{BitcoinNetwork, BlockIdentifier, StacksNetwork};
use chainhook_sdk::utils::{BlockHeights, Context};
use clap::{Parser, Subcommand};
Expand Down Expand Up @@ -351,7 +354,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
.predicates_paths
.iter()
.map(|p| load_predicate_from_path(p))
.collect::<Result<Vec<ChainhookFullSpecification>, _>>()?;
.collect::<Result<Vec<ChainhookSpecificationNetworkMap>, _>>()?;

info!(ctx.expect_logger(), "Starting service...",);

Expand Down Expand Up @@ -384,7 +387,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
(true, false) => {
let mut networks = BTreeMap::new();

networks.insert(StacksNetwork::Testnet, StacksChainhookNetworkSpecification {
networks.insert(StacksNetwork::Testnet, StacksChainhookSpecification {
start_block: Some(34239),
end_block: Some(50000),
blocks: None,
Expand All @@ -401,7 +404,7 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
})
});

networks.insert(StacksNetwork::Mainnet, StacksChainhookNetworkSpecification {
networks.insert(StacksNetwork::Mainnet, StacksChainhookSpecification {
start_block: Some(34239),
end_block: Some(50000),
blocks: None,
Expand All @@ -418,20 +421,22 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
})
});

ChainhookFullSpecification::Stacks(StacksChainhookFullSpecification {
uuid: id.to_string(),
owner_uuid: None,
name: "Hello world".into(),
version: 1,
networks,
})
ChainhookSpecificationNetworkMap::Stacks(
StacksChainhookSpecificationNetworkMap {
uuid: id.to_string(),
owner_uuid: None,
name: "Hello world".into(),
version: 1,
networks,
},
)
}
(false, true) => {
let mut networks = BTreeMap::new();

networks.insert(
BitcoinNetwork::Mainnet,
BitcoinChainhookNetworkSpecification {
BitcoinChainhookSpecification {
start_block: Some(767430),
end_block: Some(767430),
blocks: None,
Expand All @@ -451,13 +456,15 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
},
);

ChainhookFullSpecification::Bitcoin(BitcoinChainhookFullSpecification {
uuid: id.to_string(),
owner_uuid: None,
name: "Hello world".into(),
version: 1,
networks,
})
ChainhookSpecificationNetworkMap::Bitcoin(
BitcoinChainhookSpecificationNetworkMap {
uuid: id.to_string(),
owner_uuid: None,
name: "Hello world".into(),
version: 1,
networks,
},
)
}
_ => {
return Err("command `predicates new` should either provide the flag --stacks or --bitcoin".into());
Expand Down Expand Up @@ -500,9 +507,9 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
Config::default(false, cmd.testnet, cmd.mainnet, &cmd.config_path)?;
let predicate = load_predicate_from_path(&cmd.predicate_path)?;
match predicate {
ChainhookFullSpecification::Bitcoin(predicate) => {
ChainhookSpecificationNetworkMap::Bitcoin(predicate) => {
let predicate_spec = match predicate
.into_selected_network_specification(&config.network.bitcoin_network)
.into_specification_for_network(&config.network.bitcoin_network)
{
Ok(predicate) => predicate,
Err(e) => {
Expand All @@ -521,9 +528,9 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
)
.await?;
}
ChainhookFullSpecification::Stacks(predicate) => {
ChainhookSpecificationNetworkMap::Stacks(predicate) => {
let predicate_spec = match predicate
.into_selected_network_specification(&config.network.stacks_network)
.into_specification_from_network(&config.network.stacks_network)
{
Ok(predicate) => predicate,
Err(e) => {
Expand Down Expand Up @@ -567,13 +574,13 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
}
PredicatesCommand::Check(cmd) => {
let config = Config::default(false, cmd.testnet, cmd.mainnet, &cmd.config_path)?;
let predicate: ChainhookFullSpecification =
let predicate: ChainhookSpecificationNetworkMap =
load_predicate_from_path(&cmd.predicate_path)?;

match predicate {
ChainhookFullSpecification::Bitcoin(predicate) => {
ChainhookSpecificationNetworkMap::Bitcoin(predicate) => {
let _ = match predicate
.into_selected_network_specification(&config.network.bitcoin_network)
.into_specification_for_network(&config.network.bitcoin_network)
{
Ok(predicate) => predicate,
Err(e) => {
Expand All @@ -584,9 +591,9 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
}
};
}
ChainhookFullSpecification::Stacks(predicate) => {
ChainhookSpecificationNetworkMap::Stacks(predicate) => {
let _ = match predicate
.into_selected_network_specification(&config.network.stacks_network)
.into_specification_from_network(&config.network.stacks_network)
{
Ok(predicate) => predicate,
Err(e) => {
Expand Down Expand Up @@ -864,15 +871,15 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {

pub fn load_predicate_from_path(
predicate_path: &str,
) -> Result<ChainhookFullSpecification, String> {
) -> Result<ChainhookSpecificationNetworkMap, String> {
let file = std::fs::File::open(&predicate_path)
.map_err(|e| format!("unable to read file {}\n{:?}", predicate_path, e))?;
let mut file_reader = BufReader::new(file);
let mut file_buffer = vec![];
file_reader
.read_to_end(&mut file_buffer)
.map_err(|e| format!("unable to read file {}\n{:?}", predicate_path, e))?;
let predicate: ChainhookFullSpecification = serde_json::from_slice(&file_buffer)
let predicate: ChainhookSpecificationNetworkMap = serde_json::from_slice(&file_buffer)
.map_err(|e| format!("unable to parse json file {}\n{:?}", predicate_path, e))?;
Ok(predicate)
}
Expand Down
8 changes: 3 additions & 5 deletions components/chainhook-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod file;
pub mod generator;

use chainhook_sdk::chainhooks::types::ChainhookStore;
pub use chainhook_sdk::indexer::IndexerConfig;
use chainhook_sdk::observer::EventObserverConfig;
use chainhook_sdk::types::{
Expand Down Expand Up @@ -114,17 +115,14 @@ impl Config {
pub fn get_event_observer_config(&self) -> EventObserverConfig {
EventObserverConfig {
bitcoin_rpc_proxy_enabled: true,
chainhook_config: None,
ingestion_port: DEFAULT_INGESTION_PORT,
registered_chainhooks: ChainhookStore::new(),
bitcoind_rpc_username: self.network.bitcoind_rpc_username.clone(),
bitcoind_rpc_password: self.network.bitcoind_rpc_password.clone(),
bitcoind_rpc_url: self.network.bitcoind_rpc_url.clone(),
bitcoin_block_signaling: self.network.bitcoin_block_signaling.clone(),
display_logs: false,
cache_path: self.storage.working_dir.clone(),
display_stacks_ingestion_logs: false,
bitcoin_network: self.network.bitcoin_network.clone(),
stacks_network: self.network.stacks_network.clone(),
data_handler_tx: None,
prometheus_monitoring_port: self.monitoring.prometheus_monitoring_port,
}
}
Expand Down
6 changes: 3 additions & 3 deletions components/chainhook-cli/src/scan/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use chainhook_sdk::chainhooks::bitcoin::{
evaluate_bitcoin_chainhooks_on_chain_event, handle_bitcoin_hook_action,
BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
};
use chainhook_sdk::chainhooks::types::BitcoinChainhookSpecification;
use chainhook_sdk::chainhooks::bitcoin::BitcoinChainhookInstance;
use chainhook_sdk::indexer;
use chainhook_sdk::indexer::bitcoin::{
build_http_client, download_and_parse_block_with_retry, retrieve_block_hash_with_retry,
Expand All @@ -24,7 +24,7 @@ use chainhook_sdk::utils::{file_append, send_request, Context};
use std::collections::HashMap;

pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(
predicate_spec: &BitcoinChainhookSpecification,
predicate_spec: &BitcoinChainhookInstance,
unfinished_scan_data: Option<ScanningData>,
config: &Config,
ctx: &Context,
Expand Down Expand Up @@ -250,7 +250,7 @@ pub async fn scan_bitcoin_chainstate_via_rpc_using_predicate(

pub async fn process_block_with_predicates(
block: BitcoinBlockData,
predicates: &Vec<&BitcoinChainhookSpecification>,
predicates: &Vec<&BitcoinChainhookInstance>,
event_observer_config: &EventObserverConfig,
ctx: &Context,
) -> Result<u32, String> {
Expand Down
10 changes: 5 additions & 5 deletions components/chainhook-cli/src/scan/stacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use chainhook_sdk::{
utils::Context,
};
use chainhook_sdk::{
chainhooks::{
stacks::{handle_stacks_hook_action, StacksChainhookOccurrence, StacksTriggerChainhook},
types::StacksChainhookSpecification,
chainhooks::stacks::{
handle_stacks_hook_action, StacksChainhookInstance, StacksChainhookOccurrence,
StacksTriggerChainhook,
},
utils::{file_append, send_request, AbstractStacksBlock},
};
Expand Down Expand Up @@ -166,7 +166,7 @@ pub async fn get_canonical_fork_from_tsv(
}

pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
predicate_spec: &StacksChainhookSpecification,
predicate_spec: &StacksChainhookInstance,
unfinished_scan_data: Option<ScanningData>,
stacks_db_conn: &DB,
config: &Config,
Expand Down Expand Up @@ -418,7 +418,7 @@ pub async fn scan_stacks_chainstate_via_rocksdb_using_predicate(
}

pub async fn scan_stacks_chainstate_via_csv_using_predicate(
predicate_spec: &StacksChainhookSpecification,
predicate_spec: &StacksChainhookInstance,
config: &mut Config,
ctx: &Context,
) -> Result<BlockIdentifier, String> {
Expand Down
24 changes: 12 additions & 12 deletions components/chainhook-cli/src/service/http_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
};

use chainhook_sdk::{
chainhooks::types::{ChainhookFullSpecification, ChainhookSpecification},
chainhooks::types::{ChainhookSpecificationNetworkMap, ChainhookInstance},
observer::ObserverCommand,
utils::Context,
};
Expand Down Expand Up @@ -120,7 +120,7 @@ fn handle_get_predicates(
#[openapi(tag = "Managing Predicates")]
#[post("/v1/chainhooks", format = "application/json", data = "<predicate>")]
fn handle_create_predicate(
predicate: Result<Json<ChainhookFullSpecification>, rocket::serde::json::Error>,
predicate: Result<Json<ChainhookSpecificationNetworkMap>, rocket::serde::json::Error>,
api_config: &State<PredicatesApiConfig>,
background_job_tx: &State<Arc<Mutex<Sender<ObserverCommand>>>>,
ctx: &State<Context>,
Expand Down Expand Up @@ -149,7 +149,7 @@ fn handle_create_predicate(

if let Ok(mut predicates_db_conn) = open_readwrite_predicates_db_conn(api_config) {
match get_entry_from_predicates_db(
&ChainhookSpecification::either_stx_or_btc_key(&predicate_uuid),
&ChainhookInstance::either_stx_or_btc_key(&predicate_uuid),
&mut predicates_db_conn,
&ctx,
) {
Expand Down Expand Up @@ -195,7 +195,7 @@ fn handle_get_predicate(
match open_readwrite_predicates_db_conn(api_config) {
Ok(mut predicates_db_conn) => {
let (predicate, status) = match get_entry_from_predicates_db(
&ChainhookSpecification::either_stx_or_btc_key(&predicate_uuid),
&ChainhookInstance::either_stx_or_btc_key(&predicate_uuid),
&mut predicates_db_conn,
&ctx,
) {
Expand Down Expand Up @@ -281,7 +281,7 @@ pub fn get_entry_from_predicates_db(
predicate_key: &str,
predicate_db_conn: &mut Connection,
_ctx: &Context,
) -> Result<Option<(ChainhookSpecification, PredicateStatus)>, String> {
) -> Result<Option<(ChainhookInstance, PredicateStatus)>, String> {
let entry: HashMap<String, String> = predicate_db_conn.hgetall(predicate_key).map_err(|e| {
format!(
"unable to load chainhook associated with key {}: {}",
Expand All @@ -295,7 +295,7 @@ pub fn get_entry_from_predicates_db(
Some(payload) => payload,
};

let spec = ChainhookSpecification::deserialize_specification(&encoded_spec)?;
let spec = ChainhookInstance::deserialize_specification(&encoded_spec)?;

let encoded_status = match entry.get("status") {
None => Err(format!(
Expand All @@ -313,9 +313,9 @@ pub fn get_entry_from_predicates_db(
pub fn get_entries_from_predicates_db(
predicate_db_conn: &mut Connection,
ctx: &Context,
) -> Result<Vec<(ChainhookSpecification, PredicateStatus)>, String> {
) -> Result<Vec<(ChainhookInstance, PredicateStatus)>, String> {
let chainhooks_to_load: Vec<String> = predicate_db_conn
.scan_match(ChainhookSpecification::either_stx_or_btc_key("*"))
.scan_match(ChainhookInstance::either_stx_or_btc_key("*"))
.map_err(|e| format!("unable to connect to redis: {}", e.to_string()))?
.into_iter()
.collect();
Expand Down Expand Up @@ -349,7 +349,7 @@ pub fn get_entries_from_predicates_db(
pub fn load_predicates_from_redis(
config: &crate::config::Config,
ctx: &Context,
) -> Result<Vec<(ChainhookSpecification, PredicateStatus)>, String> {
) -> Result<Vec<(ChainhookInstance, PredicateStatus)>, String> {
let redis_uri: &str = config.expected_api_database_uri();
let client = redis::Client::open(redis_uri)
.map_err(|e| format!("unable to connect to redis: {}", e.to_string()))?;
Expand Down Expand Up @@ -378,19 +378,19 @@ pub fn get_routes_spec() -> (Vec<rocket::Route>, OpenApi) {
}

fn serialized_predicate_with_status(
predicate: &ChainhookSpecification,
predicate: &ChainhookInstance,
status: &PredicateStatus,
) -> JsonValue {
match (predicate, status) {
(ChainhookSpecification::Stacks(spec), status) => json!({
(ChainhookInstance::Stacks(spec), status) => json!({
"chain": "stacks",
"uuid": spec.uuid,
"network": spec.network,
"predicate": spec.predicate,
"status": status,
"enabled": spec.enabled,
}),
(ChainhookSpecification::Bitcoin(spec), status) => json!({
(ChainhookInstance::Bitcoin(spec), status) => json!({
"chain": "bitcoin",
"uuid": spec.uuid,
"network": spec.network,
Expand Down
Loading
Loading