mpt: don't flush nodes already present in the DB

It's just a waste of time.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Roman Khimov 2020-06-03 00:02:45 +03:00 committed by Evgenii Stratonikov
parent 475bf2445a
commit 2b53877dff
2 changed files with 19 additions and 0 deletions

View file

@ -14,6 +14,8 @@ type BaseNode struct {
bytes []byte
hashValid bool
bytesValid bool
isFlushed bool
}
// BaseNodeIface abstracts away basic Node functions.
@ -21,6 +23,8 @@ type BaseNodeIface interface {
Hash() util.Uint256
Type() NodeType
Bytes() []byte
IsFlushed() bool
SetFlushed()
}
// getHash returns a hash of this BaseNode.
@ -60,6 +64,17 @@ func (b *BaseNode) updateBytes(n Node) {
func (b *BaseNode) invalidateCache() {
b.bytesValid = false
b.hashValid = false
b.isFlushed = false
}
// IsFlushed checks for node flush status.
func (b *BaseNode) IsFlushed() bool {
return b.isFlushed
}
// SetFlushed sets 'flushed' flag to true for this node.
func (b *BaseNode) SetFlushed() {
b.isFlushed = true
}
// encodeNodeWithType encodes node together with it's type.

View file

@ -318,6 +318,9 @@ func (t *Trie) Flush() {
}
func (t *Trie) flush(node Node) {
if node.IsFlushed() {
return
}
switch n := node.(type) {
case *BranchNode:
for i := range n.Children {
@ -336,6 +339,7 @@ func (t *Trie) putToStore(n Node) {
panic("can't put hash node in trie")
}
_ = t.Store.Put(makeStorageKey(n.Hash().BytesBE()), n.Bytes()) // put in MemCached returns no errors
n.SetFlushed()
}
func (t *Trie) getFromStore(h util.Uint256) (Node, error) {