From 36939137d4d3bd17ed959304b27a5d84ddee64cb Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Fri, 12 Jan 2024 16:43:58 +0100 Subject: [PATCH 1/3] Expect a proposed block to always carry a valid timestamp --- apps/src/lib/node/ledger/shell/mod.rs | 26 ++++--------------- .../lib/node/ledger/shell/process_proposal.rs | 5 +++- 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/apps/src/lib/node/ledger/shell/mod.rs b/apps/src/lib/node/ledger/shell/mod.rs index e8d5da8b83..3150faa6de 100644 --- a/apps/src/lib/node/ledger/shell/mod.rs +++ b/apps/src/lib/node/ledger/shell/mod.rs @@ -79,7 +79,6 @@ use crate::config::{self, genesis, TendermintMode, ValidatorLocalConfig}; use crate::facade::tendermint::abci::types::{Misbehavior, MisbehaviorKind}; use crate::facade::tendermint::v0_37::abci::{request, response}; use crate::facade::tendermint::{self, validator}; -use crate::facade::tendermint_proto::google::protobuf::Timestamp; use crate::facade::tendermint_proto::v0_37::crypto::public_key; use crate::node::ledger::shims::abcipp_shim_types::shim; use crate::node::ledger::shims::abcipp_shim_types::shim::response::TxResult; @@ -582,25 +581,6 @@ where response } - /// Takes the optional tendermint timestamp of the block: if it's Some than - /// converts it to a [`DateTimeUtc`], otherwise retrieve from self the - /// time of the last block committed - pub fn get_block_timestamp( - &self, - tendermint_block_time: Option, - ) -> DateTimeUtc { - if let Some(t) = tendermint_block_time { - if let Ok(t) = t.try_into() { - return t; - } - } - // Default to last committed block time - self.wl_storage - .storage - .get_last_block_timestamp() - .expect("Failed to retrieve last block timestamp") - } - /// Read the value for a storage key dropping any error pub fn read_storage_key(&self, key: &Key) -> Option where @@ -1095,7 +1075,11 @@ where // Tx expiration if let Some(exp) = tx.header.expiration { - let last_block_timestamp = self.get_block_timestamp(None); + let last_block_timestamp = self + .wl_storage + .storage + .get_last_block_timestamp() + .expect("Failed to retrieve last block timestamp"); if last_block_timestamp > exp { response.code = ResultCode::ExpiredTx.into(); diff --git a/apps/src/lib/node/ledger/shell/process_proposal.rs b/apps/src/lib/node/ledger/shell/process_proposal.rs index 5677e67fc0..3c16be44b4 100644 --- a/apps/src/lib/node/ledger/shell/process_proposal.rs +++ b/apps/src/lib/node/ledger/shell/process_proposal.rs @@ -104,7 +104,10 @@ where let (tx_results, meta) = self.process_txs( &req.txs, - self.get_block_timestamp(req.time), + req.time + .expect("Missing timestamp in proposed block") + .try_into() + .expect("Failed conversion of Comet timestamp"), &native_block_proposer_address, ); From dfa0870a6753ab632356138191adb90fb980444a Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Fri, 12 Jan 2024 16:50:25 +0100 Subject: [PATCH 2/3] Changelog #2383 --- .changelog/unreleased/improvements/2383-comet-timestamp.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/2383-comet-timestamp.md diff --git a/.changelog/unreleased/improvements/2383-comet-timestamp.md b/.changelog/unreleased/improvements/2383-comet-timestamp.md new file mode 100644 index 0000000000..2ed42d717d --- /dev/null +++ b/.changelog/unreleased/improvements/2383-comet-timestamp.md @@ -0,0 +1,2 @@ +- Updated block validation to require a valid timestamp. + ([\#2383](https://github.com/anoma/namada/pull/2383)) \ No newline at end of file From 6c7081bb9dc6a873237a0eb3cdd5b4e998e7a0e8 Mon Sep 17 00:00:00 2001 From: Marco Granelli Date: Sat, 13 Jan 2024 00:17:08 +0100 Subject: [PATCH 3/3] Adds block timestamp in unit and integration tests --- apps/src/lib/node/ledger/shell/mod.rs | 6 ++++++ apps/src/lib/node/ledger/shell/testing/node.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/apps/src/lib/node/ledger/shell/mod.rs b/apps/src/lib/node/ledger/shell/mod.rs index 3150faa6de..6117528bb9 100644 --- a/apps/src/lib/node/ledger/shell/mod.rs +++ b/apps/src/lib/node/ledger/shell/mod.rs @@ -1763,6 +1763,7 @@ mod test_utils { &self, req: ProcessProposal, ) -> std::result::Result, TestError> { + let time = DateTimeUtc::now(); let (resp, tx_results) = self.shell.process_proposal(RequestProcessProposal { txs: req @@ -1780,6 +1781,11 @@ mod test_utils { ) .unwrap() .into(), + time: Some(Timestamp { + seconds: time.0.timestamp(), + nanos: time.0.timestamp_subsec_nanos() as i32, + }), + ..Default::default() }); let results = tx_results diff --git a/apps/src/lib/node/ledger/shell/testing/node.rs b/apps/src/lib/node/ledger/shell/testing/node.rs index b3ef78027e..de089a006e 100644 --- a/apps/src/lib/node/ledger/shell/testing/node.rs +++ b/apps/src/lib/node/ledger/shell/testing/node.rs @@ -36,6 +36,7 @@ use namada::types::key::tm_consensus_key_raw_hash; use namada::types::storage::{BlockHash, BlockHeight, Epoch, Header}; use namada::types::time::DateTimeUtc; use namada_sdk::queries::Client; +use namada_sdk::tendermint_proto::google::protobuf::Timestamp; use regex::Regex; use tokio::sync::mpsc; @@ -459,9 +460,14 @@ impl MockNode { self.advance_to_allowed_block(); let (proposer_address, votes) = self.prepare_request(); + let time = DateTimeUtc::now(); let req = RequestProcessProposal { txs: txs.clone().into_iter().map(|tx| tx.into()).collect(), proposer_address: proposer_address.clone().into(), + time: Some(Timestamp { + seconds: time.0.timestamp(), + nanos: time.0.timestamp_subsec_nanos() as i32, + }), ..Default::default() }; let mut locked = self.shell.lock().unwrap();