Skip to content

Commit

Permalink
Add more fields to /r/blockinfo (ordinals#3260)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphjaph authored Mar 11, 2024
1 parent 2c7c4e8 commit 35e6944
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 45 deletions.
44 changes: 43 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ rust-embed = "8.0.0"
rustls = "0.22.0"
rustls-acme = { version = "0.8.1", features = ["axum"] }
serde = { version = "1.0.137", features = ["derive"] }
serde-hex = "0.1.0"
serde_json = { version = "1.0.81", features = ["preserve_order"] }
serde_yaml = "0.9.17"
sha3 = "0.10.8"
Expand Down
3 changes: 3 additions & 0 deletions crates/test-bitcoincore-rpc/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub trait Api {
verbose: bool,
) -> Result<Value, jsonrpc_core::Error>;

#[rpc(name = "getblockstats")]
fn get_block_stats(&self, height: usize) -> Result<GetBlockStatsResult, jsonrpc_core::Error>;

#[rpc(name = "getblock")]
fn get_block(&self, blockhash: BlockHash, verbosity: u64) -> Result<String, jsonrpc_core::Error>;

Expand Down
18 changes: 9 additions & 9 deletions crates/test-bitcoincore-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ use {
Wtxid,
},
bitcoincore_rpc::json::{
Bip125Replaceable, CreateRawTransactionInput, Descriptor, EstimateMode, FinalizePsbtResult,
GetBalancesResult, GetBalancesResultEntry, GetBlockHeaderResult, GetBlockchainInfoResult,
GetDescriptorInfoResult, GetNetworkInfoResult, GetRawTransactionResult,
GetRawTransactionResultVout, GetRawTransactionResultVoutScriptPubKey, GetTransactionResult,
GetTransactionResultDetail, GetTransactionResultDetailCategory, GetTxOutResult,
GetWalletInfoResult, ImportDescriptors, ImportMultiResult, ListDescriptorsResult,
ListTransactionResult, ListUnspentResultEntry, ListWalletDirItem, ListWalletDirResult,
LoadWalletResult, SignRawTransactionInput, SignRawTransactionResult, Timestamp,
WalletProcessPsbtResult, WalletTxInfo,
Bip125Replaceable, CreateRawTransactionInput, Descriptor, EstimateMode, FeeRatePercentiles,
FinalizePsbtResult, GetBalancesResult, GetBalancesResultEntry, GetBlockHeaderResult,
GetBlockStatsResult, GetBlockchainInfoResult, GetDescriptorInfoResult, GetNetworkInfoResult,
GetRawTransactionResult, GetRawTransactionResultVout, GetRawTransactionResultVoutScriptPubKey,
GetTransactionResult, GetTransactionResultDetail, GetTransactionResultDetailCategory,
GetTxOutResult, GetWalletInfoResult, ImportDescriptors, ImportMultiResult,
ListDescriptorsResult, ListTransactionResult, ListUnspentResultEntry, ListWalletDirItem,
ListWalletDirResult, LoadWalletResult, SignRawTransactionInput, SignRawTransactionResult,
Timestamp, WalletProcessPsbtResult, WalletTxInfo,
},
jsonrpc_core::{IoHandler, Value},
jsonrpc_http_server::{CloseHandle, ServerBuilder},
Expand Down
49 changes: 48 additions & 1 deletion crates/test-bitcoincore-rpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ impl Api for Server {
Ok(
serde_json::to_value(GetBlockHeaderResult {
bits: String::new(),
chainwork: Vec::new(),
chainwork: hex::decode(
"0000000000000000000000000000000000000000000000000000000000000000",
)
.unwrap(),
confirmations: 0,
difficulty: 0.0,
hash: block_hash,
Expand All @@ -144,6 +147,50 @@ impl Api for Server {
}
}

fn get_block_stats(&self, height: usize) -> Result<GetBlockStatsResult, jsonrpc_core::Error> {
let Some(block_hash) = self.state().hashes.get(height).cloned() else {
return Err(Self::not_found());
};

Ok(GetBlockStatsResult {
avg_fee: Amount::ZERO,
avg_fee_rate: Amount::ZERO,
avg_tx_size: 0,
block_hash,
fee_rate_percentiles: FeeRatePercentiles {
fr_10th: Amount::ZERO,
fr_25th: Amount::ZERO,
fr_50th: Amount::ZERO,
fr_75th: Amount::ZERO,
fr_90th: Amount::ZERO,
},
height: height.try_into().unwrap(),
ins: 0,
max_fee: Amount::ZERO,
max_fee_rate: Amount::ZERO,
max_tx_size: 0,
median_fee: Amount::ZERO,
median_time: 0,
median_tx_size: 0,
min_fee: Amount::ZERO,
min_fee_rate: Amount::ZERO,
min_tx_size: 0,
outs: 0,
subsidy: Amount::ZERO,
sw_total_size: 0,
sw_total_weight: 0,
sw_txs: 0,
time: 0,
total_out: Amount::ZERO,
total_size: 0,
total_weight: 0,
total_fee: Amount::ZERO,
txs: 0,
utxo_increase: 0,
utxo_size_inc: 0,
})
}

fn get_block(
&self,
block_hash: BlockHash,
Expand Down
20 changes: 16 additions & 4 deletions docs/src/inscriptions/recursion.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,32 @@ Examples

```json
{
"average_fee": 0,
"average_fee_rate": 0,
"bits": 486604799,
"chainwork": 0,
"chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
"confirmations": 0,
"difficulty": 0.0,
"hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
"height": 0,
"median_time": null,
"max_fee": 0,
"max_fee_rate": 0,
"max_tx_size": 0,
"median_fee": 0,
"median_time": 1231006505,
"merkle_root": "0000000000000000000000000000000000000000000000000000000000000000",
"min_fee": 0,
"min_fee_rate": 0,
"next_block": null,
"nonce": 0,
"previous_block": null,
"subsidy": 5000000000,
"target": "00000000ffff0000000000000000000000000000000000000000000000000000",
"timestamp": 0,
"transaction_count": 0,
"timestamp": 1231006505,
"total_fee": 0,
"total_size": 0,
"total_weight": 0,
"transaction_count": 1,
"version": 1
}
```
Expand Down
24 changes: 20 additions & 4 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::{
target_as_block_hash, BlockHash, Chain, Deserialize, Height, InscriptionId, OutPoint, Pile,
Rarity, SatPoint, Serialize, SpacedRune, TxMerkleNode, TxOut,
use {
super::{
target_as_block_hash, BlockHash, Chain, Deserialize, Height, InscriptionId, OutPoint, Pile,
Rarity, SatPoint, Serialize, SpacedRune, TxMerkleNode, TxOut,
},
serde_hex::{SerHex, Strict},
};

pub use crate::templates::{
Expand Down Expand Up @@ -36,19 +39,32 @@ impl Block {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct BlockInfo {
pub average_fee: u64,
pub average_fee_rate: u64,
pub bits: u32,
pub chainwork: u128,
#[serde(with = "SerHex::<Strict>")]
pub chainwork: [u8; 32],
pub confirmations: i32,
pub difficulty: f64,
pub hash: BlockHash,
pub height: u32,
pub max_fee: u64,
pub max_fee_rate: u64,
pub max_tx_size: u32,
pub median_fee: u64,
pub median_time: Option<u64>,
pub merkle_root: TxMerkleNode,
pub min_fee: u64,
pub min_fee_rate: u64,
pub next_block: Option<BlockHash>,
pub nonce: u32,
pub previous_block: Option<BlockHash>,
pub subsidy: u64,
pub target: BlockHash,
pub timestamp: u64,
pub total_fee: u64,
pub total_size: usize,
pub total_weight: usize,
pub transaction_count: u64,
pub version: u32,
}
Expand Down
9 changes: 8 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ use {
templates::StatusHtml,
},
bitcoin::block::Header,
bitcoincore_rpc::{json::GetBlockHeaderResult, Client},
bitcoincore_rpc::{
json::{GetBlockHeaderResult, GetBlockStatsResult},
Client,
},
chrono::SubsecRound,
indicatif::{ProgressBar, ProgressStyle},
log::log_enabled,
Expand Down Expand Up @@ -992,6 +995,10 @@ impl Index {
self.client.get_block_header_info(&hash).into_option()
}

pub(crate) fn block_stats(&self, height: u64) -> Result<Option<GetBlockStatsResult>> {
self.client.get_block_stats(height).into_option()
}

pub(crate) fn get_block_by_height(&self, height: u32) -> Result<Option<Block>> {
Ok(
self
Expand Down
Loading

0 comments on commit 35e6944

Please sign in to comment.