Skip to content

Commit

Permalink
fix(btcio): breaking change in GetBlockChainInfo
Browse files Browse the repository at this point in the history
Shouldn't be a breaking change for us,
since we are not using any of the changed stuff.
See:
https://github.com/bitcoin/bitcoin/blob/master/doc/release-notes/release-notes-28.0.md#updated-rpcs
  • Loading branch information
storopoli committed Oct 16, 2024
1 parent bb7765d commit 55cd451
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 10 deletions.
6 changes: 3 additions & 3 deletions crates/btcio/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bitcoin::{
bip32::Xpriv, consensus::encode::serialize_hex, Address, Block, BlockHash, Network,
Transaction, Txid,
};
use bitcoind_json_rpc_types::v26::{GetBlockVerbosityZero, GetBlockchainInfo, GetNewAddress};
use bitcoind_json_rpc_types::v26::{GetBlockVerbosityZero, GetNewAddress};
use reqwest::{
header::{HeaderMap, AUTHORIZATION, CONTENT_TYPE},
Client,
Expand All @@ -28,8 +28,8 @@ use crate::rpc::{
error::{BitcoinRpcError, ClientError},
traits::{Broadcaster, Reader, Signer, Wallet},
types::{
CreateWallet, GetTransaction, ImportDescriptor, ImportDescriptorResult, ListDescriptors,
ListTransactions, ListUnspent, SignRawTransactionWithWallet,
CreateWallet, GetBlockchainInfo, GetTransaction, ImportDescriptor, ImportDescriptorResult,
ListDescriptors, ListTransactions, ListUnspent, SignRawTransactionWithWallet,
},
};

Expand Down
5 changes: 2 additions & 3 deletions crates/btcio/src/rpc/traits.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use async_trait::async_trait;
use bitcoin::{bip32::Xpriv, Address, Block, BlockHash, Network, Transaction, Txid};
use bitcoind_json_rpc_types::v26::GetBlockchainInfo;

use crate::rpc::{
client::ClientResult,
types::{
GetTransaction, ImportDescriptor, ImportDescriptorResult, ListTransactions, ListUnspent,
SignRawTransactionWithWallet,
GetBlockchainInfo, GetTransaction, ImportDescriptor, ImportDescriptorResult,
ListTransactions, ListUnspent, SignRawTransactionWithWallet,
},
};

Expand Down
129 changes: 129 additions & 0 deletions crates/btcio/src/rpc/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use bitcoin::{
absolute::Height, address::NetworkUnchecked, consensus, Address, Amount, BlockHash,
SignedAmount, Transaction, Txid,
Expand Down Expand Up @@ -35,6 +37,133 @@ pub enum TransactionCategory {
Orphan,
}

/// Result of JSON-RPC method `getblockchaininfo`.
///
/// Method call: `getblockchaininfo`
///
/// > Returns an object containing various state info regarding blockchain processing.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct GetBlockchainInfo {
/// Current network name as defined in BIP70 (main, test, signet, regtest).
pub chain: String,
/// The current number of blocks processed in the server.
pub blocks: u64,
/// The current number of headers we have validated.
pub headers: u64,
/// The hash of the currently best block.
#[serde(rename = "bestblockhash")]
pub best_block_hash: String,
/// The current difficulty.
pub difficulty: f64,
/// Median time for the current best block.
#[serde(rename = "mediantime")]
pub median_time: u64,
/// Estimate of verification progress (between 0 and 1).
#[serde(rename = "verificationprogress")]
pub verification_progress: f64,
/// Estimate of whether this node is in Initial Block Download (IBD) mode.
#[serde(rename = "initialblockdownload")]
pub initial_block_download: bool,
/// Total amount of work in active chain, in hexadecimal.
#[serde(rename = "chainwork")]
pub chain_work: String,
/// The estimated size of the block and undo files on disk.
pub size_on_disk: u64,
/// If the blocks are subject to pruning.
pub pruned: bool,
/// Lowest-height complete block stored (only present if pruning is enabled).
#[serde(rename = "pruneheight")]
pub prune_height: Option<u64>,
/// Whether automatic pruning is enabled (only present if pruning is enabled).
pub automatic_pruning: Option<bool>,
/// The target size used by pruning (only present if automatic pruning is enabled).
pub prune_target_size: Option<u64>,
/// Status of softforks in progress, maps softfork name -> [`Softfork`].
#[serde(default)]
pub softforks: BTreeMap<String, Softfork>,
/// Any network and blockchain warnings.
pub warnings: Vec<String>,
}

/// Status of softfork.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct Softfork {
/// The [`SoftforkType`]: one of "buried", "bip9".
#[serde(rename = "type")]
pub type_: SoftforkType,
/// The status of bip9 softforks (only for "bip9" type).
pub bip9: Option<Bip9SoftforkInfo>,
/// Height of the first block which the rules are or will be enforced (only for "buried" type,
/// or "bip9" type with "active" status).
pub height: Option<u64>,
/// `true` if the rules are enforced for the mempool and the next block.
pub active: bool,
}

/// The softfork type: one of "buried", "bip9".
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum SoftforkType {
/// Softfork is "buried" (as defined in [BIP-90]).
///
/// [BIP-90] <https://github.com/bitcoin/bips/blob/master/bip-0090.mediawiki>
Buried,
/// Softfork is "bip9" (see [BIP-9]).
///
/// [BIP-9] <https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki>
Bip9,
}

/// Status of BIP-9 softforks.
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct Bip9SoftforkInfo {
/// One of "defined", "started", "locked_in", "active", "failed".
pub status: Bip9SoftforkStatus,
/// The bit (0-28) in the block version field used to signal this softfork (only for "started"
/// status).
pub bit: Option<u8>,
/// The minimum median time past of a block at which the bit gains its meaning.
pub start_time: i64,
/// The median time past of a block at which the deployment is considered failed if not yet
/// locked in.
pub timeout: u64,
/// Height of the first block to which the status applies.
pub since: u32,
/// Numeric statistics about BIP-9 signalling for a softfork (only for "started" status).
pub statistics: Option<Bip9SoftforkStatistics>,
}

/// BIP-9 softfork status: one of "defined", "started", "locked_in", "active", "failed".
#[derive(Copy, Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Bip9SoftforkStatus {
/// BIP-9 softfork status "defined".
Defined,
/// BIP-9 softfork status "started".
Started,
/// BIP-9 softfork status "locked_in".
LockedIn,
/// BIP-9 softfork status "active".
Active,
/// BIP-9 softfork status "failed".
Failed,
}

/// Statistics for a BIP-9 softfork.
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
pub struct Bip9SoftforkStatistics {
/// The length in blocks of the BIP9 signalling period.
pub period: u32,
/// The number of blocks with the version bit set required to activate the feature.
pub threshold: Option<u32>,
/// The number of blocks elapsed since the beginning of the current period.
pub elapsed: u32,
/// The number of blocks with the version bit set in the current period.
pub count: u32,
/// `false` if there are not enough blocks left in this period to pass activation threshold.
pub possible: Option<bool>,
}

/// Models the result of JSON-RPC method `listunspent`.
///
/// # Note
Expand Down
7 changes: 3 additions & 4 deletions crates/btcio/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use bitcoin::{
taproot::ControlBlock,
Address, Amount, Block, BlockHash, Network, ScriptBuf, SignedAmount, Transaction, Txid, Work,
};
use bitcoind_json_rpc_types::v26::GetBlockchainInfo;
use strata_state::tx::InscriptionData;

use crate::{
rpc::{
traits::{Broadcaster, Reader, Signer, Wallet},
types::{
GetTransaction, ImportDescriptor, ImportDescriptorResult, ListTransactions,
ListUnspent, SignRawTransactionWithWallet,
GetBlockchainInfo, GetTransaction, ImportDescriptor, ImportDescriptorResult,
ListTransactions, ListUnspent, SignRawTransactionWithWallet,
},
ClientResult,
},
Expand Down Expand Up @@ -95,7 +94,7 @@ impl Reader for TestBitcoinClient {
automatic_pruning: None,
prune_target_size: None,
softforks: BTreeMap::new(),
warnings: "".to_string(),
warnings: Vec::new(),
})
}

Expand Down

0 comments on commit 55cd451

Please sign in to comment.