-
Notifications
You must be signed in to change notification settings - Fork 1
/
treap.go
196 lines (158 loc) · 3.56 KB
/
treap.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/rarimo/rarimo-core/x/identity/types"
)
const EmptyHashStr = "0x"
// Treap implements dynamic Merkle tree using treap data structure.
// Proof of concept: https://github.com/olegfomenko/go-treap-merkle
type Treap struct {
Keeper
}
func (t Treap) Split(ctx sdk.Context, root, key string) (string, string) {
if root == EmptyHashStr {
return EmptyHashStr, EmptyHashStr
}
node, ok := t.GetNode(ctx, root)
if !ok {
return EmptyHashStr, EmptyHashStr
}
if less(root, key) {
r1, r2 := t.Split(ctx, node.Right, key)
node.Right = r1
t.updateNode(ctx, &node)
t.SetNode(ctx, node)
return root, r2
}
r1, r2 := t.Split(ctx, node.Left, key)
node.Left = r2
t.updateNode(ctx, &node)
t.SetNode(ctx, node)
return r1, root
}
func (t Treap) Merge(ctx sdk.Context, r1, r2 string) string {
if r1 == EmptyHashStr {
return r2
}
if r2 == EmptyHashStr {
return r1
}
node1, ok := t.GetNode(ctx, r1)
if !ok {
return EmptyHashStr
}
node2, ok := t.GetNode(ctx, r2)
if !ok {
return EmptyHashStr
}
if node1.Priority > node2.Priority {
node1.Right = t.Merge(ctx, node1.Right, r2)
t.updateNode(ctx, &node1)
t.SetNode(ctx, node1)
return r1
}
node2.Left = t.Merge(ctx, r1, node2.Left)
t.updateNode(ctx, &node2)
t.SetNode(ctx, node2)
return r2
}
func (t Treap) Remove(ctx sdk.Context, key string) {
root := t.GetRootKey(ctx)
if root == EmptyHashStr {
return
}
r1, r2 := t.Split(ctx, root, key)
if r2 == key {
node, _ := t.GetNode(ctx, r2)
t.RemoveNode(ctx, r2)
t.SetRootKey(ctx, t.Merge(ctx, r1, node.Right))
return
}
root = r2
for {
node, ok := t.GetNode(ctx, root)
if !ok {
return
}
if node.Left == key {
node.Left = EmptyHashStr
t.RemoveNode(ctx, key)
t.updateNode(ctx, &node)
t.SetNode(ctx, node)
break
}
root = node.Left
}
t.SetRootKey(ctx, t.Merge(ctx, r1, r2))
}
func (t Treap) Insert(ctx sdk.Context, key string, priority uint64) {
node := types.Node{
Key: key,
Priority: priority,
Left: EmptyHashStr,
Right: EmptyHashStr,
ChildrenHash: EmptyHashStr,
Hash: key,
}
t.SetNode(ctx, node)
root := t.GetRootKey(ctx)
if root == EmptyHashStr {
t.SetRootKey(ctx, node.Key)
return
}
r1, r2 := t.Split(ctx, root, key)
r1 = t.Merge(ctx, r1, key)
t.SetRootKey(ctx, t.Merge(ctx, r1, r2))
}
func (t Treap) MerklePath(ctx sdk.Context, key string) []string {
current := t.GetRootKey(ctx)
result := make([]string, 0, 64)
for current != EmptyHashStr {
node, ok := t.GetNode(ctx, current)
if !ok {
return nil
}
if current == key {
result = append(result, node.ChildrenHash)
return result
}
if less(current, key) {
result = append(result, current)
left, ok := t.GetNode(ctx, node.Left)
if ok {
result = append(result, left.Hash)
}
current = node.Right
continue
}
result = append(result, current)
right, ok := t.GetNode(ctx, node.Right)
if ok {
result = append(result, right.Hash)
}
current = node.Left
}
return nil
}
func (t Treap) updateNode(ctx sdk.Context, node *types.Node) {
node.ChildrenHash = t.merkleHashNodes(ctx, node.Left, node.Right)
if node.ChildrenHash == EmptyHashStr {
node.Hash = node.Key
return
}
node.Hash = hash(node.ChildrenHash, node.Key)
}
func (t Treap) merkleHashNodes(ctx sdk.Context, left, right string) string {
l, okl := t.GetNode(ctx, left)
r, okr := t.GetNode(ctx, right)
if !okl && !okr {
return EmptyHashStr
}
if !okr {
return l.Hash
}
if !okl {
return r.Hash
}
return hash(r.Hash, l.Hash)
}