Skip to content

Commit

Permalink
Merge branch 'origin/grarco/comet-timestamp' (#2383)
Browse files Browse the repository at this point in the history
* origin/grarco/comet-timestamp:
  Adds block timestamp in unit and integration tests
  Changelog #2383
  Expect a proposed block to always carry a valid timestamp
  • Loading branch information
tzemanovic committed Jan 17, 2024
2 parents 9e7153d + 6c7081b commit cc5e853
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/improvements/2383-comet-timestamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Updated block validation to require a valid timestamp.
([\#2383](https://github.com/anoma/namada/pull/2383))
32 changes: 11 additions & 21 deletions crates/apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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;
Expand Down Expand Up @@ -585,25 +584,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<Timestamp>,
) -> 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<T>(&self, key: &Key) -> Option<T>
where
Expand Down Expand Up @@ -1099,7 +1079,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();
Expand Down Expand Up @@ -1799,6 +1783,7 @@ mod test_utils {
&self,
req: ProcessProposal,
) -> std::result::Result<Vec<ProcessedTx>, TestError> {
let time = DateTimeUtc::now();
let (resp, tx_results) =
self.shell.process_proposal(RequestProcessProposal {
txs: req
Expand All @@ -1816,6 +1801,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
Expand Down
5 changes: 4 additions & 1 deletion crates/apps/src/lib/node/ledger/shell/process_proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand Down
6 changes: 6 additions & 0 deletions crates/apps/src/lib/node/ledger/shell/testing/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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 namada_sdk::tx::data::ResultCode;
use regex::Regex;
use tendermint_rpc::endpoint::block;
Expand Down Expand Up @@ -506,9 +507,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();
Expand Down

0 comments on commit cc5e853

Please sign in to comment.