Skip to content

Fix #3974: Add chain-specific block time support - #4006

Merged
tjayrush merged 3 commits into
TrueBlocks:developfrom
wakamex:fix/issue-3974-chain-specific-block-times
Jul 18, 2025
Merged

Fix #3974: Add chain-specific block time support#4006
tjayrush merged 3 commits into
TrueBlocks:developfrom
wakamex:fix/issue-3974-chain-specific-block-times

Conversation

@wakamex

@wakamex wakamex commented Jun 22, 2025

Copy link
Copy Markdown
Contributor

Summary

  • Fixes incorrect timestamp calculations for non-Ethereum chains (e.g., Optimism showing 2023 dates instead of 2024)
  • Adds configurable block times for accurate timestamp-to-block conversions across different chains

Problem

TrueBlocks was using hardcoded Ethereum mainnet block times (13 seconds) for all chains. This caused incorrect timestamp calculations for chains with different block times:

  • Optimism: 2-second blocks calculated as 13-second blocks
  • Polygon: 2-second blocks calculated as 13-second blocks
  • Gnosis: 5-second blocks calculated as 13-second blocks

Example from issue #3974:

chifra when 2024-01-01 2025-01-01 --chain optimism
# Returned blocks from 2023 instead of 2024-2025

Solution

  1. Added BlockTime field to ChainGroup configuration structure
  2. Created GetBlockTime() helper function with defaults for 25+ chains
  3. Updated hardcoded values in:
    • pkg/tslib/tsdb.go: Line 100
    • pkg/identifiers/resolve.go: Line 201
  4. Added block times to configuration for existing chains

Changes

  • ✅ Add BlockTime field to ChainGroup struct
  • ✅ Create blocktime.go with chain defaults
  • ✅ Update timestamp calculations to use GetBlockTime()
  • ✅ Add block times to trueBlocks.toml
  • ✅ Add comprehensive tests
  • ✅ Add documentation

Testing

go test ./pkg/config/blocktime_test.go

Migration

No action required. System uses sensible defaults if blockTime not configured.

Fixes #3974

🤖 Generated with Claude Code

Previously, TrueBlocks used hardcoded Ethereum mainnet block times (13s) for all chains,
causing incorrect timestamp calculations for chains like Optimism (2s blocks).

Changes:
- Add BlockTime field to ChainGroup configuration structure
- Create GetBlockTime() helper that uses chain-specific values
- Update timestamp calculations in tsdb.go and resolve.go
- Add default block times for 25+ known chains
- Update config with block times for existing chains
- Add tests for multi-chain block time calculations

This ensures accurate timestamp-to-block conversions for all supported chains.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@wakamex
wakamex changed the base branch from master to develop June 22, 2025 01:56
@wakamex

wakamex commented Jun 22, 2025

Copy link
Copy Markdown
Contributor Author

this is a bigger change, so I wouldn't be surprised if something is wrong here

The test was expecting rounding behavior, but the actual implementation uses truncation when converting float to integer. Updated test to match actual behavior: 3600/13.0 = 276.92 truncates to 276, not 277.
@tjayrush

Copy link
Copy Markdown
Member

The primary reason why I never fixed this issue was because I never wanted to have a list of "known chains" in our code. This kills the long tail and is unmaintainable over the long term as (a) new chains become promenent, and (b) old chains change their block times. I recognize the issue (there's probably comments in the code saying so), but I'd much prefer this to be in a configuration file than a source code file. My real preference would be a file in each chain's ~/Library/Application Support/TrueBlocks/config//<chain_data.csv> or something similar (different for Linux). In this way, in any frontend apps (which we're building now) we can edit a config file through a frontend form, for example, if we want to allow end users to add their own chains.

I truly do thank you for the contribution, but I can't merge this as it is.

@tjayrush

Copy link
Copy Markdown
Member

Oh sorry. Just reviewing this a second time. I see that you have the ability to put the block time in the config file (per chain) and then have some fallback values for known chains. Let me review this one more time...I'll get back to you.

@tjayrush
tjayrush merged commit 1e26a72 into TrueBlocks:develop Jul 18, 2025
2 of 3 checks passed
@tjayrush

Copy link
Copy Markdown
Member

Merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timestamps on optimism are wrong

2 participants