-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromethoni_x_trie.go
176 lines (149 loc) · 3.67 KB
/
promethoni_x_trie.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package PromethoniXTrie
import (
"bytes"
"github.com/nacamp/go-simplechain/storage"
)
// PromethoniXTrie is a Merkle Patricia Trie, consists of three kinds of nodes,
// Branch Node: 16-elements array, value is [hash_0, hash_1, ..., hash_f, hash]
// Extension Node: 3-elements array, value is [ext flag, prefix path, next hash]
// Leaf Node: 3-elements array, value is [leaf flag, suffix path, value]
type PromethoniXTrie struct {
ActionLog
rootHash Hash
storage storage.Storage
}
var rootHashKey = []byte{0, 0, 1, 1}
func NewPromethoniXTrie(
isActionLogEnabled bool,
) (*PromethoniXTrie, error) {
dbStorage, err := storage.NewLevelDBStorage("./db")
if err != nil {
return nil, err
}
rootHash, err := dbStorage.Get(rootHashKey)
if err != nil && err != ErrNotFound {
return nil, err
}
t := &PromethoniXTrie{
rootHash: rootHash,
storage: dbStorage,
ActionLog: ActionLog{
IsActionLogEnabled: isActionLogEnabled,
},
}
if t.rootHash == nil || len(t.rootHash) == 0 {
return t, nil
} else if _, err := t.storage.Get(rootHash); err != nil {
return nil, err
}
return t, nil
}
func (trie *PromethoniXTrie) IsEmpty() bool {
return trie.rootHash == nil
}
func (trie *PromethoniXTrie) RootHash() Hash {
return trie.rootHash
}
// CommitNode node in trie into storage
func (trie *PromethoniXTrie) commitNode(n Node) error {
if err := n.EncodeAndHash(); err != nil {
return err
}
return trie.storage.Put(n.Details().Hash, n.Details().EncodedData)
}
func (trie *PromethoniXTrie) fetchNode(hash Hash) (Node, error) {
raw, err := trie.storage.Get(hash)
if err != nil {
return nil, err
}
return DecodeNode(raw)
}
func (trie *PromethoniXTrie) Get(key Hash) (Data, error) {
rootHash := trie.rootHash
route := keyToRoute(key)
for len(route) >= 0 {
rootNode, err := trie.fetchNode(rootHash)
if err != nil {
return nil, err
}
if rootNode.Type() == Leaf {
if !bytes.Equal(AsLeaf(rootNode).Path, route) {
break
}
return AsLeaf(rootNode).Value, nil
} else if len(route) == 0 {
break
} else {
rootHash, route, err = rootNode.NextRoute(route)
if err != nil {
return nil, err
}
}
}
return nil, ErrNotFound
}
func (trie *PromethoniXTrie) Put(key Hash, value Data) (Hash, error) {
var oldData Data = nil
var err error
action := Update
if trie.IsActionLogEnabled {
oldData, err = trie.Get(key)
if err != nil {
action = Insert
}
}
newHash, err := trie.update(trie.rootHash, keyToRoute(key), value)
if err != nil {
return nil, err
}
trie.rootHash = newHash
err = trie.storage.Put(rootHashKey, newHash)
if err != nil {
return nil, err
}
if trie.IsActionLogEnabled {
entry := &ActionLogEntry{action, key, oldData, value}
trie.ActionLogEntries = append(trie.ActionLogEntries, entry)
}
return newHash, nil
}
// Delete the node's value in trie
/*
1. ext(ext->leaf-->leaf,ext->ext--->ext)
2. branch(branch->leaf-->leaf,branch->branch-->ext->branch,branch->ext-->ext)
ext ext
| |
branch --> leaf --> leaf
/ \
[leaf] leaf
branch ext
/ \ |
[leaf] ext --> branch
|
branch
branch ext
/ \ |
[leaf] branch --> branch
/ \ / \
leaf leaf leaf leaf
*/
func (trie *PromethoniXTrie) Delete(key Hash) (Hash, error) {
var oldData Data = nil
if trie.IsActionLogEnabled {
oldData, _ = trie.Get(key)
}
newHash, err := trie.delete(trie.rootHash, keyToRoute(key))
if err != nil {
return nil, err
}
trie.rootHash = newHash
err = trie.storage.Put(rootHashKey, newHash)
if err != nil {
return nil, err
}
if trie.IsActionLogEnabled {
entry := &ActionLogEntry{Delete, key, oldData, nil}
trie.ActionLogEntries = append(trie.ActionLogEntries, entry)
}
return newHash, nil
}