forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholochain.go
1239 lines (1096 loc) · 27.6 KB
/
holochain.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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
//---------------------------------------------------------------------------------------
// Holochains are a distributed data store: DHT tightly bound to signed hash chains
// for provenance and data integrity.
package holochain
import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"github.com/boltdb/bolt"
"github.com/google/uuid"
ic "github.com/libp2p/go-libp2p-crypto"
mh "github.com/multiformats/go-multihash"
"github.com/op/go-logging"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
const Version string = "0.0.1"
// KeyEntry structure for building KeyEntryType entries
type KeyEntry struct {
ID AgentID
KeyType KeytypeType
Key []byte // marshaled public key
}
// Zome struct encapsulates logically related code, from "chromosome"
type Zome struct {
Name string
Description string
Code string // file name of DNA code
CodeHash Hash
Entries map[string]EntryDef
NucleusType string
}
// Config holds the non-DNA configuration for a holo-chain
type Config struct {
Port int
PeerModeAuthor bool
PeerModeDHTNode bool
BootstrapServer string
}
// Holochain struct holds the full "DNA" of the holochain
type Holochain struct {
Version int
Id uuid.UUID
Name string
Properties map[string]string
PropertiesSchema string
HashType string
BasedOn Hash // holochain hash for base schemas and code
Zomes map[string]*Zome
//---- private values not serialized; initialized on Load
path string
agent Agent
store Persister
encodingFormat string
hashSpec HashSpec
config Config
dht *DHT
node *Node
chain *Chain // the chain itself
}
var log = logging.MustGetLogger("holochain")
// Register function that must be called once at startup by any client app
func Register() {
gob.Register(Header{})
gob.Register(KeyEntry{})
gob.Register(Hash{})
RegisterBultinNucleii()
RegisterBultinPersisters()
logging.SetLevel(logging.INFO, "holochain")
}
func findDNA(path string) (f string, err error) {
p := path + "/" + DNAFileName
matches, err := filepath.Glob(p + ".*")
if err != nil {
return
}
for _, fn := range matches {
s := strings.Split(fn, ".")
f = s[len(s)-1]
if f == "json" || f == "yaml" || f == "toml" {
break
}
f = ""
}
if f == "" {
err = errors.New("DNA not found")
return
}
return
}
// IsConfigured checks a directory for correctly set up holochain configuration files
func (s *Service) IsConfigured(name string) (f string, err error) {
path := s.Path + "/" + name
f, err = findDNA(path)
if err != nil {
return
}
// found a format now check that there's a store
p := path + "/" + StoreFileName + ".db"
if !fileExists(p) {
err = errors.New("chain store missing: " + p)
return
}
return
}
// Load instantiates a Holochain instance
func (s *Service) Load(name string) (h *Holochain, err error) {
f, err := s.IsConfigured(name)
if err != nil {
return
}
h, err = s.load(name, f)
return
}
// New creates a new holochain structure with a randomly generated ID and default values
func New(agent Agent, path string, format string, zomes ...Zome) Holochain {
u, err := uuid.NewUUID()
if err != nil {
panic(err)
}
h := Holochain{
Id: u,
HashType: "sha2-256",
agent: agent,
path: path,
encodingFormat: format,
}
h.PrepareHashType()
h.Zomes = make(map[string]*Zome)
for i, _ := range zomes {
z := zomes[i]
h.Zomes[z.Name] = &z
}
return h
}
// DecodeDNA decodes a Holochain structure from an io.Reader
func DecodeDNA(reader io.Reader, format string) (hP *Holochain, err error) {
var h Holochain
err = Decode(reader, format, &h)
if err != nil {
return
}
hP = &h
hP.encodingFormat = format
return
}
// load unmarshals a holochain structure for the named chain and format
func (s *Service) load(name string, format string) (hP *Holochain, err error) {
path := s.Path + "/" + name
var f *os.File
f, err = os.Open(path + "/" + DNAFileName + "." + format)
if err != nil {
return
}
defer f.Close()
h, err := DecodeDNA(f, format)
if err != nil {
return
}
h.path = path
h.encodingFormat = format
// load the config
f, err = os.Open(path + "/" + ConfigFileName + "." + format)
if err != nil {
return
}
defer f.Close()
err = Decode(f, format, &h.config)
if err != nil {
return
}
// try and get the agent from the holochain instance
agent, err := LoadAgent(path)
if err != nil {
// get the default if not available
agent, err = LoadAgent(filepath.Dir(path))
}
if err != nil {
return
}
h.agent = agent
h.store, err = CreatePersister(BoltPersisterName, path+"/"+StoreFileName+".db")
if err != nil {
return
}
err = h.store.Init()
if err != nil {
return
}
h.chain, err = NewChainFromFile(h.hashSpec, path+"/"+StoreFileName+".dat")
if err != nil {
return
}
if err = h.Prepare(); err != nil {
return
}
hP = h
return
}
// Agent exposes the agent element
func (h *Holochain) Agent() Agent {
return h.agent
}
// PrepareHashType makes sure the given string is a correct multi-hash and stores
// the code and length to the Holochain struct
func (h *Holochain) PrepareHashType() (err error) {
if c, ok := mh.Names[h.HashType]; !ok {
return fmt.Errorf("Unknown hash type: %s", h.HashType)
} else {
h.hashSpec.Code = c
h.hashSpec.Length = -1
}
return
}
// Prepare sets up a holochain to run by:
// validating the DNA, loading the schema validators, setting up a Network node and setting up the DHT
func (h *Holochain) Prepare() (err error) {
if err = h.PrepareHashType(); err != nil {
return
}
for _, z := range h.Zomes {
if !fileExists(h.path + "/" + z.Code) {
return errors.New("DNA specified code file missing: " + z.Code)
}
for k := range z.Entries {
e := z.Entries[k]
sc := e.Schema
if sc != "" {
if !fileExists(h.path + "/" + sc) {
return errors.New("DNA specified schema file missing: " + sc)
} else {
if strings.HasSuffix(sc, ".json") {
if err = e.BuildJSONSchemaValidator(h.path); err != nil {
return err
}
z.Entries[k] = e
}
}
}
}
}
listenaddr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", h.config.Port)
h.node, err = NewNode(listenaddr, h.Agent().PrivKey())
if err != nil {
return
}
h.dht = NewDHT(h)
if h.config.PeerModeDHTNode {
if err = h.dht.StartDHT(); err != nil {
return
}
}
if h.config.PeerModeAuthor {
if err = h.node.StartSrc(h); err != nil {
return
}
}
return
}
// getMetaHash gets a value from the store that's a hash
func (h *Holochain) getMetaHash(key string) (hash Hash, err error) {
v, err := h.store.GetMeta(key)
if err != nil {
return
}
hash.H = v
if v == nil {
err = mkErr("Meta key '" + key + "' uninitialized")
}
return
}
// Path returns a holochain path
func (h *Holochain) Path() string {
return h.path
}
// ID returns a holochain ID hash or err if not yet defined
func (h *Holochain) ID() (id Hash, err error) {
id, err = h.getMetaHash(IDMetaKey)
return
}
// Top returns a hash of top header or err if not yet defined
func (h *Holochain) Top() (top Hash, err error) {
tp, err := h.getMetaHash(TopMetaKey)
top = tp.Clone()
return
}
// Top returns a hash of top header of the given type or err if not yet defined
func (h *Holochain) TopType(t string) (top Hash, err error) {
tp, err := h.getMetaHash(TopMetaKey + "_" + t)
top = tp.Clone()
return
}
// GenChain establishes a holochain instance by creating the initial genesis entries in the chain
// It assumes a properly set up .holochain sub-directory with a config file and
// keys for signing. See GenDev()
func (h *Holochain) GenChain() (keyHash Hash, err error) {
defer func() {
if err != nil {
panic("cleanup after failed gen not implemented! Error was: " + err.Error())
}
}()
_, err = h.ID()
if err == nil {
err = mkErr("chain already started")
return
}
if err = h.Prepare(); err != nil {
return
}
var buf bytes.Buffer
err = h.EncodeDNA(&buf)
e := GobEntry{C: buf.Bytes()}
var dnaHeader *Header
_, dnaHeader, err = h.NewEntry(time.Now(), DNAEntryType, &e)
if err != nil {
return
}
var k KeyEntry
k.ID = h.agent.ID()
k.KeyType = h.agent.KeyType()
pk := h.agent.PrivKey().GetPublic()
k.Key, err = ic.MarshalPublicKey(pk)
if err != nil {
return
}
e.C = k
keyHash, _, err = h.NewEntry(time.Now(), KeyEntryType, &e)
if err != nil {
return
}
err = h.store.PutMeta(IDMetaKey, dnaHeader.EntryLink.H)
if err != nil {
return
}
// run the init functions of each zome
for _, z := range h.Zomes {
var n Nucleus
n, err = h.makeNucleus(z)
if err != nil {
err = n.InitChain()
if err != nil {
return
}
}
}
return
}
// GenFrom copies DNA files from a source
func (s *Service) GenFrom(srcPath string, path string) (hP *Holochain, err error) {
hP, err = gen(path, func(path string) (hP *Holochain, err error) {
format, err := findDNA(srcPath)
if err != nil {
return
}
f, err := os.Open(srcPath + "/" + DNAFileName + "." + format)
if err != nil {
return
}
defer f.Close()
h, err := DecodeDNA(f, format)
if err != nil {
return
}
agent, err := LoadAgent(filepath.Dir(path))
if err != nil {
return
}
h.path = path
h.agent = agent
// make a config file
err = makeConfig(h, s)
if err != nil {
return
}
// generate a new UUID
u, err := uuid.NewUUID()
if err != nil {
return
}
h.Id = u
if err = CopyDir(srcPath+"/ui", path+"/ui"); err != nil {
return
}
if err = CopyFile(srcPath+"/schema_properties.json", path+"/schema_properties.json"); err != nil {
return
}
if err = CopyDir(srcPath+"/test", path+"/test"); err != nil {
return
}
for _, z := range h.Zomes {
var bs []byte
bs, err = readFile(srcPath, z.Code)
if err != nil {
return
}
if err = writeFile(path, z.Code, bs); err != nil {
return
}
for k := range z.Entries {
e := z.Entries[k]
sc := e.Schema
if sc != "" {
if err = CopyFile(srcPath+"/"+sc, path+"/"+sc); err != nil {
return
}
}
}
}
hP = h
return
})
return
}
// TestData holds a test entry for a chain
type TestData struct {
Zome string
FnName string
Input string
Output string
Err string
}
func makeConfig(h *Holochain, s *Service) error {
h.config.Port = DefaultPort
h.config.PeerModeDHTNode = s.Settings.DefaultPeerModeDHTNode
h.config.PeerModeAuthor = s.Settings.DefaultPeerModeAuthor
p := h.path + "/" + ConfigFileName + "." + h.encodingFormat
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
return Encode(f, h.encodingFormat, &h.config)
}
// GenDev generates starter holochain DNA files from which to develop a chain
func (s *Service) GenDev(path string, format string) (hP *Holochain, err error) {
hP, err = gen(path, func(path string) (hP *Holochain, err error) {
agent, err := LoadAgent(filepath.Dir(path))
if err != nil {
return
}
zomes := []Zome{
Zome{Name: "myZome",
Description: "this is a zygomas test zome",
NucleusType: ZygoNucleusType,
Entries: map[string]EntryDef{
"myData": EntryDef{Name: "myData", DataFormat: "zygo"},
"primes": EntryDef{Name: "primes", DataFormat: "JSON"},
"profile": EntryDef{Name: "profile", DataFormat: "JSON", Schema: "schema_profile.json"},
},
},
Zome{Name: "jsZome",
Description: "this is a javascript test zome",
NucleusType: JSNucleusType,
Entries: map[string]EntryDef{
"myOdds": EntryDef{Name: "myOdds", DataFormat: "js"},
"profile": EntryDef{Name: "profile", DataFormat: "JSON", Schema: "schema_profile.json"},
},
},
}
h := New(agent, path, format, zomes...)
err = makeConfig(&h, s)
if err != nil {
return
}
schema := `{
"title": "Properties Schema",
"type": "object",
"properties": {
"description": {
"type": "string"
},
"language": {
"type": "string"
}
}
}`
if err = writeFile(path, "schema_properties.json", []byte(schema)); err != nil {
return
}
h.PropertiesSchema = "schema_properties.json"
h.Properties = map[string]string{
"description": "a bogus test holochain",
"language": "en"}
schema = `{
"title": "Profile Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}`
if err = writeFile(path, "schema_profile.json", []byte(schema)); err != nil {
return
}
fixtures := [7]TestData{
TestData{
Zome: "myZome",
FnName: "addData",
Input: "2",
Output: "%h%"},
TestData{
Zome: "myZome",
FnName: "addData",
Input: "4",
Output: "%h%"},
TestData{
Zome: "myZome",
FnName: "addData",
Input: "5",
Err: "Error calling 'commit': Invalid entry: 5"},
TestData{
Zome: "myZome",
FnName: "addPrime",
Input: "{\"prime\":7}",
Output: "\"%h%\""}, // quoted because return value is json
TestData{
Zome: "myZome",
FnName: "addPrime",
Input: "{\"prime\":4}",
Err: `Error calling 'commit': Invalid entry: {"Atype":"hash", "prime":4, "zKeyOrder":["prime"]}`},
TestData{
Zome: "jsZome",
FnName: "addProfile",
Input: `{"firstName":"Art","lastName":"Brock"}`,
Output: `"%h%"`},
TestData{
Zome: "jsZome",
FnName: "getProperty",
Input: "_id",
Output: "%id%"},
}
fixtures2 := [2]TestData{
TestData{
Zome: "jsZome",
FnName: "addOdd",
Input: "7",
Output: "%h%"},
TestData{
Zome: "jsZome",
FnName: "addOdd",
Input: "2",
Err: "Invalid entry: 2"},
}
ui := `
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
function send() {
$.post(
"/fn/"+$('select[name=zome]').val()+"/"+$('select[name=fn]').val(),
$('#data').val(),
function(data) {
$("#result").html("result:"+data)
$("#err").html("")
}
).error(function(response) {
$("#err").html(response.responseText)
$("#result").html("")
})
;
};
</script>
</head>
<body>
<select id="zome" name="zome">
<option value="myZome">myZome</option>
</select>
<select id="fn" name="fn">
<option value="addData">addData</option>
</select>
<input id="data" name="data">
<button onclick="send();">Send</button>
send an even number and get back a hash, send and odd end get a error
<div id="result"></div>
<div id="err"></div>
</body>
</html>
`
uiPath := path + "/ui"
if err = os.MkdirAll(uiPath, os.ModePerm); err != nil {
return nil, err
}
if err = writeFile(uiPath, "index.html", []byte(ui)); err != nil {
return
}
code := make(map[string]string)
code["myZome"] = `
(expose "exposedfn" STRING)
(defn exposedfn [x] (concat "result: " x))
(expose "addData" STRING)
(defn addData [x] (commit "myData" x))
(expose "addPrime" JSON)
(defn addPrime [x] (commit "primes" x))
(defn validate [entryType entry]
(cond (== entryType "myData") (cond (== (mod entry 2) 0) true false)
(== entryType "primes") (isprime (hget entry %prime))
(== entryType "profile") true
false)
)
(defn init [] true)
`
code["jsZome"] = `
expose("getProperty",HC.STRING);
function getProperty(x) {return property(x)};
expose("addOdd",HC.STRING);
function addOdd(x) {return commit("myOdds",x);}
expose("addProfile",HC.JSON);
function addProfile(x) {return commit("profile",x);}
function validate(entry_type,entry) {
if (entry_type=="myOdds") {
return entry%2 != 0
}
if (entry_type=="profile") {
return true
}
return false
}
function init() {return true}
`
testPath := path + "/test"
if err = os.MkdirAll(testPath, os.ModePerm); err != nil {
return nil, err
}
for n, _ := range h.Zomes {
z, _ := h.Zomes[n]
switch z.NucleusType {
case JSNucleusType:
z.Code = fmt.Sprintf("zome_%s.js", z.Name)
case ZygoNucleusType:
z.Code = fmt.Sprintf("zome_%s.zy", z.Name)
default:
err = fmt.Errorf("unknown nucleus type:%s", z.NucleusType)
return
}
c, _ := code[z.Name]
if err = writeFile(path, z.Code, []byte(c)); err != nil {
return
}
}
// write out the tests
for i, d := range fixtures {
fn := fmt.Sprintf("%d.json", i)
var j []byte
t := []TestData{d}
j, err = json.Marshal(t)
if err != nil {
return
}
if err = writeFile(testPath, fn, j); err != nil {
return
}
}
// also write out some grouped tests
fn := fmt.Sprintf("%d.json", len(fixtures))
var j []byte
j, err = json.Marshal(fixtures2)
if err != nil {
return
}
if err = writeFile(testPath, fn, j); err != nil {
return
}
hP = &h
return
})
return
}
// gen calls a make function which should build the holochain structure and supporting files
func gen(path string, makeH func(path string) (hP *Holochain, err error)) (h *Holochain, err error) {
if dirExists(path) {
return nil, mkErr(path + " already exists")
}
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return nil, err
}
// cleanup the directory if we enounter an error while generating
defer func() {
if err != nil {
os.RemoveAll(path)
}
}()
h, err = makeH(path)
if err != nil {
return
}
h.Name = filepath.Base(path)
h.chain, err = NewChainFromFile(h.hashSpec, path+"/"+StoreFileName+".dat")
if err != nil {
return
}
h.store, err = CreatePersister(BoltPersisterName, path+"/"+StoreFileName+".db")
if err != nil {
return
}
err = h.store.Init()
if err != nil {
return
}
err = h.SaveDNA(false)
if err != nil {
return
}
return
}
// EncodeDNA encodes a holochain's DNA to an io.Writer
func (h *Holochain) EncodeDNA(writer io.Writer) (err error) {
return Encode(writer, h.encodingFormat, &h)
}
// SaveDNA writes the holochain DNA to a file
func (h *Holochain) SaveDNA(overwrite bool) (err error) {
p := h.path + "/" + DNAFileName + "." + h.encodingFormat
if !overwrite && fileExists(p) {
return mkErr(p + " already exists")
}
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
err = h.EncodeDNA(f)
return
}
// GenDNAHashes generates hashes for all the definition files in the DNA.
// This function should only be called by developer tools at the end of the process
// of finalizing DNA development or versioning
func (h *Holochain) GenDNAHashes() (err error) {
var b []byte
for _, z := range h.Zomes {
code := z.Code
b, err = readFile(h.path, code)
if err != nil {
return
}
err = z.CodeHash.Sum(h.hashSpec, b)
if err != nil {
return
}
for i, e := range z.Entries {
sc := e.Schema
if sc != "" {
b, err = readFile(h.path, sc)
if err != nil {
return
}
err = e.SchemaHash.Sum(h.hashSpec, b)
if err != nil {
return
}
z.Entries[i] = e
}
}
}
err = h.SaveDNA(true)
return
}
// NewEntry adds an entry and it's header to the chain and returns the header and it's hash
func (h *Holochain) NewEntry(now time.Time, t string, entry Entry) (hash Hash, header *Header, err error) {
// this is extra for now.
_, err = h.chain.AddEntry(h.hashSpec, now, t, entry, h.agent.PrivKey())
if err != nil {
return
}
// get the current top of the chain
ph, err := h.Top()
if err != nil {
ph = NullHash()
}
// and also the the top entry of this type
pth, err := h.TopType(t)
if err != nil {
pth = NullHash()
}
hash, header, err = newHeader(h.hashSpec, now, t, entry, h.agent.PrivKey(), ph, pth)
if err != nil {
return
}
// @TODO
// we have to do this stuff because currently we are persisting immediatly.
// instead we should be persisting from the Chain object.
// encode the header for saving
b, err := header.Marshal()
if err != nil {
return
}
// encode the entry into bytes
m, err := entry.Marshal()
if err != nil {
return
}
err = h.store.Put(t, hash, b, header.EntryLink, m)
return
}
// get low level access to entries/headers (only works inside a bolt transaction)
func get(hb *bolt.Bucket, eb *bolt.Bucket, key []byte, getEntry bool) (header Header, entry interface{}, err error) {
v := hb.Get(key)
err = header.Unmarshal(v, 34)
if err != nil {
return
}
if getEntry {
v = eb.Get(header.EntryLink.H)
var g GobEntry
err = g.Unmarshal(v)
if err != nil {
return
}
entry = g.C
}
return
}
func (h *Holochain) Walk(fn func(key *Hash, h *Header, entry interface{}) error, entriesToo bool) (err error) {
nullHash := NullHash()
err = h.store.(*BoltPersister).DB().View(func(tx *bolt.Tx) error {
hb := tx.Bucket([]byte(HeaderBucket))
eb := tx.Bucket([]byte(EntryBucket))
mb := tx.Bucket([]byte(MetaBucket))
key := mb.Get([]byte(TopMetaKey))
var keyH Hash
var header Header
var visited = make(map[string]bool)
for err == nil && !bytes.Equal(nullHash.H, key) {
keyH.H = key
// build up map of all visited headers to prevent loops
s := string(key)
_, present := visited[s]
if present {
err = errors.New("loop detected in walk")
} else {
visited[s] = true
var e interface{}
header, e, err = get(hb, eb, key, entriesToo)
if err == nil {
err = fn(&keyH, &header, e)
key = header.HeaderLink.H
}
}
}
if err != nil {
return err
}
// if the last item doesn't gets us to bottom, i.e. the header who's entry link is
// the same as ID key then, the chain is invalid...
if !bytes.Equal(header.EntryLink.H, mb.Get([]byte(IDMetaKey))) {
return errors.New("chain didn't end at DNA!")
}
return err
})
return
}
// Validate scans back through a chain to the beginning confirming that the last header points to DNA
// This is actually kind of bogus on your own chain, because theoretically you put it there! But
// if the holochain file was copied from somewhere you can consider this a self-check
func (h *Holochain) Validate(entriesToo bool) (valid bool, err error) {
err = h.Walk(func(key *Hash, header *Header, entry interface{}) (err error) {
// confirm the correctness of the header hash
var bH Hash
bH, _, err = header.Sum(h.hashSpec)
if err != nil {
return
}