@peerbit/shared-log 9.0.6 → 9.0.7-343b1fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.d.ts +5 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +59 -6
- package/dist/src/index.js.map +1 -1
- package/dist/src/replication.d.ts +8 -0
- package/dist/src/replication.d.ts.map +1 -1
- package/dist/src/replication.js +36 -24
- package/dist/src/replication.js.map +1 -1
- package/dist/src/role.d.ts +37 -0
- package/dist/src/role.d.ts.map +1 -1
- package/dist/src/role.js +99 -92
- package/dist/src/role.js.map +1 -1
- package/package.json +70 -70
- package/src/index.ts +79 -7
- package/src/replication.ts +37 -22
- package/src/role.ts +68 -64
package/src/replication.ts
CHANGED
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
BinaryReader,
|
|
3
3
|
deserialize,
|
|
4
4
|
field,
|
|
5
|
+
option,
|
|
5
6
|
serialize,
|
|
6
7
|
variant,
|
|
7
8
|
vec,
|
|
@@ -9,28 +10,14 @@ import {
|
|
|
9
10
|
import { PublicSignKey, equals, randomBytes } from "@peerbit/crypto";
|
|
10
11
|
import { type Index, id } from "@peerbit/indexer-interface";
|
|
11
12
|
import { TransportMessage } from "./message.js";
|
|
12
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
Observer,
|
|
15
|
+
Replicator,
|
|
16
|
+
Role,
|
|
17
|
+
SEGMENT_COORDINATE_SCALE,
|
|
18
|
+
} from "./role.js";
|
|
13
19
|
|
|
14
20
|
export type ReplicationLimits = { min: MinReplicas; max?: MinReplicas };
|
|
15
|
-
/*
|
|
16
|
-
export class ReplicatorRect {
|
|
17
|
-
|
|
18
|
-
@id({ type: Uint8Array })
|
|
19
|
-
id: Uint8Array;
|
|
20
|
-
|
|
21
|
-
@field({ type: 'string' })
|
|
22
|
-
hash: string;
|
|
23
|
-
|
|
24
|
-
@field({ type: vec(ReplicationSegment) })
|
|
25
|
-
segments: ReplicationSegment[];
|
|
26
|
-
|
|
27
|
-
constructor(properties: { hash: string; segments: ReplicationSegment[] }) {
|
|
28
|
-
this.id = randomBytes(32);
|
|
29
|
-
this.hash = properties.hash;
|
|
30
|
-
this.segments = properties.segments;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
*/
|
|
34
21
|
|
|
35
22
|
export enum ReplicationIntent {
|
|
36
23
|
Explicit = 0,
|
|
@@ -280,7 +267,35 @@ export class RequestReplicationInfoMessage extends TransportMessage {
|
|
|
280
267
|
}
|
|
281
268
|
}
|
|
282
269
|
|
|
270
|
+
// @deprecated remove when possible
|
|
283
271
|
@variant([1, 1])
|
|
272
|
+
export class ResponseRoleMessage extends TransportMessage {
|
|
273
|
+
@field({ type: option(Role) })
|
|
274
|
+
role: Observer | Replicator;
|
|
275
|
+
|
|
276
|
+
constructor(properties: { role: Observer | Replicator }) {
|
|
277
|
+
super();
|
|
278
|
+
this.role = properties.role;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
toReplicationInfoMessage(): ResponseReplicationInfoMessage {
|
|
282
|
+
return new ResponseReplicationInfoMessage({
|
|
283
|
+
segments:
|
|
284
|
+
this.role instanceof Replicator
|
|
285
|
+
? this.role.segments.map((x) => {
|
|
286
|
+
return new ReplicationRange({
|
|
287
|
+
id: randomBytes(32),
|
|
288
|
+
offset: x.offset,
|
|
289
|
+
factor: x.factor,
|
|
290
|
+
timestamp: x.timestamp,
|
|
291
|
+
});
|
|
292
|
+
})
|
|
293
|
+
: [],
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
@variant([1, 2])
|
|
284
299
|
export class ResponseReplicationInfoMessage extends TransportMessage {
|
|
285
300
|
@field({ type: vec(ReplicationRange) })
|
|
286
301
|
segments: ReplicationRange[];
|
|
@@ -291,7 +306,7 @@ export class ResponseReplicationInfoMessage extends TransportMessage {
|
|
|
291
306
|
}
|
|
292
307
|
}
|
|
293
308
|
|
|
294
|
-
@variant([1,
|
|
309
|
+
@variant([1, 3])
|
|
295
310
|
export class StartedReplicating extends TransportMessage {
|
|
296
311
|
@field({ type: vec(ReplicationRange) })
|
|
297
312
|
segments: ReplicationRange[];
|
|
@@ -302,7 +317,7 @@ export class StartedReplicating extends TransportMessage {
|
|
|
302
317
|
}
|
|
303
318
|
}
|
|
304
319
|
|
|
305
|
-
@variant([1,
|
|
320
|
+
@variant([1, 4])
|
|
306
321
|
export class StoppedReplicating extends TransportMessage {
|
|
307
322
|
@field({ type: vec(Uint8Array) })
|
|
308
323
|
segmentIds: Uint8Array[];
|
package/src/role.ts
CHANGED
|
@@ -1,16 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated
|
|
3
|
+
* Code below is deprecated and will be removed in the future.
|
|
4
|
+
* Roles have been replaces with just replication segments.
|
|
5
|
+
*/
|
|
6
|
+
import { field, variant, vec } from "@dao-xyz/borsh";
|
|
7
|
+
|
|
8
|
+
export const SEGMENT_COORDINATE_SCALE = 4294967295;
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
export const overlaps = (x1: number, x2: number, y1: number, y2: number) => {
|
|
4
11
|
if (x1 <= y2 && y1 <= x2) {
|
|
5
12
|
return true;
|
|
6
13
|
}
|
|
7
14
|
return false;
|
|
8
|
-
};
|
|
15
|
+
};
|
|
9
16
|
|
|
10
|
-
|
|
11
|
-
abstract equals(other: Role): boolean;
|
|
17
|
+
export abstract class Role {
|
|
18
|
+
abstract equals(other: Role): boolean;
|
|
12
19
|
}
|
|
13
|
-
|
|
20
|
+
|
|
14
21
|
export const NO_TYPE_VARIANT = new Uint8Array([0]);
|
|
15
22
|
|
|
16
23
|
@variant(0)
|
|
@@ -29,83 +36,80 @@ export class Observer extends Role {
|
|
|
29
36
|
}
|
|
30
37
|
}
|
|
31
38
|
|
|
32
|
-
export const REPLICATOR_TYPE_VARIANT = new Uint8Array([2])
|
|
33
|
-
|
|
34
|
-
export const SEGMENT_COORDINATE_SCALE = 4294967295;
|
|
35
|
-
/* export class ReplicationSegment {
|
|
39
|
+
export const REPLICATOR_TYPE_VARIANT = new Uint8Array([2]);
|
|
36
40
|
|
|
41
|
+
export class RoleReplicationSegment {
|
|
37
42
|
@field({ type: "u64" })
|
|
38
43
|
timestamp: bigint;
|
|
39
44
|
|
|
40
45
|
@field({ type: "u32" })
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
@field({ type: 'u32' })
|
|
44
|
-
end: number;
|
|
45
|
-
|
|
46
|
+
private factorNominator: number;
|
|
46
47
|
|
|
48
|
+
@field({ type: "u32" })
|
|
49
|
+
private offsetNominator: number;
|
|
47
50
|
|
|
48
51
|
constructor(properties: {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
timestamp
|
|
52
|
+
factor: number;
|
|
53
|
+
offset: number;
|
|
54
|
+
timestamp?: bigint;
|
|
52
55
|
}) {
|
|
53
|
-
const {
|
|
56
|
+
const { factor, timestamp, offset } = properties;
|
|
57
|
+
if (factor > 1 || factor < 0) {
|
|
58
|
+
throw new Error("Expecting factor to be between 0 and 1, got: " + factor);
|
|
59
|
+
}
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
this.timestamp = timestamp ?? BigInt(+new Date());
|
|
62
|
+
this.factorNominator = Math.round(SEGMENT_COORDINATE_SCALE * factor);
|
|
63
|
+
|
|
64
|
+
if (offset > 1 || offset < 0) {
|
|
65
|
+
throw new Error("Expecting offset to be between 0 and 1, got: " + offset);
|
|
57
66
|
}
|
|
58
|
-
this.
|
|
59
|
-
this.end = Math.round(end * SEGMENT_COORDINATE_SCALE);
|
|
60
|
-
this.timestamp = timestamp
|
|
67
|
+
this.offsetNominator = Math.round(SEGMENT_COORDINATE_SCALE * offset);
|
|
61
68
|
}
|
|
62
69
|
|
|
70
|
+
get factor(): number {
|
|
71
|
+
return this.factorNominator / SEGMENT_COORDINATE_SCALE;
|
|
72
|
+
}
|
|
63
73
|
|
|
64
|
-
|
|
74
|
+
get offset(): number {
|
|
75
|
+
return this.offsetNominator / SEGMENT_COORDINATE_SCALE;
|
|
76
|
+
}
|
|
65
77
|
}
|
|
66
|
-
*/
|
|
67
|
-
|
|
68
|
-
/* abstract class Capacity { }
|
|
69
78
|
|
|
70
79
|
@variant(2)
|
|
71
80
|
export class Replicator extends Role {
|
|
81
|
+
@field({ type: vec(RoleReplicationSegment) })
|
|
82
|
+
segments: RoleReplicationSegment[];
|
|
72
83
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
constructor(properties: {
|
|
85
|
+
factor: number;
|
|
86
|
+
timestamp?: bigint;
|
|
87
|
+
offset: number;
|
|
88
|
+
}) {
|
|
77
89
|
super();
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
factor: number;
|
|
84
|
-
offset: number;
|
|
85
|
-
}) {
|
|
86
|
-
super();
|
|
87
|
-
let timestamp = properties.timestamp || BigInt(+new Date);
|
|
88
|
-
let ranges = getSegmentsFromOffsetAndRange(properties.offset, properties.factor);
|
|
89
|
-
this.segments = ranges.map(x => new ReplicationSegment({ start: x[0], end: x[1], timestamp }));
|
|
90
|
-
}
|
|
91
|
-
*/
|
|
92
|
-
/* get factor(): number {
|
|
93
|
-
return this.segments[0]!.factor;
|
|
94
|
-
}
|
|
90
|
+
const segment: RoleReplicationSegment = new RoleReplicationSegment(
|
|
91
|
+
properties,
|
|
92
|
+
);
|
|
93
|
+
this.segments = [segment];
|
|
94
|
+
}
|
|
95
95
|
|
|
96
|
-
get
|
|
97
|
-
|
|
98
|
-
}
|
|
96
|
+
get factor(): number {
|
|
97
|
+
return this.segments[0]!.factor;
|
|
98
|
+
}
|
|
99
99
|
|
|
100
|
-
get
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
100
|
+
get offset(): number {
|
|
101
|
+
return this.segments[0]!.offset;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
get timestamp(): bigint {
|
|
105
|
+
return this.segments[0]!.timestamp;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
equals(other: Role) {
|
|
109
|
+
return (
|
|
110
|
+
other instanceof Replicator &&
|
|
111
|
+
other.factor === this.factor &&
|
|
112
|
+
other.offset === this.offset
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|