-
Notifications
You must be signed in to change notification settings - Fork 46
/
service.go
341 lines (291 loc) · 9.46 KB
/
service.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
/*
*
* * Copyright 2018 Insolar
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package logicrunner
import (
"bytes"
"context"
"net"
"net/rpc"
"sync/atomic"
"github.com/insolar/insolar/core"
"github.com/insolar/insolar/core/message"
"github.com/insolar/insolar/core/reply"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/logicrunner/goplugin/rpctypes"
"github.com/pkg/errors"
)
// StartRPC starts RPC server for isolated executors to use
func StartRPC(ctx context.Context, lr *LogicRunner) *RPC {
rpcService := &RPC{lr: lr}
rpcServer := rpc.NewServer()
err := rpcServer.Register(rpcService)
if err != nil {
panic("Fail to register LogicRunner RPC Service: " + err.Error())
}
l, e := net.Listen(lr.Cfg.RPCProtocol, lr.Cfg.RPCListen)
if e != nil {
inslogger.FromContext(ctx).Fatal("couldn't setup listener on '"+lr.Cfg.RPCListen+"' over "+lr.Cfg.RPCProtocol+": ", e)
}
lr.sock = l
inslogger.FromContext(ctx).Infof("starting LogicRunner RPC service on %q over %s", lr.Cfg.RPCListen, lr.Cfg.RPCProtocol)
go func() {
rpcServer.Accept(l)
inslogger.FromContext(ctx).Info("LogicRunner RPC service stopped")
}()
return rpcService
}
// RPC is a RPC interface for runner to use for various tasks, e.g. code fetching
type RPC struct {
lr *LogicRunner
}
// GetCode is an RPC retrieving a code by its reference
func (gpr *RPC) GetCode(req rpctypes.UpGetCodeReq, reply *rpctypes.UpGetCodeResp) error {
es := gpr.lr.UpsertExecution(req.Callee)
ctx := es.insContext
am := gpr.lr.ArtifactManager
codeDescriptor, err := am.GetCode(ctx, req.Code)
if err != nil {
return err
}
reply.Code, err = codeDescriptor.Code()
if err != nil {
return err
}
return nil
}
var serial uint64 = 1
// MakeBaseMessage makes base of logicrunner event from base of up request
func MakeBaseMessage(req rpctypes.UpBaseReq) message.BaseLogicMessage {
return message.BaseLogicMessage{
Caller: req.Callee,
Request: req.Request,
Nonce: atomicLoadAndIncrementUint64(&serial),
}
}
// RouteCall routes call from a contract to a contract through event bus.
func (gpr *RPC) RouteCall(req rpctypes.UpRouteReq, rep *rpctypes.UpRouteResp) error {
es := gpr.lr.UpsertExecution(req.Callee)
ctx := es.insContext
cr, step := gpr.lr.nextValidationStep(req.Callee)
if step >= 0 { // validate
if core.CaseRecordTypeRouteCall != cr.Type {
return errors.New("wrong validation type on RouteCall")
}
sig := HashInterface(req)
if !bytes.Equal(cr.ReqSig, sig) {
return errors.New("wrong validation sig on RouteCall")
}
rep.Result = cr.Resp.(core.Arguments)
return nil
}
var mode message.MethodReturnMode
if req.Wait {
mode = message.ReturnResult
} else {
mode = message.ReturnNoWait
}
msg := &message.CallMethod{
BaseLogicMessage: MakeBaseMessage(req.UpBaseReq),
ReturnMode: mode,
ObjectRef: req.Object,
Method: req.Method,
Arguments: req.Arguments,
}
res, err := gpr.lr.MessageBus.Send(ctx, msg)
if err != nil {
return errors.Wrap(err, "couldn't dispatch event")
}
rep.Result = res.(*reply.CallMethod).Result
gpr.lr.addObjectCaseRecord(req.Callee, core.CaseRecord{
Type: core.CaseRecordTypeRouteCall,
ReqSig: HashInterface(req),
Resp: rep.Result,
})
return nil
}
// SaveAsChild is an RPC saving data as memory of a contract as child a parent
func (gpr *RPC) SaveAsChild(req rpctypes.UpSaveAsChildReq, rep *rpctypes.UpSaveAsChildResp) error {
es := gpr.lr.UpsertExecution(req.Callee)
ctx := es.insContext
if gpr.lr.MessageBus == nil {
return errors.New("event bus was not set during initialization")
}
cr, step := gpr.lr.nextValidationStep(req.Callee)
if step >= 0 { // validate
if core.CaseRecordTypeSaveAsChild != cr.Type {
return errors.New("wrong validation type on SaveAsChild")
}
sig := HashInterface(req)
if !bytes.Equal(cr.ReqSig, sig) {
return errors.New("wrong validation sig on SaveAsChild")
}
rep.Reference = cr.Resp.(*core.RecordRef)
return nil
}
msg := &message.CallConstructor{
BaseLogicMessage: MakeBaseMessage(req.UpBaseReq),
PrototypeRef: req.Prototype,
ParentRef: req.Parent,
Name: req.ConstructorName,
Arguments: req.ArgsSerialized,
SaveAs: message.Child,
}
res, err := gpr.lr.MessageBus.Send(ctx, msg)
if err != nil {
return errors.Wrap(err, "couldn't save new object as child")
}
rep.Reference = res.(*reply.CallConstructor).Object
gpr.lr.addObjectCaseRecord(req.Callee, core.CaseRecord{
Type: core.CaseRecordTypeSaveAsChild,
ReqSig: HashInterface(req),
Resp: rep.Reference,
})
return nil
}
// GetObjChildren is an RPC returns set of object children
func (gpr *RPC) GetObjChildren(req rpctypes.UpGetObjChildrenReq, rep *rpctypes.UpGetObjChildrenResp) error {
es := gpr.lr.UpsertExecution(req.Callee)
ctx := es.insContext
cr, step := gpr.lr.nextValidationStep(req.Callee)
if step >= 0 { // validate
if core.CaseRecordTypeGetObjChildren != cr.Type {
return errors.New("wrong validation type on GetObjChildren")
}
sig := HashInterface(req)
if !bytes.Equal(cr.ReqSig, sig) {
return errors.New("wrong validation sig on GetObjChildren")
}
rep.Children = cr.Resp.([]core.RecordRef)
return nil
}
am := gpr.lr.ArtifactManager
i, err := am.GetChildren(ctx, req.Obj, nil)
if err != nil {
return errors.Wrap(err, "[ GetObjChildren ] Can't get children")
}
for i.HasNext() {
r, err := i.Next()
if err != nil {
return errors.Wrap(err, "[ GetObjChildren ] Can't get Next")
}
o, err := am.GetObject(ctx, *r, nil, false)
if err != nil {
if err == core.ErrDeactivated {
continue
}
return errors.Wrap(err, "[ GetObjChildren ] Can't get Next")
}
protoRef, err := o.Prototype()
if err != nil {
return errors.Wrap(err, "[ GetObjChildren ] Can't get prototype reference")
}
if protoRef.Equal(req.Prototype) {
rep.Children = append(rep.Children, *r)
}
}
gpr.lr.addObjectCaseRecord(req.Callee, core.CaseRecord{ // bad idea, we can store gadzillion of children
Type: core.CaseRecordTypeGetObjChildren,
ReqSig: HashInterface(req),
Resp: rep.Children,
})
return nil
}
// SaveAsDelegate is an RPC saving data as memory of a contract as child a parent
func (gpr *RPC) SaveAsDelegate(req rpctypes.UpSaveAsDelegateReq, rep *rpctypes.UpSaveAsDelegateResp) error {
es := gpr.lr.UpsertExecution(req.Callee)
ctx := es.insContext
cr, step := gpr.lr.nextValidationStep(req.Callee)
if step >= 0 { // validate
if core.CaseRecordTypeSaveAsDelegate != cr.Type {
return errors.New("wrong validation type on SaveAsDelegate")
}
sig := HashInterface(req)
if !bytes.Equal(cr.ReqSig, sig) {
return errors.New("wrong validation sig on SaveAsDelegate")
}
rep.Reference = cr.Resp.(*core.RecordRef)
return nil
}
msg := &message.CallConstructor{
BaseLogicMessage: MakeBaseMessage(req.UpBaseReq),
PrototypeRef: req.Prototype,
ParentRef: req.Into,
Name: req.ConstructorName,
Arguments: req.ArgsSerialized,
SaveAs: message.Delegate,
}
res, err := gpr.lr.MessageBus.Send(ctx, msg)
if err != nil {
return errors.Wrap(err, "couldn't save new object as delegate")
}
rep.Reference = res.(*reply.CallConstructor).Object
gpr.lr.addObjectCaseRecord(req.Callee, core.CaseRecord{
Type: core.CaseRecordTypeSaveAsDelegate,
ReqSig: HashInterface(req),
Resp: rep.Reference,
})
return nil
}
// GetDelegate is an RPC saving data as memory of a contract as child a parent
func (gpr *RPC) GetDelegate(req rpctypes.UpGetDelegateReq, rep *rpctypes.UpGetDelegateResp) error {
es := gpr.lr.UpsertExecution(req.Callee)
ctx := es.insContext
cr, step := gpr.lr.nextValidationStep(req.Callee)
if step >= 0 { // validate
if core.CaseRecordTypeGetDelegate != cr.Type {
return errors.New("wrong validation type on RouteCall")
}
sig := HashInterface(req)
if !bytes.Equal(cr.ReqSig, sig) {
return errors.New("wrong validation sig on RouteCall")
}
rep.Object = cr.Resp.(core.RecordRef)
return nil
}
am := gpr.lr.ArtifactManager
ref, err := am.GetDelegate(ctx, req.Object, req.OfType)
if err != nil {
return err
}
rep.Object = *ref
gpr.lr.addObjectCaseRecord(req.Callee, core.CaseRecord{
Type: core.CaseRecordTypeGetDelegate,
ReqSig: HashInterface(req),
Resp: rep.Object,
})
return nil
}
// DeactivateObject is an RPC saving data as memory of a contract as child a parent
func (gpr *RPC) DeactivateObject(req rpctypes.UpDeactivateObjectReq, rep *rpctypes.UpDeactivateObjectResp) error {
state := gpr.lr.GetExecution(req.Callee)
if state == nil {
return errors.New("no execution state, impossible, shouldn't be")
}
// TODO: is it race? make sure it's not!
state.deactivate = true
return nil
}
// atomicLoadAndIncrementUint64 performs CAS loop, increments counter and returns old value.
func atomicLoadAndIncrementUint64(addr *uint64) uint64 {
for {
val := atomic.LoadUint64(addr)
if atomic.CompareAndSwapUint64(addr, val, val+1) {
return val
}
}
}