Skip to content

refactor: introduce MegaDatabase trait to ensure DB and SaltEnv consistency#176

Draft
WonderLawrence wants to merge 2 commits intomainfrom
wanglang/refactor/megadatabase-trait
Draft

refactor: introduce MegaDatabase trait to ensure DB and SaltEnv consistency#176
WonderLawrence wants to merge 2 commits intomainfrom
wanglang/refactor/megadatabase-trait

Conversation

@WonderLawrence
Copy link

@WonderLawrence WonderLawrence commented Mar 1, 2026

Summary

This PR addresses a correctness issue where ExternalEnvs::salt_env could not guarantee it accessed the same database snapshot as the EVM's Database. The solution introduces a MegaDatabase trait that combines Database + SaltEnv with matching error types, ensuring type-level consistency.

Problem

Previously, SaltEnv was a separate field in ExternalEnvs, which could not guarantee that it accessed the same database snapshot as the EVM's Database. This could lead to correctness issues where SALT bucket capacity queries and database operations might read from different state snapshots.

Solution

Introduce a MegaDatabase trait that requires any database implementation to also implement SaltEnv with matching error types. This ensures at compile time that the database and SALT environment operate on the same underlying data source.

Key Changes

Core Trait Definition

  • Add MegaDatabase trait in external/salt.rs with blanket implementation
  • Any type implementing both Database and SaltEnv with matching error types automatically implements MegaDatabase

Refactoring

  • Remove salt_env field from DynamicGasCost, pass db parameter to methods instead
  • Remove salt_env from ExternalEnvs (now only contains oracle_env)
  • Update all DB: Database constraints to DB: MegaDatabase throughout the codebase
  • Remove unused BlockNumber parameter from ExternalEnvFactory::external_envs()

Supporting Changes

  • Add EmptyMegaDB wrapper implementing both Database and SaltEnv for testing
  • Implement SaltEnv for test databases (MemoryDatabase, ErrorInjectingDatabase)
  • Update documentation to reflect the new architecture

Benefits

  1. Type Safety: Compile-time guarantee that Database and SaltEnv are the same instance
  2. Backward Compatible: Blanket implementation means downstream users only need to implement both traits
  3. Simplified Interface: Removed unused BlockNumber parameter
  4. Clear Separation of Concerns: ExternalEnvs now only handles Oracle environment, SALT is provided by the database

Testing

  • All existing tests pass with the new trait structure
  • Test databases (MemoryDatabase, ErrorInjectingDatabase) properly implement SaltEnv
  • EmptyMegaDB provides a working default implementation

@Troublor
Copy link
Collaborator

Troublor commented Mar 1, 2026

是个不错的思路,不过我不确定在下游组件中好不好改。这个新的trait在mega-reth的哪个分支用上了吗?我想看看mega-reth那边是怎么用的。

@WonderLawrence WonderLawrence reopened this Mar 2, 2026
@WonderLawrence WonderLawrence marked this pull request as draft March 2, 2026 03:24
@WonderLawrence WonderLawrence force-pushed the wanglang/refactor/megadatabase-trait branch 5 times, most recently from 35b344d to fe48ae2 Compare March 2, 2026 19:06
@WonderLawrence WonderLawrence force-pushed the wanglang/refactor/megadatabase-trait branch from fe48ae2 to f69f08a Compare March 6, 2026 03:55
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.

2 participants