hiero-ledger / hiero-ledger/hiero-consensus-node

`block.timestamp` Returns Incorrect Value

Open
#22,530 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
407
Forks
226
Avg merge
3d 4h
Merged PRs (30d)
210

Description

## Summary
The EVM opcode block.timestamp returns the timestamp of a previous block instead of the current block's `firstConsTimeOfCurrentBlock`. This causes smart contracts to see an incorrect (earlier) timestamp, which can affect time-sensitive DeFi protocols, auctions, and other time-dependent contract logic.

## Environment
- Networks affected: Testnet (confirmed), Mainnet (reported)

### Reproduction Steps
1. Deploy a simple contract that captures `block.timestamp`:
```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract BlockTimestampTest {
event TimestampCaptured(uint256 blockTimestamp, uint256 blockNumber);

function captureTimestamp() external returns (uint256) {
emit TimestampCaptured(block.timestamp, block.number);
return block.timestamp;
}
}
```
2. Call `captureTimestamp()` when your transaction is not the first transaction in the block
3. Compare the emitted `block.timestamp` with the block's actual timestamp from Mirror Node API

## Evidence
### Test Case 1
Block info from Mirror Node:
```
{
"number": 28624019,
"timestamp": {
"from": "1765294936.048991000",
"to": "1765294937.954070000"
}
}
```
First transaction in block:
- Consensus timestamp: `1765294936.048991000`
- Type: `CONSENSUSSUBMITMESSAGE (non-EVM)`

Contract's block.timestamp: `1765294935`

| Expected | Actual | Difference |
|------------|------------|------------|
| 1765294936 | 1765294935 | -1 second |

### Test Case 2
Block info from Mirror Node:
```
{
"number": 28625089,
"timestamp": {
"from": "1765297076.004691853",
"to": "1765297077.941060000"
}
}
```
Contract's block.timestamp: 1765297074

| Expected | Actual | Difference |
|------------|------------|------------|
| 1765297076 | 1765297074 | -2 seconds |

## Expected Behavior
`block.timestamp` should return `firstConsTimeOfCurrentBlock.seconds()` where `firstConsTimeOfCurrentBlock` is the consensus timestamp of the first transaction in the current block.

For block 28624019:
- First transaction: 1765294936.048991000
- Expected block.timestamp: 1765294936

## Actual Behavior

`block.timestamp` returns the `firstConsTimeOfCurrentBlock` from a previous block, resulting in a timestamp 1-2 seconds earlier than expected.

## Additional Context

- The issue is reproducible and affects both testnet and mainnet
- The discrepancy is not consistent (sometimes 1 second, sometimes 2 seconds behind)

Contributor guide

Open the contributing guide

Research direction

Start by tracing the EVM implementation of block.timestamp and its use of firstConsTimeOfCurrentBlock, then compare that value with the Mirror Node API data for the supplied block numbers. Done means the opcode returns the current block's first consensus timestamp for transactions that are not first in the block, matching the reproduction contract's expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, solidity
Domain
blockchain
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.