-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding additional GRPC endpoints for block explorer (#1922)
Merge pull request #1922 The following endpoints have been added to the GRPC service based on the core requirements. GetConstants GetBlockSize GetBlockFees GetVersion GetTokensInCirculation GetNetworkDifficulty * pull/1922/head: Moving constants to proto
- Loading branch information
Showing
10 changed files
with
545 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2019. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
use tari_core::{base_node::LocalNodeCommsInterface, blocks::BlockHeader, chain_storage::HistoricalBlock}; | ||
use tonic::Status; | ||
|
||
// The maximum number of blocks that can be requested at a time. These will be streamed to the | ||
// client, so memory is not really a concern here, but a malicious client could request a large | ||
// number here to keep the node busy | ||
pub const GET_BLOCKS_MAX_HEIGHTS: usize = 1000; | ||
|
||
// The number of blocks to request from the base node at a time. This is to reduce the number of | ||
// requests to the base node, but if you'd like to stream directly, this can be set to 1. | ||
pub const GET_BLOCKS_PAGE_SIZE: usize = 10; | ||
|
||
/// Magic number for input and output sizes | ||
pub const BLOCK_INPUT_SIZE: u64 = 4; | ||
pub const BLOCK_OUTPUT_SIZE: u64 = 13; | ||
|
||
/// Returns the block heights based on the start and end heights or from_tip | ||
pub async fn block_heights( | ||
handler: LocalNodeCommsInterface, | ||
start_height: u64, | ||
end_height: u64, | ||
from_tip: u64, | ||
) -> Result<Vec<u64>, Status> | ||
{ | ||
let heights = if start_height > 0 && end_height > 0 { | ||
Ok(BlockHeader::get_height_range(start_height, end_height)) | ||
} else if from_tip > 0 { | ||
BlockHeader::get_heights_from_tip(handler, from_tip) | ||
.await | ||
.map_err(|e| Status::internal(e.to_string())) | ||
} else { | ||
return Err(Status::invalid_argument("Invalid arguments provided")); | ||
}; | ||
heights | ||
} | ||
|
||
pub fn block_size(block: &HistoricalBlock) -> u64 { | ||
let body = block.clone().block.body; | ||
|
||
let input_size = body.inputs().len() as u64 * BLOCK_INPUT_SIZE; | ||
let output_size = body.outputs().len() as u64 * BLOCK_OUTPUT_SIZE; | ||
input_size + output_size | ||
} | ||
|
||
pub fn block_fees(block: &HistoricalBlock) -> u64 { | ||
let body = block.clone().block.body; | ||
body.kernels() | ||
.iter() | ||
.map(|k| k.fee.into()) | ||
.collect::<Vec<u64>>() | ||
.iter() | ||
.sum::<u64>() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2019. The Tari Project | ||
// | ||
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
// following conditions are met: | ||
// | ||
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
// disclaimer. | ||
// | ||
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the | ||
// following disclaimer in the documentation and/or other materials provided with the distribution. | ||
// | ||
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote | ||
// products derived from this software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
pub fn median(mut list: Vec<u64>) -> Option<f64> { | ||
if list.is_empty() { | ||
return None; | ||
} | ||
list.sort(); | ||
let mid_index = list.len() / 2; | ||
let median = if list.len() % 2 == 0 { | ||
(list[mid_index - 1] + list[mid_index]) as f64 / 2.0 | ||
} else { | ||
list[mid_index] as f64 | ||
}; | ||
Some(median) | ||
} | ||
|
||
pub fn mean(list: Vec<u64>) -> Option<f64> { | ||
if list.is_empty() { | ||
return None; | ||
} | ||
let mut count = 0; | ||
let total = list.iter().inspect(|_| count += 1).sum::<u64>(); | ||
Some(total as f64 / count as f64) | ||
} | ||
/// TODO Implement the function for grpc responsed | ||
pub fn quantile(_list: Vec<u64>) -> Option<f64> { | ||
None | ||
} | ||
|
||
/// TODO Implement the function for grpc responsed | ||
pub fn quartile(_list: Vec<u64>) -> Option<f64> { | ||
None | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn median() { | ||
let mut values = vec![1u64, 8u64, 3u64, 9u64]; | ||
let median_value = super::median(values); | ||
assert_eq!(median_value, Some(5.5f64)) | ||
} | ||
|
||
#[test] | ||
fn mean() { | ||
let values = vec![1u64, 8u64, 3u64, 9u64]; | ||
let mean_value = super::mean(values); | ||
assert_eq!(mean_value, Some(5.25f64)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod blocks; | ||
pub mod helpers; | ||
pub mod server; |
Oops, something went wrong.