-
Notifications
You must be signed in to change notification settings - Fork 382
/
keeper.go
627 lines (595 loc) · 16.9 KB
/
keeper.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
package vm
// TODO: move most of the logic in ROOT/gno.land/...
import (
"bytes"
"fmt"
"os"
"regexp"
"strings"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/stdlibs"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/sdk"
"github.com/gnolang/gno/tm2/pkg/sdk/auth"
"github.com/gnolang/gno/tm2/pkg/sdk/bank"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
)
const (
maxAllocTx = 500 * 1000 * 1000
maxAllocQuery = 1500 * 1000 * 1000 // higher limit for queries
)
// vm.VMKeeperI defines a module interface that supports Gno
// smart contracts programming (scripting).
type VMKeeperI interface {
AddPackage(ctx sdk.Context, msg MsgAddPackage) error
Call(ctx sdk.Context, msg MsgCall) (res string, err error)
Run(ctx sdk.Context, msg MsgRun) (res string, err error)
}
var _ VMKeeperI = &VMKeeper{}
// VMKeeper holds all package code and store state.
type VMKeeper struct {
baseKey store.StoreKey
iavlKey store.StoreKey
acck auth.AccountKeeper
bank bank.BankKeeper
stdlibsDir string
// cached, the DeliverTx persistent state.
gnoStore gno.Store
maxCycles int64 // max allowed cylces on VM executions
}
// NewVMKeeper returns a new VMKeeper.
func NewVMKeeper(
baseKey store.StoreKey,
iavlKey store.StoreKey,
acck auth.AccountKeeper,
bank bank.BankKeeper,
stdlibsDir string,
maxCycles int64,
) *VMKeeper {
// TODO: create an Options struct to avoid too many constructor parameters
vmk := &VMKeeper{
baseKey: baseKey,
iavlKey: iavlKey,
acck: acck,
bank: bank,
stdlibsDir: stdlibsDir,
maxCycles: maxCycles,
}
return vmk
}
func (vm *VMKeeper) Initialize(ms store.MultiStore) {
if vm.gnoStore != nil {
panic("should not happen")
}
alloc := gno.NewAllocator(maxAllocTx)
baseSDKStore := ms.GetStore(vm.baseKey)
iavlSDKStore := ms.GetStore(vm.iavlKey)
vm.gnoStore = gno.NewStore(alloc, baseSDKStore, iavlSDKStore)
vm.initBuiltinPackagesAndTypes(vm.gnoStore)
if vm.gnoStore.NumMemPackages() > 0 {
// for now, all mem packages must be re-run after reboot.
// TODO remove this, and generally solve for in-mem garbage collection
// and memory management across many objects/types/nodes/packages.
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Store: vm.gnoStore,
})
defer m2.Release()
gno.DisableDebug()
m2.PreprocessAllFilesAndSaveBlockNodes()
gno.EnableDebug()
}
}
func (vm *VMKeeper) getGnoStore(ctx sdk.Context) gno.Store {
// construct main store if nil.
if vm.gnoStore == nil {
panic("VMKeeper must first be initialized")
}
switch ctx.Mode() {
case sdk.RunTxModeDeliver:
// swap sdk store of existing store.
// this is needed due to e.g. gas wrappers.
baseSDKStore := ctx.Store(vm.baseKey)
iavlSDKStore := ctx.Store(vm.iavlKey)
vm.gnoStore.SwapStores(baseSDKStore, iavlSDKStore)
// clear object cache for every transaction.
// NOTE: this is inefficient, but simple.
// in the future, replace with more advanced caching strategy.
vm.gnoStore.ClearObjectCache()
return vm.gnoStore
case sdk.RunTxModeCheck:
// For query??? XXX Why not RunTxModeQuery?
simStore := vm.gnoStore.Fork()
baseSDKStore := ctx.Store(vm.baseKey)
iavlSDKStore := ctx.Store(vm.iavlKey)
simStore.SwapStores(baseSDKStore, iavlSDKStore)
return simStore
case sdk.RunTxModeSimulate:
// always make a new store for simulate for isolation.
simStore := vm.gnoStore.Fork()
baseSDKStore := ctx.Store(vm.baseKey)
iavlSDKStore := ctx.Store(vm.iavlKey)
simStore.SwapStores(baseSDKStore, iavlSDKStore)
return simStore
default:
panic("should not happen")
}
}
var reRunPath = regexp.MustCompile(`gno\.land/r/g[a-z0-9]+/run`)
// AddPackage adds a package with given fileset.
func (vm *VMKeeper) AddPackage(ctx sdk.Context, msg MsgAddPackage) (err error) {
creator := msg.Creator
pkgPath := msg.Package.Path
memPkg := msg.Package
deposit := msg.Deposit
gnostore := vm.getGnoStore(ctx)
// Validate arguments.
if creator.IsZero() {
return std.ErrInvalidAddress("missing creator address")
}
creatorAcc := vm.acck.GetAccount(ctx, creator)
if creatorAcc == nil {
return std.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", creator))
}
if err := msg.Package.Validate(); err != nil {
return ErrInvalidPkgPath(err.Error())
}
if pv := gnostore.GetPackage(pkgPath, false); pv != nil {
return ErrInvalidPkgPath("package already exists: " + pkgPath)
}
if reRunPath.MatchString(pkgPath) {
return ErrInvalidPkgPath("reserved package name: " + pkgPath)
}
// Pay deposit from creator.
pkgAddr := gno.DerivePkgAddr(pkgPath)
// TODO: ACLs.
// - if r/system/names does not exists -> skip validation.
// - loads r/system/names data state.
// - lookup r/system/names.namespaces for `{r,p}/NAMES`.
// - check if caller is in Admins or Editors.
// - check if namespace is not in pause.
err = vm.bank.SendCoins(ctx, creator, pkgAddr, deposit)
if err != nil {
return err
}
// Parse and run the files, construct *PV.
msgCtx := stdlibs.ExecContext{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight(),
Timestamp: ctx.BlockTime().Unix(),
Msg: msg,
OrigCaller: creator.Bech32(),
OrigSend: deposit,
OrigSendSpent: new(std.Coins),
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx),
EventLogger: ctx.EventLogger(),
}
// Parse and run the files, construct *PV.
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Store: gnostore,
Alloc: gnostore.GetAllocator(),
Context: msgCtx,
MaxCycles: vm.maxCycles,
GasMeter: ctx.GasMeter(),
})
defer m2.Release()
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case store.OutOfGasException: // panic in consumeGas()
panic(r)
default:
err = errors.Wrap(fmt.Errorf("%v", r), "VM addpkg panic: %v\n%s\n",
r, m2.String())
return
}
}
}()
m2.RunMemPackage(memPkg, true)
return nil
}
// Call calls a public Gno function (for delivertx).
func (vm *VMKeeper) Call(ctx sdk.Context, msg MsgCall) (res string, err error) {
pkgPath := msg.PkgPath // to import
fnc := msg.Func
gnostore := vm.getGnoStore(ctx)
// Get the package and function type.
pv := gnostore.GetPackage(pkgPath, false)
pl := gno.PackageNodeLocation(pkgPath)
pn := gnostore.GetBlockNode(pl).(*gno.PackageNode)
ft := pn.GetStaticTypeOf(gnostore, gno.Name(fnc)).(*gno.FuncType)
// Make main Package with imports.
mpn := gno.NewPackageNode("main", "main", nil)
mpn.Define("pkg", gno.TypedValue{T: &gno.PackageType{}, V: pv})
mpv := mpn.NewPackage()
// Parse expression.
argslist := ""
for i := range msg.Args {
if i > 0 {
argslist += ","
}
argslist += fmt.Sprintf("arg%d", i)
}
expr := fmt.Sprintf(`pkg.%s(%s)`, fnc, argslist)
xn := gno.MustParseExpr(expr)
// Send send-coins to pkg from caller.
pkgAddr := gno.DerivePkgAddr(pkgPath)
caller := msg.Caller
send := msg.Send
err = vm.bank.SendCoins(ctx, caller, pkgAddr, send)
if err != nil {
return "", err
}
// Convert Args to gno values.
cx := xn.(*gno.CallExpr)
if cx.Varg {
panic("variadic calls not yet supported")
}
if len(msg.Args) != len(ft.Params) {
panic(fmt.Sprintf("wrong number of arguments in call to %s: want %d got %d", fnc, len(ft.Params), len(msg.Args)))
}
for i, arg := range msg.Args {
argType := ft.Params[i].Type
atv := convertArgToGno(arg, argType)
cx.Args[i] = &gno.ConstExpr{
TypedValue: atv,
}
}
// Make context.
// NOTE: if this is too expensive,
// could it be safely partially memoized?
msgCtx := stdlibs.ExecContext{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight(),
Timestamp: ctx.BlockTime().Unix(),
Msg: msg,
OrigCaller: caller.Bech32(),
OrigSend: send,
OrigSendSpent: new(std.Coins),
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx),
EventLogger: ctx.EventLogger(),
}
// Construct machine and evaluate.
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: os.Stdout, // XXX
Store: gnostore,
Context: msgCtx,
Alloc: gnostore.GetAllocator(),
MaxCycles: vm.maxCycles,
GasMeter: ctx.GasMeter(),
})
defer m.Release()
m.SetActivePackage(mpv)
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case store.OutOfGasException: // panic in consumeGas()
panic(r)
default:
err = errors.Wrap(fmt.Errorf("%v", r), "VM call panic: %v\n%s\n",
r, m.String())
return
}
}
}()
rtvs := m.Eval(xn)
for i, rtv := range rtvs {
res = res + rtv.String()
if i < len(rtvs)-1 {
res += "\n"
}
}
return res, nil
// TODO pay for gas? TODO see context?
}
// Run executes arbitrary Gno code in the context of the caller's realm.
func (vm *VMKeeper) Run(ctx sdk.Context, msg MsgRun) (res string, err error) {
caller := msg.Caller
pkgAddr := caller
gnostore := vm.getGnoStore(ctx)
send := msg.Send
memPkg := msg.Package
// coerce path to right one.
// the path in the message must be "" or the following path.
// this is already checked in MsgRun.ValidateBasic
memPkg.Path = "gno.land/r/" + msg.Caller.String() + "/run"
// Validate arguments.
callerAcc := vm.acck.GetAccount(ctx, caller)
if callerAcc == nil {
return "", std.ErrUnknownAddress(fmt.Sprintf("account %s does not exist", caller))
}
if err := msg.Package.Validate(); err != nil {
return "", ErrInvalidPkgPath(err.Error())
}
// Send send-coins to pkg from caller.
err = vm.bank.SendCoins(ctx, caller, pkgAddr, send)
if err != nil {
return "", err
}
// Parse and run the files, construct *PV.
msgCtx := stdlibs.ExecContext{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight(),
Timestamp: ctx.BlockTime().Unix(),
Msg: msg,
OrigCaller: caller.Bech32(),
OrigSend: send,
OrigSendSpent: new(std.Coins),
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx),
EventLogger: ctx.EventLogger(),
}
// Parse and run the files, construct *PV.
buf := new(bytes.Buffer)
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: buf,
Store: gnostore,
Alloc: gnostore.GetAllocator(),
Context: msgCtx,
MaxCycles: vm.maxCycles,
GasMeter: ctx.GasMeter(),
})
// XXX MsgRun does not have pkgPath. How do we find it on chain?
defer m.Release()
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case store.OutOfGasException: // panic in consumeGas()
panic(r)
default:
err = errors.Wrap(fmt.Errorf("%v", r), "VM run main addpkg panic: %v\n%s\n",
r, m.String())
return
}
}
}()
_, pv := m.RunMemPackage(memPkg, false)
m2 := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: "",
Output: buf,
Store: gnostore,
Alloc: gnostore.GetAllocator(),
Context: msgCtx,
MaxCycles: vm.maxCycles,
GasMeter: ctx.GasMeter(),
})
defer m2.Release()
m2.SetActivePackage(pv)
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case store.OutOfGasException: // panic in consumeGas()
panic(r)
default:
err = errors.Wrap(fmt.Errorf("%v", r), "VM run main call panic: %v\n%s\n",
r, m2.String())
return
}
}
}()
m2.RunMain()
res = buf.String()
return res, nil
}
// QueryFuncs returns public facing function signatures.
func (vm *VMKeeper) QueryFuncs(ctx sdk.Context, pkgPath string) (fsigs FunctionSignatures, err error) {
store := vm.getGnoStore(ctx)
// Ensure pkgPath is realm.
if !gno.IsRealmPath(pkgPath) {
err = ErrInvalidPkgPath(fmt.Sprintf(
"package is not realm: %s", pkgPath))
return nil, err
}
// Get Package.
pv := store.GetPackage(pkgPath, false)
if pv == nil {
err = ErrInvalidPkgPath(fmt.Sprintf(
"package not found: %s", pkgPath))
return nil, err
}
// Iterate over public functions.
pblock := pv.GetBlock(store)
for _, tv := range pblock.Values {
if tv.T.Kind() != gno.FuncKind {
continue // must be function
}
fv := tv.GetFunc()
if fv.IsMethod {
continue // cannot be method
}
fname := string(fv.Name)
first := fname[0:1]
if strings.ToUpper(first) != first {
continue // must be exposed
}
fsig := FunctionSignature{
FuncName: fname,
}
ft := fv.Type.(*gno.FuncType)
for _, param := range ft.Params {
pname := string(param.Name)
if pname == "" {
pname = "_"
}
ptype := gno.BaseOf(param.Type).String()
fsig.Params = append(fsig.Params,
NamedType{Name: pname, Type: ptype},
)
}
for _, result := range ft.Results {
rname := string(result.Name)
if rname == "" {
rname = "_"
}
rtype := gno.BaseOf(result.Type).String()
fsig.Results = append(fsig.Results,
NamedType{Name: rname, Type: rtype},
)
}
fsigs = append(fsigs, fsig)
}
return fsigs, nil
}
// QueryEval evaluates a gno expression (readonly, for ABCI queries).
// TODO: modify query protocol to allow MsgEval.
// TODO: then, rename to "Eval".
func (vm *VMKeeper) QueryEval(ctx sdk.Context, pkgPath string, expr string) (res string, err error) {
alloc := gno.NewAllocator(maxAllocQuery)
gnostore := vm.getGnoStore(ctx)
pkgAddr := gno.DerivePkgAddr(pkgPath)
// Get Package.
pv := gnostore.GetPackage(pkgPath, false)
if pv == nil {
err = ErrInvalidPkgPath(fmt.Sprintf(
"package not found: %s", pkgPath))
return "", err
}
// Parse expression.
xx, err := gno.ParseExpr(expr)
if err != nil {
return "", err
}
// Construct new machine.
msgCtx := stdlibs.ExecContext{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight(),
Timestamp: ctx.BlockTime().Unix(),
// Msg: msg,
// OrigCaller: caller,
// OrigSend: send,
// OrigSendSpent: nil,
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx), // safe as long as ctx is a fork to be discarded.
EventLogger: ctx.EventLogger(),
}
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: pkgPath,
Output: os.Stdout, // XXX
Store: gnostore,
Context: msgCtx,
Alloc: alloc,
MaxCycles: vm.maxCycles,
GasMeter: ctx.GasMeter(),
})
defer m.Release()
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case store.OutOfGasException: // panic in consumeGas()
panic(r)
default:
err = errors.Wrap(fmt.Errorf("%v", r), "VM query eval panic: %v\n%s\n",
r, m.String())
return
}
}
}()
rtvs := m.Eval(xx)
res = ""
for i, rtv := range rtvs {
res += rtv.String()
if i < len(rtvs)-1 {
res += "\n"
}
}
return res, nil
}
// QueryEvalString evaluates a gno expression (readonly, for ABCI queries).
// The result is expected to be a single string (not a tuple).
// TODO: modify query protocol to allow MsgEval.
// TODO: then, rename to "EvalString".
func (vm *VMKeeper) QueryEvalString(ctx sdk.Context, pkgPath string, expr string) (res string, err error) {
alloc := gno.NewAllocator(maxAllocQuery)
gnostore := vm.getGnoStore(ctx)
pkgAddr := gno.DerivePkgAddr(pkgPath)
// Get Package.
pv := gnostore.GetPackage(pkgPath, false)
if pv == nil {
err = ErrInvalidPkgPath(fmt.Sprintf(
"package not found: %s", pkgPath))
return "", err
}
// Parse expression.
xx, err := gno.ParseExpr(expr)
if err != nil {
return "", err
}
// Construct new machine.
msgCtx := stdlibs.ExecContext{
ChainID: ctx.ChainID(),
Height: ctx.BlockHeight(),
Timestamp: ctx.BlockTime().Unix(),
// Msg: msg,
// OrigCaller: caller,
// OrigSend: jsend,
// OrigSendSpent: nil,
OrigPkgAddr: pkgAddr.Bech32(),
Banker: NewSDKBanker(vm, ctx), // safe as long as ctx is a fork to be discarded.
EventLogger: ctx.EventLogger(),
}
m := gno.NewMachineWithOptions(
gno.MachineOptions{
PkgPath: pkgPath,
Output: os.Stdout, // XXX
Store: gnostore,
Context: msgCtx,
Alloc: alloc,
MaxCycles: vm.maxCycles,
GasMeter: ctx.GasMeter(),
})
defer m.Release()
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case store.OutOfGasException: // panic in consumeGas()
panic(r)
default:
err = errors.Wrap(fmt.Errorf("%v", r), "VM query eval string panic: %v\n%s\n",
r, m.String())
return
}
}
}()
rtvs := m.Eval(xx)
if len(rtvs) != 1 {
return "", errors.New("expected 1 string result, got %d", len(rtvs))
} else if rtvs[0].T.Kind() != gno.StringKind {
return "", errors.New("expected 1 string result, got %v", rtvs[0].T.Kind())
}
res = rtvs[0].GetString()
return res, nil
}
func (vm *VMKeeper) QueryFile(ctx sdk.Context, filepath string) (res string, err error) {
store := vm.getGnoStore(ctx)
dirpath, filename := std.SplitFilepath(filepath)
if filename != "" {
memFile := store.GetMemFile(dirpath, filename)
if memFile == nil {
return "", fmt.Errorf("file %q is not available", filepath) // TODO: XSS protection
}
return memFile.Body, nil
} else {
memPkg := store.GetMemPackage(dirpath)
for i, memfile := range memPkg.Files {
if i > 0 {
res += "\n"
}
res += memfile.Name
}
return res, nil
}
}