From 10a4b0917e9a26d8cbfcf544fa43bb9836c5312c Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Fri, 12 Dec 2025 07:33:33 -0500 Subject: [PATCH] add method to log a state diff from two sets of AccountMutations --- core/types/bal/bal.go | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/core/types/bal/bal.go b/core/types/bal/bal.go index 3b172d4bff..42f3e8a790 100644 --- a/core/types/bal/bal.go +++ b/core/types/bal/bal.go @@ -19,10 +19,13 @@ package bal import ( "bytes" "encoding/json" + "fmt" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/log" "github.com/holiman/uint256" "log/slog" "maps" + "slices" ) // idxAccessListBuilder is responsible for producing the state accesses and @@ -596,6 +599,54 @@ func (a *AccountMutations) String() string { return res.String() } +func (a *AccountMutations) LogDiff(other *AccountMutations) { + var diff []interface{} + + if a.Balance != nil || other.Balance != nil { + if a.Balance == nil || other.Balance == nil || !a.Balance.Eq(other.Balance) { + diff = append(diff, "local balance", a.Balance, "remote balance", other.Balance) + } + } + if (len(a.Code) != 0 || len(other.Code) != 0) && !bytes.Equal(a.Code, other.Code) { + diff = append(diff, "local code", a.Code, "remote code", other.Code) + } + if a.Nonce != nil || other.Nonce != nil { + if a.Nonce == nil || other.Nonce == nil || *a.Nonce != *other.Nonce { + diff = append(diff, "local nonce", a.Nonce, "remote nonce", other.Nonce) + } + } + + if a.StorageWrites != nil || other.StorageWrites != nil { + if !maps.Equal(a.StorageWrites, other.StorageWrites) { + union := make(map[common.Hash]struct{}) + for slot, _ := range a.StorageWrites { + union[slot] = struct{}{} + } + for slot, _ := range other.StorageWrites { + union[slot] = struct{}{} + } + + orderedKeys := slices.SortedFunc(maps.Keys(union), func(hash common.Hash, hash2 common.Hash) int { + return bytes.Compare(hash[:], hash2[:]) + }) + + for _, key := range orderedKeys { + aVal, inA := a.StorageWrites[key] + otherVal, inOther := other.StorageWrites[key] + + if (inA && !inOther) || (!inA && inOther) || !bytes.Equal(aVal[:], otherVal[:]) { + diff = append(diff, fmt.Sprintf("storageLocal%x", key), aVal) + diff = append(diff, fmt.Sprintf("storageLocal%x", key), otherVal) + } + } + } + } + + if len(diff) > 0 { + log.Error("diff between remote/local BAL", diff...) + } +} + // Eq returns whether the calling instance is equal to the provided one. func (a *AccountMutations) Eq(other *AccountMutations) bool { if a.Balance != nil || other.Balance != nil {