Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test ibc e2e fix for #3389 #3420

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Our `DateTimeUtc` type allowed a relaxed representation of RFC3339 strings.
We now enforce a string subset of this format, to guarantee deterministic
serialization. ([\#3389](https://github.com/anoma/namada/pull/3389))
1 change: 1 addition & 0 deletions Cargo.lock

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

10 changes: 4 additions & 6 deletions crates/apps_lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3475,12 +3475,10 @@ pub mod args {

fn def(app: App) -> App {
app.arg(NAMADA_START_TIME.def().help(wrap!(
"The start time of the ledger. Accepts a relaxed form of \
RFC3339. A space or a 'T' are accepted as the separator \
between the date and time components. Additional spaces are \
allowed between each component.\nAll of these examples are \
equivalent:\n2023-01-20T12:12:12Z\n2023-01-20 \
12:12:12Z\n2023- 01-20T12: 12:12Z"
"The start time of the ledger. Accepts a strict subset of \
RFC3339. A 'T' is accepted as the separator between the date \
and time components.\nHere is a valid timestamp: \
2023-01-20T12:12:12Z"
)))
.arg(
PATH_OPT
Expand Down
5 changes: 3 additions & 2 deletions crates/apps_lib/src/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,8 @@ pub struct Metadata<ID> {
mod test {
use std::path::PathBuf;

use namada::core::time::test_utils::GENESIS_TIME;

use super::*;

/// Test that the [`finalize`] returns deterministic output with the same
Expand All @@ -868,8 +870,7 @@ mod test {
let chain_id_prefix: ChainIdPrefix =
FromStr::from_str("test-prefix").unwrap();

let genesis_time =
DateTimeUtc::from_str("2021-12-31T00:00:00Z").unwrap();
let genesis_time = DateTimeUtc::from_str(GENESIS_TIME).unwrap();

let consensus_timeout_commit =
crate::facade::tendermint::Timeout::from_str("1s").unwrap();
Expand Down
138 changes: 122 additions & 16 deletions crates/core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ impl Display for DateTimeUtc {
}

impl DateTimeUtc {
const FORMAT: &'static str = "%Y-%m-%dT%H:%M:%S%.9f+00:00";

/// Returns a DateTimeUtc which corresponds to the current date.
pub fn now() -> Self {
Self(
Expand Down Expand Up @@ -183,7 +185,19 @@ impl DateTimeUtc {

/// Returns an rfc3339 string or an error.
pub fn to_rfc3339(&self) -> String {
chrono::DateTime::to_rfc3339(&self.0)
self.0.format(DateTimeUtc::FORMAT).to_string()
}

/// Parses a rfc3339 string, or returns an error.
pub fn from_rfc3339(s: &str) -> Result<Self, ParseError> {
use chrono::format;
use chrono::format::strftime::StrftimeItems;

let format = StrftimeItems::new(Self::FORMAT);
let mut parsed = format::Parsed::new();
format::parse(&mut parsed, s, format)?;

parsed.to_datetime_with_timezone(&chrono::Utc).map(Self)
}

/// Returns the DateTimeUtc corresponding to one second in the future
Expand All @@ -196,8 +210,9 @@ impl DateTimeUtc {
impl FromStr for DateTimeUtc {
type Err = ParseError;

#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.parse::<DateTime<Utc>>()?))
Self::from_rfc3339(s)
}
}

Expand Down Expand Up @@ -238,7 +253,7 @@ impl BorshSerialize for DateTimeUtc {
&self,
writer: &mut W,
) -> std::io::Result<()> {
let raw = self.0.to_rfc3339();
let raw = self.to_rfc3339();
BorshSerialize::serialize(&raw, writer)
}
}
Expand All @@ -247,9 +262,8 @@ impl BorshDeserialize for DateTimeUtc {
fn deserialize_reader<R: Read>(reader: &mut R) -> std::io::Result<Self> {
use std::io::{Error, ErrorKind};
let raw: String = BorshDeserialize::deserialize_reader(reader)?;
let actual = DateTime::parse_from_rfc3339(&raw)
.map_err(|err| Error::new(ErrorKind::InvalidData, err))?;
Ok(Self(actual.into()))
Self::from_rfc3339(&raw)
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
}
}

Expand Down Expand Up @@ -279,13 +293,35 @@ impl From<DateTime<Utc>> for DateTimeUtc {
}

impl TryFrom<prost_types::Timestamp> for DateTimeUtc {
type Error = prost_types::TimestampError;
type Error = String;

fn try_from(
timestamp: prost_types::Timestamp,
) -> Result<Self, Self::Error> {
let system_time: std::time::SystemTime = timestamp.try_into()?;
Ok(Self(system_time.into()))
let seconds = timestamp.seconds;
let nanoseconds: u32 = timestamp.nanos.try_into().map_err(|err| {
format!(
"Failed to convert prost_types::Timestamp nanos {} to u32: \
{err}",
timestamp.nanos
)
})?;

let parsed = {
use chrono::format;

let mut p = format::Parsed::new();

p.nanosecond = Some(nanoseconds);
p.timestamp = Some(seconds);
p
};

let dt = parsed.to_datetime_with_timezone(&Utc).map_err(|err| {
format!("Failed to parse {parsed:?} as a chrono::DateTime: {err}")
})?;

Ok(Self(dt))
}
}

Expand All @@ -302,7 +338,7 @@ impl From<DateTimeUtc> for prost_types::Timestamp {
impl TryFrom<crate::tendermint_proto::google::protobuf::Timestamp>
for DateTimeUtc
{
type Error = prost_types::TimestampError;
type Error = String;

fn try_from(
timestamp: crate::tendermint_proto::google::protobuf::Timestamp,
Expand All @@ -324,30 +360,29 @@ impl TryFrom<Rfc3339String> for DateTimeUtc {
type Error = chrono::ParseError;

fn try_from(str: Rfc3339String) -> Result<Self, Self::Error> {
let utc = DateTime::parse_from_rfc3339(&str.0)?;
Ok(Self(utc.into()))
Self::from_rfc3339(&str.0)
}
}

impl From<DateTimeUtc> for Rfc3339String {
fn from(dt: DateTimeUtc) -> Self {
Self(DateTime::to_rfc3339(&dt.0))
Self(dt.to_rfc3339())
}
}

impl TryFrom<DateTimeUtc> for crate::tendermint::time::Time {
type Error = crate::tendermint::Error;

fn try_from(dt: DateTimeUtc) -> Result<Self, Self::Error> {
Self::parse_from_rfc3339(&DateTime::to_rfc3339(&dt.0))
Self::parse_from_rfc3339(&dt.to_rfc3339())
}
}

impl TryFrom<crate::tendermint::time::Time> for DateTimeUtc {
type Error = chrono::ParseError;
type Error = String;

fn try_from(t: crate::tendermint::time::Time) -> Result<Self, Self::Error> {
Rfc3339String(t.to_rfc3339()).try_into()
crate::tendermint_proto::google::protobuf::Timestamp::from(t).try_into()
}
}

Expand All @@ -362,3 +397,74 @@ impl From<DurationNanos> for crate::tendermint::Timeout {
Self::from(std::time::Duration::from(val))
}
}

#[cfg(any(test, feature = "testing"))]
pub mod test_utils {
//! Time related test utilities.

/// Genesis time used during tests.
pub const GENESIS_TIME: &str = "2023-08-30T00:00:00.000000000+00:00";
}

#[cfg(test)]
mod core_time_tests {
use proptest::prelude::*;

use super::*;

proptest! {
#[test]
fn test_valid_reverse_datetime_utc_encoding_roundtrip(
year in 1974..=3_000,
month in 1..=12,
day in 1..=28,
hour in 0..=23,
min in 0..=59,
sec in 0..=59,
nanos in 0..=999_999_999,
)
{
let timestamp = format!("{year:04}-{month:02}-{day:02}T{hour:02}:{min:02}:{sec:02}.{nanos:09}+00:00");
println!("Testing timestamp: {timestamp}");
test_valid_reverse_datetime_utc_encoding_roundtrip_inner(&timestamp);
}
}

fn test_valid_reverse_datetime_utc_encoding_roundtrip_inner(
timestamp: &str,
) {
// we should be able to parse our custom datetime
let datetime = DateTimeUtc::from_rfc3339(timestamp).unwrap();

// the chrono datetime, which uses a superset of
// our datetime format should also be parsable
let datetime_inner = DateTime::parse_from_rfc3339(timestamp)
.unwrap()
.with_timezone(&Utc);
assert_eq!(datetime, DateTimeUtc(datetime_inner));

let encoded = datetime.to_rfc3339();

assert_eq!(encoded, timestamp);
}

#[test]
fn test_invalid_datetime_utc_encoding() {
// NB: this is a valid rfc3339 string, but we enforce
// a subset of the format to get deterministic encoding
// results
const TIMESTAMP: &str = "1966-03-03T00:06:56.520Z";
// const TIMESTAMP: &str = "1966-03-03T00:06:56.520+00:00";

// this is a valid rfc3339 string
assert!(DateTime::parse_from_rfc3339(TIMESTAMP).is_ok());

// but it cannot be parsed as a `DateTimeUtc`
assert!(DateTimeUtc::from_rfc3339(TIMESTAMP).is_err());
}

#[test]
fn test_valid_test_utils_genesis_time() {
assert!(DateTimeUtc::from_rfc3339(test_utils::GENESIS_TIME).is_ok());
}
}
5 changes: 3 additions & 2 deletions crates/namada/src/ledger/native_vp/ibc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,11 @@ fn match_value(
/// A dummy header used for testing
#[cfg(any(test, feature = "testing", feature = "benches"))]
pub fn get_dummy_header() -> crate::storage::Header {
use crate::tendermint::time::Time as TmTime;
use crate::time::{DateTimeUtc, DurationSecs};
crate::storage::Header {
hash: crate::hash::Hash([0; 32]),
time: TmTime::now().try_into().unwrap(),
#[allow(clippy::disallowed_methods, clippy::arithmetic_side_effects)]
time: DateTimeUtc::now() + DurationSecs(5),
next_validators_hash: crate::hash::Hash([0; 32]),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ mod test_finalize_block {
FinalizeBlock, ProcessedTx,
};

const WRAPPER_GAS_LIMIT: u64 = 10_000;
const WRAPPER_GAS_LIMIT: u64 = 11_000;
const STORAGE_VALUE: &str = "test_value";

/// Make a wrapper tx and a processed tx from the wrapped tx that can be
Expand Down
2 changes: 1 addition & 1 deletion crates/tests/fixtures/txs.json

Large diffs are not rendered by default.

Loading
Loading