Skip to content

Commit d7d81d7

Browse files
committed
core/state/snapshot: extract and split cap method, cover corners
1 parent e146fbe commit d7d81d7

5 files changed

Lines changed: 461 additions & 239 deletions

File tree

‎core/state/snapshot/difflayer.go‎

Lines changed: 0 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import (
2222
"sync"
2323

2424
"github.com/ethereum/go-ethereum/common"
25-
"github.com/ethereum/go-ethereum/core/rawdb"
26-
"github.com/ethereum/go-ethereum/ethdb"
2725
"github.com/ethereum/go-ethereum/log"
2826
"github.com/ethereum/go-ethereum/rlp"
2927
)
@@ -169,125 +167,6 @@ func (dl *diffLayer) Update(blockRoot common.Hash, accounts map[common.Hash][]by
169167
return newDiffLayer(dl, dl.number+1, blockRoot, accounts, storage)
170168
}
171169

172-
// Cap traverses downwards the diff tree until the number of allowed layers are
173-
// crossed. All diffs beyond the permitted number are flattened downwards. If
174-
// the layer limit is reached, memory cap is also enforced (but not before). The
175-
// block numbers for the disk layer and first diff layer are returned for GC.
176-
func (dl *diffLayer) Cap(layers int, memory uint64) (uint64, uint64) {
177-
// Dive until we run out of layers or reach the persistent database
178-
if layers > 2 {
179-
// If we still have diff layers below, recurse
180-
if parent, ok := dl.parent.(*diffLayer); ok {
181-
return parent.Cap(layers-1, memory)
182-
}
183-
// Diff stack too shallow, return block numbers without modifications
184-
return dl.parent.(*diskLayer).number, dl.number
185-
}
186-
// We're out of layers, flatten anything below, stopping if it's the disk or if
187-
// the memory limit is not yet exceeded.
188-
switch parent := dl.parent.(type) {
189-
case *diskLayer:
190-
return parent.number, dl.number
191-
case *diffLayer:
192-
// Flatten the parent into the grandparent. The flattening internally obtains a
193-
// write lock on grandparent.
194-
flattened := parent.flatten().(*diffLayer)
195-
196-
dl.lock.Lock()
197-
defer dl.lock.Unlock()
198-
199-
dl.parent = flattened
200-
if flattened.memory < memory {
201-
diskNumber, _ := flattened.parent.Info()
202-
return diskNumber, flattened.number
203-
}
204-
default:
205-
panic(fmt.Sprintf("unknown data layer: %T", parent))
206-
}
207-
// If the bottommost layer is larger than our memory cap, persist to disk
208-
var (
209-
parent = dl.parent.(*diffLayer)
210-
base = parent.parent.(*diskLayer)
211-
batch = base.db.NewBatch()
212-
)
213-
parent.lock.RLock()
214-
defer parent.lock.RUnlock()
215-
216-
// Start by temporarily deleting the current snapshot block marker. This
217-
// ensures that in the case of a crash, the entire snapshot is invalidated.
218-
rawdb.DeleteSnapshotBlock(batch)
219-
220-
// Mark the original base as stale as we're going to create a new wrapper
221-
base.lock.Lock()
222-
if base.stale {
223-
panic("parent disk layer is stale") // we've committed into the same base from two children, boo
224-
}
225-
base.stale = true
226-
base.lock.Unlock()
227-
228-
// Push all the accounts into the database
229-
for hash, data := range parent.accountData {
230-
if len(data) > 0 {
231-
// Account was updated, push to disk
232-
rawdb.WriteAccountSnapshot(batch, hash, data)
233-
base.cache.Set(string(hash[:]), data)
234-
235-
if batch.ValueSize() > ethdb.IdealBatchSize {
236-
if err := batch.Write(); err != nil {
237-
log.Crit("Failed to write account snapshot", "err", err)
238-
}
239-
batch.Reset()
240-
}
241-
} else {
242-
// Account was deleted, remove all storage slots too
243-
rawdb.DeleteAccountSnapshot(batch, hash)
244-
base.cache.Set(string(hash[:]), nil)
245-
246-
it := rawdb.IterateStorageSnapshots(base.db, hash)
247-
for it.Next() {
248-
if key := it.Key(); len(key) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator
249-
batch.Delete(key)
250-
base.cache.Delete(string(key[1:]))
251-
}
252-
}
253-
it.Release()
254-
}
255-
}
256-
// Push all the storage slots into the database
257-
for accountHash, storage := range parent.storageData {
258-
for storageHash, data := range storage {
259-
if len(data) > 0 {
260-
rawdb.WriteStorageSnapshot(batch, accountHash, storageHash, data)
261-
base.cache.Set(string(append(accountHash[:], storageHash[:]...)), data)
262-
} else {
263-
rawdb.DeleteStorageSnapshot(batch, accountHash, storageHash)
264-
base.cache.Set(string(append(accountHash[:], storageHash[:]...)), nil)
265-
}
266-
}
267-
if batch.ValueSize() > ethdb.IdealBatchSize {
268-
if err := batch.Write(); err != nil {
269-
log.Crit("Failed to write storage snapshot", "err", err)
270-
}
271-
batch.Reset()
272-
}
273-
}
274-
// Update the snapshot block marker and write any remainder data
275-
newBase := &diskLayer{
276-
root: parent.root,
277-
number: parent.number,
278-
cache: base.cache,
279-
db: base.db,
280-
journal: base.journal,
281-
}
282-
rawdb.WriteSnapshotBlock(batch, newBase.number, newBase.root)
283-
if err := batch.Write(); err != nil {
284-
log.Crit("Failed to write leftover snapshot", "err", err)
285-
}
286-
dl.parent = newBase
287-
288-
return newBase.number, dl.number
289-
}
290-
291170
// flatten pushes all data from this point downwards, flattening everything into
292171
// a single diff at the bottom. Since usually the lowermost diff is the largest,
293172
// the flattening bulds up from there in reverse.

‎core/state/snapshot/difflayer_test.go‎

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ package snapshot
1818

1919
import (
2020
"bytes"
21-
"fmt"
2221
"math/big"
2322
"math/rand"
2423
"testing"
25-
"time"
2624

27-
"github.com/allegro/bigcache"
2825
"github.com/ethereum/go-ethereum/common"
29-
"github.com/ethereum/go-ethereum/core/rawdb"
3026
"github.com/ethereum/go-ethereum/rlp"
3127
)
3228

@@ -192,113 +188,12 @@ func TestInsertAndMerge(t *testing.T) {
192188
}
193189
}
194190

195-
// TestCapTree tests some functionality regarding capping/flattening
196-
func TestCapTree(t *testing.T) {
197-
198-
var (
199-
storage = make(map[common.Hash]map[common.Hash][]byte)
200-
)
201-
setAccount := func(accKey string) map[common.Hash][]byte {
202-
return map[common.Hash][]byte{
203-
common.HexToHash(accKey): randomAccount(),
204-
}
205-
}
206-
// the bottom-most layer, aside from the 'disk layer'
207-
cache, _ := bigcache.NewBigCache(bigcache.Config{ // TODO(karalabe): dedup
208-
Shards: 1,
209-
LifeWindow: time.Hour,
210-
MaxEntriesInWindow: 1 * 1024,
211-
MaxEntrySize: 1,
212-
HardMaxCacheSize: 1,
213-
})
214-
215-
base := &diskLayer{
216-
journal: "",
217-
db: rawdb.NewMemoryDatabase(),
218-
cache: cache,
219-
number: 0,
220-
root: common.HexToHash("0x01"),
221-
}
222-
// The lowest difflayer
223-
a1 := base.Update(common.HexToHash("0xa1"), setAccount("0xa1"), storage)
224-
225-
a2 := a1.Update(common.HexToHash("0xa2"), setAccount("0xa2"), storage)
226-
b2 := a1.Update(common.HexToHash("0xb2"), setAccount("0xb2"), storage)
227-
228-
a3 := a2.Update(common.HexToHash("0xa3"), setAccount("0xa3"), storage)
229-
b3 := b2.Update(common.HexToHash("0xb3"), setAccount("0xb3"), storage)
230-
231-
checkExist := func(layer *diffLayer, key string) error {
232-
accountKey := common.HexToHash(key)
233-
data, _ := layer.Account(accountKey)
234-
if data == nil {
235-
return fmt.Errorf("expected %x to exist, got nil", accountKey)
236-
}
237-
return nil
238-
}
239-
shouldErr := func(layer *diffLayer, key string) error {
240-
accountKey := common.HexToHash(key)
241-
data, err := layer.Account(accountKey)
242-
if err == nil {
243-
return fmt.Errorf("expected error, got data %x", data)
244-
}
245-
return nil
246-
}
247-
248-
// check basics
249-
if err := checkExist(b3, "0xa1"); err != nil {
250-
t.Error(err)
251-
}
252-
if err := checkExist(b3, "0xb2"); err != nil {
253-
t.Error(err)
254-
}
255-
if err := checkExist(b3, "0xb3"); err != nil {
256-
t.Error(err)
257-
}
258-
// Now, merge the a-chain
259-
diskNum, diffNum := a3.Cap(0, 1024)
260-
if diskNum != 0 {
261-
t.Errorf("disk layer err, got %d exp %d", diskNum, 0)
262-
}
263-
if diffNum != 2 {
264-
t.Errorf("diff layer err, got %d exp %d", diffNum, 2)
265-
}
266-
// At this point, a2 got merged into a1. Thus, a1 is now modified,
267-
// and as a1 is the parent of b2, b2 should no longer be able to iterate into parent
268-
269-
// These should still be accessible
270-
if err := checkExist(b3, "0xb2"); err != nil {
271-
t.Error(err)
272-
}
273-
if err := checkExist(b3, "0xb3"); err != nil {
274-
t.Error(err)
275-
}
276-
//b2ParentNum, _ := b2.parent.Info()
277-
//if b2.parent.invalid == false
278-
// t.Errorf("err, exp parent to be invalid, got %v", b2.parent, b2ParentNum)
279-
//}
280-
// But these would need iteration into the modified parent:
281-
if err := shouldErr(b3, "0xa1"); err != nil {
282-
t.Error(err)
283-
}
284-
if err := shouldErr(b3, "0xa2"); err != nil {
285-
t.Error(err)
286-
}
287-
if err := shouldErr(b3, "0xa3"); err != nil {
288-
t.Error(err)
289-
}
290-
}
291-
292191
type emptyLayer struct{}
293192

294193
func (emptyLayer) Update(blockRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer {
295194
panic("implement me")
296195
}
297196

298-
func (emptyLayer) Cap(layers int, memory uint64) (uint64, uint64) {
299-
panic("implement me")
300-
}
301-
302197
func (emptyLayer) Journal() error {
303198
panic("implement me")
304199
}
@@ -403,7 +298,6 @@ func BenchmarkSearchSlot(b *testing.B) {
403298
// Without sorting and tracking accountlist
404299
// BenchmarkFlatten-6 300 5511511 ns/op
405300
func BenchmarkFlatten(b *testing.B) {
406-
407301
fill := func(parent snapshot, blocknum int) *diffLayer {
408302
accounts := make(map[common.Hash][]byte)
409303
storage := make(map[common.Hash]map[common.Hash][]byte)

‎core/state/snapshot/disklayer.go‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,6 @@ func (dl *diskLayer) Update(blockHash common.Hash, accounts map[common.Hash][]by
126126
return newDiffLayer(dl, dl.number+1, blockHash, accounts, storage)
127127
}
128128

129-
// Cap traverses downwards the diff tree until the number of allowed layers are
130-
// crossed. All diffs beyond the permitted number are flattened downwards.
131-
func (dl *diskLayer) Cap(layers int, memory uint64) (uint64, uint64) {
132-
return dl.number, dl.number
133-
}
134-
135129
// Journal commits an entire diff hierarchy to disk into a single journal file.
136130
func (dl *diskLayer) Journal() error {
137131
// There's no journalling a disk layer

0 commit comments

Comments
 (0)