From 4001f37005db590e2b9aa9d36f8c827fb8bde3d2 Mon Sep 17 00:00:00 2001 From: dean65 Date: Fri, 6 May 2022 02:16:57 +0800 Subject: [PATCH] fix sorting difflayer.storage --- core/blockchain.go | 9 ++------- core/types/block.go | 7 +++++++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index a6f20cf6b7..ac57b8af0e 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -521,14 +521,9 @@ func (bc *BlockChain) cacheDiffLayer(diffLayer *types.DiffLayer, diffLayerCh cha sort.SliceStable(diffLayer.Storages, func(i, j int) bool { return diffLayer.Storages[i].Account.Hex() < diffLayer.Storages[j].Account.Hex() }) - for _, storage := range diffLayer.Storages { + for index := range diffLayer.Storages { // Sort keys and vals by key. - sort.SliceStable(storage.Keys, func(i, j int) bool { - return storage.Keys[i] < storage.Keys[j] - }) - sort.SliceStable(storage.Vals, func(i, j int) bool { - return storage.Keys[i] < storage.Keys[j] - }) + sort.Sort(&diffLayer.Storages[index]) } if bc.diffLayerCache.Len() >= diffLayerCacheLimit { diff --git a/core/types/block.go b/core/types/block.go index da3bf163bf..7be5b2a3ed 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -480,6 +480,13 @@ type DiffStorage struct { Vals [][]byte } +func (storage *DiffStorage) Len() int { return len(storage.Keys) } +func (storage *DiffStorage) Swap(i, j int) { + storage.Keys[i], storage.Keys[j] = storage.Keys[j], storage.Keys[i] + storage.Vals[i], storage.Vals[j] = storage.Vals[j], storage.Vals[i] +} +func (storage *DiffStorage) Less(i, j int) bool { return storage.Keys[i] < storage.Keys[j] } + type DiffAccountsInTx struct { TxHash common.Hash Accounts map[common.Address]*big.Int