From 9d718661f663e3d724257defa2cbd3a0a72d2945 Mon Sep 17 00:00:00 2001 From: Francis Li Date: Tue, 8 Oct 2024 16:38:36 -0700 Subject: [PATCH] Add sorting --- core/state/state_object.go | 4 ++++ core/state/statedb.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/core/state/state_object.go b/core/state/state_object.go index 422badb19b..07c4ab2f6f 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "maps" + "slices" "time" "github.com/ethereum/go-ethereum/common" @@ -348,6 +349,9 @@ func (s *stateObject) updateTrie() (Trie, error) { // Cache the items for preloading used = append(used, common.CopyBytes(key[:])) // Copy needed for closure } + // Perform deletes in sorted order to make the touched trie nodes deterministic + slices.SortFunc(deletions, func(a, b common.Hash) int { return bytes.Compare(a[:], b[:]) }) + for _, key := range deletions { if err := tr.DeleteStorage(s.address, key[:]); err != nil { s.db.setError(err) diff --git a/core/state/statedb.go b/core/state/statedb.go index 566344a4c7..156634cb28 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -18,6 +18,7 @@ package state import ( + "bytes" "errors" "fmt" "maps" @@ -935,6 +936,9 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { } usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure } + // Perform deletes in sorted order to make the touched trie nodes deterministic + slices.SortFunc(deletedAddrs, func(a, b common.Address) int { return bytes.Compare(a[:], b[:]) }) + for _, deletedAddr := range deletedAddrs { s.deleteStateObject(deletedAddr) s.AccountDeleted += 1