-
Notifications
You must be signed in to change notification settings - Fork 336
/
ownership.go
542 lines (474 loc) · 12.1 KB
/
ownership.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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
package gno
import (
"encoding/binary"
"fmt"
)
/*
## Ownership
In Gno, all objects are automatically persisted to disk after
every atomic "transaction" (a function call that must return
immediately.) when new objects are associated with a
"ownership tree" which is maintained overlaying the possibly
cyclic object graph (NOTE: cyclic references for persistence
not supported at this stage). The ownership tree is composed
of objects (arrays, structs, maps, and blocks) and
derivatives (pointers, slices, and so on) with optional
struct-tag annotations to define the ownership tree.
If an object hangs off of the ownership tree, it becomes
included in the Merkle root, and is said to be "real". The
Merkle-ized state of reality gets updated with state
transition transactions; during such a transaction, some new
temporary objects may "become real" by becoming associated in
the ownership tree (say, assigned to a struct field or
appended to a slice that was part of the ownership tree prior
to the transaction), but those that don't get garbage
collected and forgotten.
In the first release of Gno, all fields are owned in the same
realm, and no cyclic dependencies are allowed outside the
bounds of a realm transaction (this will change in phase 2,
where ref-counted references and weak references will be
supported).
*/
type ObjectID struct {
RealmID // base
NewTime uint64 // time created
}
func (oid ObjectID) String() string {
if oid.RealmID.IsZero() {
// XXX what's at the very top?
return fmt.Sprintf("OIDNONE:%d", oid.NewTime)
} else {
return fmt.Sprintf("OID%X:%d",
oid.RealmID.Bytes(), oid.NewTime)
}
}
func (oid ObjectID) Bytes() []byte {
bz := make([]byte, HashSize+8)
copy(bz[:HashSize], oid.RealmID.Bytes())
binary.BigEndian.PutUint64(
bz[HashSize:], uint64(oid.NewTime))
return bz
}
// TODO: make faster by making RealmID a pointer
// and enforcing that the value of RealmID is never zero.
func (oid ObjectID) IsZero() bool {
if debug {
if oid.RealmID.IsZero() && oid.NewTime != 0 {
panic("should not happen")
}
}
return oid.RealmID.IsZero()
}
type Object interface {
GetObjectInfo() *ObjectInfo
GetObjectID() ObjectID
MustGetObjectID() ObjectID
SetObjectID(oid ObjectID)
GetHash() ValueHash
SetHash(ValueHash)
GetOwner() Object
GetOwnerID() ObjectID
SetOwner(Object)
GetIsOwned() bool
GetIsReal() bool
GetModTime() uint64
IncRefCount() int
DecRefCount() int
GetRefCount() int
GetIsNewReal() bool
SetIsNewReal(bool)
GetIsDirty() bool
SetIsDirty(bool, uint64)
GetIsDeleted() bool
SetIsDeleted(bool, uint64)
GetIsProcessing() bool
SetIsProcessing(bool)
GetIsTransient() bool
// Saves to realm along the way if owned, and also (dirty
// or new).
// ValueImage(rlm *Realm, owned bool) *ValueImage
}
var _ Object = &ArrayValue{}
var _ Object = &StructValue{}
var _ Object = &MapValue{}
var _ Object = &Block{}
type ObjectInfo struct {
ID ObjectID // set if real.
Hash ValueHash // zero if dirty.
OwnerID ObjectID // parent in the ownership tree.
ModTime uint64 // time last updated.
RefCount int // deleted/gc'd if 0.
isNewReal bool
isDirty bool
isDeleted bool
isProcessing bool
owner Object // mem reference to owner.
}
func (oi *ObjectInfo) String() string {
return fmt.Sprintf(
"OI[%s#%X,owner=%s,refs=%d,new:%v,drt:%v,del:%v]",
oi.ID.String(),
oi.Hash.Bytes(),
oi.OwnerID.String(),
oi.RefCount,
oi.GetIsNewReal(),
oi.GetIsDirty(),
oi.GetIsDeleted(),
)
}
func (oi *ObjectInfo) Bytes() []byte {
if debug {
if oi.ID.IsZero() {
panic("should not happen")
}
if oi.Hash.IsZero() {
panic("should not happen")
}
if oi.OwnerID.IsZero() {
panic("should not happen")
}
}
bz := make([]byte, 0, 100)
bz = append(bz, sizedBytes(oi.ID.Bytes())...)
bz = append(bz, sizedBytes(oi.Hash.Bytes())...)
bz = append(bz, sizedBytes(oi.OwnerID.Bytes())...)
bz = append(bz, varintBytes(int64(oi.ModTime))...)
bz = append(bz, varintBytes(int64(oi.RefCount))...)
return bz
}
func (oi *ObjectInfo) GetObjectInfo() *ObjectInfo {
return oi
}
func (oi *ObjectInfo) GetObjectID() ObjectID {
return oi.ID
}
func (oi *ObjectInfo) MustGetObjectID() ObjectID {
if oi.ID.IsZero() {
panic("unexpected zero object id")
}
return oi.ID
}
func (oi *ObjectInfo) SetObjectID(oid ObjectID) {
oi.ID = oid
}
func (oi *ObjectInfo) GetHash() ValueHash {
return oi.Hash
}
func (oi *ObjectInfo) SetHash(vh ValueHash) {
oi.Hash = vh
}
func (oi *ObjectInfo) GetOwner() Object {
return oi.owner
}
func (oi *ObjectInfo) SetOwner(po Object) {
oi.OwnerID = po.GetObjectID()
oi.owner = po
}
func (oi *ObjectInfo) GetOwnerID() ObjectID {
if oi.owner == nil {
return ObjectID{}
} else {
return oi.owner.GetObjectID()
}
}
func (oi *ObjectInfo) GetIsOwned() bool {
return !oi.OwnerID.IsZero()
}
// NOTE: does not return true for new reals.
func (oi *ObjectInfo) GetIsReal() bool {
return !oi.ID.IsZero()
}
func (oi *ObjectInfo) GetModTime() uint64 {
return oi.ModTime
}
func (oi *ObjectInfo) IncRefCount() int {
oi.RefCount++
return oi.RefCount
}
func (oi *ObjectInfo) DecRefCount() int {
oi.RefCount--
if oi.RefCount < 0 {
// This may happen for uninitialized values.
if debug {
if oi.GetIsReal() {
panic("should not happen")
}
}
}
return oi.RefCount
}
func (oi *ObjectInfo) GetRefCount() int {
return oi.RefCount
}
func (oi *ObjectInfo) GetIsNewReal() bool {
return oi.isNewReal
}
func (oi *ObjectInfo) SetIsNewReal(x bool) {
oi.isNewReal = x
}
func (oi *ObjectInfo) GetIsDirty() bool {
return oi.isDirty
}
func (oi *ObjectInfo) SetIsDirty(x bool, mt uint64) {
if x {
oi.Hash = ValueHash{}
oi.ModTime = mt
}
oi.isDirty = x
}
func (oi *ObjectInfo) GetIsDeleted() bool {
return oi.isDeleted
}
func (oi *ObjectInfo) SetIsDeleted(x bool, mt uint64) {
// NOTE: Don't over-write modtime.
// Consider adding a DelTime, or just log it somewhere, or
// continue to ignore it.
oi.isDirty = x
}
func (oi *ObjectInfo) GetIsProcessing() bool {
return oi.isProcessing
}
func (oi *ObjectInfo) SetIsProcessing(x bool) {
oi.isProcessing = x
}
func (oi *ObjectInfo) GetIsTransient() bool {
return false
}
// Returns the value as an object if it is an object,
// or is a pointer or slice of an object.
func (tv *TypedValue) GetObject() Object {
switch cv := tv.V.(type) {
case PointerValue:
// TODO: In terms of defining the object dependency graph,
// whether the relevant object is the pointer base or
// the pointed object (.base or .typedvalue) depends
// on the number of references to the base. In the future
// when supporting ref-counted and weak references,
// calculate this on the fly or with a pre-pass.
return cv.TypedValue.GetObject()
case *ArrayValue:
return cv
case *SliceValue:
if cv.Base == nil {
// otherwise `return cv.Base` returns a typed-nil.
return nil
} else {
return cv.Base
}
case *StructValue:
return cv
case *FuncValue:
return nil
case *MapValue:
return cv
case BoundMethodValue:
rov, ok := cv.Receiver.V.(Object)
if ok {
return rov
} else {
return nil
}
case nativeValue:
// native values don't work with realms,
// but this function shouldn't happen.
// XXX panic?
return nil
case blockValue:
if cv.Block == nil {
panic("should not happen")
}
return cv.Block
default:
return nil
}
}
//----------------------------------------
// ExtendedObject
// ExtendedObject is for storing native arrays, slices, structs, maps, as
// well as Gno maps. It implements Object for gno maps, but not for native
// types which are not supported by realm persistence. ExtendedObject is
// required for *MapValue for the machine state to be persistable between
// slot access and assignment to said slot.
type ExtendedObject struct {
BaseMap *MapValue // if base is gno map
BaseNative *nativeValue // if base is native array/slice/struct/map.
Index TypedValue // integer index or arbitrary map key
Path ValuePath // value path for (native) selectors
}
func (eo ExtendedObject) GetObjectInfo() *ObjectInfo {
if eo.BaseMap != nil {
return eo.BaseMap.GetObjectInfo()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetObjectID() ObjectID {
if eo.BaseMap != nil {
return eo.BaseMap.GetObjectID()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) MustGetObjectID() ObjectID {
if eo.BaseMap != nil {
return eo.BaseMap.MustGetObjectID()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetObjectID(oid ObjectID) {
if eo.BaseMap != nil {
eo.BaseMap.SetObjectID(oid)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetHash() ValueHash {
if eo.BaseMap != nil {
return eo.BaseMap.GetHash()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetHash(vh ValueHash) {
if eo.BaseMap != nil {
eo.BaseMap.SetHash(vh)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetOwner() Object {
if eo.BaseMap != nil {
return eo.BaseMap.GetOwner()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetOwnerID() ObjectID {
if eo.BaseMap != nil {
return eo.BaseMap.GetOwnerID()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetOwner(obj Object) {
if eo.BaseMap != nil {
eo.BaseMap.SetOwner(obj)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsOwned() bool {
if eo.BaseMap != nil {
return eo.BaseMap.GetIsOwned()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsReal() bool {
if eo.BaseMap != nil {
return eo.BaseMap.GetIsReal()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetModTime() uint64 {
if eo.BaseMap != nil {
return eo.BaseMap.GetModTime()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) IncRefCount() int {
if eo.BaseMap != nil {
return eo.BaseMap.IncRefCount()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) DecRefCount() int {
if eo.BaseMap != nil {
return eo.BaseMap.DecRefCount()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetRefCount() int {
if eo.BaseMap != nil {
return eo.BaseMap.GetRefCount()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsNewReal() bool {
if eo.BaseMap != nil {
return eo.BaseMap.GetIsNewReal()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetIsNewReal(b bool) {
if eo.BaseMap != nil {
eo.BaseMap.SetIsNewReal(b)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsDirty() bool {
if eo.BaseMap != nil {
return eo.BaseMap.GetIsDirty()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetIsDirty(b bool, mt uint64) {
if eo.BaseMap != nil {
eo.BaseMap.SetIsDirty(b, mt)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsDeleted() bool {
if eo.BaseMap != nil {
return eo.BaseMap.GetIsDeleted()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetIsDeleted(b bool, mt uint64) {
if eo.BaseMap != nil {
eo.BaseMap.SetIsDeleted(b, mt)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsProcessing() bool {
if eo.BaseMap != nil {
return eo.BaseMap.GetIsProcessing()
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) SetIsProcessing(b bool) {
if eo.BaseMap != nil {
eo.BaseMap.SetIsProcessing(b)
} else {
panic("native values are not realm compatible")
}
}
func (eo ExtendedObject) GetIsTransient() bool {
if eo.BaseMap != nil {
return false
} else {
return true // native values cannot be realm persisted.
}
}
/*
func (eo ExtendedObject) ValueImage(rlm *Realm, owned bool) *ValueImage {
if eo.BaseMap != nil {
return eo.BaseMap.ValueImage(rlm, owned)
} else {
panic("native values are not realm compatible")
}
}
*/