forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain.go
365 lines (320 loc) · 7.75 KB
/
chain.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (C) 2013-2017, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// implements in-memory chain representation with marshaling, & validation
package holochain
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
ic "github.com/libp2p/go-libp2p-crypto"
"io"
"os"
"time"
)
// WalkerFn a function type for call Walk
type WalkerFn func(key *Hash, header *Header, entry Entry) error
var ErrHashNotFound error = errors.New("hash not found")
// Chain structure for providing in-memory access to chain data, entries headers and hashes
type Chain struct {
Hashes []Hash
Headers []*Header
Entries []Entry
TypeTops map[string]int // pointer to index of top of a given type
Hmap map[string]int // map header hashes to index number
Emap map[string]int // map entry hashes to index number
//---
s *os.File // if this stream is not nil, new entries will get marshaled to it
}
// NewChain creates and empty chain
func NewChain() (chain *Chain) {
c := Chain{
Headers: make([]*Header, 0),
Entries: make([]Entry, 0),
Hashes: make([]Hash, 0),
TypeTops: make(map[string]int),
Hmap: make(map[string]int),
Emap: make(map[string]int),
}
chain = &c
return
}
// Creates a chain from a file, loading any data there, and setting it to be persisted to
// if no file exists it will be created
func NewChainFromFile(h HashSpec, path string) (c *Chain, err error) {
c = NewChain()
var f *os.File
if fileExists(path) {
f, err = os.Open(path)
if err != nil {
return
}
var i int
for {
var header *Header
var e Entry
header, e, err = readPair(f)
if err != nil && err.Error() == "EOF" {
err = nil
break
}
if err != nil {
return
}
c.addPair(header, e, i)
i++
}
f.Close()
i--
// if we read anything then we have to calculate the final hash and add it
if i >= 0 {
hd := c.Headers[i]
var hash Hash
// hash the header
hash, _, err = hd.Sum(h)
if err != nil {
return
}
c.Hashes = append(c.Hashes, hash)
c.Hmap[hash.String()] = i
// finally validate that it all hashes out correctly
/* err = c.Validate(h)
if err != nil {
return
}
*/
}
f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
return
}
} else {
f, err = os.Create(path)
if err != nil {
return
}
}
c.s = f
return
}
// Top returns the latest header
func (c *Chain) Top() (header *Header) {
l := len(c.Headers)
if l > 0 {
header = c.Headers[l-1]
}
return
}
// TopType returns the latest header of a given type
func (c *Chain) TopType(entryType string) (header *Header) {
i, ok := c.TypeTops[entryType]
if ok {
header = c.Headers[i]
}
return
}
// AddEntry creates a new header and adds it to a chain
func (c *Chain) AddEntry(h HashSpec, now time.Time, entryType string, e Entry, key ic.PrivKey) (hash Hash, err error) {
// get the previous hashes
var ph, pth Hash
//@TODO make this transactional
l := len(c.Hashes)
if l == 0 {
ph = NullHash()
} else {
ph = c.Hashes[l-1]
}
i, ok := c.TypeTops[entryType]
if !ok {
pth = NullHash()
} else {
pth = c.Hashes[i]
}
var g GobEntry
g = *e.(*GobEntry)
hash, header, err := newHeader(h, now, entryType, e, key, ph, pth)
c.Hashes = append(c.Hashes, hash)
c.Headers = append(c.Headers, header)
c.Entries = append(c.Entries, &g)
c.TypeTops[entryType] = l
c.Emap[header.EntryLink.String()] = l
c.Hmap[hash.String()] = l
if c.s != nil {
err = writePair(c.s, header, &g)
}
return
}
// Get returns the header of a given hash
func (c *Chain) Get(h Hash) (header *Header, err error) {
i, ok := c.Hmap[h.String()]
if ok {
header = c.Headers[i]
} else {
err = ErrHashNotFound
}
return
}
// GetEntry returns the entry of a given entry hash
func (c *Chain) GetEntry(h Hash) (entry Entry, err error) {
i, ok := c.Emap[h.String()]
if ok {
entry = c.Entries[i]
} else {
err = ErrHashNotFound
}
return
}
// GetEntryHeader returns the header of a given entry hash
func (c *Chain) GetEntryHeader(h Hash) (header *Header, err error) {
i, ok := c.Emap[h.String()]
if ok {
header = c.Headers[i]
} else {
err = ErrHashNotFound
}
return
}
func writePair(writer io.Writer, header *Header, entry Entry) (err error) {
err = MarshalHeader(writer, header)
if err != nil {
return
}
err = MarshalEntry(writer, entry)
return
}
func readPair(reader io.Reader) (header *Header, entry Entry, err error) {
var hd Header
err = UnmarshalHeader(reader, &hd, 34)
if err != nil {
return
}
header = &hd
entry, err = UnmarshalEntry(reader)
return
}
// MarshalChain serializes a chain data to a writer
func (c *Chain) MarshalChain(writer io.Writer) (err error) {
var l = uint64(len(c.Headers))
err = binary.Write(writer, binary.LittleEndian, l)
if err != nil {
return err
}
for i, h := range c.Headers {
e := c.Entries[i]
err = writePair(writer, h, e)
if err != nil {
return
}
}
hash := c.Hashes[l-1]
err = hash.MarshalHash(writer)
return
}
// addPair adds header and entry pairs to the chain during unmarshaling
// This call assumes that Hashes array is one element behind the Headers and Entries
// because for each pair (except the 0th) it adds the hash of the previous entry
// thus it also means that you must add the last Hash after you have finished calling addPair
func (c *Chain) addPair(header *Header, entry Entry, i int) {
if i > 0 {
s := header.HeaderLink.String()
h, _ := NewHash(s)
c.Hashes = append(c.Hashes, h)
c.Hmap[s] = i - 1
}
c.Headers = append(c.Headers, header)
c.Entries = append(c.Entries, entry)
c.TypeTops[header.Type] = i
c.Emap[header.EntryLink.String()] = i
}
// UnmarshalChain unserializes a chain from a reader
func UnmarshalChain(reader io.Reader) (c *Chain, err error) {
c = NewChain()
var l, i uint64
err = binary.Read(reader, binary.LittleEndian, &l)
if err != nil {
return
}
for i = 0; i < l; i++ {
var header *Header
var e Entry
header, e, err = readPair(reader)
if err != nil {
return
}
c.addPair(header, e, int(i))
}
// decode final hash
var h Hash
err = h.UnmarshalHash(reader)
if err != nil {
return
}
c.Hashes = append(c.Hashes, h)
c.Hmap[h.String()] = int(i - 1)
return
}
// Walk traverses chain from most recent to first entry calling fn on each one
func (c *Chain) Walk(fn WalkerFn) (err error) {
l := len(c.Headers)
for i := l - 1; i >= 0; i-- {
err = fn(&c.Hashes[i], c.Headers[i], c.Entries[i])
if err != nil {
return
}
}
return
}
// Validate traverses chain confirming the hashes
// @TODO confirm that TypeLinks are also correct
// @TODO confirm signatures
func (c *Chain) Validate(h HashSpec) (err error) {
l := len(c.Headers)
for i := l - 1; i >= 0; i-- {
hd := c.Headers[i]
var hash Hash
// hash the header
hash, _, err = hd.Sum(h)
if err != nil {
return
}
var nexth Hash
if i == l-1 {
nexth = c.Hashes[i]
} else {
nexth = c.Headers[i+1].HeaderLink
}
if !bytes.Equal(hash.H, nexth.H) {
err = fmt.Errorf("header hash mismatch at link %d", i)
return
}
var b []byte
b, err = c.Entries[i].Marshal()
if err != nil {
return
}
err = hash.Sum(h, b)
if err != nil {
return
}
if !bytes.Equal(hash.H, hd.EntryLink.H) {
err = fmt.Errorf("entry hash mismatch at link %d", i)
return
}
}
return
}
// String converts a chain to a textual dump of the headers and entries
func (c *Chain) String() string {
l := len(c.Headers)
r := ""
for i := 0; i < l; i++ {
r += fmt.Sprintf("Header:%v\n", *c.Headers[i])
r += fmt.Sprintf("Entry:%v\n\n", c.Entries[i])
}
r += "Hashlist:\n"
for i := 0; i < len(c.Headers); i++ {
r += fmt.Sprintf("%s\n", c.Hashes[i].String())
}
return r
}