-
Notifications
You must be signed in to change notification settings - Fork 382
/
handler.go
214 lines (195 loc) · 5.37 KB
/
handler.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
package vm
import (
"fmt"
"strings"
abci "github.com/gnolang/gno/pkgs/bft/abci/types"
"github.com/gnolang/gno/pkgs/sdk"
"github.com/gnolang/gno/pkgs/sdk/auth"
"github.com/gnolang/gno/pkgs/std"
)
type vmHandler struct {
vm *VMKeeper
}
// NewHandler returns a handler for "vm" type messages.
func NewHandler(vm *VMKeeper) vmHandler {
return vmHandler{
vm: vm,
}
}
func (vh vmHandler) Process(ctx sdk.Context, msg std.Msg) sdk.Result {
switch msg := msg.(type) {
case MsgAddPackage:
return vh.handleMsgAddPackage(ctx, msg)
case MsgCall:
return vh.handleMsgCall(ctx, msg)
default:
errMsg := fmt.Sprintf("unrecognized vm message type: %T", msg)
return abciResult(std.ErrUnknownRequest(errMsg))
}
}
// Handle MsgAddPackage.
func (vh vmHandler) handleMsgAddPackage(ctx sdk.Context, msg MsgAddPackage) sdk.Result {
amount, err := std.ParseCoins("1000000ugnot") // XXX calculate
if err != nil {
return abciResult(err)
}
err = vh.vm.bank.SendCoins(ctx, msg.Creator, auth.FeeCollectorAddress(), amount)
if err != nil {
return abciResult(err)
}
err = vh.vm.AddPackage(ctx, msg)
if err != nil {
return abciResult(err)
}
return sdk.Result{}
}
// Handle MsgCall.
func (vh vmHandler) handleMsgCall(ctx sdk.Context, msg MsgCall) (res sdk.Result) {
amount, err := std.ParseCoins("1000000ugnot") // XXX calculate
if err != nil {
return abciResult(err)
}
err = vh.vm.bank.SendCoins(ctx, msg.Caller, auth.FeeCollectorAddress(), amount)
if err != nil {
return abciResult(err)
}
resstr := ""
resstr, err = vh.vm.Call(ctx, msg)
if err != nil {
return abciResult(err)
}
res.Data = []byte(resstr)
return
/* TODO handle events.
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyXXX, types.AttributeValueXXX),
),
)
*/
}
//----------------------------------------
// Query
// query paths
const (
QueryPackage = "package"
QueryStore = "store"
QueryRender = "qrender"
QueryFuncs = "qfuncs"
QueryEval = "qeval"
QueryFile = "qfile"
)
func (vh vmHandler) Query(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
switch secondPart(req.Path) {
case QueryPackage:
return vh.queryPackage(ctx, req)
case QueryStore:
return vh.queryStore(ctx, req)
case QueryRender:
return vh.queryRender(ctx, req)
case QueryFuncs:
return vh.queryFuncs(ctx, req)
case QueryEval:
return vh.queryEval(ctx, req)
case QueryFile:
return vh.queryFile(ctx, req)
default:
res = sdk.ABCIResponseQueryFromError(
std.ErrUnknownRequest(fmt.Sprintf(
"unknown vm query endpoint %s in %s",
secondPart(req.Path), req.Path)))
return
}
}
// queryPackage fetch a package's files.
func (vh vmHandler) queryPackage(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
res.Data = []byte(fmt.Sprintf("TODO: parse parts get or make fileset..."))
return
}
// queryPackage fetch items from the store.
func (vh vmHandler) queryStore(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
res.Data = []byte(fmt.Sprintf("TODO: fetch from store"))
return
}
// queryRender calls .Render(<path>) in readonly mode.
func (vh vmHandler) queryRender(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
reqData := string(req.Data)
reqParts := strings.Split(reqData, "\n")
if len(reqParts) != 2 {
panic("expected two lines in query input data")
}
pkgPath := reqParts[0]
path := reqParts[1]
expr := fmt.Sprintf("Render(%q)", path)
result, err := vh.vm.QueryEvalString(ctx, pkgPath, expr)
if err != nil {
res = sdk.ABCIResponseQueryFromError(err)
return
}
res.Data = []byte(result)
return
}
// queryFuncs returns public facing function signatures as JSON.
func (vh vmHandler) queryFuncs(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
reqData := string(req.Data)
reqParts := strings.Split(reqData, "\n")
if len(reqParts) != 1 {
panic("expected one line in query input data")
}
pkgPath := reqParts[0]
fsigs, err := vh.vm.QueryFuncs(ctx, pkgPath)
if err != nil {
res = sdk.ABCIResponseQueryFromError(err)
return
}
res.Data = []byte(fsigs.JSON())
return
}
// queryEval evaluates any expression in readonly mode and returns the results.
func (vh vmHandler) queryEval(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
reqData := string(req.Data)
reqParts := strings.Split(reqData, "\n")
if len(reqParts) != 2 {
panic("expected two lines in query input data")
}
pkgPath := reqParts[0]
expr := reqParts[1]
result, err := vh.vm.QueryEval(ctx, pkgPath, expr)
if err != nil {
res = sdk.ABCIResponseQueryFromError(err)
return
}
res.Data = []byte(result)
return
}
// queryFile returns the file bytes, or list of files if directory.
// if file, res.Value is []byte("file").
// if dir, res.Value is []byte("dir").
func (vh vmHandler) queryFile(ctx sdk.Context, req abci.RequestQuery) (res abci.ResponseQuery) {
filepath := string(req.Data)
result, err := vh.vm.QueryFile(ctx, filepath)
if err != nil {
res = sdk.ABCIResponseQueryFromError(err)
return
}
res.Data = []byte(result)
return
}
//----------------------------------------
// misc
func abciResult(err error) sdk.Result {
return sdk.ABCIResultFromError(err)
}
// returns the second component of a path.
func secondPart(path string) string {
parts := strings.Split(path, "/")
if len(parts) < 2 {
return ""
} else {
if parts[0] != "vm" {
panic("should not happen")
}
return parts[1]
}
}