cojson 0.16.3 → 0.16.5
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/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +20 -0
- package/dist/coValue.d.ts +1 -1
- package/dist/coValue.d.ts.map +1 -1
- package/dist/coValue.js.map +1 -1
- package/dist/coValueContentMessage.d.ts +10 -0
- package/dist/coValueContentMessage.d.ts.map +1 -0
- package/dist/coValueContentMessage.js +46 -0
- package/dist/coValueContentMessage.js.map +1 -0
- package/dist/coValueCore/coValueCore.d.ts +6 -10
- package/dist/coValueCore/coValueCore.d.ts.map +1 -1
- package/dist/coValueCore/coValueCore.js +20 -125
- package/dist/coValueCore/coValueCore.js.map +1 -1
- package/dist/coValueCore/verifiedState.d.ts +1 -0
- package/dist/coValueCore/verifiedState.d.ts.map +1 -1
- package/dist/coValueCore/verifiedState.js +14 -27
- package/dist/coValueCore/verifiedState.js.map +1 -1
- package/dist/coValues/group.d.ts +18 -10
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/group.js +237 -67
- package/dist/coValues/group.js.map +1 -1
- package/dist/ids.d.ts +3 -3
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js.map +1 -1
- package/dist/localNode.d.ts +11 -6
- package/dist/localNode.d.ts.map +1 -1
- package/dist/localNode.js +7 -2
- package/dist/localNode.js.map +1 -1
- package/dist/queue/LocalTransactionsSyncQueue.d.ts +24 -0
- package/dist/queue/LocalTransactionsSyncQueue.d.ts.map +1 -0
- package/dist/queue/LocalTransactionsSyncQueue.js +55 -0
- package/dist/queue/LocalTransactionsSyncQueue.js.map +1 -0
- package/dist/queue/StoreQueue.d.ts +9 -6
- package/dist/queue/StoreQueue.d.ts.map +1 -1
- package/dist/queue/StoreQueue.js +10 -2
- package/dist/queue/StoreQueue.js.map +1 -1
- package/dist/storage/storageAsync.d.ts +11 -3
- package/dist/storage/storageAsync.d.ts.map +1 -1
- package/dist/storage/storageAsync.js +59 -46
- package/dist/storage/storageAsync.js.map +1 -1
- package/dist/storage/storageSync.d.ts +9 -3
- package/dist/storage/storageSync.d.ts.map +1 -1
- package/dist/storage/storageSync.js +48 -35
- package/dist/storage/storageSync.js.map +1 -1
- package/dist/storage/syncUtils.d.ts +2 -1
- package/dist/storage/syncUtils.d.ts.map +1 -1
- package/dist/storage/syncUtils.js +4 -0
- package/dist/storage/syncUtils.js.map +1 -1
- package/dist/storage/types.d.ts +3 -2
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync.d.ts +6 -6
- package/dist/sync.d.ts.map +1 -1
- package/dist/sync.js +33 -56
- package/dist/sync.js.map +1 -1
- package/dist/tests/StorageApiAsync.test.d.ts +2 -0
- package/dist/tests/StorageApiAsync.test.d.ts.map +1 -0
- package/dist/tests/StorageApiAsync.test.js +574 -0
- package/dist/tests/StorageApiAsync.test.js.map +1 -0
- package/dist/tests/StorageApiSync.test.d.ts +2 -0
- package/dist/tests/StorageApiSync.test.d.ts.map +1 -0
- package/dist/tests/StorageApiSync.test.js +426 -0
- package/dist/tests/StorageApiSync.test.js.map +1 -0
- package/dist/tests/StoreQueue.test.js +9 -21
- package/dist/tests/StoreQueue.test.js.map +1 -1
- package/dist/tests/SyncStateManager.test.js +18 -8
- package/dist/tests/SyncStateManager.test.js.map +1 -1
- package/dist/tests/group.inheritance.test.js +274 -2
- package/dist/tests/group.inheritance.test.js.map +1 -1
- package/dist/tests/group.removeMember.test.js +152 -1
- package/dist/tests/group.removeMember.test.js.map +1 -1
- package/dist/tests/group.roleOf.test.js +2 -2
- package/dist/tests/group.roleOf.test.js.map +1 -1
- package/dist/tests/group.test.js +81 -3
- package/dist/tests/group.test.js.map +1 -1
- package/dist/tests/sync.auth.test.js +22 -10
- package/dist/tests/sync.auth.test.js.map +1 -1
- package/dist/tests/sync.load.test.js +30 -25
- package/dist/tests/sync.load.test.js.map +1 -1
- package/dist/tests/sync.mesh.test.js +12 -6
- package/dist/tests/sync.mesh.test.js.map +1 -1
- package/dist/tests/sync.peerReconciliation.test.js +6 -4
- package/dist/tests/sync.peerReconciliation.test.js.map +1 -1
- package/dist/tests/sync.storage.test.js +8 -14
- package/dist/tests/sync.storage.test.js.map +1 -1
- package/dist/tests/sync.storageAsync.test.js +31 -14
- package/dist/tests/sync.storageAsync.test.js.map +1 -1
- package/dist/tests/sync.test.js +5 -9
- package/dist/tests/sync.test.js.map +1 -1
- package/dist/tests/sync.upload.test.js +31 -1
- package/dist/tests/sync.upload.test.js.map +1 -1
- package/dist/tests/testStorage.d.ts +2 -3
- package/dist/tests/testStorage.d.ts.map +1 -1
- package/dist/tests/testStorage.js +16 -8
- package/dist/tests/testStorage.js.map +1 -1
- package/dist/tests/testUtils.d.ts +4 -0
- package/dist/tests/testUtils.d.ts.map +1 -1
- package/dist/tests/testUtils.js +22 -4
- package/dist/tests/testUtils.js.map +1 -1
- package/dist/typeUtils/accountOrAgentIDfromSessionID.d.ts +2 -2
- package/dist/typeUtils/accountOrAgentIDfromSessionID.d.ts.map +1 -1
- package/dist/typeUtils/expectGroup.d.ts.map +1 -1
- package/dist/typeUtils/expectGroup.js +6 -5
- package/dist/typeUtils/expectGroup.js.map +1 -1
- package/package.json +1 -1
- package/src/coValue.ts +1 -4
- package/src/coValueContentMessage.ts +73 -0
- package/src/coValueCore/coValueCore.ts +36 -192
- package/src/coValueCore/verifiedState.ts +28 -35
- package/src/coValues/group.ts +329 -99
- package/src/ids.ts +3 -3
- package/src/localNode.ts +15 -10
- package/src/queue/LocalTransactionsSyncQueue.ts +96 -0
- package/src/queue/StoreQueue.ts +22 -12
- package/src/storage/storageAsync.ts +78 -56
- package/src/storage/storageSync.ts +66 -45
- package/src/storage/syncUtils.ts +9 -1
- package/src/storage/types.ts +6 -5
- package/src/sync.ts +47 -67
- package/src/tests/StorageApiAsync.test.ts +829 -0
- package/src/tests/StorageApiSync.test.ts +628 -0
- package/src/tests/StoreQueue.test.ts +10 -24
- package/src/tests/SyncStateManager.test.ts +22 -21
- package/src/tests/group.inheritance.test.ts +415 -1
- package/src/tests/group.removeMember.test.ts +244 -1
- package/src/tests/group.roleOf.test.ts +2 -2
- package/src/tests/group.test.ts +105 -5
- package/src/tests/sync.auth.test.ts +22 -10
- package/src/tests/sync.load.test.ts +32 -26
- package/src/tests/sync.mesh.test.ts +12 -6
- package/src/tests/sync.peerReconciliation.test.ts +6 -4
- package/src/tests/sync.storage.test.ts +8 -14
- package/src/tests/sync.storageAsync.test.ts +39 -14
- package/src/tests/sync.test.ts +6 -14
- package/src/tests/sync.upload.test.ts +38 -1
- package/src/tests/testStorage.ts +19 -13
- package/src/tests/testUtils.ts +29 -5
- package/src/typeUtils/accountOrAgentIDfromSessionID.ts +2 -2
- package/src/typeUtils/expectGroup.ts +8 -5
package/src/sync.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
1
|
import { Histogram, ValueType, metrics } from "@opentelemetry/api";
|
|
2
2
|
import { PeerState } from "./PeerState.js";
|
|
3
3
|
import { SyncStateManager } from "./SyncStateManager.js";
|
|
4
|
+
import {
|
|
5
|
+
getTransactionSize,
|
|
6
|
+
knownStateFromContent,
|
|
7
|
+
} from "./coValueContentMessage.js";
|
|
4
8
|
import { CoValueCore } from "./coValueCore/coValueCore.js";
|
|
5
9
|
import { getDependedOnCoValuesFromRawData } from "./coValueCore/utils.js";
|
|
6
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
CoValueHeader,
|
|
12
|
+
Transaction,
|
|
13
|
+
VerifiedState,
|
|
14
|
+
} from "./coValueCore/verifiedState.js";
|
|
7
15
|
import { Signature } from "./crypto/crypto.js";
|
|
8
|
-
import { RawCoID, SessionID } from "./ids.js";
|
|
16
|
+
import { RawCoID, SessionID, isRawCoID } from "./ids.js";
|
|
9
17
|
import { LocalNode } from "./localNode.js";
|
|
10
18
|
import { logger } from "./logger.js";
|
|
11
19
|
import { CoValuePriority } from "./priority.js";
|
|
12
20
|
import { IncomingMessagesQueue } from "./queue/IncomingMessagesQueue.js";
|
|
21
|
+
import { LocalTransactionsSyncQueue } from "./queue/LocalTransactionsSyncQueue.js";
|
|
13
22
|
import { accountOrAgentIDfromSessionID } from "./typeUtils/accountOrAgentIDfromSessionID.js";
|
|
14
23
|
import { isAccountID } from "./typeUtils/isAccountID.js";
|
|
15
24
|
|
|
@@ -57,10 +66,12 @@ export type NewContentMessage = {
|
|
|
57
66
|
};
|
|
58
67
|
|
|
59
68
|
export type SessionNewContent = {
|
|
69
|
+
// The index where to start appending the new transactions. The index counting starts from 1.
|
|
60
70
|
after: number;
|
|
61
71
|
newTransactions: Transaction[];
|
|
62
72
|
lastSignature: Signature;
|
|
63
73
|
};
|
|
74
|
+
|
|
64
75
|
export type DoneMessage = {
|
|
65
76
|
action: "done";
|
|
66
77
|
id: RawCoID;
|
|
@@ -162,13 +173,9 @@ export class SyncManager {
|
|
|
162
173
|
}
|
|
163
174
|
|
|
164
175
|
handleSyncMessage(msg: SyncMessage, peer: PeerState) {
|
|
165
|
-
if (msg.id
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
});
|
|
169
|
-
return;
|
|
170
|
-
} else if (!msg.id.startsWith("co_z")) {
|
|
171
|
-
logger.warn("Received sync message with invalid id", {
|
|
176
|
+
if (!isRawCoID(msg.id)) {
|
|
177
|
+
const errorType = msg.id ? "invalid" : "undefined";
|
|
178
|
+
logger.warn(`Received sync message with ${errorType} id`, {
|
|
172
179
|
msg,
|
|
173
180
|
});
|
|
174
181
|
return;
|
|
@@ -431,12 +438,9 @@ export class SyncManager {
|
|
|
431
438
|
|
|
432
439
|
recordTransactionsSize(newTransactions: Transaction[], source: string) {
|
|
433
440
|
for (const tx of newTransactions) {
|
|
434
|
-
const
|
|
435
|
-
tx.privacy === "private"
|
|
436
|
-
? tx.encryptedChanges.length
|
|
437
|
-
: tx.changes.length;
|
|
441
|
+
const size = getTransactionSize(tx);
|
|
438
442
|
|
|
439
|
-
this.transactionsSizeHistogram.record(
|
|
443
|
+
this.transactionsSizeHistogram.record(size, {
|
|
440
444
|
source,
|
|
441
445
|
});
|
|
442
446
|
}
|
|
@@ -674,7 +678,7 @@ export class SyncManager {
|
|
|
674
678
|
const syncedPeers = [];
|
|
675
679
|
|
|
676
680
|
if (from !== "storage") {
|
|
677
|
-
this.
|
|
681
|
+
this.storeContent(msg);
|
|
678
682
|
}
|
|
679
683
|
|
|
680
684
|
for (const peer of this.peersInPriorityOrder()) {
|
|
@@ -736,60 +740,18 @@ export class SyncManager {
|
|
|
736
740
|
};
|
|
737
741
|
}
|
|
738
742
|
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
for (const trackingSet of this.dirtyCoValuesTrackingSets) {
|
|
746
|
-
trackingSet.add(coValue.id);
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
queueMicrotask(() => {
|
|
750
|
-
if (this.requestedSyncs.has(coValue.id)) {
|
|
751
|
-
this.syncCoValue(coValue);
|
|
752
|
-
}
|
|
753
|
-
});
|
|
754
|
-
|
|
755
|
-
this.requestedSyncs.add(coValue.id);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
|
-
storeCoValue(coValue: CoValueCore, data: NewContentMessage[] | undefined) {
|
|
759
|
-
const storage = this.local.storage;
|
|
760
|
-
|
|
761
|
-
if (!storage || !data) return;
|
|
762
|
-
|
|
763
|
-
// Try to store the content as-is for performance
|
|
764
|
-
// In case that some transactions are missing, a correction will be requested, but it's an edge case
|
|
765
|
-
storage.store(data, (correction) => {
|
|
766
|
-
if (!coValue.hasVerifiedContent()) return;
|
|
767
|
-
|
|
768
|
-
const newContentPieces = coValue.verified.newContentSince(correction);
|
|
769
|
-
|
|
770
|
-
if (!newContentPieces) return;
|
|
743
|
+
private syncQueue = new LocalTransactionsSyncQueue((content) =>
|
|
744
|
+
this.syncContent(content),
|
|
745
|
+
);
|
|
746
|
+
syncHeader = this.syncQueue.syncHeader;
|
|
747
|
+
syncLocalTransaction = this.syncQueue.syncTransaction;
|
|
771
748
|
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
"Correction requested by storage after sending a correction content",
|
|
775
|
-
{
|
|
776
|
-
response,
|
|
777
|
-
knownState: coValue.knownState(),
|
|
778
|
-
},
|
|
779
|
-
);
|
|
780
|
-
});
|
|
781
|
-
});
|
|
782
|
-
}
|
|
749
|
+
syncContent(content: NewContentMessage) {
|
|
750
|
+
const coValue = this.local.getCoValue(content.id);
|
|
783
751
|
|
|
784
|
-
|
|
785
|
-
this.requestedSyncs.delete(coValue.id);
|
|
752
|
+
this.storeContent(content);
|
|
786
753
|
|
|
787
|
-
|
|
788
|
-
const knownState = this.local.storage.getKnownState(coValue.id);
|
|
789
|
-
const newContentPieces = coValue.verified.newContentSince(knownState);
|
|
790
|
-
|
|
791
|
-
this.storeCoValue(coValue, newContentPieces);
|
|
792
|
-
}
|
|
754
|
+
const contentKnownState = knownStateFromContent(content);
|
|
793
755
|
|
|
794
756
|
for (const peer of this.peersInPriorityOrder()) {
|
|
795
757
|
if (peer.closed) continue;
|
|
@@ -803,7 +765,11 @@ export class SyncManager {
|
|
|
803
765
|
continue;
|
|
804
766
|
}
|
|
805
767
|
|
|
806
|
-
this
|
|
768
|
+
// We assume that the peer already knows anything before this content
|
|
769
|
+
// Any eventual reconciliation will be handled through the known state messages exchange
|
|
770
|
+
this.trySendToPeer(peer, content);
|
|
771
|
+
peer.combineOptimisticWith(coValue.id, contentKnownState);
|
|
772
|
+
peer.trackToldKnownState(coValue.id);
|
|
807
773
|
}
|
|
808
774
|
|
|
809
775
|
for (const peer of this.getPeers()) {
|
|
@@ -811,6 +777,20 @@ export class SyncManager {
|
|
|
811
777
|
}
|
|
812
778
|
}
|
|
813
779
|
|
|
780
|
+
private storeContent(content: NewContentMessage) {
|
|
781
|
+
const storage = this.local.storage;
|
|
782
|
+
|
|
783
|
+
if (!storage) return;
|
|
784
|
+
|
|
785
|
+
// Try to store the content as-is for performance
|
|
786
|
+
// In case that some transactions are missing, a correction will be requested, but it's an edge case
|
|
787
|
+
storage.store(content, (correction) => {
|
|
788
|
+
return this.local
|
|
789
|
+
.getCoValue(content.id)
|
|
790
|
+
.verified?.newContentSince(correction);
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
|
|
814
794
|
waitForSyncWithPeer(peerId: PeerID, id: RawCoID, timeout: number) {
|
|
815
795
|
const { syncState } = this;
|
|
816
796
|
const currentSyncState = syncState.getCurrentSyncState(peerId, id);
|