Skip to content

Commit

Permalink
Remove proto timestamp type for u64
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Oct 6, 2023
1 parent 65f564a commit 5ec7e51
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 124 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.

6 changes: 3 additions & 3 deletions applications/minotari_app_grpc/proto/network.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ message Peer {
/// Peer's addresses
repeated Address addresses = 3;
/// Last connection attempt to peer
google.protobuf.Timestamp last_connection = 4;
uint64 last_connection = 4;
/// Flags for the peer.
uint32 flags = 5;
google.protobuf.Timestamp banned_until= 6;
uint64 banned_until= 6;
string banned_reason= 7;
google.protobuf.Timestamp offline_at = 8;
uint64 offline_at = 8;
/// Features supported by the peer
uint32 features = 9;
/// used as information for more efficient protocol negotiation.
Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_app_grpc/proto/wallet.proto
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ message TransactionInfo {
uint64 fee = 7;
bool is_cancelled = 8;
bytes excess_sig = 9;
google.protobuf.Timestamp timestamp = 10;
uint64 timestamp = 10;
string message = 11;
}

Expand Down
12 changes: 8 additions & 4 deletions applications/minotari_app_grpc/src/conversions/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use tari_comms::{connectivity::ConnectivityStatus, net_address::MultiaddrWithStats, peer_manager::Peer};
use tari_utilities::ByteArray;

use crate::{conversions::naive_datetime_to_timestamp, tari_rpc as grpc};
use crate::tari_rpc as grpc;

#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
Expand All @@ -32,14 +32,18 @@ impl From<Peer> for grpc::Peer {
let public_key = peer.public_key.to_vec();
let node_id = peer.node_id.to_vec();
let mut addresses = Vec::with_capacity(peer.addresses.len());
let last_connection = peer.addresses.last_seen().map(naive_datetime_to_timestamp);
let last_connection = peer
.addresses
.last_seen()
.map(|f| f.timestamp() as u64)
.unwrap_or_default();
for address in peer.addresses.addresses() {
addresses.push(address.clone().into())
}
let flags = u32::from(peer.flags.bits());
let banned_until = peer.banned_until.map(naive_datetime_to_timestamp);
let banned_until = peer.banned_until.map(|f| f.timestamp() as u64).unwrap_or_default();
let banned_reason = peer.banned_reason.to_string();
let offline_at = peer.offline_at().map(naive_datetime_to_timestamp);
let offline_at = peer.offline_at().map(|f| f.timestamp() as u64).unwrap_or_default();
let features = peer.features.bits();

let supported_protocols = peer.supported_protocols.into_iter().map(|p| p.to_vec()).collect();
Expand Down
111 changes: 54 additions & 57 deletions applications/minotari_console_wallet/src/grpc/wallet_grpc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,56 @@ use futures::{
SinkExt,
};
use log::*;
use minotari_app_grpc::{
conversions::naive_datetime_to_timestamp,
tari_rpc::{
self,
payment_recipient::PaymentType,
wallet_server,
CheckConnectivityResponse,
ClaimHtlcRefundRequest,
ClaimHtlcRefundResponse,
ClaimShaAtomicSwapRequest,
ClaimShaAtomicSwapResponse,
CoinSplitRequest,
CoinSplitResponse,
CommitmentSignature,
CreateBurnTransactionRequest,
CreateBurnTransactionResponse,
CreateTemplateRegistrationRequest,
CreateTemplateRegistrationResponse,
GetAddressResponse,
GetBalanceRequest,
GetBalanceResponse,
GetCoinbaseRequest,
GetCoinbaseResponse,
GetCompletedTransactionsRequest,
GetCompletedTransactionsResponse,
GetConnectivityRequest,
GetIdentityRequest,
GetIdentityResponse,
GetTransactionInfoRequest,
GetTransactionInfoResponse,
GetUnspentAmountsResponse,
GetVersionRequest,
GetVersionResponse,
ImportUtxosRequest,
ImportUtxosResponse,
RegisterValidatorNodeRequest,
RegisterValidatorNodeResponse,
RevalidateRequest,
RevalidateResponse,
SendShaAtomicSwapRequest,
SendShaAtomicSwapResponse,
SetBaseNodeRequest,
SetBaseNodeResponse,
TransactionDirection,
TransactionEvent,
TransactionEventRequest,
TransactionEventResponse,
TransactionInfo,
TransactionStatus,
TransferRequest,
TransferResponse,
TransferResult,
},
use minotari_app_grpc::tari_rpc::{
self,
payment_recipient::PaymentType,
wallet_server,
CheckConnectivityResponse,
ClaimHtlcRefundRequest,
ClaimHtlcRefundResponse,
ClaimShaAtomicSwapRequest,
ClaimShaAtomicSwapResponse,
CoinSplitRequest,
CoinSplitResponse,
CommitmentSignature,
CreateBurnTransactionRequest,
CreateBurnTransactionResponse,
CreateTemplateRegistrationRequest,
CreateTemplateRegistrationResponse,
GetAddressResponse,
GetBalanceRequest,
GetBalanceResponse,
GetCoinbaseRequest,
GetCoinbaseResponse,
GetCompletedTransactionsRequest,
GetCompletedTransactionsResponse,
GetConnectivityRequest,
GetIdentityRequest,
GetIdentityResponse,
GetTransactionInfoRequest,
GetTransactionInfoResponse,
GetUnspentAmountsResponse,
GetVersionRequest,
GetVersionResponse,
ImportUtxosRequest,
ImportUtxosResponse,
RegisterValidatorNodeRequest,
RegisterValidatorNodeResponse,
RevalidateRequest,
RevalidateResponse,
SendShaAtomicSwapRequest,
SendShaAtomicSwapResponse,
SetBaseNodeRequest,
SetBaseNodeResponse,
TransactionDirection,
TransactionEvent,
TransactionEventRequest,
TransactionEventResponse,
TransactionInfo,
TransactionStatus,
TransferRequest,
TransferResponse,
TransferResult,
};
use minotari_wallet::{
connectivity_service::{OnlineStatus, WalletConnectivityInterface},
Expand Down Expand Up @@ -777,7 +774,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
is_cancelled: txn.cancelled.is_some(),
direction: TransactionDirection::from(txn.direction) as i32,
fee: txn.fee.into(),
timestamp: Some(naive_datetime_to_timestamp(txn.timestamp)),
timestamp: txn.timestamp.timestamp() as u64,
excess_sig: txn
.transaction
.first_kernel_excess_sig()
Expand Down Expand Up @@ -1113,7 +1110,7 @@ fn convert_wallet_transaction_into_transaction_info(
direction: TransactionDirection::Inbound as i32,
fee: 0,
excess_sig: Default::default(),
timestamp: Some(naive_datetime_to_timestamp(tx.timestamp)),
timestamp: tx.timestamp.timestamp() as u64,
message: tx.message,
},
PendingOutbound(tx) => TransactionInfo {
Expand All @@ -1126,7 +1123,7 @@ fn convert_wallet_transaction_into_transaction_info(
direction: TransactionDirection::Outbound as i32,
fee: tx.fee.into(),
excess_sig: Default::default(),
timestamp: Some(naive_datetime_to_timestamp(tx.timestamp)),
timestamp: tx.timestamp.timestamp() as u64,
message: tx.message,
},
Completed(tx) => TransactionInfo {
Expand All @@ -1138,7 +1135,7 @@ fn convert_wallet_transaction_into_transaction_info(
is_cancelled: tx.cancelled.is_some(),
direction: TransactionDirection::from(tx.direction) as i32,
fee: tx.fee.into(),
timestamp: Some(naive_datetime_to_timestamp(tx.timestamp)),
timestamp: tx.timestamp.timestamp() as u64,
excess_sig: tx
.transaction
.first_kernel_excess_sig()
Expand Down
1 change: 0 additions & 1 deletion comms/dht/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ futures = "^0.3.1"
log = "0.4.8"
log-mdc = "0.1.0"
prost = "=0.9.0"
prost-types = "=0.9.0"
rand = "0.8"
serde = "1.0.90"
thiserror = "1.0.26"
Expand Down
28 changes: 7 additions & 21 deletions comms/dht/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{
cmp,
convert::{TryFrom, TryInto},
fmt,
fmt::Display,
};

use bitflags::bitflags;
use chrono::{DateTime, NaiveDateTime, Utc};
use prost_types::Timestamp;
use serde::{Deserialize, Serialize};
use tari_comms::{message::MessageTag, peer_manager::NodeId, types::CommsPublicKey, NodeIdentity};
use tari_utilities::{epoch_time::EpochTime, ByteArray, ByteArrayError};
Expand All @@ -39,21 +37,6 @@ use thiserror::Error;
pub use crate::proto::envelope::{dht_header::Destination, DhtEnvelope, DhtHeader, DhtMessageType};
use crate::version::DhtProtocolVersion;

/// Utility function that converts a `chrono::DateTime<Utc>` to a `prost_type::Timestamp`
pub(crate) fn datetime_to_timestamp(datetime: DateTime<Utc>) -> Timestamp {
Timestamp {
seconds: datetime.timestamp(),
nanos: datetime.timestamp_subsec_nanos().try_into().unwrap_or(i32::MAX),
}
}

/// Utility function that converts a `prost::Timestamp` to a `chrono::DateTime<Utc>`
pub(crate) fn timestamp_to_datetime(timestamp: Timestamp) -> Option<DateTime<Utc>> {
let naive =
NaiveDateTime::from_timestamp_opt(timestamp.seconds, u32::try_from(cmp::max(0, timestamp.nanos)).unwrap())?;
Some(DateTime::from_utc(naive, Utc))
}

/// Utility function that converts a `chrono::DateTime` to a `EpochTime`
pub(crate) fn datetime_to_epochtime(datetime: DateTime<Utc>) -> EpochTime {
#[allow(clippy::cast_sign_loss)]
Expand Down Expand Up @@ -229,7 +212,11 @@ impl TryFrom<DhtHeader> for DhtMessageHeader {
)
};

let expires = header.expires.and_then(timestamp_to_datetime);
let expires = match header.expires {
0 => None,
t => Some(EpochTime::from_secs_since_epoch(t)),
};

let version = DhtProtocolVersion::try_from(header.major)?;

Ok(Self {
Expand All @@ -240,7 +227,7 @@ impl TryFrom<DhtHeader> for DhtMessageHeader {
message_type: DhtMessageType::from_i32(header.message_type).ok_or(DhtMessageError::InvalidMessageType)?,
flags: DhtMessageFlags::from_bits(header.flags).ok_or(DhtMessageError::InvalidMessageFlags)?,
message_tag: MessageTag::from(header.message_tag),
expires: expires.map(datetime_to_epochtime),
expires,
})
}
}
Expand All @@ -258,7 +245,6 @@ impl TryFrom<Option<DhtHeader>> for DhtMessageHeader {

impl From<DhtMessageHeader> for DhtHeader {
fn from(header: DhtMessageHeader) -> Self {
let expires = header.expires.map(epochtime_to_datetime);
Self {
major: header.version.as_major(),
ephemeral_public_key: header
Expand All @@ -271,7 +257,7 @@ impl From<DhtMessageHeader> for DhtHeader {
message_type: header.message_type as i32,
flags: header.flags.bits(),
message_tag: header.message_tag.as_value(),
expires: expires.map(datetime_to_timestamp),
expires: header.expires.map(EpochTime::as_u64).unwrap_or_default(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions comms/dht/src/outbound/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::{
crypt,
dedup,
discovery::DhtDiscoveryRequester,
envelope::{datetime_to_epochtime, datetime_to_timestamp, DhtMessageFlags, DhtMessageHeader, NodeDestination},
envelope::{datetime_to_epochtime, DhtMessageFlags, DhtMessageHeader, NodeDestination},
message_signature::MessageSignature,
outbound::{
message::{DhtOutboundMessage, OutboundEncryption, SendFailure},
Expand Down Expand Up @@ -447,7 +447,7 @@ where S: Service<DhtOutboundMessage, Response = (), Error = PipelineError>
ephemeral_public_key: ephemeral_public_key.clone(),
message_signature: message_signature.clone(),
is_broadcast,
expires: expires.map(datetime_to_timestamp),
expires: expires_epochtime.map(EpochTime::as_u64),
},
send_state,
)
Expand Down
2 changes: 1 addition & 1 deletion comms/dht/src/outbound/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub struct DhtOutboundMessage {
pub reply: MessagingReplyTx,
pub dht_flags: DhtMessageFlags,
pub is_broadcast: bool,
pub expires: Option<prost_types::Timestamp>,
pub expires: Option<u64>,
}

impl fmt::Display for DhtOutboundMessage {
Expand Down
2 changes: 1 addition & 1 deletion comms/dht/src/outbound/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ where
flags: dht_flags.bits(),
destination: Some(destination.into()),
message_tag: tag.as_value(),
expires,
expires: expires.unwrap_or_default(),
});
let envelope = DhtEnvelope::new(dht_header, body.into());

Expand Down
2 changes: 1 addition & 1 deletion comms/dht/src/proto/envelope.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ message DhtHeader {
// Message trace ID
uint64 message_tag = 11;
// Expiry timestamp for the message
google.protobuf.Timestamp expires = 12;
uint64 expires = 12;
}

message DhtEnvelope {
Expand Down
4 changes: 2 additions & 2 deletions comms/dht/src/proto/store_forward.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ package tari.dht.store_forward;
// start_time is provided then only messages after the specified time will be sent, otherwise all applicable messages
// will be sent.
message StoredMessagesRequest {
google.protobuf.Timestamp since = 1;
uint64 since = 1;
uint32 request_id = 2;
uint32 limit = 3;
}

// Storage for a single message envelope, including the date and time when the element was stored
message StoredMessage {
google.protobuf.Timestamp stored_at = 1;
uint64 stored_at = 1;
uint32 version = 2;
tari.dht.envelope.DhtHeader dht_header = 3;
bytes body = 4;
Expand Down
Loading

0 comments on commit 5ec7e51

Please sign in to comment.