-
Notifications
You must be signed in to change notification settings - Fork 0
/
logicexecutor.go
204 lines (163 loc) · 6.13 KB
/
logicexecutor.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
//
// Copyright 2019 Insolar Technologies GmbH
//
// 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 logicexecutor
import (
"bytes"
"context"
"github.com/insolar/insolar/logicrunner/builtin/foundation"
"github.com/pkg/errors"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/insolar/record"
"github.com/insolar/insolar/instrumentation/inslogger"
"github.com/insolar/insolar/instrumentation/instracer"
"github.com/insolar/insolar/logicrunner/artifacts"
"github.com/insolar/insolar/logicrunner/common"
"github.com/insolar/insolar/logicrunner/machinesmanager"
"github.com/insolar/insolar/logicrunner/requestresult"
)
//go:generate minimock -i github.com/insolar/insolar/logicrunner/logicexecutor.LogicExecutor -o ./ -s _mock.go -g
type LogicExecutor interface {
Execute(ctx context.Context, transcript *common.Transcript) (artifacts.RequestResult, error)
ExecuteMethod(ctx context.Context, transcript *common.Transcript) (artifacts.RequestResult, error)
ExecuteConstructor(ctx context.Context, transcript *common.Transcript) (artifacts.RequestResult, error)
}
type logicExecutor struct {
MachinesManager machinesmanager.MachinesManager `inject:""`
DescriptorsCache artifacts.DescriptorsCache `inject:""`
}
func NewLogicExecutor() LogicExecutor {
return &logicExecutor{}
}
func (le *logicExecutor) Execute(ctx context.Context, transcript *common.Transcript) (artifacts.RequestResult, error) {
ctx, _ = inslogger.WithField(ctx, "name", transcript.Request.Method)
switch transcript.Request.CallType {
case record.CTMethod:
return le.ExecuteMethod(ctx, transcript)
case record.CTSaveAsChild:
return le.ExecuteConstructor(ctx, transcript)
default:
return nil, errors.New("Unknown request call type")
}
}
func (le *logicExecutor) ExecuteMethod(ctx context.Context, transcript *common.Transcript) (artifacts.RequestResult, error) {
ctx, span := instracer.StartSpan(ctx, "logicExecutor.ExecuteMethod")
defer span.End()
inslogger.FromContext(ctx).Debug("Executing method")
request := transcript.Request
objDesc := transcript.ObjectDescriptor
protoDesc, codeDesc, err := le.DescriptorsCache.ByObjectDescriptor(ctx, objDesc)
if err != nil {
return nil, errors.Wrap(err, "couldn't get descriptors")
}
// it's needed to assure that we call method on ref, that has same prototype as proxy, that we import in contract code
if request.Prototype != nil && !request.Prototype.Equal(*protoDesc.HeadRef()) {
err := errors.New("proxy call error: try to call method of prototype as method of another prototype")
errResBuf, err := foundation.MarshalMethodErrorResult(err)
if err != nil {
return nil, errors.Wrap(err, "couldn't marshal result")
}
return requestresult.New(errResBuf, *objDesc.HeadRef()), nil
}
executor, err := le.MachinesManager.GetExecutor(codeDesc.MachineType())
if err != nil {
return nil, errors.Wrap(err, "couldn't get executor")
}
transcript.LogicContext = le.genLogicCallContext(ctx, transcript, protoDesc, codeDesc)
newData, result, err := executor.CallMethod(
ctx, transcript.LogicContext, *codeDesc.Ref(), objDesc.Memory(), request.Method, request.Arguments,
)
if err != nil {
return nil, errors.Wrap(err, "executor error")
}
if len(result) == 0 {
return nil, errors.New("return of method is empty")
}
if len(newData) == 0 {
return nil, errors.New("object state is empty")
}
res := requestresult.New(result, *objDesc.HeadRef())
if request.Immutable {
return res, nil
}
switch {
case transcript.Deactivate:
res.SetDeactivate(objDesc)
case !bytes.Equal(objDesc.Memory(), newData):
res.SetAmend(objDesc, newData)
}
return res, nil
}
func (le *logicExecutor) ExecuteConstructor(
ctx context.Context, transcript *common.Transcript,
) (
artifacts.RequestResult, error,
) {
ctx, span := instracer.StartSpan(ctx, "LogicRunner.executeConstructorCall")
defer span.End()
inslogger.FromContext(ctx).Debug("Executing constructor")
request := transcript.Request
if request.Prototype == nil {
return nil, errors.New("prototype reference is required")
}
protoDesc, codeDesc, err := le.DescriptorsCache.ByPrototypeRef(ctx, *request.Prototype)
if err != nil {
return nil, errors.Wrap(err, "couldn't get descriptors")
}
executor, err := le.MachinesManager.GetExecutor(codeDesc.MachineType())
if err != nil {
return nil, errors.Wrap(err, "couldn't get executor")
}
transcript.LogicContext = le.genLogicCallContext(ctx, transcript, protoDesc, codeDesc)
newData, result, err := executor.CallConstructor(ctx, transcript.LogicContext, *codeDesc.Ref(), request.Method, request.Arguments)
if err != nil {
return nil, errors.Wrap(err, "executor error")
}
if len(result) == 0 {
return nil, errors.New("return of constructor is empty")
}
res := requestresult.New(result, *transcript.Request.Object)
if newData != nil {
res.SetActivate(*request.Base, *request.Prototype, newData)
}
return res, nil
}
func (le *logicExecutor) genLogicCallContext(
ctx context.Context,
transcript *common.Transcript,
protoDesc artifacts.PrototypeDescriptor,
codeDesc artifacts.CodeDescriptor,
) *insolar.LogicCallContext {
request := transcript.Request
reqRef := transcript.RequestRef
res := &insolar.LogicCallContext{
Mode: insolar.ExecuteCallMode,
Request: &reqRef,
Callee: nil, // below
Prototype: protoDesc.HeadRef(),
Code: codeDesc.Ref(),
Caller: &request.Caller,
CallerPrototype: &request.CallerPrototype,
TraceID: inslogger.TraceID(ctx),
}
if oDesc := transcript.ObjectDescriptor; oDesc != nil {
res.Parent = oDesc.Parent()
// should be the same as request.Object
res.Callee = oDesc.HeadRef()
} else {
res.Callee = transcript.Request.Object
}
return res
}