forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzygonucleus.go
577 lines (514 loc) · 14 KB
/
zygonucleus.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
// 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
//----------------------------------------------------------------------------------------
// ZygoNucleus implements a zygomys use of the Nucleus interface
package holochain
import (
"encoding/json"
"errors"
"fmt"
zygo "github.com/glycerine/zygomys/repl"
"math"
"strconv"
"strings"
"time"
)
const (
ZygoNucleusType = "zygo"
)
type ZygoNucleus struct {
env *zygo.Glisp
interfaces []Interface
lastResult zygo.Sexp
}
// Name returns the string value under which this nucleus is registered
func (z *ZygoNucleus) Type() string { return ZygoNucleusType }
// InitChain runs the application init function
// this function gets called after the genesis entries are added to the chain
func (z *ZygoNucleus) InitChain() (err error) {
err = z.env.LoadString(`(init)`)
if err != nil {
return
}
result, err := z.env.Run()
if err != nil {
err = fmt.Errorf("Error executing init: %v", err)
return
}
switch result.(type) {
case *zygo.SexpBool:
r := result.(*zygo.SexpBool).Val
if !r {
err = fmt.Errorf("init failed")
}
case *zygo.SexpSentinel:
err = errors.New("init should return boolean, got nil")
default:
err = errors.New("init should return boolean, got: " + fmt.Sprintf("%v", result))
}
return
}
// ValidateEntry checks the contents of an entry against the validation rules
// this is the zgo implementation
func (z *ZygoNucleus) ValidateEntry(d *EntryDef, entry interface{}) (err error) {
// @todo handle JSON if schema type is different
var e string
switch d.DataFormat {
case "zygo":
e = entry.(string)
case "string":
e = "\"" + sanitizeString(entry.(string)) + "\""
case "JSON":
e = fmt.Sprintf(`(unjson (raw "%s"))`, sanitizeString(entry.(string)))
default:
err = errors.New("data format not implemented: " + d.DataFormat)
return
}
err = z.env.LoadString(fmt.Sprintf(`(validate "%s" %s)`, d.Name, e))
if err != nil {
return
}
result, err := z.env.Run()
if err != nil {
err = fmt.Errorf("Error executing validate: %v", err)
return
}
switch result.(type) {
case *zygo.SexpBool:
r := result.(*zygo.SexpBool).Val
if !r {
err = fmt.Errorf("Invalid entry: %v", entry)
}
case *zygo.SexpSentinel:
err = errors.New("validate should return boolean, got nil")
default:
err = errors.New("validate should return boolean, got: " + fmt.Sprintf("%v", result))
}
return
}
// GetInterface returns an Interface of the given name
func (z *ZygoNucleus) GetInterface(iface string) (i *Interface, err error) {
for _, x := range z.interfaces {
if x.Name == iface {
i = &x
break
}
}
if i == nil {
err = errors.New("couldn't find exposed function: " + iface)
}
return
}
// Interfaces returns the list of application exposed functions the nucleus
func (z *ZygoNucleus) Interfaces() (i []Interface) {
i = z.interfaces
return
}
// sanatizeString makes sure all quotes are quoted
func sanitizeString(s string) string {
return strings.Replace(s, "\"", "\\\"", -1)
}
// Call calls the zygo function that was registered with expose
func (z *ZygoNucleus) Call(iface string, params interface{}) (result interface{}, err error) {
i, err := z.GetInterface(iface)
if err != nil {
return
}
var code string
switch i.Schema {
case STRING:
code = fmt.Sprintf(`(%s "%s")`, iface, sanitizeString(params.(string)))
case JSON:
code = fmt.Sprintf(`(json (%s (unjson (raw "%s"))))`, iface, sanitizeString(params.(string)))
default:
err = errors.New("params type not implemented")
return
}
err = z.env.LoadString(code)
if err != nil {
return
}
result, err = z.env.Run()
if err == nil {
switch i.Schema {
case STRING:
switch t := result.(type) {
case *zygo.SexpStr:
result = t.S
case *zygo.SexpInt:
result = fmt.Sprintf("%d", t.Val)
case *zygo.SexpRaw:
result = string(t.Val)
default:
result = fmt.Sprintf("%v", result)
}
case JSON:
// type should always be SexpRaw
switch t := result.(type) {
case *zygo.SexpRaw:
result = t.Val
default:
err = errors.New("expected SexpRaw return type!")
}
}
}
return
}
// These are the zygo implementations of the library functions that must available in
// all Nucleii implementations.
const (
ZygoLibrary = `(def STRING 0) (def JSON 1)`
)
// expose registers an interfaces defined in the DNA for calling by external clients
// (you should probably never need to call this function as it is called by the DNA's expose functions)
func (z *ZygoNucleus) expose(iface Interface) (err error) {
z.interfaces = append(z.interfaces, iface)
return
}
// put exposes DHTPut to zygo
func (z *ZygoNucleus) put(env *zygo.Glisp, h *Holochain, hash string) (result *zygo.SexpHash, err error) {
result, err = zygo.MakeHash(nil, "hash", env)
if err != nil {
return nil, err
}
var key Hash
key, err = NewHash(hash)
if err != nil {
return
}
err = h.dht.SendPut(key)
if err != nil {
err = result.HashSet(env.MakeSymbol("error"), &zygo.SexpStr{S: err.Error()})
} else {
err = result.HashSet(env.MakeSymbol("result"), &zygo.SexpStr{S: "ok"})
}
return result, err
}
// putmeta exposes DHTPutMeta to zygo
func (z *ZygoNucleus) putmeta(env *zygo.Glisp, h *Holochain, hash string, metahash string, metatype string) (result *zygo.SexpHash, err error) {
result, err = zygo.MakeHash(nil, "hash", env)
if err != nil {
return nil, err
}
var key Hash
key, err = NewHash(hash)
if err != nil {
return
}
var metakey Hash
metakey, err = NewHash(metahash)
if err != nil {
return
}
err = h.dht.SendPutMeta(MetaReq{O: key, M: metakey, T: metatype})
if err != nil {
err = result.HashSet(env.MakeSymbol("error"), &zygo.SexpStr{S: err.Error()})
} else {
err = result.HashSet(env.MakeSymbol("result"), &zygo.SexpStr{S: "ok"})
}
return result, err
}
// get exposes DHTGet to zygo
func (z *ZygoNucleus) get(env *zygo.Glisp, h *Holochain, hash string) (result *zygo.SexpHash, err error) {
result, err = zygo.MakeHash(nil, "hash", env)
if err != nil {
return nil, err
}
var key Hash
key, err = NewHash(hash)
if err != nil {
return
}
response, err := h.dht.SendGet(key)
if err == nil {
switch t := response.(type) {
case *GobEntry:
// @TODO figure out encoding by entry type.
j, err := json.Marshal(t.C)
if err == nil {
err = result.HashSet(env.MakeSymbol("result"), &zygo.SexpStr{S: string(j)})
}
// @TODO what about if the hash was of a header??
default:
err = fmt.Errorf("unexpected response type from SendGet: %v", t)
}
} else {
err = result.HashSet(env.MakeSymbol("error"), &zygo.SexpStr{S: err.Error()})
}
return result, err
}
// getmeta exposes GetPutMeta to zygo
func (z *ZygoNucleus) getmeta(env *zygo.Glisp, h *Holochain, metahash string, metatype string) (result *zygo.SexpHash, err error) {
result, err = zygo.MakeHash(nil, "hash", env)
if err != nil {
return nil, err
}
var metakey Hash
metakey, err = NewHash(metahash)
if err != nil {
return
}
response, err := h.dht.SendGetMeta(MetaQuery{H: metakey, T: metatype})
if err == nil {
switch t := response.(type) {
case []Entry:
// @TODO figure out encoding by entry type.
j, err := json.Marshal(t)
if err == nil {
err = result.HashSet(env.MakeSymbol("result"), &zygo.SexpStr{S: string(j)})
}
default:
err = fmt.Errorf("unexpected response type from SendGetMeta: %v", t)
}
} else {
err = result.HashSet(env.MakeSymbol("error"), &zygo.SexpStr{S: err.Error()})
}
return result, err
}
// NewZygoNucleus builds an zygo execution environment with user specified code
func NewZygoNucleus(h *Holochain, code string) (n Nucleus, err error) {
var z ZygoNucleus
z.env = zygo.NewGlispSandbox()
z.env.AddFunction("version",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
return &zygo.SexpStr{S: Version}, nil
})
addExtras(&z)
// use a closure so that the registered zygo function can call Expose on the correct ZygoNucleus obj
z.env.AddFunction("expose",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 2 {
return zygo.SexpNull, zygo.WrongNargs
}
var i Interface
switch t := args[0].(type) {
case *zygo.SexpStr:
i.Name = t.S
default:
return zygo.SexpNull,
errors.New("1st argument of expose should be string")
}
switch t := args[1].(type) {
case *zygo.SexpInt:
i.Schema = InterfaceSchemaType(t.Val)
default:
return zygo.SexpNull,
errors.New("2nd argument of expose should be integer")
}
err := z.expose(i)
return zygo.SexpNull, err
})
z.env.AddFunction("property",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 1 {
return zygo.SexpNull, zygo.WrongNargs
}
var prop string
switch t := args[0].(type) {
case *zygo.SexpStr:
prop = t.S
default:
return zygo.SexpNull,
errors.New("1st argument of expose should be string")
}
p, err := h.GetProperty(prop)
if err != nil {
return zygo.SexpNull, err
}
result := zygo.SexpStr{S: p}
return &result, err
})
z.env.AddFunction("commit",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 2 {
return zygo.SexpNull, zygo.WrongNargs
}
var entryType string
var entry string
switch t := args[0].(type) {
case *zygo.SexpStr:
entryType = t.S
default:
return zygo.SexpNull,
errors.New("1st argument of commit should be string")
}
switch t := args[1].(type) {
case *zygo.SexpStr:
entry = t.S
case *zygo.SexpHash:
entry = zygo.SexpToJson(t)
default:
return zygo.SexpNull,
errors.New("2nd argument of commit should be string or hash")
}
err = h.ValidateEntry(entryType, entry)
var headerHash Hash
if err == nil {
e := GobEntry{C: entry}
headerHash, _, err = h.NewEntry(time.Now(), entryType, &e)
}
if err != nil {
return zygo.SexpNull, err
}
var result = zygo.SexpStr{S: headerHash.String()}
return &result, nil
})
z.env.AddFunction("put",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 1 {
return zygo.SexpNull, zygo.WrongNargs
}
var hashstr string
switch t := args[0].(type) {
case *zygo.SexpStr:
hashstr = t.S
default:
return zygo.SexpNull,
errors.New("argument of put should be string")
}
result, err := z.put(env, h, hashstr)
return result, err
})
z.env.AddFunction("get",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 1 {
return zygo.SexpNull, zygo.WrongNargs
}
var hashstr string
switch t := args[0].(type) {
case *zygo.SexpStr:
hashstr = t.S
default:
return zygo.SexpNull,
errors.New("argument of put should be string")
}
result, err := z.get(env, h, hashstr)
return result, err
})
z.env.AddFunction("putmeta",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 3 {
return zygo.SexpNull, zygo.WrongNargs
}
var hashstr string
switch t := args[0].(type) {
case *zygo.SexpStr:
hashstr = t.S
default:
return zygo.SexpNull,
errors.New("1st argument of putmeta should be string")
}
var metahashstr string
switch t := args[0].(type) {
case *zygo.SexpStr:
metahashstr = t.S
default:
return zygo.SexpNull,
errors.New("2nd argument of putmeta should be string")
}
var typestr string
switch t := args[0].(type) {
case *zygo.SexpStr:
typestr = t.S
default:
return zygo.SexpNull,
errors.New("3rd argument of putmeta should be string")
}
result, err := z.putmeta(env, h, hashstr, metahashstr, typestr)
return result, err
})
z.env.AddFunction("getmeta",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
if len(args) != 2 {
return zygo.SexpNull, zygo.WrongNargs
}
var hashstr string
switch t := args[0].(type) {
case *zygo.SexpStr:
hashstr = t.S
default:
return zygo.SexpNull,
errors.New("1st argument of gettmeta should be string")
}
var typestr string
switch t := args[0].(type) {
case *zygo.SexpStr:
typestr = t.S
default:
return zygo.SexpNull,
errors.New("2nd argument of getmeta should be string")
}
result, err := z.getmeta(env, h, hashstr, typestr)
return result, err
})
_, err = z.Run(ZygoLibrary + code)
if err != nil {
return
}
n = &z
return
}
// Run executes zygo code
func (z *ZygoNucleus) Run(code string) (result zygo.Sexp, err error) {
err = z.env.LoadString(code)
if err != nil {
err = errors.New("Zygomys load error: " + err.Error())
return
}
result, err = z.env.Run()
if err != nil {
err = errors.New("Zygomys exec error: " + err.Error())
return
}
z.lastResult = result
return
}
// extra functions we want to have available for app developers in zygo
func isPrime(t int64) bool {
// math.Mod requires floats.
x := float64(t)
// 1 or less aren't primes.
if x <= 1 {
return false
}
// Solve half of the integer set directly
if math.Mod(x, 2) == 0 {
return x == 2
}
// Main loop. i needs to be float because of math.Mod.
for i := 3.0; i <= math.Floor(math.Sqrt(x)); i += 2.0 {
if math.Mod(x, i) == 0 {
return false
}
}
// It's a prime!
return true
}
func addExtras(z *ZygoNucleus) {
z.env.AddFunction("isprime",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
switch t := args[0].(type) {
case *zygo.SexpInt:
return &zygo.SexpBool{Val: isPrime(t.Val)}, nil
default:
return zygo.SexpNull,
errors.New("argument to isprime should be int")
}
})
z.env.AddFunction("atoi",
func(env *zygo.Glisp, name string, args []zygo.Sexp) (zygo.Sexp, error) {
var i int64
var e error
switch t := args[0].(type) {
case *zygo.SexpStr:
i, e = strconv.ParseInt(t.S, 10, 64)
if e != nil {
return zygo.SexpNull, e
}
default:
return zygo.SexpNull,
errors.New("argument to atoi should be string")
}
return &zygo.SexpInt{Val: i}, nil
})
}