-
Notifications
You must be signed in to change notification settings - Fork 0
/
procs.go
164 lines (130 loc) · 4.34 KB
/
procs.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
//
// 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 logicrunner
import (
"context"
"github.com/pkg/errors"
"go.opencensus.io/stats"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/insolar/jet"
"github.com/insolar/insolar/insolar/payload"
"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/builtin/foundation"
"github.com/insolar/insolar/logicrunner/common"
"github.com/insolar/insolar/logicrunner/metrics"
"github.com/insolar/insolar/logicrunner/requestresult"
)
// ------------- CheckOurRole
type CheckOurRole struct {
target insolar.Reference
role insolar.DynamicRole
pulseNumber insolar.PulseNumber
jetCoordinator jet.Coordinator
}
var ErrCantExecute = errors.New("can't executeAndReply this object")
func (ch *CheckOurRole) Proceed(ctx context.Context) error {
ctx, span := instracer.StartSpan(ctx, "CheckOurRole")
defer span.End()
// TODO do map of supported objects for pulse, go to jetCoordinator only if map is empty for ref
isAuthorized, err := ch.jetCoordinator.IsMeAuthorizedNow(ctx, ch.role, *ch.target.GetLocal())
if err != nil {
return errors.Wrap(err, "authorization failed with error")
}
if !isAuthorized {
return ErrCantExecute
}
return nil
}
// ------------- RegisterIncomingRequest
type RegisterIncomingRequest struct {
request record.IncomingRequest
result chan *payload.RequestInfo
ArtifactManager artifacts.Client
}
func NewRegisterIncomingRequest(request record.IncomingRequest, dep *Dependencies) *RegisterIncomingRequest {
return &RegisterIncomingRequest{
request: request,
ArtifactManager: dep.ArtifactManager,
result: make(chan *payload.RequestInfo, 1),
}
}
func (r *RegisterIncomingRequest) setResult(result *payload.RequestInfo) { // nolint
r.result <- result
}
// getResult is blocking
func (r *RegisterIncomingRequest) getResult() *payload.RequestInfo { // nolint
return <-r.result
}
func (r *RegisterIncomingRequest) Proceed(ctx context.Context) error {
ctx, span := instracer.StartSpan(ctx, "RegisterIncomingRequest.Proceed")
defer span.End()
inslogger.FromContext(ctx).Debug("registering incoming request")
reqInfo, err := r.ArtifactManager.RegisterIncomingRequest(ctx, &r.request)
if err != nil {
return err
}
r.setResult(reqInfo)
return nil
}
type AddFreshRequest struct {
broker ExecutionBrokerI
requestRef insolar.Reference
request record.IncomingRequest
}
func (c *AddFreshRequest) Proceed(ctx context.Context) error {
tr := common.NewTranscriptCloneContext(ctx, c.requestRef, c.request)
c.broker.AddFreshRequest(ctx, tr)
return nil
}
type RecordErrorResult struct {
artifactManager artifacts.Client
err error
objectRef insolar.Reference
requestRef insolar.Reference
result []byte
}
func (r *RecordErrorResult) Proceed(ctx context.Context) error {
ctx, span := instracer.StartSpan(ctx, "RecordErrorResult.Proceed")
defer span.End()
inslogger.FromContext(ctx).Debug("recording error result")
resultWithErr, err := foundation.MarshalMethodErrorResult(r.err)
if err != nil {
return errors.Wrap(err, "couldn't marshal result")
}
result := requestresult.New(resultWithErr, r.objectRef)
err = r.artifactManager.RegisterResult(ctx, r.requestRef, result)
if err != nil {
return errors.Wrap(err, "couldn't register result")
}
r.result = resultWithErr
return nil
}
func ProcessLogicalError(ctx context.Context, err error) bool {
e, ok := errors.Cause(err).(*payload.CodedError)
if ok {
switch e.Code {
case payload.CodeNotFound:
return true
case payload.CodeLoopDetected:
stats.Record(ctx, metrics.CallMethodLoopDetected.M(1))
return true
}
}
return false
}