-
Notifications
You must be signed in to change notification settings - Fork 46
/
logicrunner.go
493 lines (430 loc) · 13.3 KB
/
logicrunner.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
/*
* 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 - infrastructure for executing smartcontracts
package logicrunner
import (
"bytes"
"context"
"encoding/gob"
"net"
"sync"
"time"
"github.com/pkg/errors"
"github.com/insolar/insolar/configuration"
"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/builtin"
"github.com/insolar/insolar/logicrunner/goplugin"
)
type Ref = core.RecordRef
// Context of one contract execution
type ExecutionState struct {
sync.Mutex
noWait bool
validate bool
insContext context.Context
callContext *core.LogicCallContext
deactivate bool
request *Ref
objectbody *ObjectBody
}
// LogicRunner is a general interface of contract executor
type LogicRunner struct {
Executors [core.MachineTypesLastID]core.MachineLogicExecutor
ArtifactManager core.ArtifactManager
MessageBus core.MessageBus
Ledger core.Ledger
Network core.Network
machinePrefs []core.MachineType
Cfg *configuration.LogicRunner
execution map[Ref]*ExecutionState // if object exists, we are validating or executing it right now
executionMutex sync.Mutex
// TODO move caseBind to context
caseBind core.CaseBind
caseBindMutex sync.Mutex
caseBindReplays map[Ref]core.CaseBindReplay
caseBindReplaysMutex sync.Mutex
consensus map[Ref]*Consensus
consensusMutex sync.Mutex
sock net.Listener
}
// NewLogicRunner is constructor for LogicRunner
func NewLogicRunner(cfg *configuration.LogicRunner) (*LogicRunner, error) {
if cfg == nil {
return nil, errors.New("LogicRunner have nil configuration")
}
res := LogicRunner{
ArtifactManager: nil,
Ledger: nil,
Cfg: cfg,
execution: make(map[Ref]*ExecutionState),
caseBind: core.CaseBind{Records: make(map[Ref][]core.CaseRecord)},
caseBindReplays: make(map[Ref]core.CaseBindReplay),
}
return &res, nil
}
// Start starts logic runner component
func (lr *LogicRunner) Start(ctx context.Context, c core.Components) error {
am := c.Ledger.GetArtifactManager()
lr.ArtifactManager = am
lr.MessageBus = c.MessageBus
lr.Ledger = c.Ledger
lr.Network = c.Network
if lr.Cfg.BuiltIn != nil {
bi := builtin.NewBuiltIn(lr.MessageBus, lr.ArtifactManager)
if err := lr.RegisterExecutor(core.MachineTypeBuiltin, bi); err != nil {
return err
}
lr.machinePrefs = append(lr.machinePrefs, core.MachineTypeBuiltin)
}
if lr.Cfg.GoPlugin != nil {
if lr.Cfg.RPCListen != "" {
StartRPC(ctx, lr)
}
gp, err := goplugin.NewGoPlugin(lr.Cfg, lr.MessageBus, lr.ArtifactManager)
if err != nil {
return err
}
if err := lr.RegisterExecutor(core.MachineTypeGoPlugin, gp); err != nil {
return err
}
lr.machinePrefs = append(lr.machinePrefs, core.MachineTypeGoPlugin)
}
// TODO: use separate handlers
if err := lr.MessageBus.Register(core.TypeCallMethod, lr.Execute); err != nil {
return err
}
if err := lr.MessageBus.Register(core.TypeCallConstructor, lr.Execute); err != nil {
return err
}
if err := lr.MessageBus.Register(core.TypeExecutorResults, lr.ExecutorResults); err != nil {
return err
}
if err := lr.MessageBus.Register(core.TypeValidateCaseBind, lr.ValidateCaseBind); err != nil {
return err
}
if err := lr.MessageBus.Register(core.TypeValidationResults, lr.ProcessValidationResults); err != nil {
return err
}
return nil
}
// Stop stops logic runner component and its executors
func (lr *LogicRunner) Stop(ctx context.Context) error {
reterr := error(nil)
for _, e := range lr.Executors {
if e == nil {
continue
}
err := e.Stop()
if err != nil {
reterr = errors.Wrap(reterr, err.Error())
}
}
if lr.sock != nil {
if err := lr.sock.Close(); err != nil {
return err
}
}
return reterr
}
// Execute runs a method on an object, ATM just thin proxy to `GoPlugin.Exec`
func (lr *LogicRunner) Execute(ctx context.Context, inmsg core.SignedMessage) (core.Reply, error) {
// TODO do not pass here message.ValidateCaseBind and message.ExecutorResults
msg, ok := inmsg.Message().(message.IBaseLogicMessage)
if !ok {
return nil, errors.New("Execute( ! message.IBaseLogicMessage )")
}
ref := msg.GetReference()
es := lr.UpsertExecution(ref)
es.Lock()
es.insContext = ctx
lr.caseBindReplaysMutex.Lock()
cb, validate := lr.caseBindReplays[ref]
lr.caseBindReplaysMutex.Unlock()
var vb ValidationBehaviour
if validate {
vb = ValidationChecker{lr: lr, cb: cb}
} else {
vb = ValidationSaver{lr: lr}
}
// TODO do map of supported objects for pulse, go to jetCoordinator only if map is empty for ref
isAuthorized, err := lr.Ledger.GetJetCoordinator().IsAuthorized(
ctx,
vb.GetRole(),
*msg.Target(),
lr.pulse().PulseNumber,
lr.Network.GetNodeID(),
)
if err != nil {
return nil, errors.New("Authorization failed with error: " + err.Error())
}
if !isAuthorized {
return nil, errors.New("Can't execute this object")
}
vb.Begin(ref, core.CaseRecord{
Type: core.CaseRecordTypeStart,
Resp: msg,
})
es.request, err = vb.RegisterRequest(msg)
if err != nil {
return nil, errors.Wrap(err, "Can't create request")
}
es.callContext = &core.LogicCallContext{
Caller: msg.GetCaller(),
Callee: &ref,
Request: es.request,
Time: time.Now(), // TODO: probably we should take it from e
Pulse: *lr.pulse(),
TraceID: inslogger.TraceID(ctx),
}
switch m := msg.(type) {
case *message.CallMethod:
re, err := lr.executeMethodCall(es, m, vb)
return re, err
case *message.CallConstructor:
re, err := lr.executeConstructorCall(es, m, vb)
return re, err
default:
panic("Unknown e type")
}
}
// ObjectBody is an inner representation of object and all it accessory
// make it private again when we start it serialize before sending
type ObjectBody struct {
objDescriptor core.ObjectDescriptor
Object []byte
ClassHeadRef *Ref
CodeMachineType core.MachineType
CodeRef *Ref
}
func init() {
gob.Register(&ObjectBody{})
}
func (lr *LogicRunner) getObjectMessage(es *ExecutionState, objref Ref) error {
ctx := es.insContext
cr, step := lr.nextValidationStep(objref)
// TODO: move this to vb, when vb become a part of es
if es.objectbody != nil { // already have something
if step > 0 { // check signature
if core.CaseRecordTypeSignObject != cr.Type {
return errors.Errorf("Wrong validation type on CaseRecordTypeSignObject %d, ", cr.Type)
}
if !bytes.Equal(cr.ReqSig, HashInterface(objref)) {
return errors.New("Wrong validation sig on CaseRecordTypeSignObject")
}
if !bytes.Equal(cr.Resp.([]byte), HashInterface(es.objectbody)) {
return errors.New("Wrong validation comparision on CaseRecordTypeSignObject")
}
} else {
lr.addObjectCaseRecord(objref, core.CaseRecord{
Type: core.CaseRecordTypeSignObject,
ReqSig: HashInterface(objref),
Resp: HashInterface(es.objectbody),
})
}
return nil
}
if step >= 0 { // validate
if core.CaseRecordTypeGetObject != cr.Type {
return errors.Errorf("Wrong validation type on CaseRecordTypeGetObject %d", cr.Type)
}
sig := HashInterface(objref)
if !bytes.Equal(cr.ReqSig, sig) {
return errors.New("Wrong validation sig on CaseRecordTypeGetObject")
}
es.objectbody = cr.Resp.(*ObjectBody)
return nil
}
objDesc, err := lr.ArtifactManager.GetObject(ctx, objref, nil, false)
if err != nil {
return errors.Wrap(err, "couldn't get object")
}
protoRef, err := objDesc.Prototype()
if err != nil {
return errors.Wrap(err, "couldn't get prototype reference")
}
protoDesc, err := lr.ArtifactManager.GetObject(ctx, *protoRef, nil, false)
if err != nil {
return errors.Wrap(err, "couldn't get object's class")
}
codeRef, err := protoDesc.Code()
if err != nil {
return errors.Wrap(err, "couldn't get code reference")
}
codeDesc, err := lr.ArtifactManager.GetCode(ctx, *codeRef)
if err != nil {
return errors.Wrap(err, "couldn't get code")
}
es.objectbody = &ObjectBody{
objDescriptor: objDesc,
Object: objDesc.Memory(),
ClassHeadRef: protoDesc.HeadRef(),
CodeMachineType: codeDesc.MachineType(),
CodeRef: codeDesc.Ref(),
}
bcopy := *es.objectbody
copy(bcopy.Object, es.objectbody.Object)
lr.addObjectCaseRecord(objref, core.CaseRecord{
Type: core.CaseRecordTypeGetObject,
ReqSig: HashInterface(objref),
Resp: &bcopy,
})
return nil
}
func (lr *LogicRunner) executeMethodCall(es *ExecutionState, m *message.CallMethod, vb ValidationBehaviour) (core.Reply, error) {
ctx := es.insContext
es.noWait = false
defer func() {
if !es.noWait {
es.Unlock()
}
}()
err := lr.getObjectMessage(es, m.ObjectRef)
if err != nil {
return nil, errors.Wrap(err, "couldn't get object message")
}
es.callContext.Prototype = es.objectbody.ClassHeadRef
vb.ModifyContext(es.callContext)
executor, err := lr.GetExecutor(es.objectbody.CodeMachineType)
if err != nil {
return nil, errors.Wrap(err, "no executor registered")
}
executer := func() (*reply.CallMethod, error) {
defer func() {
if es.noWait {
es.Unlock()
}
}()
newData, result, err := executor.CallMethod(
ctx, es.callContext, *es.objectbody.CodeRef, es.objectbody.Object, m.Method, m.Arguments,
)
if err != nil {
return nil, errors.Wrap(err, "executor error")
}
if vb.NeedSave() {
am := lr.ArtifactManager
if es.deactivate {
_, err = am.DeactivateObject(
ctx, Ref{}, *es.request, es.objectbody.objDescriptor,
)
} else {
es.objectbody.objDescriptor, err = am.UpdateObject(
ctx, Ref{}, *es.request, es.objectbody.objDescriptor, newData,
)
}
if err != nil {
return nil, errors.Wrap(err, "couldn't update object")
}
_, err = am.RegisterResult(ctx, *es.request, result)
if err != nil {
return nil, errors.Wrap(err, "couldn't save results")
}
}
es.objectbody.Object = newData
re := &reply.CallMethod{Data: newData, Result: result}
vb.End(m.ObjectRef, core.CaseRecord{
Type: core.CaseRecordTypeResult,
Resp: re,
})
return re, nil
}
switch m.ReturnMode {
case message.ReturnResult:
return executer()
case message.ReturnNoWait:
es.noWait = true
go func() {
_, err := executer()
if err != nil {
inslogger.FromContext(ctx).Error(err)
}
}()
return &reply.CallMethod{}, nil
}
return nil, errors.Errorf("Invalid ReturnMode #%d", m.ReturnMode)
}
func (lr *LogicRunner) executeConstructorCall(es *ExecutionState, m *message.CallConstructor, vb ValidationBehaviour) (core.Reply, error) {
ctx := es.insContext
defer func() {
es.Unlock()
}()
protoDesc, err := lr.ArtifactManager.GetObject(ctx, m.PrototypeRef, nil, false)
if err != nil {
return nil, errors.Wrap(err, "couldn't get prototype")
}
es.callContext.Prototype = protoDesc.HeadRef()
codeRef, err := protoDesc.Code()
if err != nil {
return nil, errors.Wrap(err, "couldn't code reference")
}
codeDesc, err := lr.ArtifactManager.GetCode(ctx, *codeRef)
if err != nil {
return nil, errors.Wrap(err, "couldn't code")
}
executor, err := lr.GetExecutor(codeDesc.MachineType())
if err != nil {
return nil, errors.Wrap(err, "no executer registered")
}
newData, err := executor.CallConstructor(ctx, es.callContext, *codeDesc.Ref(), m.Name, m.Arguments)
if err != nil {
return nil, errors.Wrap(err, "executer error")
}
switch m.SaveAs {
case message.Child, message.Delegate:
if vb.NeedSave() {
_, err = lr.ArtifactManager.ActivateObject(
ctx,
Ref{}, *es.request, m.ParentRef, m.PrototypeRef, m.SaveAs == message.Delegate, newData,
)
}
vb.End(m.GetReference(), core.CaseRecord{
Type: core.CaseRecordTypeResult,
Resp: &reply.CallConstructor{Object: es.request},
})
return &reply.CallConstructor{Object: es.request}, err
default:
return nil, errors.New("unsupported type of save object")
}
}
func (lr *LogicRunner) OnPulse(pulse core.Pulse) error {
insctx := context.TODO()
lr.RefreshConsensus()
// start of new Pulse, lock CaseBind data, copy it, clean original, unlock original
objectsRecords := lr.refreshCaseBind()
// TODO INS-666
// TODO make refresh lr.Execution - Unlock mutexes n-1 time for each object, send some info for callers, do empty object
if len(objectsRecords) == 0 {
return nil
}
// send copy for validation
for ref, records := range objectsRecords {
_, err := lr.MessageBus.Send(
insctx,
&message.ValidateCaseBind{RecordRef: ref, CaseRecords: records, Pulse: pulse},
)
if err != nil {
panic("Error while sending caseBind data to validators: " + err.Error())
}
results := message.ExecutorResults{RecordRef: ref, CaseRecords: records}
_, err = lr.MessageBus.Send(insctx, &results)
if err != nil {
return errors.New("error while sending caseBind data to new executor")
}
}
return nil
}