erigontech / erigontech/erigon
IBS 2-Cache Phase 4b: Remove external GetOrNewStateObject callers; reimplement SetStorage override
- Dominant language
- Go
- Stars
- 3.6k
- Forks
- 1.5k
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 463
Description
Part of #19623 — Rationalize IntraBlockState to a 2-Cache Model.
Depends on #19703 (Phase 4).
Required before #19704 (Phase 5) which deletes `stateObject` entirely.
## Problem
Two files outside `execution/state/` call `GetOrNewStateObject` directly, returning a `*stateObject`. One file calls methods on that object. `stateObject` cannot be removed in Phase 5 while these callers exist.
Additionally, `IBS.SetStorage` (used by RPC state overrides) delegates to `stateObject.SetStorage`, which must be reimplemented without stateObject.
## External callers to fix
### 1. `execution/vm/runtime/runtime.go:226`
```go
sender, err := cfg.State.GetOrNewStateObject(cfg.Origin)
// ...
ret, leftOverGas, err := vmenv.Call(sender.Address(), address, ...)
```
`sender.Address()` is just `cfg.Origin`. Fix: remove the `GetOrNewStateObject` call entirely and use `cfg.Origin` directly in `vmenv.Call`.
### 2. `execution/abi/bind/backends/simulated.go:746-750`
```go
from, err := statedb.GetOrNewStateObject(accounts.InternAddress(call.From))
from.SetBalance(*(&uint256.Int{}).SetAllOne(), true, tracing.BalanceChangeUnspecified)
```
Fix: replace with `statedb.SetBalance(accounts.InternAddress(call.From), *maxInt, tracing.BalanceChangeUnspecified)`.
### 3. `rpc/ethapi/state_overrides.go:69` — `ibs.SetStorage(addr, storage)`
`IBS.SetStorage` internally calls `stateObject.SetStorage` which installs a "fake storage" map. This is only used for `eth_call` state overrides. Fix: move the fake-storage map onto `IntraBlockState` directly (as `fakeStorage map[Address]Storage`), and implement `GetState` to check `fakeStorage` before reading through `versionMap` or `stateReader`.
## Changes
- `execution/vm/runtime/runtime.go` — remove `GetOrNewStateObject` call; use `cfg.Origin` directly.
- `execution/abi/bind/backends/simulated.go` — replace `GetOrNewStateObject + SetBalance` with `ibs.SetBalance`.
- `execution/state/intra_block_state.go` — add `fakeStorage map[Address]Storage`; move `SetStorage`/`fakeStorage` logic from `state_object.go` onto IBS; update `GetState` to check `fakeStorage` first.
- `execution/state/state_object.go` — remove `fakeStorage` field and `SetStorage` method (moved to IBS).
- Remove `GetOrNewStateObject` from the public API of `IntraBlockState` (make unexported or delete).
## New feature tests
- **`TestSetStorageOverrideOnIBS`** — call `ibs.SetStorage(addr, fakeMap)` then `ibs.GetState(addr, key)`; assert fake values are returned.
- **`TestRuntimeCallNoGetOrNewStateObject`** — run `runtime.Execute` for a simple contract; assert it completes without needing `GetOrNewStateObject`.
- **`TestSimulatedBackendSetBalance`** — run a simulated call; assert fake balance is applied via `ibs.SetBalance`, not `stateObject.SetBalance`.
## Regression tests
- **`TestSetStorage`** (if existing) — must pass.
- Run `go test ./rpc/ethapi/...` and `go test ./execution/abi/...` — no failures.
- Run `go test ./execution/vm/runtime/...` — no failures.
- All Phase 4 regression tests must still pass.
- `make test-short` passes.
## Files affected
- `execution/vm/runtime/runtime.go`
- `execution/abi/bind/backends/simulated.go`
- `execution/state/intra_block_state.go` — add `fakeStorage`; update `GetState`; remove `GetOrNewStateObject`
- `execution/state/state_object.go` — remove `fakeStorage`, `SetStorage`
## Acceptance criteria
- `GetOrNewStateObject` is no longer part of `IntraBlockState`'s public API
- `SetStorage` works without stateObject
- All RPC state-override tests pass
- `make test-short` passes
Contributor guide
Research direction
Start by reading execution/state/intra_block_state.go and state_object.go, then inspect the callers in execution/vm/runtime/runtime.go and execution/abi/bind/backends/simulated.go. Run the listed runtime, ABI, and RPC tests before changing the state API. Done means the external callers no longer use GetOrNewStateObject, SetStorage works through IBS, and the named regression tests plus make test-short pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- api, backend, testing
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100