/
cachenode.go
72 lines (62 loc) · 1.45 KB
/
cachenode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) 2020 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package mptrie
import (
"google.golang.org/protobuf/proto"
)
type cacheNode struct {
dirty bool
serializable
mpt *merklePatriciaTrie
hashVal []byte
ser []byte
}
func (cn *cacheNode) Hash() ([]byte, error) {
return cn.hash(false)
}
func (cn *cacheNode) hash(flush bool) ([]byte, error) {
if cn.hashVal != nil {
return cn.hashVal, nil
}
pb, err := cn.proto(flush)
if err != nil {
return nil, err
}
ser, err := proto.Marshal(pb)
if err != nil {
return nil, err
}
cn.ser = ser
cn.hashVal = cn.mpt.hashFunc(ser)
return cn.hashVal, nil
}
func (cn *cacheNode) delete() error {
h, err := cn.hash(false)
if err != nil {
return err
}
if !cn.dirty {
if err := cn.mpt.deleteNode(h); err != nil {
return err
}
}
cn.hashVal = nil
cn.ser = nil
return nil
}
func (cn *cacheNode) store() (node, error) {
h, err := cn.hash(true)
if err != nil {
return nil, err
}
if cn.dirty {
if err := cn.mpt.putNode(h, cn.ser); err != nil {
return nil, err
}
cn.dirty = false
}
return newHashNode(cn.mpt, h), nil
}