diff --git a/core/state/snapshot/difflayer.go b/core/state/snapshot/difflayer.go index 779c1ea98c..93bf7d2d70 100644 --- a/core/state/snapshot/difflayer.go +++ b/core/state/snapshot/difflayer.go @@ -525,19 +525,20 @@ func (dl *diffLayer) StorageList(accountHash common.Hash) ([]common.Hash, bool) dl.lock.RUnlock() return list, destructed // the cached list can't be nil } + storageMap := dl.storageData[accountHash] dl.lock.RUnlock() - // No old sorted account list exists, generate a new one - dl.lock.Lock() - defer dl.lock.Unlock() - - storageMap := dl.storageData[accountHash] storageList := make([]common.Hash, 0, len(storageMap)) for k := range storageMap { storageList = append(storageList, k) } slices.SortFunc(storageList, common.Hash.Cmp) + + // No old sorted account list exists, generate a new one + dl.lock.Lock() dl.storageList[accountHash] = storageList dl.memory += uint64(len(dl.storageList)*common.HashLength + common.HashLength) + dl.lock.Unlock() + return storageList, destructed } diff --git a/core/state/snapshot/snapshot.go b/core/state/snapshot/snapshot.go index 89a4c16c20..d9d8997d86 100644 --- a/core/state/snapshot/snapshot.go +++ b/core/state/snapshot/snapshot.go @@ -818,10 +818,12 @@ func (t *Tree) Verify(root common.Hash) error { // The lock of snapTree is assumed to be held already. func (t *Tree) disklayer() *diskLayer { var snap snapshot + t.lock.RLock() for _, s := range t.layers { snap = s break } + t.lock.RUnlock() if snap == nil { return nil } @@ -829,6 +831,8 @@ func (t *Tree) disklayer() *diskLayer { case *diskLayer: return layer case *diffLayer: + layer.lock.RLock() + defer layer.lock.RUnlock() return layer.origin default: panic(fmt.Sprintf("%T: undefined layer", snap)) @@ -848,9 +852,6 @@ func (t *Tree) diskRoot() common.Hash { // generating is an internal helper function which reports whether the snapshot // is still under the construction. func (t *Tree) generating() (bool, error) { - t.lock.Lock() - defer t.lock.Unlock() - layer := t.disklayer() if layer == nil { return false, errors.New("disk layer is missing") @@ -862,9 +863,6 @@ func (t *Tree) generating() (bool, error) { // DiskRoot is a external helper function to return the disk layer root. func (t *Tree) DiskRoot() common.Hash { - t.lock.Lock() - defer t.lock.Unlock() - return t.diskRoot() }