Check post state root on expected error in state tests

Adds logic to verify the post state root when an error is expected in state tests and a post state root is defined. This ensures the state root matches the expected value after reverting to the snapshot.
This commit is contained in:
Xuanyu Hu 2026-01-07 14:44:01 +08:00 committed by MariusVanDerWijden
parent 957a3602d9
commit b72d2921b4

View file

@ -234,6 +234,20 @@ func (t *StateTest) Run(subtest StateSubtest, vmconfig vm.Config, snapshotter bo
if err != nil {
// Here, an error exists but it was expected.
// We do not check the post state or logs.
// However, if the test defines a post state root, we should check it.
// In case of an error, the state is reverted to the snapshot, so we need to
// recalculate the root.
post := t.json.Post[subtest.Fork][subtest.Index]
if post.Root != (common.UnprefixedHash{}) {
config, _, err := GetChainConfig(subtest.Fork)
if err != nil {
return fmt.Errorf("failed to get chain config: %w", err)
}
root = st.StateDB.IntermediateRoot(config.IsEIP158(new(big.Int).SetUint64(t.json.Env.Number)))
if root != common.Hash(post.Root) {
return fmt.Errorf("post state root mismatch: got %x, want %x", root, post.Root)
}
}
return nil
}
post := t.json.Post[subtest.Fork][subtest.Index]