-
Notifications
You must be signed in to change notification settings - Fork 26
/
node_leaf.go
205 lines (165 loc) · 5.23 KB
/
node_leaf.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
197
198
199
200
201
202
203
204
205
package graviton
import "io"
import "bytes"
import "fmt"
import "encoding/binary"
import "golang.org/x/xerrors"
var gET_CHECKED bool = true // all gets go through value checks
type leaf struct {
findex, fpos uint32
key []byte
keybuf [MINBLOCK]byte
keyhash [HASHSIZE]byte
hash_check [HASHSIZE]byte // used to verify check
hash [HASHSIZE]byte
leaf_init bool
value []byte
dirty bool
//dirtyhash bool
loaded_partial bool
}
func newLeaf(keyhash [HASHSIZE]byte, key, value []byte) *leaf {
key_copy := make([]byte, len(key))
copy(key_copy, key[:])
value_copy := make([]byte, len(value))
copy(value_copy, value[:])
l := &leaf{
dirty: true, // new leaf is by default dirty
keyhash: keyhash,
value: value_copy,
}
l.key = append(l.keybuf[:0], key...)
rst := sum(l.value)
copy(l.hash_check[:], leafHash(l.keyhash[:], rst[:]))
copy(l.hash[:], l.hash_check[:])
l.leaf_init = true
//fmt.Printf("leafhash new %x key '%s' value '%s'\n", l.hash_check, string(key), string(value))
return l
}
func leafHash(hkey, hvalue []byte) []byte {
rst := make([]byte, 0, HASHSIZE)
h := hasher()
h.Write([]byte{leafNODE})
h.Write(hkey)
h.Write(hvalue)
rst = h.Sum(rst)
return rst
}
func (l *leaf) Hash(store *Store) ([]byte, error) {
if l.loaded_partial { // if leaf is loaded partially, load it fully now
if err := l.loadfullleaffromstore(store); err != nil {
return nil, err
}
}
return l.hash[:], nil
}
func (l *leaf) isDirty() bool {
return l.dirty
}
func (l *leaf) Position() (uint32, uint32) {
return l.findex, l.fpos
}
// this always assummes that keyhash already matches to new keyhash
// this function is only used once , in node_inner.go insert
func (l *leaf) Put(store *Store, keyhash [HASHSIZE]byte, value []byte) error {
if l.loaded_partial { // if leaf is loaded partially, load it fully now
if err := l.loadfullleaffromstore(store); err != nil {
return err
}
}
// overwrite created new branch. Old versions are all accessible using previous root
l.value = value
rst := sum(l.value)
copy(l.hash[:], leafHash(l.keyhash[:], rst[:])) // use hash of key and hash of value
copy(l.hash_check[:], l.hash[:])
l.dirty = true
l.findex, l.fpos = 0, 0
return nil
}
// should we return a copy
func (l *leaf) Get(store *Store, keyhash [HASHSIZE]byte) ([]byte, error) {
if l.loaded_partial { // if leaf is loaded partially, load it fully now
if err := l.loadfullleaffromstore(store); err != nil {
return nil, err
}
}
if l.keyhash == keyhash {
return l.value, nil
}
return nil, xerrors.Errorf("%w: collision, keyhash %x not found, keyhash in ram %x", ErrNotFound, keyhash,l.keyhash)
}
func (l *leaf) Delete(store *Store, keyhash [HASHSIZE]byte) (bool, bool, error) {
if l.loaded_partial { // if leaf is loaded partially, load it fully now
if err := l.loadfullleaffromstore(store); err != nil {
return false, false, err
}
}
match := l.keyhash == keyhash
return match, match, nil
}
func (l *leaf) load_partial(store *Store) error {
if l.loaded_partial { // if leaf is loaded partially, load it fully now
return l.loadfullleaffromstore(store)
}
return nil
}
func (l *leaf) loadfullleaffromstore(store *Store) error { // loading leaf from store
//fmt.Printf("loading leaf findex %d fpos %d\n", l.findex, l.fpos)
if l.findex <= 0 && l.fpos <= 0 {
return xerrors.Errorf("Invalid findex %d fpos %d", l.findex, l.fpos)
}
var buf_array [4 * MINBLOCK]byte
buf := buf_array[:]
read_again:
_, err := store.read(l.findex, l.fpos, buf[:]) // atleast keylen, key, valuelen will be available in this read, if value is small,it's also available
if err != nil && err != io.EOF {
return err
}
var done int
l.key = l.keybuf[:0]
l.value = l.value[:0]
if value, bytecount := binary.Uvarint(buf[:]); bytecount > 0 {
l.key = append(l.keybuf[:0], buf[bytecount:uint64(bytecount)+value]...)
done += bytecount + int(value)
} else {
return xerrors.Errorf("invalid key size")
}
if value, bytecount := binary.Uvarint(buf[done:]); bytecount > 0 {
if done+bytecount+int(value) > len(buf) {
buf = make([]byte, done+bytecount+int(value), done+bytecount+int(value))
goto read_again
}
//fmt.Printf("value %d bytecount %d done %d len(buf) %d\n", value,bytecount, done, len(buf))
l.value = append(l.value, buf[done+bytecount:done+bytecount+int(value)]...)
done += bytecount + int(value)
} else {
return xerrors.Errorf("invalid value size")
}
// time for data integrity
l.keyhash = sum(l.key)
// we also need to calculate hash, see whether it matches with what is stored
rst := sum(l.value)
copy(l.hash[:], leafHash(l.keyhash[:], rst[:])) // use hash of key and hash of value
if gET_CHECKED {
if bytes.Compare(l.hash_check[:], l.hash[:]) != 0 {
//fmt.Printf("hash_check %x hash %x keyhash %x\n", l.hash_check, l.hash, l.keyhash)
return fmt.Errorf("Key/Value data Corruption, key '%x'", l.key)
}
}
l.loaded_partial = false
return nil
}
func (l *leaf) Prove(store *Store, keyhash [HASHSIZE]byte, proof *Proof) error {
if l.loaded_partial { // if leaf is loaded partially, load it fully now
if err := l.loadfullleaffromstore(store); err != nil {
return err
}
}
if l.keyhash == keyhash {
proof.addValue(l.value)
return nil
}
rst := sum(l.value)
proof.addCollision(l.keyhash[:], rst[:])
return nil
}