Skip to content

Commit

Permalink
Only use TTL short for regtest
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Apr 10, 2024
1 parent a09e787 commit ec12a5e
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,20 @@ impl From<SpendingInput> for SpendingValue {
}
}

#[inline]
fn ttl_long(config: &Config) -> u32 {
// Regtest networks change often, so they should always have short TTL
if config.network_type.is_regtest() {
config.mempool_rest_ttl_short
} else {
config.mempool_rest_ttl_long
}
}

fn ttl_by_depth(height: Option<usize>, query: &Query, config: &Config) -> u32 {
height.map_or(config.mempool_rest_ttl_short, |height| {
if query.chain().best_height() - height >= CONF_FINAL {
config.mempool_rest_ttl_long
ttl_long(config)
} else {
config.mempool_rest_ttl_short
}
Expand Down Expand Up @@ -727,7 +737,7 @@ fn handle_request(
.get_block_with_meta(&hash)
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?;
let block_value = BlockValue::new(blockhm);
json_response(block_value, config.mempool_rest_ttl_long)
json_response(block_value, ttl_long(config))
}
(&Method::GET, Some(&"block"), Some(hash), Some(&"status"), None, None) => {
let hash = BlockHash::from_hex(hash)?;
Expand All @@ -741,7 +751,7 @@ fn handle_request(
.chain()
.get_block_txids(&hash)
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?;
json_response(txids, config.mempool_rest_ttl_long)
json_response(txids, ttl_long(config))
}
(&Method::GET, Some(&INTERNAL_PREFIX), Some(&"block"), Some(hash), Some(&"txs"), None) => {
let hash = BlockHash::from_hex(hash)?;
Expand All @@ -765,7 +775,7 @@ fn handle_request(
.ok_or_else(|| HttpError::not_found("Block not found".to_string()))?;

let header_hex = hex::encode(encode::serialize(&header));
http_message(StatusCode::OK, header_hex, config.mempool_rest_ttl_long)
http_message(StatusCode::OK, header_hex, ttl_long(config))
}
(&Method::GET, Some(&"block"), Some(hash), Some(&"raw"), None, None) => {
let hash = BlockHash::from_hex(hash)?;
Expand All @@ -779,7 +789,7 @@ fn handle_request(
.header("Content-Type", "application/octet-stream")
.header(
"Cache-Control",
format!("public, max-age={:}", config.mempool_rest_ttl_long),
format!("public, max-age={:}", ttl_long(config)),
)
.header("X-Powered-By", &**VERSION_STRING)
.body(Body::from(raw))
Expand All @@ -795,11 +805,7 @@ fn handle_request(
if index >= txids.len() {
bail!(HttpError::not_found("tx index out of range".to_string()));
}
http_message(
StatusCode::OK,
txids[index].to_hex(),
config.mempool_rest_ttl_long,
)
http_message(StatusCode::OK, txids[index].to_hex(), ttl_long(config))
}
(&Method::GET, Some(&"block"), Some(hash), Some(&"txs"), start_index, None) => {
let hash = BlockHash::from_hex(hash)?;
Expand Down

0 comments on commit ec12a5e

Please sign in to comment.