Skip to content

Commit f9e089f

Browse files
author
Ludo Galabru
committed
fix: clean expectations
1 parent 3ee98a8 commit f9e089f

File tree

7 files changed

+59
-46
lines changed

7 files changed

+59
-46
lines changed

components/chainhook-cli/src/config/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ pub mod file;
22
pub mod generator;
33

44
pub use chainhook_event_observer::indexer::IndexerConfig;
5-
use chainhook_event_observer::observer::{
6-
EventObserverConfig,
7-
};
5+
use chainhook_event_observer::observer::EventObserverConfig;
86
use chainhook_types::{BitcoinBlockSignaling, BitcoinNetwork, StacksNetwork};
97
pub use file::ConfigFile;
108
use std::collections::HashSet;
119
use std::fs::File;
1210
use std::io::{BufReader, Read};
1311
use std::path::PathBuf;
1412

15-
use crate::service::{DEFAULT_INGESTION_PORT, DEFAULT_CONTROL_PORT};
13+
use crate::service::{DEFAULT_CONTROL_PORT, DEFAULT_INGESTION_PORT};
1614

1715
const DEFAULT_MAINNET_TSV_ARCHIVE: &str =
1816
"https://archive.hiro.so/mainnet/stacks-blockchain-api/mainnet-stacks-blockchain-api-latest.gz";

components/chainhook-cli/src/service/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,12 @@ impl Service {
115115

116116
info!(
117117
self.ctx.expect_logger(),
118-
"Listening for new blockchain events on port {}", DEFAULT_INGESTION_PORT
118+
"Listening for new blockchain events on port {}", event_observer_config.ingestion_port
119119
);
120120
info!(
121121
self.ctx.expect_logger(),
122-
"Listening for chainhook predicate registrations on port {}", DEFAULT_CONTROL_PORT
122+
"Listening for chainhook predicate registrations on port {}",
123+
event_observer_config.control_port
123124
);
124125

125126
// let ordinal_index = match initialize_ordinal_index(&event_observer_config, None, &self.ctx)

components/chainhook-event-observer/Observer.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ control_port = 20446
77
ingestion_port = 20445
88
bitcoind_rpc_username = "devnet"
99
bitcoind_rpc_password = "devnet"
10-
bitcoin_node_rpc_host = "localhost"
11-
bitcoin_node_rpc_port = 18443
10+
bitcoind_rpc_host = "localhost"
11+
bitcoind_rpc_port = 18443
1212
stacks_node_rpc_host = "localhost"
1313
stacks_node_rpc_port = 20443
1414
operators = ["74738ff5-5367-5958-9aee-98fffdcd1876"]

components/chainhook-event-observer/src/chainhooks/stacks/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -548,15 +548,16 @@ pub fn serialized_event_with_decoded_clarity_value(
548548
}
549549

550550
pub fn expect_decoded_clarity_value(hex_value: &str) -> ClarityValue {
551-
let hex_value = hex_value
552-
.strip_prefix("0x")
553-
.expect("unable to decode clarity value emitted by stacks-node");
554-
let value_bytes =
555-
hex_bytes(&hex_value).expect("unable to decode clarity value emitted by stacks-node");
556-
ClarityValue::consensus_deserialize(&mut Cursor::new(&value_bytes))
551+
try_decode_clarity_value(hex_value)
557552
.expect("unable to decode clarity value emitted by stacks-node")
558553
}
559554

555+
pub fn try_decode_clarity_value(hex_value: &str) -> Option<ClarityValue> {
556+
let hex_value = hex_value.strip_prefix("0x")?;
557+
let value_bytes = hex_bytes(&hex_value).ok()?;
558+
ClarityValue::consensus_deserialize(&mut Cursor::new(&value_bytes)).ok()
559+
}
560+
560561
pub fn serialized_decoded_clarity_value(hex_value: &str, ctx: &Context) -> serde_json::Value {
561562
let hex_value = match hex_value.strip_prefix("0x") {
562563
Some(hex_value) => hex_value,

components/chainhook-event-observer/src/indexer/bitcoin/mod.rs

+33-28
Original file line numberDiff line numberDiff line change
@@ -296,34 +296,39 @@ pub fn standardize_bitcoin_block(
296296

297297
let mut inputs = vec![];
298298
let mut sats_in = 0;
299-
for input in tx.vin.drain(..) {
299+
for (index, input) in tx.vin.drain(..).enumerate() {
300300
if input.is_coinbase() {
301301
continue;
302302
}
303-
let value = input
304-
.prevout
305-
.as_ref()
306-
.expect("not provided for coinbase txs")
307-
.value
308-
.to_sat();
309-
sats_in += value;
303+
let prevout = input.prevout.as_ref().ok_or(format!(
304+
"error retrieving prevout for transaction {}, input #{} (block #{})",
305+
tx.txid, index, block.height
306+
))?;
307+
308+
let txid = input.txid.as_ref().ok_or(format!(
309+
"error retrieving txid for transaction {}, input #{} (block #{})",
310+
tx.txid, index, block.height
311+
))?;
312+
313+
let vout = input.vout.ok_or(format!(
314+
"error retrieving vout for transaction {}, input #{} (block #{})",
315+
tx.txid, index, block.height
316+
))?;
317+
318+
let script_sig = input.script_sig.ok_or(format!(
319+
"error retrieving script_sig for transaction {}, input #{} (block #{})",
320+
tx.txid, index, block.height
321+
))?;
322+
323+
sats_in += prevout.value.to_sat();
310324
inputs.push(TxIn {
311325
previous_output: OutPoint {
312-
txid: format!(
313-
"0x{}",
314-
input
315-
.txid
316-
.expect("not provided for coinbase txs")
317-
.to_string()
318-
),
319-
vout: input.vout.expect("not provided for coinbase txs"),
320-
block_height: input.prevout.expect("not provided for coinbase txs").height,
321-
value,
326+
txid: format!("0x{}", txid.to_string()),
327+
vout,
328+
block_height: prevout.height,
329+
value: prevout.value.to_sat(),
322330
},
323-
script_sig: format!(
324-
"0x{}",
325-
hex::encode(&input.script_sig.expect("not provided for coinbase txs").hex)
326-
),
331+
script_sig: format!("0x{}", hex::encode(&script_sig.hex)),
327332
sequence: input.sequence,
328333
witness: input
329334
.txinwitness
@@ -578,15 +583,15 @@ fn try_parse_stacks_operation(
578583
// }
579584
// }
580585

581-
let pox_cycle_id = pox_config.get_pox_cycle_id(block_height);
582-
let pox_cycle_len = pox_config.get_pox_cycle_len();
583-
let pox_cycle_pos = pox_config.get_pos_in_pox_cycle(block_height);
586+
let pox_cycle_index = pox_config.get_pox_cycle_id(block_height);
587+
let pox_cycle_length = pox_config.get_pox_cycle_len();
588+
let pox_cycle_position = pox_config.get_pos_in_pox_cycle(block_height);
584589

585590
StacksBaseChainOperation::BlockCommitted(StacksBlockCommitmentData {
586591
block_hash: res.stacks_block_hash,
587-
pox_cycle_id,
588-
pox_cycle_len,
589-
pox_cycle_pos,
592+
pox_cycle_index,
593+
pox_cycle_length,
594+
pox_cycle_position,
590595
pox_sats_burnt,
591596
pox_sats_transferred,
592597
// mining_address_pre_commit: None,

components/chainhook-event-observer/src/observer/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,15 @@ pub async fn start_observer_commands_handler(
544544
}
545545
ObserverCommand::ProcessBitcoinBlock(block_data) => {
546546
let new_block =
547-
standardize_bitcoin_block(block_data, &config.bitcoin_network, &ctx)?;
547+
match standardize_bitcoin_block(block_data, &config.bitcoin_network, &ctx) {
548+
Ok(block) => block,
549+
Err(e) => {
550+
ctx.try_log(|logger| {
551+
slog::error!(logger, "Error standardizing block: {}", e)
552+
});
553+
continue;
554+
}
555+
};
548556
bitcoin_block_store.insert(new_block.block_identifier.clone(), new_block);
549557
}
550558
ObserverCommand::CacheBitcoinBlock(block) => {

components/chainhook-types-rs/src/rosetta.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,9 @@ pub enum StacksBaseChainOperation {
321321
#[serde(rename_all = "snake_case")]
322322
pub struct StacksBlockCommitmentData {
323323
pub block_hash: String,
324-
pub pox_cycle_id: u64,
325-
pub pox_cycle_len: u64,
326-
pub pox_cycle_pos: u64,
324+
pub pox_cycle_index: u64,
325+
pub pox_cycle_length: u64,
326+
pub pox_cycle_position: u64,
327327
pub pox_sats_burnt: u64,
328328
pub pox_sats_transferred: Vec<PoxReward>,
329329
// pub mining_address_pre_commit: Option<String>,

0 commit comments

Comments
 (0)