Skip to content

Commit

Permalink
blocks: Add get_chain_tips.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Nov 7, 2024
1 parent d1dd0af commit 1c82ab0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
21 changes: 20 additions & 1 deletion src/client/rpc_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bitcoin::{
};
use bitcoincore_rpc::{
json::{
self, GetRawTransactionResult, GetRawTransactionResultVin,
self, GetChainTipsResultStatus, GetRawTransactionResult, GetRawTransactionResultVin,
GetRawTransactionResultVinScriptSig, GetRawTransactionResultVout,
GetRawTransactionResultVoutScriptPubKey, GetTransactionResult, GetTransactionResultDetail,
GetTransactionResultDetailCategory, GetTxOutResult, SignRawTransactionResult, WalletTxInfo,
Expand Down Expand Up @@ -609,6 +609,25 @@ impl RpcApi for Client {
errors: None,
})
}

#[tracing::instrument(skip_all)]
fn get_chain_tips(&self) -> bitcoincore_rpc::Result<json::GetChainTipsResult> {
let height = self.ledger.get_block_height().unwrap();
let hash = if height == 0 {
BlockHash::all_zeros()
} else {
self.ledger.get_block_with_height(height)?.block_hash()
};

let tip = json::GetChainTipsResultTip {
height: height as u64,
hash,
branch_length: height as usize,
status: GetChainTipsResultStatus::Active,
};

Ok(vec![tip])
}
}

#[cfg(test)]
Expand Down
22 changes: 20 additions & 2 deletions src/ledger/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::utils;
use bitcoin::block::{Header, Version};
use bitcoin::consensus::{Decodable, Encodable};
use bitcoin::hashes::Hash;
use bitcoin::{Address, Block, BlockHash, CompactTarget, Transaction, Txid};
use bitcoin::{Address, Block, BlockHash, CompactTarget, Transaction, TxMerkleNode, Txid};
use rusqlite::params;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
Expand Down Expand Up @@ -163,10 +163,28 @@ impl Ledger {
let mut encoded_hash: Vec<u8> = Vec::new();
hash.consensus_encode(&mut encoded_hash).unwrap();

// Handle genesis block.
if hash == BlockHash::all_zeros() {
return Ok(Block {
header: Header {
version: Version::TWO,
prev_blockhash: BlockHash::all_zeros(),
merkle_root: TxMerkleNode::all_zeros(),
time: 0,
bits: CompactTarget::default(),
nonce: 0,
},
txdata: vec![],
});
}

let qr = match self.database.lock().unwrap().query_row(
"SELECT body FROM blocks WHERE hash = ?1",
params![encoded_hash],
|row| Ok(row.get::<_, Vec<u8>>(0).unwrap()),
|row| {
tracing::error!("row {:?}", row);
Ok(row.get::<_, Vec<u8>>(0).unwrap())
},
) {
Ok(qr) => qr,
Err(e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ impl Ledger {
(
height INTEGER NOT NULL,
time INTEGER NOT NULL,
hash BLOB NOT NULL,
hash TEXT NOT NULL,
coinbase TEXT NOT NULL,
body BLOB NOT NULL
CONSTRAINT height PRIMARY KEY
);
INSERT INTO blocks (height, time, hash, coinbase, body) VALUES (0, 500000000, 0, 0, 0);
INSERT INTO blocks (height, time, hash, coinbase, body) VALUES (0, 500000000, '00000000000000000000', 0, 0);
CREATE TABLE mempool
(
Expand Down

0 comments on commit 1c82ab0

Please sign in to comment.