Skip to content

Commit

Permalink
fix: handle large years (#9032)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 4, 2024
1 parent d3ce9f0 commit eb04665
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ use anvil_core::eth::{
utils::meets_eip155,
};
use anvil_rpc::error::RpcError;
use chrono::Datelike;
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use foundry_evm::{
backend::{DatabaseError, DatabaseResult, RevertStateSnapshotAction},
Expand Down Expand Up @@ -1123,7 +1124,12 @@ impl Backend {

node_info!(" Block Number: {}", block_number);
node_info!(" Block Hash: {:?}", block_hash);
node_info!(" Block Time: {:?}\n", timestamp.to_rfc2822());
if timestamp.year() > 9999 {
// rf2822 panics with more than 4 digits
node_info!(" Block Time: {:?}\n", timestamp.to_rfc3339());
} else {
node_info!(" Block Time: {:?}\n", timestamp.to_rfc2822());
}

let outcome = MinedBlockOutcome { block_number, included, invalid };

Expand Down
12 changes: 12 additions & 0 deletions crates/anvil/tests/it/anvil.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! tests for anvil specific logic

use alloy_eips::BlockNumberOrTag;
use alloy_primitives::Address;
use alloy_provider::Provider;
use anvil::{spawn, NodeConfig};
Expand Down Expand Up @@ -76,3 +77,14 @@ async fn test_can_use_default_genesis_timestamp() {
provider.get_block(0.into(), false.into()).await.unwrap().unwrap().header.timestamp
);
}

#[tokio::test(flavor = "multi_thread")]
async fn test_can_handle_large_timestamp() {
let (api, _handle) = spawn(NodeConfig::test()).await;
let num = 317071597274;
api.evm_set_next_block_timestamp(num).unwrap();
api.mine_one().await;

let block = api.block_by_number(BlockNumberOrTag::Latest).await.unwrap().unwrap();
assert_eq!(block.header.timestamp, num);
}

0 comments on commit eb04665

Please sign in to comment.