Skip to content

Commit 1c82ab0

Browse files
committed
blocks: Add get_chain_tips.
1 parent d1dd0af commit 1c82ab0

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/client/rpc_api.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::{
1717
};
1818
use bitcoincore_rpc::{
1919
json::{
20-
self, GetRawTransactionResult, GetRawTransactionResultVin,
20+
self, GetChainTipsResultStatus, GetRawTransactionResult, GetRawTransactionResultVin,
2121
GetRawTransactionResultVinScriptSig, GetRawTransactionResultVout,
2222
GetRawTransactionResultVoutScriptPubKey, GetTransactionResult, GetTransactionResultDetail,
2323
GetTransactionResultDetailCategory, GetTxOutResult, SignRawTransactionResult, WalletTxInfo,
@@ -609,6 +609,25 @@ impl RpcApi for Client {
609609
errors: None,
610610
})
611611
}
612+
613+
#[tracing::instrument(skip_all)]
614+
fn get_chain_tips(&self) -> bitcoincore_rpc::Result<json::GetChainTipsResult> {
615+
let height = self.ledger.get_block_height().unwrap();
616+
let hash = if height == 0 {
617+
BlockHash::all_zeros()
618+
} else {
619+
self.ledger.get_block_with_height(height)?.block_hash()
620+
};
621+
622+
let tip = json::GetChainTipsResultTip {
623+
height: height as u64,
624+
hash,
625+
branch_length: height as usize,
626+
status: GetChainTipsResultStatus::Active,
627+
};
628+
629+
Ok(vec![tip])
630+
}
612631
}
613632

614633
#[cfg(test)]

src/ledger/block.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::utils;
66
use bitcoin::block::{Header, Version};
77
use bitcoin::consensus::{Decodable, Encodable};
88
use bitcoin::hashes::Hash;
9-
use bitcoin::{Address, Block, BlockHash, CompactTarget, Transaction, Txid};
9+
use bitcoin::{Address, Block, BlockHash, CompactTarget, Transaction, TxMerkleNode, Txid};
1010
use rusqlite::params;
1111
use std::str::FromStr;
1212
use std::time::{SystemTime, UNIX_EPOCH};
@@ -163,10 +163,28 @@ impl Ledger {
163163
let mut encoded_hash: Vec<u8> = Vec::new();
164164
hash.consensus_encode(&mut encoded_hash).unwrap();
165165

166+
// Handle genesis block.
167+
if hash == BlockHash::all_zeros() {
168+
return Ok(Block {
169+
header: Header {
170+
version: Version::TWO,
171+
prev_blockhash: BlockHash::all_zeros(),
172+
merkle_root: TxMerkleNode::all_zeros(),
173+
time: 0,
174+
bits: CompactTarget::default(),
175+
nonce: 0,
176+
},
177+
txdata: vec![],
178+
});
179+
}
180+
166181
let qr = match self.database.lock().unwrap().query_row(
167182
"SELECT body FROM blocks WHERE hash = ?1",
168183
params![encoded_hash],
169-
|row| Ok(row.get::<_, Vec<u8>>(0).unwrap()),
184+
|row| {
185+
tracing::error!("row {:?}", row);
186+
Ok(row.get::<_, Vec<u8>>(0).unwrap())
187+
},
170188
) {
171189
Ok(qr) => qr,
172190
Err(e) => {

src/ledger/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ impl Ledger {
125125
(
126126
height INTEGER NOT NULL,
127127
time INTEGER NOT NULL,
128-
hash BLOB NOT NULL,
128+
hash TEXT NOT NULL,
129129
coinbase TEXT NOT NULL,
130130
body BLOB NOT NULL
131131
132132
CONSTRAINT height PRIMARY KEY
133133
);
134-
INSERT INTO blocks (height, time, hash, coinbase, body) VALUES (0, 500000000, 0, 0, 0);
134+
INSERT INTO blocks (height, time, hash, coinbase, body) VALUES (0, 500000000, '00000000000000000000', 0, 0);
135135
136136
CREATE TABLE mempool
137137
(

0 commit comments

Comments
 (0)