From e8731404d0893dd1d9b61b1f3d4e7c4f93396010 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 2 Nov 2025 22:23:17 +0800 Subject: [PATCH 1/5] get stable LogsHash result --- core/state/statedb.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/state/statedb.go b/core/state/statedb.go index b770698255e4..2f0ddaf7ea70 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -22,6 +22,7 @@ import ( "fmt" "maps" "slices" + "sort" "sync" "sync/atomic" "time" @@ -264,6 +265,10 @@ func (s *StateDB) Logs() []*types.Log { for _, lgs := range s.logs { logs = append(logs, lgs...) } + // Due to the map iteration is not stable, we need to sort the logs by TxIndex, then the LogsHash result is stable. + sort.Slice(logs, func(i, j int) bool { + return logs[i].TxIndex < logs[j].TxIndex + }) return logs } From 9b9c13880daa6bd4b9828ea671efb3d9f3d8fc65 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 2 Nov 2025 22:36:37 +0800 Subject: [PATCH 2/5] Update core/state/statedb.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- core/state/statedb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 2f0ddaf7ea70..270b5f64acba 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -265,7 +265,7 @@ func (s *StateDB) Logs() []*types.Log { for _, lgs := range s.logs { logs = append(logs, lgs...) } - // Due to the map iteration is not stable, we need to sort the logs by TxIndex, then the LogsHash result is stable. + // Because map iteration is not stable, we need to sort the logs by TxIndex, then the LogsHash result is stable. sort.Slice(logs, func(i, j int) bool { return logs[i].TxIndex < logs[j].TxIndex }) From 26fbb4046ef9e6bf87b04df277daa67bc50a0941 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 2 Nov 2025 22:38:12 +0800 Subject: [PATCH 3/5] Update core/state/statedb.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- core/state/statedb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 270b5f64acba..aeab91abf17d 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -267,7 +267,7 @@ func (s *StateDB) Logs() []*types.Log { } // Because map iteration is not stable, we need to sort the logs by TxIndex, then the LogsHash result is stable. sort.Slice(logs, func(i, j int) bool { - return logs[i].TxIndex < logs[j].TxIndex + return logs[i].TxIndex < logs[j].TxIndex || (logs[i].TxIndex == logs[j].TxIndex && logs[i].Index < logs[j].Index) }) return logs } From 207f434ca221b6115e0c2b6a30647c17dab0f19c Mon Sep 17 00:00:00 2001 From: maskpp Date: Tue, 4 Nov 2025 13:44:07 +0800 Subject: [PATCH 4/5] fix comments --- core/state/statedb.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index aeab91abf17d..035f686cb1ed 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -267,7 +267,7 @@ func (s *StateDB) Logs() []*types.Log { } // Because map iteration is not stable, we need to sort the logs by TxIndex, then the LogsHash result is stable. sort.Slice(logs, func(i, j int) bool { - return logs[i].TxIndex < logs[j].TxIndex || (logs[i].TxIndex == logs[j].TxIndex && logs[i].Index < logs[j].Index) + return logs[i].Index < logs[j].Index }) return logs } From c69412548f0136f0c734a117693ea33a99f268d7 Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 4 Nov 2025 20:12:47 +0800 Subject: [PATCH 5/5] Remove comment on log sorting stability Removed comment about sorting logs by TxIndex. --- core/state/statedb.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 035f686cb1ed..364bc4085035 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -265,7 +265,6 @@ func (s *StateDB) Logs() []*types.Log { for _, lgs := range s.logs { logs = append(logs, lgs...) } - // Because map iteration is not stable, we need to sort the logs by TxIndex, then the LogsHash result is stable. sort.Slice(logs, func(i, j int) bool { return logs[i].Index < logs[j].Index })