cojson 0.18.0 → 0.18.1

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.
Files changed (62) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/CHANGELOG.md +6 -0
  3. package/dist/coValueCore/branching.d.ts +36 -0
  4. package/dist/coValueCore/branching.d.ts.map +1 -0
  5. package/dist/coValueCore/branching.js +122 -0
  6. package/dist/coValueCore/branching.js.map +1 -0
  7. package/dist/coValueCore/coValueCore.d.ts +71 -5
  8. package/dist/coValueCore/coValueCore.d.ts.map +1 -1
  9. package/dist/coValueCore/coValueCore.js +162 -53
  10. package/dist/coValueCore/coValueCore.js.map +1 -1
  11. package/dist/coValueCore/decodeTransactionChangesAndMeta.d.ts +3 -0
  12. package/dist/coValueCore/decodeTransactionChangesAndMeta.d.ts.map +1 -0
  13. package/dist/coValueCore/decodeTransactionChangesAndMeta.js +59 -0
  14. package/dist/coValueCore/decodeTransactionChangesAndMeta.js.map +1 -0
  15. package/dist/coValueCore/verifiedState.d.ts.map +1 -1
  16. package/dist/coValues/coList.d.ts +3 -3
  17. package/dist/coValues/coList.d.ts.map +1 -1
  18. package/dist/coValues/coList.js +4 -7
  19. package/dist/coValues/coList.js.map +1 -1
  20. package/dist/coValues/coMap.d.ts +3 -3
  21. package/dist/coValues/coMap.d.ts.map +1 -1
  22. package/dist/coValues/coMap.js +6 -6
  23. package/dist/coValues/coMap.js.map +1 -1
  24. package/dist/coValues/coStream.d.ts +3 -3
  25. package/dist/coValues/coStream.d.ts.map +1 -1
  26. package/dist/coValues/coStream.js +4 -4
  27. package/dist/coValues/coStream.js.map +1 -1
  28. package/dist/ids.d.ts.map +1 -1
  29. package/dist/ids.js.map +1 -1
  30. package/dist/jsonStringify.d.ts +1 -0
  31. package/dist/jsonStringify.d.ts.map +1 -1
  32. package/dist/jsonStringify.js +8 -0
  33. package/dist/jsonStringify.js.map +1 -1
  34. package/dist/permissions.d.ts +2 -7
  35. package/dist/permissions.d.ts.map +1 -1
  36. package/dist/permissions.js +72 -70
  37. package/dist/permissions.js.map +1 -1
  38. package/dist/tests/branching.test.d.ts +2 -0
  39. package/dist/tests/branching.test.d.ts.map +1 -0
  40. package/dist/tests/branching.test.js +99 -0
  41. package/dist/tests/branching.test.js.map +1 -0
  42. package/dist/tests/coValueCore.test.js +2 -3
  43. package/dist/tests/coValueCore.test.js.map +1 -1
  44. package/dist/tests/sync.storage.test.js +1 -3
  45. package/dist/tests/sync.storage.test.js.map +1 -1
  46. package/dist/tests/sync.upload.test.js +1 -3
  47. package/dist/tests/sync.upload.test.js.map +1 -1
  48. package/package.json +2 -2
  49. package/src/coValueCore/branching.ts +198 -0
  50. package/src/coValueCore/coValueCore.ts +255 -72
  51. package/src/coValueCore/decodeTransactionChangesAndMeta.ts +81 -0
  52. package/src/coValueCore/verifiedState.ts +1 -1
  53. package/src/coValues/coList.ts +8 -10
  54. package/src/coValues/coMap.ts +8 -11
  55. package/src/coValues/coStream.ts +7 -8
  56. package/src/ids.ts +4 -1
  57. package/src/jsonStringify.ts +8 -0
  58. package/src/permissions.ts +80 -89
  59. package/src/tests/branching.test.ts +141 -0
  60. package/src/tests/coValueCore.test.ts +2 -3
  61. package/src/tests/sync.storage.test.ts +3 -3
  62. package/src/tests/sync.upload.test.ts +3 -3
@@ -1,4 +1,4 @@
1
1
 
2
- > cojson@0.18.0 build /home/runner/_work/jazz/jazz/packages/cojson
2
+ > cojson@0.18.1 build /home/runner/_work/jazz/jazz/packages/cojson
3
3
  > rm -rf ./dist && tsc --sourceMap --outDir dist
4
4
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # cojson
2
2
 
3
+ ## 0.18.1
4
+
5
+ ### Patch Changes
6
+
7
+ - cojson-core-wasm@0.18.1
8
+
3
9
  ## 0.18.0
4
10
 
5
11
  ### Minor Changes
@@ -0,0 +1,36 @@
1
+ import { CoValueCore } from "../exports.js";
2
+ import { RawCoID } from "../ids.js";
3
+ import { AvailableCoValueCore } from "./coValueCore.js";
4
+ import { CoValueHeader } from "./verifiedState.js";
5
+ import { CoValueKnownState } from "../sync.js";
6
+ export declare function getBranchHeader({ type, branchName, ownerId, sourceId, }: {
7
+ type: CoValueHeader["type"];
8
+ branchName: string;
9
+ ownerId: RawCoID;
10
+ sourceId: RawCoID;
11
+ }): CoValueHeader;
12
+ /**
13
+ * Given a coValue, a branch name and an owner id, returns the id for the branch
14
+ */
15
+ export declare function getBranchId(coValue: CoValueCore, name: string, ownerId: RawCoID): RawCoID;
16
+ export type BranchCommit = {
17
+ branch: CoValueKnownState["sessions"];
18
+ };
19
+ /**
20
+ * Given a coValue, a branch name and an owner id, creates a new branch CoValue
21
+ */
22
+ export declare function createBranch(coValue: CoValueCore, name: string, ownerId: RawCoID): CoValueCore;
23
+ /**
24
+ * Given a branch coValue, returns the source coValue if available
25
+ */
26
+ export declare function getBranchSource(coValue: CoValueCore): AvailableCoValueCore | undefined;
27
+ export type MergeCommit = {
28
+ merge: CoValueKnownState["sessions"];
29
+ id: RawCoID;
30
+ count: number;
31
+ };
32
+ /**
33
+ * Given a branch coValue, merges the branch into the source coValue
34
+ */
35
+ export declare function mergeBranch(branch: CoValueCore): CoValueCore;
36
+ //# sourceMappingURL=branching.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"branching.d.ts","sourceRoot":"","sources":["../../src/coValueCore/branching.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAa,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,oBAAoB,EAAe,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,wBAAgB,eAAe,CAAC,EAC9B,IAAI,EACJ,UAAU,EACV,OAAO,EACP,QAAQ,GACT,EAAE;IACD,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;CACnB,GAAG,aAAa,CAkBhB;AAED;;GAEG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GACf,OAAO,CAeT;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,iBAAiB,CAAC,UAAU,CAAC,CAAC;CACvC,CAAC;AAEF;;GAEG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,WAAW,EACpB,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GACf,WAAW,CAsBb;AAED;;GAEG;AACH,wBAAgB,eAAe,CAC7B,OAAO,EAAE,WAAW,GACnB,oBAAoB,GAAG,SAAS,CAkBlC;AAED,MAAM,MAAM,WAAW,GAAG;IAExB,KAAK,EAAE,iBAAiB,CAAC,UAAU,CAAC,CAAC;IAErC,EAAE,EAAE,OAAO,CAAC;IAEZ,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,WAAW,CAiE5D"}
@@ -0,0 +1,122 @@
1
+ import { idforHeader } from "./coValueCore.js";
2
+ export function getBranchHeader({ type, branchName, ownerId, sourceId, }) {
3
+ return {
4
+ type,
5
+ // Branch name and source id are stored in the meta field
6
+ // and used to generate the unique id for the branch
7
+ meta: {
8
+ branch: branchName,
9
+ source: sourceId,
10
+ },
11
+ ruleset: {
12
+ type: "ownedByGroup",
13
+ // The owner is part of the id generation, making it possible to have multiple branches with the same name
14
+ // but different owners
15
+ group: ownerId,
16
+ },
17
+ // The meta is enough to have reproducible unique id for the branch
18
+ uniqueness: "",
19
+ };
20
+ }
21
+ /**
22
+ * Given a coValue, a branch name and an owner id, returns the id for the branch
23
+ */
24
+ export function getBranchId(coValue, name, ownerId) {
25
+ if (!coValue.verified) {
26
+ throw new Error("CoValueCore: getBranchId called on coValue without verified state");
27
+ }
28
+ const header = getBranchHeader({
29
+ type: coValue.verified.header.type,
30
+ branchName: name,
31
+ ownerId,
32
+ sourceId: coValue.id,
33
+ });
34
+ return idforHeader(header, coValue.node.crypto);
35
+ }
36
+ /**
37
+ * Given a coValue, a branch name and an owner id, creates a new branch CoValue
38
+ */
39
+ export function createBranch(coValue, name, ownerId) {
40
+ if (!coValue.verified) {
41
+ throw new Error("CoValueCore: createBranch called on coValue without verified state");
42
+ }
43
+ const header = getBranchHeader({
44
+ type: coValue.verified.header.type,
45
+ branchName: name,
46
+ ownerId,
47
+ sourceId: coValue.id,
48
+ });
49
+ const value = coValue.node.createCoValue(header);
50
+ // Create a branch commit to identify the starting point of the branch
51
+ value.makeTransaction([], "private", {
52
+ branch: coValue.knownState().sessions,
53
+ });
54
+ return value;
55
+ }
56
+ /**
57
+ * Given a branch coValue, returns the source coValue if available
58
+ */
59
+ export function getBranchSource(coValue) {
60
+ if (!coValue.verified) {
61
+ return undefined;
62
+ }
63
+ const sourceId = coValue.verified.header.meta?.source;
64
+ if (!sourceId) {
65
+ return undefined;
66
+ }
67
+ const source = coValue.node.getCoValue(sourceId);
68
+ if (!source.isAvailable()) {
69
+ return undefined;
70
+ }
71
+ return source;
72
+ }
73
+ /**
74
+ * Given a branch coValue, merges the branch into the source coValue
75
+ */
76
+ export function mergeBranch(branch) {
77
+ if (!branch.verified) {
78
+ throw new Error("CoValueCore: mergeBranch called on coValue without verified state");
79
+ }
80
+ const sourceId = branch.verified.header.meta?.source;
81
+ if (!sourceId) {
82
+ throw new Error("CoValueCore: mergeBranch called on a non-branch coValue");
83
+ }
84
+ const target = getBranchSource(branch);
85
+ if (!target) {
86
+ throw new Error("CoValueCore: unable to find source branch");
87
+ }
88
+ // Look for previous merge commits, to see which transactions needs to be merged
89
+ // Done mostly for performance reasons, as we could merge all the transactions every time and nothing would change
90
+ const mergedTransactions = target.mergeCommits.reduce((acc, { commit }) => {
91
+ if (commit.id !== branch.id) {
92
+ return acc;
93
+ }
94
+ for (const [sessionID, count] of Object.entries(commit.merge)) {
95
+ acc[sessionID] = Math.max(acc[sessionID] ?? 0, count);
96
+ }
97
+ return acc;
98
+ }, {});
99
+ // Get the valid transactions from the branch, skipping the branch source and the previously merged transactions
100
+ const branchValidTransactions = branch
101
+ .getValidTransactions({
102
+ from: mergedTransactions,
103
+ ignorePrivateTransactions: false,
104
+ skipBranchSource: true,
105
+ })
106
+ .filter((tx) => tx.changes.length > 0);
107
+ // If there are no valid transactions to merge, we don't want to create a merge commit
108
+ if (branchValidTransactions.length === 0) {
109
+ return target;
110
+ }
111
+ // Create a merge commit to identify the merge point
112
+ target.makeTransaction([], "private", {
113
+ merge: { ...branch.knownState().sessions },
114
+ id: branch.id,
115
+ count: branchValidTransactions.length,
116
+ });
117
+ for (const { tx, changes } of branchValidTransactions) {
118
+ target.makeTransaction(changes, tx.privacy);
119
+ }
120
+ return target;
121
+ }
122
+ //# sourceMappingURL=branching.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"branching.js","sourceRoot":"","sources":["../../src/coValueCore/branching.ts"],"names":[],"mappings":"AAEA,OAAO,EAAwB,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAIrE,MAAM,UAAU,eAAe,CAAC,EAC9B,IAAI,EACJ,UAAU,EACV,OAAO,EACP,QAAQ,GAMT;IACC,OAAO;QACL,IAAI;QACJ,yDAAyD;QACzD,oDAAoD;QACpD,IAAI,EAAE;YACJ,MAAM,EAAE,UAAU;YAClB,MAAM,EAAE,QAAQ;SACjB;QACD,OAAO,EAAE;YACP,IAAI,EAAE,cAAc;YACpB,0GAA0G;YAC1G,uBAAuB;YACvB,KAAK,EAAE,OAAO;SACf;QACD,mEAAmE;QACnE,UAAU,EAAE,EAAE;KACf,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CACzB,O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AAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,CAAC;QACxD,CAAC;QAED,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAmC,CACpC,CAAC;IAEF,gHAAgH;IAChH,MAAM,uBAAuB,GAAG,MAAM;SACnC,oBAAoB,CAAC;QACpB,IAAI,EAAE,kBAAkB;QACxB,yBAAyB,EAAE,KAAK;QAChC,gBAAgB,EAAE,IAAI;KACvB,CAAC;SACD,MAAM,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAEzC,sFAAsF;IACtF,IAAI,uBAAuB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzC,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,oDAAoD;IACpD,MAAM,CAAC,eAAe,CAAC,EAAE,EAAE,SAAS,EAAE;QACpC,KAAK,EAAE,EAAE,GAAG,MAAM,CAAC,UAAU,EAAE,CAAC,QAAQ,EAAE;QAC1C,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,KAAK,EAAE,uBAAuB,CAAC,MAAM;KAChB,CAAC,CAAC;IAEzB,KAAK,MAAM,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,uBAAuB,EAAE,CAAC;QACtD,MAAM,CAAC,eAAe,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC;IAC9C,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -4,17 +4,32 @@ import type { RawCoValue } from "../coValue.js";
4
4
  import type { ControlledAccountOrAgent } from "../coValues/account.js";
5
5
  import type { RawGroup } from "../coValues/group.js";
6
6
  import { CryptoProvider, Hash, KeyID, KeySecret, Signature, SignerID } from "../crypto/crypto.js";
7
- import { RawCoID, SessionID, TransactionID } from "../ids.js";
7
+ import { AgentID, RawCoID, SessionID, TransactionID } from "../ids.js";
8
8
  import { JsonObject, JsonValue } from "../jsonValue.js";
9
9
  import { LocalNode, ResolveAccountAgentError } from "../localNode.js";
10
10
  import { CoValueKnownState, PeerID } from "../sync.js";
11
11
  import { CoValueHeader, Transaction, VerifiedState } from "./verifiedState.js";
12
+ import { MergeCommit } from "./branching.js";
13
+ import { type RawAccountID } from "../coValues/account.js";
12
14
  export declare function idforHeader(header: CoValueHeader, crypto: CryptoProvider): RawCoID;
15
+ export type VerifiedTransaction = {
16
+ author: RawAccountID | AgentID;
17
+ txID: TransactionID;
18
+ tx: Transaction;
19
+ madeAt: number;
20
+ isValidated: boolean;
21
+ changes: JsonValue[] | undefined;
22
+ meta: JsonObject | undefined;
23
+ isValid: boolean;
24
+ hasInvalidChanges: boolean;
25
+ hasInvalidMeta: boolean;
26
+ hasMetaBeenParsed: boolean;
27
+ };
13
28
  export type DecryptedTransaction = {
14
29
  txID: TransactionID;
15
30
  changes: JsonValue[];
16
31
  madeAt: number;
17
- trusting?: boolean;
32
+ tx: Transaction;
18
33
  };
19
34
  export type AvailableCoValueCore = CoValueCore & {
20
35
  verified: VerifiedState;
@@ -39,7 +54,6 @@ export declare class CoValueCore {
39
54
  private readonly peers;
40
55
  private _cachedContent?;
41
56
  readonly listeners: Set<(core: CoValueCore, unsub: () => void) => void>;
42
- private readonly _decryptionCache;
43
57
  private _cachedDependentOn?;
44
58
  private counter;
45
59
  private constructor();
@@ -90,13 +104,65 @@ export declare class CoValueCore {
90
104
  getCurrentContent(options?: {
91
105
  ignorePrivateTransactions: true;
92
106
  }): RawCoValue;
107
+ branchStart: {
108
+ branch: CoValueKnownState["sessions"];
109
+ madeAt: number;
110
+ } | undefined;
111
+ mergeCommits: {
112
+ commit: MergeCommit;
113
+ madeAt: number;
114
+ }[];
115
+ resetParsedTransactions(): void;
116
+ verifiedTransactions: VerifiedTransaction[];
117
+ private verifiedTransactionsKnownSessions;
118
+ /**
119
+ * Loads the new transaction from the SessionMap into verifiedTransactions as a VerifiedTransaction.
120
+ *
121
+ * If the transaction is already loaded from the SessionMap in the past, it will not be loaded again.
122
+ *
123
+ * Used to have a fast way to iterate over the CoValue transactions, and track their validation/decoding state.
124
+ *
125
+ * @param preload - Optional preload object containing the transaction, changes, and meta.
126
+ * If provided, the transaction will be preloaded with the given changes and meta.
127
+ *
128
+ * @internal
129
+ * */
130
+ loadVerifiedTransactionsFromLogs(preload?: {
131
+ transaction: Transaction;
132
+ changes: JsonValue[];
133
+ meta: JsonObject | undefined;
134
+ }): void;
135
+ /**
136
+ * Iterates over the verifiedTransactions and marks them as valid or invalid, based on the group membership of the authors of the transactions .
137
+ */
138
+ private determineValidTransactions;
139
+ /**
140
+ * Parses the meta information of a transaction, and set the branchStart and mergeCommits.
141
+ */
142
+ private parseMetaInformation;
143
+ /**
144
+ * Loads the new transactions from SessionMap and:
145
+ * - Validates each transaction based on the group membership of the authors
146
+ * - Decodes the changes & meta for each transaction
147
+ * - Parses the meta information of the transaction
148
+ */
149
+ private parseNewTransactions;
150
+ /**
151
+ * Returns the valid transactions matching the criteria specified in the options
152
+ */
93
153
  getValidTransactions(options?: {
94
154
  ignorePrivateTransactions: boolean;
95
- knownTransactions?: CoValueKnownState["sessions"];
155
+ from?: CoValueKnownState["sessions"];
156
+ to?: CoValueKnownState["sessions"];
157
+ knownTransactions?: Set<Transaction>;
158
+ skipBranchSource?: boolean;
96
159
  }): DecryptedTransaction[];
160
+ createBranch(name: string, ownerId: RawCoID): CoValueCore;
161
+ mergeBranch(): CoValueCore;
162
+ getBranchId(name: string, ownerId: RawCoID): `co_z${string}`;
97
163
  getValidSortedTransactions(options?: {
98
164
  ignorePrivateTransactions: boolean;
99
- knownTransactions: CoValueKnownState["sessions"];
165
+ knownTransactions?: Set<Transaction>;
100
166
  }): DecryptedTransaction[];
101
167
  compareTransactions(a: Pick<DecryptedTransaction, "madeAt" | "txID">, b: Pick<DecryptedTransaction, "madeAt" | "txID">): number;
102
168
  getCurrentReadKey(): {
@@ -1 +1 @@
1
- {"version":3,"file":"coValueCore.d.ts","sourceRoot":"","sources":["../../src/co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;IACnD,EAAE,EAAE,OAAO,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,0CAA0C,GAAG;IACvD,IAAI,EAAE,uCAAuC,CAAC;IAC9C,EAAE,EAAE,OAAO,CAAC;IACZ,SAAS,EAAE,SAAS,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,uBAAuB,GAC/B,+CAA+C,GAC/C,0CAA0C,GAC1C,wBAAwB,GACxB,gBAAgB,GAChB,qBAAqB,CAAC"}
@@ -2,7 +2,6 @@ import { ValueType, metrics } from "@opentelemetry/api";
2
2
  import { err, ok } from "neverthrow";
3
3
  import { CO_VALUE_LOADING_CONFIG } from "../config.js";
4
4
  import { coreToCoValue } from "../coreToCoValue.js";
5
- import { parseJSON } from "../jsonStringify.js";
6
5
  import { logger } from "../logger.js";
7
6
  import { determineValidTransactions } from "../permissions.js";
8
7
  import { emptyKnownState } from "../sync.js";
@@ -11,6 +10,8 @@ import { expectGroup } from "../typeUtils/expectGroup.js";
11
10
  import { getDependedOnCoValuesFromRawData } from "./utils.js";
12
11
  import { VerifiedState } from "./verifiedState.js";
13
12
  import { SessionMap } from "./SessionMap.js";
13
+ import { createBranch, getBranchId, getBranchSource, mergeBranch, } from "./branching.js";
14
+ import { decodeTransactionChangesAndMeta } from "./decodeTransactionChangesAndMeta.js";
14
15
  export function idforHeader(header, crypto) {
15
16
  const hash = crypto.shortHash(header);
16
17
  return `co_z${hash.slice("shortHash_z".length)}`;
@@ -33,9 +34,12 @@ export class CoValueCore {
33
34
  constructor(init, node) {
34
35
  this.peers = new Map();
35
36
  this.listeners = new Set();
36
- this._decryptionCache = {};
37
37
  this.missingDependencies = new Set();
38
38
  this.deferredUpdates = 0;
39
+ // The list of merge commits that have been made
40
+ this.mergeCommits = [];
41
+ this.verifiedTransactions = [];
42
+ this.verifiedTransactionsKnownSessions = {};
39
43
  this.readKeyCache = new Map();
40
44
  this.crypto = node.crypto;
41
45
  if ("header" in init) {
@@ -220,12 +224,12 @@ export class CoValueCore {
220
224
  throw new Error("CoValueCore: internalShamefullyCloneVerifiedStateFrom called on coValue with verified sessions present!");
221
225
  }
222
226
  this._verified = state.clone();
223
- this._cachedContent = undefined;
224
- this._cachedDependentOn = undefined;
227
+ this.internalShamefullyResetCachedContent();
225
228
  }
226
229
  internalShamefullyResetCachedContent() {
227
230
  this._cachedContent = undefined;
228
231
  this._cachedDependentOn = undefined;
232
+ this.resetParsedTransactions();
229
233
  }
230
234
  subscribeToGroupInvalidation() {
231
235
  if (!this.verified) {
@@ -240,7 +244,8 @@ export class CoValueCore {
240
244
  const entry = this.node.getCoValue(groupId);
241
245
  if (entry.isAvailable()) {
242
246
  this.groupInvalidationSubscription = entry.subscribe((_groupUpdate) => {
243
- this._cachedContent = undefined;
247
+ // When the group is updated, we need to reset the cached content because the transactions validity might have changed
248
+ this.internalShamefullyResetCachedContent();
244
249
  this.notifyUpdate("immediate");
245
250
  }, false);
246
251
  }
@@ -395,18 +400,14 @@ export class CoValueCore {
395
400
  throw new Error("Can't make transaction without read key secret");
396
401
  }
397
402
  result = this.verified.makeNewPrivateTransaction(sessionID, signerAgent, changes, keyID, keySecret, meta);
398
- if (result.transaction.privacy === "private") {
399
- this._decryptionCache[result.transaction.encryptedChanges] = changes;
400
- if (result.transaction.meta) {
401
- this._decryptionCache[result.transaction.meta] = meta;
402
- }
403
- }
404
403
  }
405
404
  else {
406
405
  result = this.verified.makeNewTrustingTransaction(sessionID, signerAgent, changes, meta);
407
406
  }
408
407
  const { transaction, signature } = result;
409
408
  this.node.syncManager.recordTransactionsSize([transaction], "local");
409
+ // We pre-populate the parsed transactions and meta for the new transaction, to skip the parsing step later
410
+ this.loadVerifiedTransactionsFromLogs({ transaction, changes, meta });
410
411
  const session = this.verified.sessions.get(sessionID);
411
412
  const txIdx = session ? session.transactions.length - 1 : 0;
412
413
  this.updateContentAndNotifyUpdate("immediate");
@@ -427,57 +428,162 @@ export class CoValueCore {
427
428
  }
428
429
  return newContent;
429
430
  }
431
+ // Reset the parsed transactions and branches, to validate them again from scratch when the group is updated
432
+ resetParsedTransactions() {
433
+ this.branchStart = undefined;
434
+ this.mergeCommits = [];
435
+ for (const transaction of this.verifiedTransactions) {
436
+ transaction.isValidated = false;
437
+ transaction.hasMetaBeenParsed = false;
438
+ }
439
+ }
440
+ /**
441
+ * Loads the new transaction from the SessionMap into verifiedTransactions as a VerifiedTransaction.
442
+ *
443
+ * If the transaction is already loaded from the SessionMap in the past, it will not be loaded again.
444
+ *
445
+ * Used to have a fast way to iterate over the CoValue transactions, and track their validation/decoding state.
446
+ *
447
+ * @param preload - Optional preload object containing the transaction, changes, and meta.
448
+ * If provided, the transaction will be preloaded with the given changes and meta.
449
+ *
450
+ * @internal
451
+ * */
452
+ loadVerifiedTransactionsFromLogs(preload) {
453
+ if (!this.verified) {
454
+ return;
455
+ }
456
+ for (const [sessionID, sessionLog] of this.verified.sessions.entries()) {
457
+ const count = this.verifiedTransactionsKnownSessions[sessionID] ?? 0;
458
+ sessionLog.transactions.forEach((tx, txIndex) => {
459
+ if (txIndex < count) {
460
+ return;
461
+ }
462
+ this.verifiedTransactions.push({
463
+ author: accountOrAgentIDfromSessionID(sessionID),
464
+ txID: {
465
+ sessionID,
466
+ txIndex,
467
+ },
468
+ madeAt: tx.madeAt,
469
+ isValidated: false,
470
+ isValid: false,
471
+ changes: tx === preload?.transaction ? preload.changes : undefined,
472
+ meta: tx === preload?.transaction ? preload.meta : undefined,
473
+ hasInvalidChanges: false,
474
+ hasInvalidMeta: false,
475
+ hasMetaBeenParsed: false,
476
+ tx,
477
+ });
478
+ });
479
+ this.verifiedTransactionsKnownSessions[sessionID] =
480
+ sessionLog.transactions.length;
481
+ }
482
+ }
483
+ /**
484
+ * Iterates over the verifiedTransactions and marks them as valid or invalid, based on the group membership of the authors of the transactions .
485
+ */
486
+ determineValidTransactions() {
487
+ determineValidTransactions(this);
488
+ }
489
+ /**
490
+ * Parses the meta information of a transaction, and set the branchStart and mergeCommits.
491
+ */
492
+ parseMetaInformation(transaction) {
493
+ if (!transaction.meta ||
494
+ !transaction.isValid ||
495
+ transaction.hasMetaBeenParsed) {
496
+ return;
497
+ }
498
+ transaction.hasMetaBeenParsed = true;
499
+ if (transaction.meta?.["branch"] &&
500
+ (!this.branchStart || transaction.madeAt < this.branchStart.madeAt)) {
501
+ this.branchStart = {
502
+ branch: transaction.meta.branch,
503
+ madeAt: transaction.madeAt,
504
+ };
505
+ }
506
+ if (transaction.meta?.["merge"]) {
507
+ const mergeCommit = transaction.meta;
508
+ this.mergeCommits.push({
509
+ commit: mergeCommit,
510
+ madeAt: transaction.madeAt,
511
+ });
512
+ }
513
+ }
514
+ /**
515
+ * Loads the new transactions from SessionMap and:
516
+ * - Validates each transaction based on the group membership of the authors
517
+ * - Decodes the changes & meta for each transaction
518
+ * - Parses the meta information of the transaction
519
+ */
520
+ parseNewTransactions(ignorePrivateTransactions) {
521
+ if (!this.isAvailable()) {
522
+ return;
523
+ }
524
+ this.loadVerifiedTransactionsFromLogs();
525
+ this.determineValidTransactions();
526
+ for (const transaction of this.verifiedTransactions) {
527
+ decodeTransactionChangesAndMeta(this, transaction, ignorePrivateTransactions);
528
+ this.parseMetaInformation(transaction);
529
+ }
530
+ }
531
+ /**
532
+ * Returns the valid transactions matching the criteria specified in the options
533
+ */
430
534
  getValidTransactions(options) {
431
535
  if (!this.verified) {
432
- throw new Error("CoValueCore: getValidTransactions called on coValue without verified state");
536
+ return [];
433
537
  }
434
- const validTransactions = determineValidTransactions(this, options?.knownTransactions);
435
- const allTransactions = [];
436
- for (const { txID, tx } of validTransactions) {
437
- if (options?.knownTransactions?.[txID.sessionID] >= txID.txIndex) {
538
+ this.parseNewTransactions(options?.ignorePrivateTransactions ?? false);
539
+ const matchingTransactions = [];
540
+ for (const transaction of this.verifiedTransactions) {
541
+ if (!isValidTransactionWithChanges(transaction)) {
438
542
  continue;
439
543
  }
440
- let changes;
441
- if (tx.privacy === "private") {
442
- if (options?.ignorePrivateTransactions) {
443
- continue;
444
- }
445
- const readKey = this.getReadKey(tx.keyUsed);
446
- if (!readKey) {
447
- continue;
448
- }
449
- let decryptedChanges = this._decryptionCache[tx.encryptedChanges];
450
- if (!decryptedChanges) {
451
- decryptedChanges = this.verified.decryptTransaction(txID.sessionID, txID.txIndex, readKey);
452
- this._decryptionCache[tx.encryptedChanges] = decryptedChanges;
453
- }
454
- if (!decryptedChanges) {
455
- logger.error("Failed to decrypt transaction despite having key", {
456
- err: new Error("Failed to decrypt transaction despite having key"),
457
- });
458
- continue;
459
- }
460
- changes = decryptedChanges;
544
+ if (options?.knownTransactions?.has(transaction.tx)) {
545
+ continue;
461
546
  }
462
- else {
463
- try {
464
- changes = parseJSON(tx.changes);
465
- }
466
- catch (e) {
467
- logger.error("Failed to parse trusting transaction on " + this.id, {
468
- err: e,
469
- });
470
- continue;
471
- }
547
+ options?.knownTransactions?.add(transaction.tx);
548
+ const { txID, madeAt } = transaction;
549
+ const from = options?.from?.[txID.sessionID] ?? -1;
550
+ const to = options?.to?.[txID.sessionID] ?? Infinity;
551
+ // The txIndex starts at 0 and from/to are referring to the count of transactions
552
+ if (from > txID.txIndex || to < txID.txIndex) {
553
+ continue;
472
554
  }
473
- allTransactions.push({
474
- txID,
475
- madeAt: tx.madeAt,
476
- changes,
477
- trusting: tx.privacy === "trusting",
555
+ matchingTransactions.push(transaction);
556
+ }
557
+ const source = getBranchSource(this);
558
+ // If this is a branch, we load the valid transactions from the source
559
+ if (source && this.branchStart && !options?.skipBranchSource) {
560
+ const sourceTransactions = source.getValidTransactions({
561
+ to: this.branchStart.branch,
562
+ ignorePrivateTransactions: options?.ignorePrivateTransactions ?? false,
563
+ knownTransactions: options?.knownTransactions,
478
564
  });
565
+ for (const { changes, tx, madeAt, txID } of sourceTransactions) {
566
+ matchingTransactions.push({
567
+ txID: {
568
+ sessionID: `${txID.sessionID}_branch_${source.id}`,
569
+ txIndex: txID.txIndex,
570
+ },
571
+ madeAt,
572
+ changes,
573
+ tx,
574
+ });
575
+ }
479
576
  }
480
- return allTransactions;
577
+ return matchingTransactions;
578
+ }
579
+ createBranch(name, ownerId) {
580
+ return createBranch(this, name, ownerId);
581
+ }
582
+ mergeBranch() {
583
+ return mergeBranch(this);
584
+ }
585
+ getBranchId(name, ownerId) {
586
+ return getBranchId(this, name, ownerId);
481
587
  }
482
588
  getValidSortedTransactions(options) {
483
589
  const allTransactions = this.getValidTransactions(options);
@@ -653,4 +759,7 @@ export class CoValueCore {
653
759
  });
654
760
  }
655
761
  }
762
+ function isValidTransactionWithChanges(transaction) {
763
+ return Boolean(transaction.isValid && transaction.changes);
764
+ }
656
765
  //# sourceMappingURL=coValueCore.js.map