-
Notifications
You must be signed in to change notification settings - Fork 0
/
ledger.go
216 lines (177 loc) · 7.88 KB
/
ledger.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
/*
* 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 core
import "context"
// JetRole is number representing a node role.
type JetRole int
const (
// RoleVirtualExecutor is responsible for current pulse CPU operations.
RoleVirtualExecutor = JetRole(iota + 1)
// RoleVirtualValidator is responsible for previous pulse CPU operations.
RoleVirtualValidator
// RoleLightExecutor is responsible for current pulse Disk operations.
RoleLightExecutor
// RoleLightValidator is responsible for previous pulse Disk operations.
RoleLightValidator
// RoleHeavyExecutor is responsible for permanent Disk operations.
RoleHeavyExecutor
)
// Ledger is the global ledger handler. Other system parts communicate with ledger through it.
type Ledger interface {
// GetArtifactManager returns artifact manager to work with.
GetArtifactManager() ArtifactManager
// GetJetCoordinator returns jet coordinator to work with.
GetJetCoordinator() JetCoordinator
// GetPulseManager returns pulse manager to work with.
GetPulseManager() PulseManager
}
// PulseManager provides Ledger's methods related to Pulse.
type PulseManager interface {
// Current returns current pulse structure.
Current(context.Context) (*Pulse, error)
// Set set's new pulse and closes current jet drop.
Set(context.Context, Pulse) error
}
// JetCoordinator provides methods for calculating Jet affinity
// (e.g. to which Jet a message should be sent).
type JetCoordinator interface {
// IsAuthorized checks for role on concrete pulse for the address.
IsAuthorized(ctx context.Context, role JetRole, obj RecordRef, pulse PulseNumber, node RecordRef) (bool, error)
// QueryRole returns node refs responsible for role bound operations for given object and pulse.
QueryRole(ctx context.Context, role JetRole, obj RecordRef, pulse PulseNumber) ([]RecordRef, error)
}
// ArtifactManager is a high level storage interface.
type ArtifactManager interface {
// GenesisRef returns the root record reference.
//
// Root record is the parent for all top-level records.
GenesisRef() *RecordRef
// RegisterRequest creates or check call request record and returns it RecordRef.
// (used by VM on executing side)
RegisterRequest(ctx context.Context, message Message) (*RecordID, error)
// RegisterValidation marks provided object state as approved or disapproved.
//
// When fetching object, validity can be specified.
RegisterValidation(ctx context.Context, object RecordRef, state RecordID, isValid bool, validationMessages []Message) error
// RegisterResult saves VM method call result.
RegisterResult(ctx context.Context, request RecordRef, payload []byte) (*RecordID, error)
// GetCode returns code from code record by provided reference according to provided machine preference.
//
// This method is used by VM to fetch code for execution.
GetCode(ctx context.Context, ref RecordRef) (CodeDescriptor, error)
// GetObject returns descriptor for provided state.
//
// If provided state is nil, the latest state will be returned (with deactivation check). Returned descriptor will
// provide methods for fetching all related data.
GetObject(ctx context.Context, head RecordRef, state *RecordID, approved bool) (ObjectDescriptor, error)
// GetDelegate returns provided object's delegate reference for provided type.
//
// Object delegate should be previously created for this object. If object delegate does not exist, an error will
// be returned.
GetDelegate(ctx context.Context, head, asType RecordRef) (*RecordRef, error)
// GetChildren returns children iterator.
//
// During iteration children refs will be fetched from remote source (parent object).
GetChildren(ctx context.Context, parent RecordRef, pulse *PulseNumber) (RefIterator, error)
// DeclareType creates new type record in storage.
//
// Type is a contract interface. It contains one method signature.
DeclareType(ctx context.Context, domain, request RecordRef, typeDec []byte) (*RecordID, error)
// DeployCode creates new code record in storage.
//
// Code records are used to activate prototype.
DeployCode(ctx context.Context, domain, request RecordRef, code []byte, machineType MachineType) (*RecordID, error)
// ActivatePrototype creates activate object record in storage. Provided prototype reference will be used as objects prototype
// memory as memory of created object. 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".
ActivatePrototype(
ctx context.Context,
domain, request, parent, code RecordRef,
memory []byte,
) (ObjectDescriptor, 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, request, parent, prototype RecordRef,
asDelegate bool,
memory []byte,
) (ObjectDescriptor, error)
// UpdatePrototype creates amend object record in storage. Provided reference should be a reference to the head of
// the prototype. Provided memory well be the new object memory.
//
// Returned reference will be the latest object state (exact) reference.
UpdatePrototype(
ctx context.Context,
domain, request RecordRef,
obj ObjectDescriptor,
memory []byte,
code *RecordRef,
) (ObjectDescriptor, 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 reference will be the latest object state (exact) reference.
UpdateObject(
ctx context.Context,
domain, request RecordRef,
obj ObjectDescriptor,
memory []byte,
) (ObjectDescriptor, error)
// DeactivateObject creates deactivate object record in storage. Provided reference should be a reference to the head
// of the object. If object is already deactivated, an error should be returned.
//
// Deactivated object cannot be changed.
DeactivateObject(ctx context.Context, domain, request RecordRef, obj ObjectDescriptor) (*RecordID, error)
}
// CodeDescriptor represents meta info required to fetch all code data.
type CodeDescriptor interface {
// Ref returns reference to represented code record.
Ref() *RecordRef
// MachineType returns code machine type for represented code.
MachineType() MachineType
// Code returns code data.
Code() ([]byte, error)
}
// ObjectDescriptor represents meta info required to fetch all object data.
type ObjectDescriptor interface {
// HeadRef returns head reference to represented object record.
HeadRef() *RecordRef
// StateID returns reference to object state record.
StateID() *RecordID
// Memory fetches object memory from storage.
Memory() []byte
// IsPrototype determines if the object is a prototype.
IsPrototype() bool
// Code returns code reference.
Code() (*RecordRef, error)
// Prototype returns prototype reference.
Prototype() (*RecordRef, error)
// Children returns object's children references.
Children(pulse *PulseNumber) (RefIterator, error)
// ChildPointer returns the latest child for this object.
ChildPointer() *RecordID
// Parent returns object's parent.
Parent() *RecordRef
}
// RefIterator is used for iteration over affined children(parts) of container.
type RefIterator interface {
Next() (*RecordRef, error)
HasNext() bool
}