core/state: check for code/codehash mismatch in fuzzer

This commit is contained in:
Martin Holst Swende 2024-12-16 15:19:38 +01:00
parent efeb350092
commit e7ba44b42b
No known key found for this signature in database
GPG Key ID: 683B438C05A5DDF0
2 changed files with 16 additions and 1 deletions

View File

@ -217,6 +217,7 @@ func (j *scopedJournal) revert(s *StateDB) {
} }
} else { } else {
if !bytes.Equal(obj.CodeHash(), journalHash) { if !bytes.Equal(obj.CodeHash(), journalHash) {
// TODO @holiman set the code back!
obj.setCode(common.BytesToHash(data.codeHash), nil) obj.setCode(common.BytesToHash(data.codeHash), nil)
} }
} }

View File

@ -28,6 +28,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
@ -233,7 +234,7 @@ func fuzzJournals(t *testing.T, data []byte) {
return return
} }
} }
if s.GetNonce(addr) == 0 && emptyStorage { if emptyStorage {
s.CreateContract(addr) s.CreateContract(addr)
// We also set some code here, to prevent the // We also set some code here, to prevent the
// CreateContract action from being performed twice in a row, // CreateContract action from being performed twice in a row,
@ -345,6 +346,19 @@ func fuzzJournals(t *testing.T, data []byte) {
if !slices.Equal(accs1, accs2) { if !slices.Equal(accs1, accs2) {
t.Fatalf("mismatched dirty-sets:\n%v\n%v", accs1, accs2) t.Fatalf("mismatched dirty-sets:\n%v\n%v", accs1, accs2)
} }
for _, addr := range accs1 {
if cHash := stateDbs[0].GetCodeHash(addr); cHash != types.EmptyCodeHash && cHash != (common.Hash{}) {
have := crypto.Keccak256Hash(stateDbs[0].GetCode(addr))
if have != cHash {
t.Fatalf("0: mismatched codehash <-> code.\ncodehash: %x\nhash(code): %x\n", cHash, have)
}
have = crypto.Keccak256Hash(stateDbs[1].GetCode(addr))
if have != cHash {
t.Fatalf("1: mismatched codehash <-> code.\ncodehash: %x\nhash(code): %x\n", cHash, have)
}
}
}
} }
h1, err1 := stateDbs[0].Commit(0, false) h1, err1 := stateDbs[0].Commit(0, false)
h2, err2 := stateDbs[1].Commit(0, false) h2, err2 := stateDbs[1].Commit(0, false)