Skip to content

blockchain: isolate tx cache during prefetch - #1020

Open
yoomee1313 wants to merge 1 commit into
kaiachain:devfrom
yoomee1313:codex/fix-prefetch-tx-cache
Open

blockchain: isolate tx cache during prefetch#1020
yoomee1313 wants to merge 1 commit into
kaiachain:devfrom
yoomee1313:codex/fix-prefetch-tx-cache

Conversation

@yoomee1313

@yoomee1313 yoomee1313 commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Summary

  • During mainnet sync, one block was rejected once and later imported successfully.
  • GasUsed mismatch was 15000 (134249803 remote vs 134234803 local).
  • The delta matches one Kaia signature-validation key.
  • The peer was v2.2.2, so logs/code-version differences made the symptom hard to isolate.

Cause

  • EVM-based prefetch calls tx.AsMessageWithAccountKeyPicker(...).
  • That method mutates tx wrapper caches, including validatedGas.
  • Prefetch used the original block tx pointer.
  • Speculative prefetch state could overwrite caches later used by real insertChain execution.
  • The added unit test reproduces the 15000 gas cache pollution case.

Fix

  • Use a fresh transaction wrapper before prefetch execution:
types.NewTx(tx.GetTxInternalData())
  • This keeps original block transaction caches untouched.
  • It does not deep-copy tx data, signatures, or payload.

History

  • 5e25a0f71: added copied tx prefetching.
  • c57ec37bd: removed copied tx logic.
  • Later locks prevent data races, but not logical cache pollution from speculative prefetch.
  • kaiabft avoids this differently via d4671fa8a, but that is a larger behavior/performance change, so this PR keeps the fix minimal for dev.

Scope

  • Affected:
    • block import/sync through insertChain prefetch paths
  • Not affected:
    • proposer block creation path
    • proposer path does not use statePrefetcher, Prefetch, or PrefetchTx

Test

  • Before the fix, the regression test fails because prefetch overwrites the original tx gas cache.
  • After the fix, the same regression test passes and the full blockchain package test passes.
# Before the fix: copyTxForPrefetch returns the original tx.
$ go test ./blockchain -run TestCopyTxForPrefetchDoesNotOverwriteOriginalValidatedGas -count=1
INFO[08/06,17:37:04 +08] [5] InitDeriveSha                             initial=0 withGov=false
--- FAIL: TestCopyTxForPrefetchDoesNotOverwriteOriginalValidatedGas (0.00s)
    state_prefetcher_test.go:92:
        Error Trace:    /Users/yumiel/.codex/worktrees/6174/kaia/blockchain/state_prefetcher_test.go:92
        Error:          Not equal:
                        expected: 0x8ca0
                        actual  : 0x5208
        Test:           TestCopyTxForPrefetchDoesNotOverwriteOriginalValidatedGas
FAIL
FAIL    github.com/kaiachain/kaia/blockchain    0.549s
FAIL

# After the fix.
$ go test ./blockchain -run TestCopyTxForPrefetchDoesNotOverwriteOriginalValidatedGas -count=1
ok      github.com/kaiachain/kaia/blockchain    0.547s

$ go test ./blockchain -count=1
ok      github.com/kaiachain/kaia/blockchain    20.236s

@yoomee1313
yoomee1313 force-pushed the codex/fix-prefetch-tx-cache branch from 5f786e9 to d0d2bb4 Compare August 6, 2026 09:31
@yoomee1313
yoomee1313 force-pushed the codex/fix-prefetch-tx-cache branch from d0d2bb4 to 0957146 Compare August 6, 2026 09:35
@yoomee1313 yoomee1313 self-assigned this Aug 6, 2026
@ian0371 ian0371 added the do not merge Do not merge just yet label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not merge Do not merge just yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants