use std::cmp;
use tari_core::base_node::LocalNodeCommsInterface;
use tari_node_components::blocks::HistoricalBlock;
use tonic::Status;
pub const GET_BLOCKS_MAX_HEIGHTS: usize = 1000;
pub const GET_BLOCKS_PAGE_SIZE: usize = 10;
pub const BLOCK_INPUT_SIZE: u64 = 4;
pub const BLOCK_OUTPUT_SIZE: u64 = 13;
pub async fn block_heights(
mut handler: LocalNodeCommsInterface,
start_height: u64,
end_height: u64,
from_tip: u64,
) -> Result<(u64, u64), Status> {
if end_height > 0 {
if start_height > end_height {
return Err(Status::invalid_argument("Start height was greater than end height"));
}
Ok((start_height, end_height))
} else if from_tip > 0 {
let metadata = handler
.get_metadata()
.await
.map_err(|e| Status::internal(e.to_string()))?;
let tip = metadata.best_block_height();
let height_from_tip = cmp::min(tip, from_tip);
let start = tip.saturating_sub(height_from_tip);
Ok((start, tip))
} else {
Err(Status::invalid_argument("Invalid arguments provided"))
}
}
pub fn block_size(block: &HistoricalBlock) -> u64 {
let body = &block.block().body;
let input_size = (body.inputs().len() as u64).saturating_mul(BLOCK_INPUT_SIZE);
let output_size = (body.outputs().len() as u64).saturating_mul(BLOCK_OUTPUT_SIZE);
input_size.saturating_add(output_size)
}
pub fn block_fees(block: &HistoricalBlock) -> u64 {
let body = &block.block().body;
body.kernels()
.iter()
.filter(|k| !k.is_coinbase())
.map(|k| k.fee.into())
.collect::<Vec<u64>>()
.iter()
.sum::<u64>()
}