Skip to content

Commit 200fc82

Browse files
authored
Merge pull request #164 from 0xB10C/2021-02-getchaintips
add: support for getchaintips
2 parents 2ecab3a + 328ea68 commit 200fc82

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

client/src/client.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,12 @@ pub trait RpcApi: Sized {
849849
self.call("getmempoolentry", &[into_json(txid)?])
850850
}
851851

852+
/// Get information about all known tips in the block tree, including the
853+
/// main chain as well as stale branches.
854+
fn get_chain_tips(&self) -> Result<json::GetChainTipsResult> {
855+
self.call("getchaintips", &[])
856+
}
857+
852858
fn send_to_address(
853859
&self,
854860
address: &Address,

integration_test/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ fn main() {
184184
test_rescan_blockchain(&cl);
185185
test_create_wallet(&cl);
186186
test_get_tx_out_set_info(&cl);
187+
test_get_chain_tips(&cl);
187188
test_get_net_totals(&cl);
188189
test_get_network_hash_ps(&cl);
189190
test_uptime(&cl);
@@ -998,6 +999,11 @@ fn test_get_tx_out_set_info(cl: &Client) {
998999
cl.get_tx_out_set_info().unwrap();
9991000
}
10001001

1002+
fn test_get_chain_tips(cl: &Client) {
1003+
let tips = cl.get_chain_tips().unwrap();
1004+
assert_eq!(tips.len(), 1);
1005+
}
1006+
10011007
fn test_get_net_totals(cl: &Client) {
10021008
cl.get_net_totals().unwrap();
10031009
}

json/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,41 @@ pub struct FinalizePsbtResult {
11821182
pub complete: bool,
11831183
}
11841184

1185+
/// Models the result of "getchaintips"
1186+
pub type GetChainTipsResult = Vec<GetChainTipsResultTip>;
1187+
1188+
/// Models a single chain tip for the result of "getchaintips"
1189+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
1190+
pub struct GetChainTipsResultTip {
1191+
/// Block height of the chain tip
1192+
pub height: u64,
1193+
/// Header hash of the chain tip
1194+
pub hash: bitcoin::BlockHash,
1195+
/// Length of the branch (number of blocks since the last common block)
1196+
#[serde(rename = "branchlen")]
1197+
pub branch_length: usize,
1198+
/// Status of the tip as seen by Bitcoin Core
1199+
pub status: GetChainTipsResultStatus,
1200+
}
1201+
1202+
#[derive(Copy, Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
1203+
#[serde(rename_all = "lowercase")]
1204+
pub enum GetChainTipsResultStatus {
1205+
/// The branch contains at least one invalid block
1206+
Invalid,
1207+
/// Not all blocks for this branch are available, but the headers are valid
1208+
#[serde(rename = "headers-only")]
1209+
HeadersOnly,
1210+
/// All blocks are available for this branch, but they were never fully validated
1211+
#[serde(rename = "valid-headers")]
1212+
ValidHeaders,
1213+
/// This branch is not part of the active chain, but is fully validated
1214+
#[serde(rename = "valid-fork")]
1215+
ValidFork,
1216+
/// This is the tip of the active main chain, which is certainly valid
1217+
Active,
1218+
}
1219+
11851220
impl FinalizePsbtResult {
11861221
pub fn transaction(&self) -> Option<Result<Transaction, encode::Error>> {
11871222
self.hex.as_ref().map(|h| encode::deserialize(h))

0 commit comments

Comments
 (0)