Skip to content

Commit e8e1912

Browse files
committed
refactor: block standardization
1 parent bd1e9c3 commit e8e1912

File tree

13 files changed

+1245
-939
lines changed

13 files changed

+1245
-939
lines changed

Cargo.lock

Lines changed: 93 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ tracing-subscriber = "0.2"
6868
tracing-appender = "0.1"
6969
ctrlc = "3.1.9"
7070
strum = { version = "0.22", features = ["derive"] }
71+
bitcoincore-rpc = "0.14.0"
7172

7273
[dependencies.tui]
7374
version = "0.15.0"

node-bindings/lib/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,16 @@ export interface StacksBlockMetadata {
179179
bitcoin_anchor_block_identifier: BlockIdentifier;
180180
/**
181181
*
182-
* @type {BlockIdentifier}
182+
* @type {number}
183183
* @memberof StacksBlockMetadata
184184
*/
185-
bitcoin_genesis_block_identifier: BlockIdentifier;
185+
pox_cycle_index: number;
186186
/**
187187
*
188188
* @type {number}
189189
* @memberof StacksBlockMetadata
190190
*/
191-
pox_cycle_index: number;
191+
pox_cycle_position: number;
192192
/**
193193
*
194194
* @type {number}

src/bin.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod macros;
1818
mod frontend;
1919
mod generate;
2020
mod integrate;
21+
mod indexer;
2122
mod poke;
2223
mod publish;
2324
mod runnner;

src/indexer/chains/bitcoin.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use bitcoincore_rpc::{Auth, Client, RpcApi};
2+
use bitcoincore_rpc::bitcoin::hashes::Hash;
3+
use bitcoincore_rpc::bitcoin::BlockHash;
4+
use clarity_repl::clarity::util::hash::{hex_bytes};
5+
use crate::indexer::IndexerConfig;
6+
use crate::types::{BitcoinBlockData, BitcoinBlockMetadata, BlockIdentifier};
7+
use rocket::serde::json::{Value as JsonValue};
8+
9+
#[allow(dead_code)]
10+
#[derive(Deserialize)]
11+
pub struct NewBurnBlock {
12+
burn_block_hash: String,
13+
burn_block_height: u64,
14+
reward_slot_holders: Vec<String>,
15+
burn_amount: u64,
16+
}
17+
18+
pub fn standardize_bitcoin_block(indexer_config: &IndexerConfig, marshalled_block: JsonValue) -> BitcoinBlockData {
19+
let mut transactions = vec![];
20+
21+
let auth = Auth::UserPass(
22+
indexer_config.bitcoin_node_rpc_username.clone(),
23+
indexer_config.bitcoin_node_rpc_password.clone());
24+
25+
let rpc = Client::new(&indexer_config.bitcoin_node_rpc_url, auth).unwrap();
26+
27+
let partial_block: NewBurnBlock = serde_json::from_value(marshalled_block).unwrap();
28+
let block_height = partial_block.burn_block_height;
29+
let block_hash = {
30+
let block_hash_str = partial_block.burn_block_hash.strip_prefix("0x").unwrap();
31+
let mut block_hash_bytes = hex_bytes(&block_hash_str).unwrap();
32+
block_hash_bytes.reverse();
33+
BlockHash::from_slice(&block_hash_bytes).unwrap()
34+
};
35+
let block = rpc.get_block(&block_hash).unwrap();
36+
37+
for txdata in block.txdata.iter() {
38+
// let _ = tx.send(DevnetEvent::debug(format!(
39+
// "Tx.out: {:?}", txdata.output
40+
// )));
41+
}
42+
43+
BitcoinBlockData {
44+
block_identifier: BlockIdentifier {
45+
hash: block.header.block_hash().to_string(),
46+
index: block_height,
47+
},
48+
parent_block_identifier: BlockIdentifier {
49+
hash: block.header.prev_blockhash.to_string(),
50+
index: block_height - 1,
51+
},
52+
timestamp: block.header.time,
53+
metadata: BitcoinBlockMetadata {},
54+
transactions: transactions,
55+
}
56+
}
57+

src/indexer/chains/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub mod bitcoin;
2+
pub mod stacks;
3+
4+
pub use bitcoin::standardize_bitcoin_block;
5+
pub use stacks::standardize_stacks_block;

0 commit comments

Comments
 (0)