Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add tenure-height to the /v2/info endpoint #5308

Merged
merged 17 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to the versioning scheme outlined in the [README.md](RE
- `get-tenure-info?` added
- `get-block-info?` removed
- Added `/v3/signer/{signer_pubkey}/{reward_cycle}` endpoint
- Added `tenure_height` to `/v2/info` endpoint
- Added optional `timeout_ms` to `events_observer` configuration

## [2.5.0.0.7]
Expand Down
1 change: 1 addition & 0 deletions docs/rpc/api/core-node/get-info.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"stacks_tip": "b1807a2d3f7f8c7922f7c1d60d7c34145ade05d789640dc7dc9ec1021e07bb54",
"stacks_tip_consensus_hash": "17f76e597bab45646956f38dd39573085d72cbc0",
"unanchored_tip": "0000000000000000000000000000000000000000000000000000000000000000",
"tenure_height": 523,
"exit_at_block_height": null,
"is_fully_synced": false
}
5 changes: 5 additions & 0 deletions docs/rpc/api/core-node/get-info.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"stacks_tip",
"stacks_tip_consensus_hash",
"unanchored_tip",
"tenure_height",
"exit_at_block_height",
"is_fully_synced"
],
Expand Down Expand Up @@ -69,6 +70,10 @@
"type": "string",
"description": "the latest microblock hash if any microblocks were processed. if no microblock has been processed for the current block, a 000.., hex array is returned"
},
"tenure_height": {
"type": "integer",
"description": "the latest Stacks tenure height"
},
"exit_at_block_height": {
"type": "integer",
"description": "the block height at which the testnet network will be reset. not applicable for mainnet"
Expand Down
1 change: 1 addition & 0 deletions stacks-signer/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub(crate) mod tests {
stacks_tip_consensus_hash: generate_random_consensus_hash(),
unanchored_tip: None,
unanchored_seq: Some(0),
tenure_height: thread_rng().next_u64(),
exit_at_block_height: None,
is_fully_synced: false,
genesis_chainstate_hash: Sha256Sum::zero(),
Expand Down
26 changes: 21 additions & 5 deletions stackslib/src/net/api/getinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ use stacks_common::util::hash::{Hash160, Sha256Sum};
use crate::burnchains::affirmation::AffirmationMap;
use crate::burnchains::Txid;
use crate::chainstate::burn::db::sortdb::SortitionDB;
use crate::chainstate::nakamoto::NakamotoChainState;
use crate::chainstate::stacks::db::StacksChainState;
use crate::core::mempool::MemPoolDB;
use crate::net::http::{
parse_json, Error, HttpRequest, HttpRequestContents, HttpRequestPreamble, HttpResponse,
HttpResponseContents, HttpResponsePayload, HttpResponsePreamble,
HttpResponseContents, HttpResponsePayload, HttpResponsePreamble, HttpServerError,
};
use crate::net::httpcore::{
HttpPreambleExtensions, RPCRequestHandler, StacksHttpRequest, StacksHttpResponse,
Expand Down Expand Up @@ -81,6 +82,7 @@ pub struct RPCPeerInfoData {
pub genesis_chainstate_hash: Sha256Sum,
pub unanchored_tip: Option<StacksBlockId>,
pub unanchored_seq: Option<u16>,
pub tenure_height: u64,
pub exit_at_block_height: Option<u64>,
pub is_fully_synced: bool,
#[serde(default)]
Expand All @@ -106,6 +108,7 @@ impl RPCPeerInfoData {
chainstate: &StacksChainState,
exit_at_block_height: Option<u64>,
genesis_chainstate_hash: &Sha256Sum,
coinbase_height: u64,
ibd: bool,
) -> RPCPeerInfoData {
let server_version = version_string(
Expand Down Expand Up @@ -148,7 +151,7 @@ impl RPCPeerInfoData {
stacks_tip_consensus_hash: network.stacks_tip.consensus_hash.clone(),
unanchored_tip: unconfirmed_tip,
unanchored_seq: unconfirmed_seq,
exit_at_block_height: exit_at_block_height,
exit_at_block_height,
is_fully_synced,
genesis_chainstate_hash: genesis_chainstate_hash.clone(),
node_public_key: Some(public_key_buf),
Expand All @@ -169,6 +172,7 @@ impl RPCPeerInfoData {
.map(|cid| format!("{}", cid))
.collect(),
),
tenure_height: coinbase_height,
}
}
}
Expand Down Expand Up @@ -217,16 +221,28 @@ impl RPCRequestHandler for RPCPeerInfoRequestHandler {
node: &mut StacksNodeState,
) -> Result<(HttpResponsePreamble, HttpResponseContents), NetError> {
let ibd = node.ibd;
let rpc_peer_info =

let rpc_peer_info: Result<RPCPeerInfoData, StacksHttpResponse> =
node.with_node_state(|network, _sortdb, chainstate, _mempool, rpc_args| {
RPCPeerInfoData::from_network(
let coinbase_height = network.stacks_tip.coinbase_height;

Ok(RPCPeerInfoData::from_network(
network,
chainstate,
rpc_args.exit_at_block_height.clone(),
&rpc_args.genesis_chainstate_hash,
coinbase_height,
ibd,
)
))
});

let rpc_peer_info = match rpc_peer_info {
Ok(rpc_peer_info) => rpc_peer_info,
Err(response) => {
return response.try_into_contents().map_err(NetError::from);
}
};

let mut preamble = HttpResponsePreamble::ok_json(&preamble);
preamble.set_canonical_stacks_tip_height(Some(node.canonical_stacks_tip_height()));
let body = HttpResponseContents::try_from_json(&rpc_peer_info)?;
Expand Down
14 changes: 4 additions & 10 deletions stackslib/src/net/api/postmempoolquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ impl StacksMemPoolStream {

Self {
tx_query,
last_randomized_txid: last_randomized_txid,
last_randomized_txid,
num_txs: 0,
max_txs: max_txs,
max_txs,
coinbase_height,
corked: false,
finished: false,
Expand Down Expand Up @@ -275,14 +275,8 @@ impl RPCRequestHandler for RPCMempoolQueryRequestHandler {
.ok_or(NetError::SendError("`mempool_query` not set".into()))?;
let page_id = self.page_id.take();

let stream_res = node.with_node_state(|network, sortdb, chainstate, mempool, _rpc_args| {
let header = self.get_stacks_chain_tip(&preamble, sortdb, chainstate)
.map_err(|e| StacksHttpResponse::new_error(&preamble, &HttpServerError::new(format!("Failed to load chain tip: {:?}", &e))))?;

let coinbase_height = NakamotoChainState::get_coinbase_height(&mut chainstate.index_conn(), &header.index_block_hash())
.map_err(|e| StacksHttpResponse::new_error(&preamble, &HttpServerError::new(format!("Failed to load coinbase height: {:?}", &e))))?
.unwrap_or(0);

let stream_res = node.with_node_state(|network, _sortdb, _chainstate, mempool, _rpc_args| {
let coinbase_height = network.stacks_tip.coinbase_height;
let max_txs = network.connection_opts.mempool_max_tx_query;
debug!(
"Begin mempool query";
Expand Down
12 changes: 7 additions & 5 deletions stackslib/src/net/api/tests/getinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn test_try_parse_request() {
.try_parse_request(&parsed_preamble.expect_request(), &bytes[offset..])
.unwrap();

// parsed request consumes headers that would not be in a constructed reqeuest
// parsed request consumes headers that would not be in a constructed request
parsed_request.clear_headers();
parsed_request.add_header(
"X-Canonical-Stacks-Tip-Height".to_string(),
Expand All @@ -63,10 +63,10 @@ fn test_try_parse_request() {

#[test]
fn test_getinfo_compat() {
let old_getinfo_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false}"#;
let getinfo_no_pubkey_hash_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false,"node_public_key":"029b27d345e7bd2a6627262cefe6e97d9bc482f41ec32ec76a7bec391bb441798d"}"#;
let getinfo_no_pubkey_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false,"node_public_key_hash":"046e6f832a83ff0da4a550907d3a44412cc1e4bf"}"#;
let getinfo_full_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false,"node_public_key":"029b27d345e7bd2a6627262cefe6e97d9bc482f41ec32ec76a7bec391bb441798d","node_public_key_hash":"046e6f832a83ff0da4a550907d3a44412cc1e4bf"}"#;
let old_getinfo_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false, "tenure_height": 42}"#;
let getinfo_no_pubkey_hash_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false,"node_public_key":"029b27d345e7bd2a6627262cefe6e97d9bc482f41ec32ec76a7bec391bb441798d", "tenure_height": 42}"#;
let getinfo_no_pubkey_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false,"node_public_key_hash":"046e6f832a83ff0da4a550907d3a44412cc1e4bf", "tenure_height": 0}"#;
let getinfo_full_json = r#"{"peer_version":402653189,"pox_consensus":"b712eb731b613eebae814a8f416c5c15bc8391ec","burn_block_height":727631,"stable_pox_consensus":"53b5ed79842080500d7d83daa36aa1069dedf983","stable_burn_block_height":727624,"server_version":"stacks-node 0.0.1 (feat/faster-inv-generation:68f33190a, release build, linux [x86_64])","network_id":1,"parent_network_id":3652501241,"stacks_tip_height":52537,"stacks_tip":"b3183f2ac588e12319ff0fde78f97e62c92a218d87828c35710c29aaf7adbedc","stacks_tip_consensus_hash":"b712eb731b613eebae814a8f416c5c15bc8391ec","genesis_chainstate_hash":"74237aa39aa50a83de11a4f53e9d3bb7d43461d1de9873f402e5453ae60bc59b","unanchored_tip":"e76f68d607480e9984b4062b2691fb60a88423177898f5780b40ace17ae8982a","unanchored_seq":0,"exit_at_block_height":null,"is_fully_synced":false,"node_public_key":"029b27d345e7bd2a6627262cefe6e97d9bc482f41ec32ec76a7bec391bb441798d","node_public_key_hash":"046e6f832a83ff0da4a550907d3a44412cc1e4bf", "tenure_height": 2423}"#;

// they all parse
for json_obj in &[
Expand Down Expand Up @@ -102,4 +102,6 @@ fn test_try_make_response() {
Some(1)
);
let resp = response.decode_peer_info().unwrap();

assert_eq!(resp.tenure_height, 1);
}
1 change: 0 additions & 1 deletion stackslib/src/net/api/tests/gettenureinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use stacks_common::types::net::PeerHost;
use stacks_common::types::Address;

use super::test_rpc;
use crate::net::api::getinfo::RPCPeerInfoData;
use crate::net::api::tests::TestRPC;
use crate::net::api::*;
use crate::net::connection::ConnectionOptions;
Expand Down
Loading