From 403b0c62af9ed2f2eefc48e0feb5025d8c853ecc Mon Sep 17 00:00:00 2001 From: stringhandler Date: Mon, 4 Sep 2023 15:02:20 +0200 Subject: [PATCH] fix!: remove timestamp from header in proto files (#5667) Description --- The protobuf definition of BlockHeader was using a `timestamp` type which included `nanoseconds`. This data was discarded anyway and only the seconds were included. I've deleted these and made the conversions simpler Motivation and Context --- Removing extra data from a packet How Has This Been Tested? --- Existing tests What process can a PR reviewer use to test or verify this change? --- Since this data was discarded, it was only really up to a malicious party to populate this data. This has now been removed. Breaking Changes --- - [ ] None - [ ] Requires data directory on base node to be deleted - [x] Requires hard fork - [ ] Other - Please specify BREAKING CHANGE: Previous BlockHeader protobuf messages are incompatible --- Cargo.lock | 1 - .../minotari_app_grpc/proto/block.proto | 3 +- .../src/conversions/block_header.rs | 16 ++------ .../minotari_app_grpc/src/conversions/mod.rs | 25 ------------ .../minotari_merge_mining_proxy/src/proxy.rs | 2 +- applications/minotari_miner/src/miner.rs | 6 +-- base_layer/core/Cargo.toml | 1 - base_layer/core/src/proto/block.proto | 3 +- base_layer/core/src/proto/block_header.rs | 13 ++----- base_layer/core/src/proto/mod.rs | 2 - base_layer/core/src/proto/utils.rs | 38 ------------------- 11 files changed, 13 insertions(+), 97 deletions(-) delete mode 100644 base_layer/core/src/proto/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 736f0d2abe..b83940464e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5649,7 +5649,6 @@ dependencies = [ "num-traits", "once_cell", "prost 0.9.0", - "prost-types 0.9.0", "rand 0.8.5", "randomx-rs", "serde", diff --git a/applications/minotari_app_grpc/proto/block.proto b/applications/minotari_app_grpc/proto/block.proto index d6d8eae344..9822767bc5 100644 --- a/applications/minotari_app_grpc/proto/block.proto +++ b/applications/minotari_app_grpc/proto/block.proto @@ -24,7 +24,6 @@ syntax = "proto3"; package tari.rpc; import "transaction.proto"; -import "google/protobuf/timestamp.proto"; // The BlockHeader contains all the metadata for the block, including proof of work, a link to the previous block // and the transaction kernels. @@ -38,7 +37,7 @@ message BlockHeader { // Hash of the block previous to this in the chain. bytes prev_hash = 4; // Timestamp at which the block was built. - google.protobuf.Timestamp timestamp = 5; + uint64 timestamp = 5; // This is the UTXO merkle root of the outputs // This is calculated as Hash (txo MMR root || roaring bitmap hash of UTXO indices) bytes output_mr = 6; diff --git a/applications/minotari_app_grpc/src/conversions/block_header.rs b/applications/minotari_app_grpc/src/conversions/block_header.rs index 799c21b801..25beb80f40 100644 --- a/applications/minotari_app_grpc/src/conversions/block_header.rs +++ b/applications/minotari_app_grpc/src/conversions/block_header.rs @@ -24,12 +24,9 @@ use std::convert::TryFrom; use tari_common_types::types::{FixedHash, PrivateKey}; use tari_core::{blocks::BlockHeader, proof_of_work::ProofOfWork}; -use tari_utilities::ByteArray; +use tari_utilities::{epoch_time::EpochTime, ByteArray}; -use crate::{ - conversions::{datetime_to_timestamp, timestamp_to_datetime}, - tari_rpc as grpc, -}; +use crate::tari_rpc as grpc; impl From for grpc::BlockHeader { fn from(h: BlockHeader) -> Self { @@ -39,7 +36,7 @@ impl From for grpc::BlockHeader { version: u32::from(h.version), height: h.height, prev_hash: h.prev_hash.to_vec(), - timestamp: datetime_to_timestamp(h.timestamp), + timestamp: h.timestamp.as_u64(), input_mr: h.input_mr.to_vec(), output_mr: h.output_mr.to_vec(), output_mmr_size: h.output_mmr_size, @@ -65,11 +62,6 @@ impl TryFrom for BlockHeader { let total_script_offset = PrivateKey::from_bytes(&header.total_script_offset).map_err(|err| err.to_string())?; - let timestamp = header - .timestamp - .and_then(timestamp_to_datetime) - .ok_or_else(|| "timestamp not provided or was negative".to_string())?; - let pow = match header.pow { Some(p) => ProofOfWork::try_from(p)?, None => return Err("No proof of work provided".into()), @@ -78,7 +70,7 @@ impl TryFrom for BlockHeader { version: u16::try_from(header.version).map_err(|_| "header version too large")?, height: header.height, prev_hash: FixedHash::try_from(header.prev_hash).map_err(|err| err.to_string())?, - timestamp, + timestamp: EpochTime::from(header.timestamp), input_mr: FixedHash::try_from(header.input_mr).map_err(|err| err.to_string())?, output_mr: FixedHash::try_from(header.output_mr).map_err(|err| err.to_string())?, output_mmr_size: header.output_mmr_size, diff --git a/applications/minotari_app_grpc/src/conversions/mod.rs b/applications/minotari_app_grpc/src/conversions/mod.rs index 45a6bbfe32..b716c253db 100644 --- a/applications/minotari_app_grpc/src/conversions/mod.rs +++ b/applications/minotari_app_grpc/src/conversions/mod.rs @@ -41,11 +41,7 @@ mod transaction_kernel; mod transaction_output; mod unblinded_output; -use std::convert::TryFrom; - -use chrono::Utc; use prost_types::Timestamp; -use tari_utilities::epoch_time::EpochTime; pub use self::{ aggregate_body::*, @@ -68,13 +64,6 @@ pub use self::{ }; use crate::{tari_rpc as grpc, tari_rpc::BlockGroupRequest}; -/// Utility function that converts a `EpochTime` to a `prost::Timestamp` -/// Returns None if the EpochTime is negative or larger than i64::MAX. -pub(self) fn datetime_to_timestamp(datetime: EpochTime) -> Option { - let seconds = i64::try_from(datetime.as_u64()).ok()?; - Some(Timestamp { seconds, nanos: 0 }) -} - /// Utility function that converts a `chrono::NaiveDateTime` to a `prost::Timestamp` pub fn naive_datetime_to_timestamp(datetime: chrono::NaiveDateTime) -> Timestamp { Timestamp { @@ -83,20 +72,6 @@ pub fn naive_datetime_to_timestamp(datetime: chrono::NaiveDateTime) -> Timestamp } } -/// Converts a protobuf Timestamp to an EpochTime. -/// Returns None if the timestamp is negative. -pub(self) fn timestamp_to_datetime(timestamp: Timestamp) -> Option { - u64::try_from(timestamp.seconds).ok().map(Into::into) -} - -/// Current unix time as timestamp (seconds part only) -pub fn timestamp() -> Timestamp { - Timestamp { - seconds: Utc::now().timestamp(), - nanos: 0, - } -} - impl From for grpc::IntegerValue { fn from(value: u64) -> Self { Self { value } diff --git a/applications/minotari_merge_mining_proxy/src/proxy.rs b/applications/minotari_merge_mining_proxy/src/proxy.rs index 9f4c422afc..af8d538908 100644 --- a/applications/minotari_merge_mining_proxy/src/proxy.rs +++ b/applications/minotari_merge_mining_proxy/src/proxy.rs @@ -941,7 +941,7 @@ fn try_into_json_block_header(header: grpc::BlockHeaderResponse) -> Result for BlockHeader { let total_script_offset = PrivateKey::from_bytes(&header.total_script_offset).map_err(|err| err.to_string())?; - let timestamp = header - .timestamp - .and_then(timestamp_to_datetime) - .ok_or_else(|| "timestamp not provided or is negative".to_string())?; - let pow = match header.pow { Some(p) => ProofOfWork::try_from(p)?, None => return Err("No proof of work provided".into()), @@ -55,7 +49,7 @@ impl TryFrom for BlockHeader { version: u16::try_from(header.version).map_err(|err| err.to_string())?, height: header.height, prev_hash: FixedHash::try_from(header.prev_hash).map_err(|err| err.to_string())?, - timestamp, + timestamp: EpochTime::from(header.timestamp), output_mr: FixedHash::try_from(header.output_mr).map_err(|err| err.to_string())?, output_mmr_size: header.output_mmr_size, kernel_mr: FixedHash::try_from(header.kernel_mr).map_err(|err| err.to_string())?, @@ -72,12 +66,11 @@ impl TryFrom for BlockHeader { impl From for proto::BlockHeader { fn from(header: BlockHeader) -> Self { - let timestamp = datetime_to_timestamp(header.timestamp).unwrap(); Self { version: u32::try_from(header.version).unwrap(), height: header.height, prev_hash: header.prev_hash.to_vec(), - timestamp: Some(timestamp), + timestamp: header.timestamp.as_u64(), output_mr: header.output_mr.to_vec(), kernel_mr: header.kernel_mr.to_vec(), input_mr: header.input_mr.to_vec(), diff --git a/base_layer/core/src/proto/mod.rs b/base_layer/core/src/proto/mod.rs index d78b990bb6..d15c96deec 100644 --- a/base_layer/core/src/proto/mod.rs +++ b/base_layer/core/src/proto/mod.rs @@ -53,5 +53,3 @@ mod block; mod block_header; #[cfg(any(feature = "base_node", feature = "base_node_proto"))] mod sidechain_feature; -#[cfg(any(feature = "base_node", feature = "base_node_proto"))] -mod utils; diff --git a/base_layer/core/src/proto/utils.rs b/base_layer/core/src/proto/utils.rs deleted file mode 100644 index 0033681318..0000000000 --- a/base_layer/core/src/proto/utils.rs +++ /dev/null @@ -1,38 +0,0 @@ -// 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 std::convert::TryFrom; - -use prost_types::Timestamp; -use tari_utilities::epoch_time::EpochTime; - -/// Utility function that converts a `prost::Timestamp` to a `chrono::DateTime` -/// Returns None if the timestamp is negative -pub(super) fn timestamp_to_datetime(timestamp: Timestamp) -> Option { - u64::try_from(timestamp.seconds).ok().map(Into::into) -} - -/// Utility function that converts a `chrono::DateTime` to a `prost::Timestamp` -pub(super) fn datetime_to_timestamp(datetime: EpochTime) -> Option { - let seconds = i64::try_from(datetime.as_u64()).ok()?; - Some(Timestamp { seconds, nanos: 0 }) -}