Skip to content

Commit

Permalink
fix!: remove timestamp from header in proto files (#5667)
Browse files Browse the repository at this point in the history
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.

<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [ ] None
- [ ] Requires data directory on base node to be deleted
- [x] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
BREAKING CHANGE: Previous BlockHeader protobuf messages are incompatible
  • Loading branch information
stringhandler authored Sep 4, 2023
1 parent c91a35f commit 403b0c6
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 97 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions applications/minotari_app_grpc/proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
16 changes: 4 additions & 12 deletions applications/minotari_app_grpc/src/conversions/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockHeader> for grpc::BlockHeader {
fn from(h: BlockHeader) -> Self {
Expand All @@ -39,7 +36,7 @@ impl From<BlockHeader> 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,
Expand All @@ -65,11 +62,6 @@ impl TryFrom<grpc::BlockHeader> 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()),
Expand All @@ -78,7 +70,7 @@ impl TryFrom<grpc::BlockHeader> 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,
Expand Down
25 changes: 0 additions & 25 deletions applications/minotari_app_grpc/src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand All @@ -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<Timestamp> {
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 {
Expand All @@ -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<EpochTime> {
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<u64> for grpc::IntegerValue {
fn from(value: u64) -> Self {
Self { value }
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_merge_mining_proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ fn try_into_json_block_header(header: grpc::BlockHeaderResponse) -> Result<json:
"orphan_status": false,
"prev_hash": header.prev_hash.to_hex(),
"reward": reward,
"timestamp": header.timestamp.map(|ts| ts.seconds.into()).unwrap_or_else(|| json!(null)),
"timestamp": header.timestamp
}))
}

Expand Down
6 changes: 3 additions & 3 deletions applications/minotari_miner/src/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ use std::{
time::{Duration, Instant},
};

use chrono::Utc;
use crossbeam::channel::{bounded, Select, Sender, TrySendError};
use futures::Stream;
use log::*;
use minotari_app_grpc::{conversions::timestamp, tari_rpc::BlockHeader};
use minotari_app_grpc::tari_rpc::BlockHeader;
use thread::JoinHandle;

use super::difficulty::BlockHeaderSha3;
Expand Down Expand Up @@ -246,8 +247,7 @@ pub fn mining_task(
return;
}
if !(share_mode) {
#[allow(clippy::cast_sign_loss)]
hasher.set_forward_timestamp(timestamp().seconds as u64);
hasher.set_forward_timestamp(Utc::now().timestamp() as u64);
}
}
hasher.inc_nonce();
Expand Down
1 change: 0 additions & 1 deletion base_layer/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ num-derive = "0.3.3"
num-format = "0.4.0"
once_cell = "1.8.0"
prost = "0.9"
prost-types = "0.9"
rand = "0.8"
randomx-rs = { version = "1.2.1", optional = true }
serde = { version = "1.0.106", features = ["derive"] }
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/proto/block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
syntax = "proto3";

import "google/protobuf/wrappers.proto";
import "google/protobuf/timestamp.proto";
import "transaction.proto";
import "types.proto";

Expand All @@ -31,7 +30,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;
Expand Down
13 changes: 3 additions & 10 deletions base_layer/core/src/proto/block_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
use std::convert::TryFrom;

use tari_common_types::types::{FixedHash, PrivateKey};
use tari_utilities::ByteArray;
use tari_utilities::{epoch_time::EpochTime, ByteArray};

use super::core as proto;
use crate::{
blocks::BlockHeader,
proof_of_work::{PowAlgorithm, ProofOfWork},
proto::utils::{datetime_to_timestamp, timestamp_to_datetime},
};

//---------------------------------- BlockHeader --------------------------------------------//
Expand All @@ -42,11 +41,6 @@ impl TryFrom<proto::BlockHeader> 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()),
Expand All @@ -55,7 +49,7 @@ impl TryFrom<proto::BlockHeader> 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())?,
Expand All @@ -72,12 +66,11 @@ impl TryFrom<proto::BlockHeader> for BlockHeader {

impl From<BlockHeader> 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(),
Expand Down
2 changes: 0 additions & 2 deletions base_layer/core/src/proto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
38 changes: 0 additions & 38 deletions base_layer/core/src/proto/utils.rs

This file was deleted.

0 comments on commit 403b0c6

Please sign in to comment.