@@ -25,25 +25,68 @@ import (
2525 "github.com/ethereum/go-ethereum/common"
2626 "github.com/ethereum/go-ethereum/core/rawdb"
2727 "github.com/ethereum/go-ethereum/crypto"
28+ "github.com/ethereum/go-ethereum/ethdb"
2829 "github.com/ethereum/go-ethereum/log"
2930 "github.com/ethereum/go-ethereum/trie/trienode"
3031 "github.com/ethereum/go-ethereum/trie/triestate"
3132 "golang.org/x/crypto/sha3"
3233)
3334
35+ // trienodebuffer is a collection of modified trie nodes to aggregate the disk
36+ // write. The content of the trienodebuffer must be checked before diving into
37+ // disk (since it basically is not-yet-written data).
38+ type trienodebuffer interface {
39+ // node retrieves the trie node with given node info.
40+ node (owner common.Hash , path []byte , hash common.Hash ) (* trienode.Node , error )
41+
42+ // commit merges the dirty nodes into the trienodebuffer. This operation won't take
43+ // the ownership of the nodes map which belongs to the bottom-most diff layer.
44+ // It will just hold the node references from the given map which are safe to
45+ // copy.
46+ commit (nodes map [common.Hash ]map [string ]* trienode.Node ) trienodebuffer
47+
48+ // revert is the reverse operation of commit. It also merges the provided nodes
49+ // into the trienodebuffer, the difference is that the provided node set should
50+ // revert the changes made by the last state transition.
51+ revert (db ethdb.KeyValueReader , nodes map [common.Hash ]map [string ]* trienode.Node ) error
52+
53+ // flush persists the in-memory dirty trie node into the disk if the configured
54+ // memory threshold is reached. Note, all data must be written atomically.
55+ flush (db ethdb.KeyValueStore , clean * fastcache.Cache , id uint64 , force bool ) error
56+
57+ // setSize sets the buffer size to the provided number, and invokes a flush
58+ // operation if the current memory usage exceeds the new limit.
59+ setSize (size int , db ethdb.KeyValueStore , clean * fastcache.Cache , id uint64 ) error
60+
61+ // reset cleans up the disk cache.
62+ reset ()
63+
64+ // empty returns an indicator if trienodebuffer contains any state transition inside.
65+ empty () bool
66+
67+ // getSize return the trienodebuffer used size.
68+ getSize () (uint64 , uint64 )
69+
70+ // getAllNodes return all the trie nodes are cached in trienodebuffer.
71+ getAllNodes () map [common.Hash ]map [string ]* trienode.Node
72+
73+ // getLayers return the size of cached difflayers.
74+ getLayers () uint64
75+ }
76+
3477// diskLayer is a low level persistent layer built on top of a key-value store.
3578type diskLayer struct {
3679 root common.Hash // Immutable, root hash to which this layer was made for
3780 id uint64 // Immutable, corresponding state id
3881 db * Database // Path-based trie database
3982 cleans * fastcache.Cache // GC friendly memory cache of clean node RLPs
40- buffer * nodebuffer // Node buffer to aggregate writes
83+ buffer trienodebuffer // Node buffer to aggregate writes
4184 stale bool // Signals that the layer became stale (state progressed)
4285 lock sync.RWMutex // Lock used to protect stale flag
4386}
4487
4588// newDiskLayer creates a new disk layer based on the passing arguments.
46- func newDiskLayer (root common.Hash , id uint64 , db * Database , cleans * fastcache.Cache , buffer * nodebuffer ) * diskLayer {
89+ func newDiskLayer (root common.Hash , id uint64 , db * Database , cleans * fastcache.Cache , buffer trienodebuffer ) * diskLayer {
4790 // Initialize a clean cache if the memory allowance is not zero
4891 // or reuse the provided cache if it is not nil (inherited from
4992 // the original disk layer).
@@ -294,14 +337,15 @@ func (dl *diskLayer) setBufferSize(size int) error {
294337}
295338
296339// size returns the approximate size of cached nodes in the disk layer.
297- func (dl * diskLayer ) size () common.StorageSize {
340+ func (dl * diskLayer ) size () ( common.StorageSize , common. StorageSize ) {
298341 dl .lock .RLock ()
299342 defer dl .lock .RUnlock ()
300343
301344 if dl .stale {
302- return 0
345+ return 0 , 0
303346 }
304- return common .StorageSize (dl .buffer .size )
347+ nodeBuf , nodeImmutableBuf := dl .buffer .getSize ()
348+ return common .StorageSize (nodeBuf ), common .StorageSize (nodeImmutableBuf )
305349}
306350
307351// resetCache releases the memory held by clean cache to prevent memory leak.
0 commit comments