-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact.go
303 lines (263 loc) · 8.45 KB
/
artifact.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
//
// 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 artifact
import (
"context"
"github.com/pkg/errors"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/insolar/record"
"github.com/insolar/insolar/ledger/object"
)
//go:generate minimock -i github.com/insolar/insolar/ledger/artifact.Manager -o ./ -s _gen_mock.go -g
// Manager implements methods required for direct ledger access.
type Manager interface {
// GetObject returns descriptor for provided state.
//
// If provided state is nil, the latest state will be returned (w/o deactivation check).
GetObject(ctx context.Context, head insolar.Reference) (ObjectDescriptor, error)
// RegisterRequest creates request record in storage.
RegisterRequest(ctx context.Context, req record.IncomingRequest) (*insolar.ID, error)
// RegisterResult saves payload result in storage (emulates of save call result by VM).
RegisterResult(ctx context.Context, obj, request insolar.Reference, payload []byte) (*insolar.ID, error)
// ActivateObject creates activate object record in storage.
// If memory is not provided, the prototype default memory will be used.
//
// Request reference will be this object's identifier and referred as "object head".
ActivateObject(
ctx context.Context,
domain, obj, parent, prototype insolar.Reference,
memory []byte,
) error
// UpdateObject creates amend object record in storage.
// Provided reference should be a reference to the head of the object.
// Provided memory well be the new object memory.
//
// Returned descriptor is the latest object state (exact) reference.
UpdateObject(ctx context.Context, domain, request insolar.Reference, obj ObjectDescriptor, memory []byte) error
// DeployCode creates new code record in storage (code records are used to activate prototypes).
DeployCode(
ctx context.Context,
domain insolar.Reference,
request insolar.Reference,
code []byte,
machineType insolar.MachineType,
) (*insolar.ID, error)
}
// Scope implements Manager interface.
type Scope struct {
PulseNumber insolar.PulseNumber
PCS insolar.PlatformCryptographyScheme
RecordModifier object.RecordModifier
RecordAccessor object.RecordAccessor
IndexAccessor object.IndexAccessor
IndexModifier object.IndexModifier
}
// GetObject returns descriptor for provided state.
//
// If provided state is nil, the latest state will be returned (w/o deactivation check).
func (m *Scope) GetObject(
ctx context.Context,
head insolar.Reference,
) (ObjectDescriptor, error) {
idx, err := m.IndexAccessor.ForID(ctx, m.PulseNumber, *head.GetLocal())
if err != nil {
return nil, err
}
rec, err := m.RecordAccessor.ForID(ctx, *idx.Lifeline.LatestState)
if err != nil {
return nil, err
}
concrete := record.Unwrap(&rec.Virtual)
state, ok := concrete.(record.State)
if !ok {
return nil, errors.New("invalid object record")
}
desc := &objectDescriptor{
head: head,
state: *idx.Lifeline.LatestState,
prototype: state.GetImage(),
isPrototype: state.GetIsPrototype(),
parent: idx.Lifeline.Parent,
}
if state.GetMemory() != nil {
desc.memory = state.GetMemory()
}
return desc, nil
}
// RegisterRequest creates request record in storage.
func (m *Scope) RegisterRequest(ctx context.Context, req record.IncomingRequest) (*insolar.ID, error) {
virtRec := record.Wrap(&req)
return m.setRecord(ctx, virtRec)
}
// RegisterResult saves payload result in storage (emulates of save call result by VM).
func (m *Scope) RegisterResult(
ctx context.Context, obj, request insolar.Reference, payload []byte,
) (*insolar.ID, error) {
res := record.Result{
Object: *obj.GetLocal(),
Request: request,
Payload: payload,
}
virtRec := record.Wrap(&res)
return m.setRecord(ctx, virtRec)
}
// ActivateObject creates activate object record in storage.
// If memory is not provided, the prototype default memory will be used.
//
// Request reference will be this object's identifier and referred as "object head".
func (m *Scope) ActivateObject(
ctx context.Context,
domain, obj, parent, prototype insolar.Reference,
memory []byte,
) error {
return m.activateObject(ctx, domain, obj, prototype, false, parent, memory)
}
func (m *Scope) activateObject(
ctx context.Context,
domain insolar.Reference,
obj insolar.Reference,
prototype insolar.Reference,
isPrototype bool,
parent insolar.Reference,
memory []byte,
) error {
_, err := m.IndexAccessor.ForID(ctx, m.PulseNumber, *parent.GetLocal())
if err != nil {
return errors.Wrapf(err, "not found parent index for activated object: %v", parent.String())
}
stateRecord := record.Activate{
Request: obj,
Image: prototype,
IsPrototype: isPrototype,
Parent: parent,
}
err = m.updateStateObject(ctx, obj, &stateRecord, memory)
if err != nil {
return errors.Wrap(err, "fail to store activation state")
}
return nil
}
// UpdateObject creates amend object record in storage.
// Provided reference should be a reference to the head of the object.
// Provided memory well be the new object memory.
//
// Returned descriptor is the latest object state (exact) reference.
func (m *Scope) UpdateObject(
ctx context.Context,
domain, request insolar.Reference,
objDesc ObjectDescriptor,
memory []byte,
) error {
if objDesc.IsPrototype() {
return errors.New("object is not an instance")
}
var (
image *insolar.Reference
err error
)
if objDesc.IsPrototype() {
image, err = objDesc.Code()
} else {
image, err = objDesc.Prototype()
}
if err != nil {
return errors.Wrap(err, "failed to update object")
}
amendRecord := record.Amend{
Request: request,
Image: *image,
IsPrototype: objDesc.IsPrototype(),
PrevState: *objDesc.StateID(),
}
return m.updateStateObject(ctx, *objDesc.HeadRef(), &amendRecord, memory)
}
// DeployCode creates new code record in storage (code records are used to activate prototypes).
func (m *Scope) DeployCode(
ctx context.Context,
domain insolar.Reference,
request insolar.Reference,
code []byte,
machineType insolar.MachineType,
) (*insolar.ID, error) {
codeRec := record.Code{
Request: request,
Code: code,
MachineType: machineType,
}
return m.setRecord(
ctx,
record.Wrap(&codeRec),
)
}
func (m *Scope) setRecord(ctx context.Context, rec record.Virtual) (*insolar.ID, error) {
hash := record.HashVirtual(m.PCS.ReferenceHasher(), rec)
id := insolar.NewID(m.PulseNumber, hash)
matRec := record.Material{
Virtual: rec,
JetID: insolar.ZeroJetID,
ID: *id,
}
return id, m.RecordModifier.Set(ctx, matRec)
}
func (m *Scope) updateStateObject(
ctx context.Context,
objRef insolar.Reference,
stateObject record.State,
memory []byte,
) error {
var virtRecord record.Virtual
switch so := stateObject.(type) {
case *record.Activate:
so.Memory = memory
virtRecord = record.Wrap(so)
case *record.Amend:
so.Memory = memory
virtRecord = record.Wrap(so)
default:
panic("unknown state object type")
}
idx, err := m.IndexAccessor.ForID(ctx, m.PulseNumber, *objRef.GetLocal())
// No index on our node.
if err != nil {
if err != object.ErrIndexNotFound {
return errors.Wrap(err, "failed get index for updating state object")
}
if stateObject.ID() != record.StateActivation {
return errors.Wrap(err, "index not found for updating non Activation state object")
}
// We are activating the object. There is no index for it yet.
idx = record.Index{
Lifeline: record.Lifeline{StateID: record.StateUndefined},
PendingRecords: []insolar.ID{},
ObjID: *objRef.GetLocal(),
}
}
id, err := m.setRecord(ctx, virtRecord)
if err != nil {
return errors.Wrap(err, "fail set record for state object")
}
// update index
idx.Lifeline.StateID = stateObject.ID()
idx.Lifeline.LatestState = id
if stateObject.ID() == record.StateActivation {
idx.Lifeline.Parent = stateObject.(*record.Activate).Parent
}
err = m.IndexModifier.SetIndex(ctx, m.PulseNumber, idx)
if err != nil {
return errors.Wrap(err, "fail set index for state object")
}
return nil
}