@optimystic/db-core 1.18.0 → 1.20.0
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/README.md +2 -1
- package/dist/src/blocks/helpers.d.ts +7 -0
- package/dist/src/blocks/helpers.d.ts.map +1 -1
- package/dist/src/blocks/helpers.js +7 -1
- package/dist/src/blocks/helpers.js.map +1 -1
- package/dist/src/btree/btree.d.ts +100 -4
- package/dist/src/btree/btree.d.ts.map +1 -1
- package/dist/src/btree/btree.js +252 -90
- package/dist/src/btree/btree.js.map +1 -1
- package/dist/src/btree/node-division.d.ts +37 -0
- package/dist/src/btree/node-division.d.ts.map +1 -0
- package/dist/src/btree/node-division.js +133 -0
- package/dist/src/btree/node-division.js.map +1 -0
- package/dist/src/chain/chain.d.ts +23 -0
- package/dist/src/chain/chain.d.ts.map +1 -1
- package/dist/src/chain/chain.js +43 -6
- package/dist/src/chain/chain.js.map +1 -1
- package/dist/src/cluster/structs.d.ts +41 -8
- package/dist/src/cluster/structs.d.ts.map +1 -1
- package/dist/src/cluster/structs.js +18 -7
- package/dist/src/cluster/structs.js.map +1 -1
- package/dist/src/collection/action.d.ts +29 -0
- package/dist/src/collection/action.d.ts.map +1 -1
- package/dist/src/collection/action.js +6 -0
- package/dist/src/collection/action.js.map +1 -1
- package/dist/src/collection/collection.d.ts +566 -120
- package/dist/src/collection/collection.d.ts.map +1 -1
- package/dist/src/collection/collection.js +1084 -495
- package/dist/src/collection/collection.js.map +1 -1
- package/dist/src/collection/head-commit.d.ts +110 -0
- package/dist/src/collection/head-commit.d.ts.map +1 -0
- package/dist/src/collection/head-commit.js +65 -0
- package/dist/src/collection/head-commit.js.map +1 -0
- package/dist/src/collection/index.d.ts +2 -0
- package/dist/src/collection/index.d.ts.map +1 -1
- package/dist/src/collection/index.js +2 -0
- package/dist/src/collection/index.js.map +1 -1
- package/dist/src/collection/segments.d.ts +77 -0
- package/dist/src/collection/segments.d.ts.map +1 -0
- package/dist/src/collection/segments.js +110 -0
- package/dist/src/collection/segments.js.map +1 -0
- package/dist/src/collection/struct.d.ts +176 -1
- package/dist/src/collection/struct.d.ts.map +1 -1
- package/dist/src/collection/struct.js +70 -0
- package/dist/src/collection/struct.js.map +1 -1
- package/dist/src/collection/write-attempts.d.ts +245 -0
- package/dist/src/collection/write-attempts.d.ts.map +1 -0
- package/dist/src/collection/write-attempts.js +439 -0
- package/dist/src/collection/write-attempts.js.map +1 -0
- package/dist/src/collections/tree/struct.d.ts +100 -17
- package/dist/src/collections/tree/struct.d.ts.map +1 -1
- package/dist/src/collections/tree/struct.js +38 -7
- package/dist/src/collections/tree/struct.js.map +1 -1
- package/dist/src/collections/tree/tree.d.ts +17 -5
- package/dist/src/collections/tree/tree.d.ts.map +1 -1
- package/dist/src/collections/tree/tree.js +117 -29
- package/dist/src/collections/tree/tree.js.map +1 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/log/log.d.ts +5 -1
- package/dist/src/log/log.d.ts.map +1 -1
- package/dist/src/log/log.js +7 -1
- package/dist/src/log/log.js.map +1 -1
- package/dist/src/network/block-size.d.ts +67 -0
- package/dist/src/network/block-size.d.ts.map +1 -0
- package/dist/src/network/block-size.js +82 -0
- package/dist/src/network/block-size.js.map +1 -0
- package/dist/src/network/i-repo.d.ts +3 -0
- package/dist/src/network/i-repo.d.ts.map +1 -1
- package/dist/src/network/index.d.ts +5 -0
- package/dist/src/network/index.d.ts.map +1 -1
- package/dist/src/network/index.js +5 -0
- package/dist/src/network/index.js.map +1 -1
- package/dist/src/network/promise-shortfall-error.d.ts +40 -0
- package/dist/src/network/promise-shortfall-error.d.ts.map +1 -0
- package/dist/src/network/promise-shortfall-error.js +67 -0
- package/dist/src/network/promise-shortfall-error.js.map +1 -0
- package/dist/src/network/refuse-solo.d.ts +8 -0
- package/dist/src/network/refuse-solo.d.ts.map +1 -0
- package/dist/src/network/refuse-solo.js +8 -0
- package/dist/src/network/refuse-solo.js.map +1 -0
- package/dist/src/network/stale-failure.d.ts +20 -3
- package/dist/src/network/stale-failure.d.ts.map +1 -1
- package/dist/src/network/stale-failure.js +24 -3
- package/dist/src/network/stale-failure.js.map +1 -1
- package/dist/src/network/struct.d.ts +161 -13
- package/dist/src/network/struct.d.ts.map +1 -1
- package/dist/src/network/struct.js.map +1 -1
- package/dist/src/network/transaction-size.d.ts +59 -0
- package/dist/src/network/transaction-size.d.ts.map +1 -0
- package/dist/src/network/transaction-size.js +74 -0
- package/dist/src/network/transaction-size.js.map +1 -0
- package/dist/src/network/validity-rejected-error.d.ts +28 -0
- package/dist/src/network/validity-rejected-error.d.ts.map +1 -0
- package/dist/src/network/validity-rejected-error.js +51 -0
- package/dist/src/network/validity-rejected-error.js.map +1 -0
- package/dist/src/offline/entry.d.ts +72 -0
- package/dist/src/offline/entry.d.ts.map +1 -0
- package/dist/src/offline/entry.js +73 -0
- package/dist/src/offline/entry.js.map +1 -0
- package/dist/src/offline/index.d.ts +5 -0
- package/dist/src/offline/index.d.ts.map +1 -0
- package/dist/src/offline/index.js +5 -0
- package/dist/src/offline/index.js.map +1 -0
- package/dist/src/offline/journal.d.ts +61 -0
- package/dist/src/offline/journal.d.ts.map +1 -0
- package/dist/src/offline/journal.js +2 -0
- package/dist/src/offline/journal.js.map +1 -0
- package/dist/src/offline/queue.d.ts +484 -0
- package/dist/src/offline/queue.d.ts.map +1 -0
- package/dist/src/offline/queue.js +1236 -0
- package/dist/src/offline/queue.js.map +1 -0
- package/dist/src/offline/reachability.d.ts +25 -0
- package/dist/src/offline/reachability.d.ts.map +1 -0
- package/dist/src/offline/reachability.js +13 -0
- package/dist/src/offline/reachability.js.map +1 -0
- package/dist/src/offline/status.d.ts +148 -0
- package/dist/src/offline/status.d.ts.map +1 -0
- package/dist/src/offline/status.js +63 -0
- package/dist/src/offline/status.js.map +1 -0
- package/dist/src/offline/wake.d.ts +30 -0
- package/dist/src/offline/wake.d.ts.map +1 -0
- package/dist/src/offline/wake.js +68 -0
- package/dist/src/offline/wake.js.map +1 -0
- package/dist/src/reactivity/config.d.ts +8 -4
- package/dist/src/reactivity/config.d.ts.map +1 -1
- package/dist/src/reactivity/config.js +7 -3
- package/dist/src/reactivity/config.js.map +1 -1
- package/dist/src/reactivity/rotation.d.ts +9 -3
- package/dist/src/reactivity/rotation.d.ts.map +1 -1
- package/dist/src/reactivity/rotation.js +8 -2
- package/dist/src/reactivity/rotation.js.map +1 -1
- package/dist/src/testing/test-transactor.d.ts +31 -0
- package/dist/src/testing/test-transactor.d.ts.map +1 -1
- package/dist/src/testing/test-transactor.js +60 -0
- package/dist/src/testing/test-transactor.js.map +1 -1
- package/dist/src/transaction/commit-failure-class.d.ts +103 -0
- package/dist/src/transaction/commit-failure-class.d.ts.map +1 -0
- package/dist/src/transaction/commit-failure-class.js +96 -0
- package/dist/src/transaction/commit-failure-class.js.map +1 -0
- package/dist/src/transaction/coordinator.d.ts +224 -31
- package/dist/src/transaction/coordinator.d.ts.map +1 -1
- package/dist/src/transaction/coordinator.js +841 -362
- package/dist/src/transaction/coordinator.js.map +1 -1
- package/dist/src/transaction/entry-commit.d.ts +139 -0
- package/dist/src/transaction/entry-commit.d.ts.map +1 -0
- package/dist/src/transaction/entry-commit.js +199 -0
- package/dist/src/transaction/entry-commit.js.map +1 -0
- package/dist/src/transaction/index.d.ts +4 -1
- package/dist/src/transaction/index.d.ts.map +1 -1
- package/dist/src/transaction/index.js +1 -0
- package/dist/src/transaction/index.js.map +1 -1
- package/dist/src/transaction/transaction.d.ts +20 -0
- package/dist/src/transaction/transaction.d.ts.map +1 -1
- package/dist/src/transaction/validator.d.ts +7 -0
- package/dist/src/transaction/validator.d.ts.map +1 -1
- package/dist/src/transaction/validator.js +21 -12
- package/dist/src/transaction/validator.js.map +1 -1
- package/dist/src/transactor/change-notifier.d.ts +13 -4
- package/dist/src/transactor/change-notifier.d.ts.map +1 -1
- package/dist/src/transactor/change-notifier.js.map +1 -1
- package/dist/src/transactor/network-transactor.d.ts +58 -8
- package/dist/src/transactor/network-transactor.d.ts.map +1 -1
- package/dist/src/transactor/network-transactor.js +244 -50
- package/dist/src/transactor/network-transactor.js.map +1 -1
- package/dist/src/transactor/transactor-source.d.ts +22 -11
- package/dist/src/transactor/transactor-source.d.ts.map +1 -1
- package/dist/src/transactor/transactor-source.js +9 -13
- package/dist/src/transactor/transactor-source.js.map +1 -1
- package/dist/src/transactor/transactor.d.ts +14 -0
- package/dist/src/transactor/transactor.d.ts.map +1 -1
- package/dist/src/transform/digest.d.ts +20 -0
- package/dist/src/transform/digest.d.ts.map +1 -1
- package/dist/src/transform/digest.js +23 -3
- package/dist/src/transform/digest.js.map +1 -1
- package/dist/src/utility/visit-every.d.ts +14 -0
- package/dist/src/utility/visit-every.d.ts.map +1 -0
- package/dist/src/utility/visit-every.js +27 -0
- package/dist/src/utility/visit-every.js.map +1 -0
- package/package.json +1 -1
- package/src/blocks/helpers.ts +8 -1
- package/src/btree/btree.ts +266 -88
- package/src/btree/node-division.ts +155 -0
- package/src/chain/chain.ts +55 -6
- package/src/cluster/structs.ts +42 -7
- package/src/collection/action.ts +34 -0
- package/src/collection/collection.ts +1167 -540
- package/src/collection/head-commit.ts +148 -0
- package/src/collection/index.ts +2 -0
- package/src/collection/segments.ts +149 -0
- package/src/collection/struct.ts +201 -1
- package/src/collection/write-attempts.ts +506 -0
- package/src/collections/tree/struct.ts +97 -17
- package/src/collections/tree/tree.ts +129 -31
- package/src/index.ts +2 -0
- package/src/log/log.ts +7 -1
- package/src/network/block-size.ts +81 -0
- package/src/network/i-repo.ts +3 -0
- package/src/network/index.ts +5 -0
- package/src/network/promise-shortfall-error.ts +69 -0
- package/src/network/refuse-solo.ts +7 -0
- package/src/network/stale-failure.ts +27 -4
- package/src/network/struct.ts +169 -15
- package/src/network/transaction-size.ts +73 -0
- package/src/network/validity-rejected-error.ts +55 -0
- package/src/offline/entry.ts +127 -0
- package/src/offline/index.ts +4 -0
- package/src/offline/journal.ts +60 -0
- package/src/offline/queue.ts +1342 -0
- package/src/offline/reachability.ts +32 -0
- package/src/offline/status.ts +183 -0
- package/src/offline/wake.ts +67 -0
- package/src/reactivity/config.ts +8 -4
- package/src/reactivity/rotation.ts +9 -3
- package/src/testing/test-transactor.ts +65 -0
- package/src/transaction/commit-failure-class.ts +133 -0
- package/src/transaction/coordinator.ts +969 -393
- package/src/transaction/entry-commit.ts +285 -0
- package/src/transaction/index.ts +4 -0
- package/src/transaction/transaction.ts +21 -0
- package/src/transaction/validator.ts +22 -12
- package/src/transactor/change-notifier.ts +13 -4
- package/src/transactor/network-transactor.ts +252 -55
- package/src/transactor/transactor-source.ts +29 -13
- package/src/transactor/transactor.ts +16 -0
- package/src/transform/digest.ts +36 -3
- package/src/utility/visit-every.ts +25 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { PromiseShortfall, ShortfallFailure, SoloRefusalReason } from "./struct.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A pend the cohort could not accept because too few of its members answered the promise round: the
|
|
5
|
+
* approvals fell short of the bar with no member refusing the write as a lost race or a held block,
|
|
6
|
+
* and too few rejecting it for a rejection. Nobody judged the write; the cohort was not all there.
|
|
7
|
+
* Not retryable by a write path — every coordinator of the cohort meets the same silent members, so a
|
|
8
|
+
* re-read and re-pend fails the same way until they answer again. Nothing was saved: the members that
|
|
9
|
+
* approved release their reservation when the writer cancels.
|
|
10
|
+
*
|
|
11
|
+
* ONE class for every place it is raised, as `TransactionExpiredError` is. The cluster coordinator
|
|
12
|
+
* (db-p2p `ClusterCoordinator`) raises it; the coordinating repo returns its facts as a
|
|
13
|
+
* {@link ShortfallFailure} ({@link toFailure}), which is what lets a remote writer receive them at
|
|
14
|
+
* all; and each write path (`Collection.sync`, `TransactionCoordinator.commit`) raises it again from
|
|
15
|
+
* that refusal.
|
|
16
|
+
*
|
|
17
|
+
* The message is the sentence the coordinator has always thrown, byte for byte: a downstream
|
|
18
|
+
* repository matches it to retry a silent cohort (backlog
|
|
19
|
+
* `debt-a-downstream-repo-classifies-retries-by-parsing-our-error-text`). Test for this class, or for
|
|
20
|
+
* the refusal with `isShortfallFailure`, rather than for the text.
|
|
21
|
+
*/
|
|
22
|
+
export class PromiseShortfallError extends Error {
|
|
23
|
+
/** Approve votes the round received. */
|
|
24
|
+
readonly approvals: number;
|
|
25
|
+
/** Reject votes the round received — fewer than would make the round a rejection. */
|
|
26
|
+
readonly rejections: number;
|
|
27
|
+
/** How many members the cohort the round ran on has. */
|
|
28
|
+
readonly peerCount: number;
|
|
29
|
+
/** The approvals the round needed. */
|
|
30
|
+
readonly needed: number;
|
|
31
|
+
/** Peer-id strings of the cohort members that cast no promise vote at all. */
|
|
32
|
+
readonly silentPeers: readonly string[];
|
|
33
|
+
/** Why a lone coordinator refused to write alone, when that is what this shortfall is
|
|
34
|
+
* ({@link PromiseShortfall.soloRefused}); absent when a consensus round fell short. */
|
|
35
|
+
readonly soloRefused?: SoloRefusalReason;
|
|
36
|
+
|
|
37
|
+
constructor(shortfall: PromiseShortfall) {
|
|
38
|
+
// NOTE: `shortfall` off the wire is trusted as shaped (`isShortfallFailure` checks only presence),
|
|
39
|
+
// so a malformed coordinator answer throws a TypeError here (no `silentPeers` array) instead of
|
|
40
|
+
// this error. Either still ends the write; if hostile coordinators become a concern, validate the
|
|
41
|
+
// shape where the refusal is received, as for `TransactionExpiredError`.
|
|
42
|
+
super(`Failed to get super-majority: ${shortfall.approvals}/${shortfall.peerCount} approvals (needed ${shortfall.needed}, ${shortfall.rejections} rejections)`);
|
|
43
|
+
this.name = 'PromiseShortfallError';
|
|
44
|
+
this.approvals = shortfall.approvals;
|
|
45
|
+
this.rejections = shortfall.rejections;
|
|
46
|
+
this.peerCount = shortfall.peerCount;
|
|
47
|
+
this.needed = shortfall.needed;
|
|
48
|
+
this.silentPeers = [...shortfall.silentPeers];
|
|
49
|
+
if (shortfall.soloRefused !== undefined) this.soloRefused = shortfall.soloRefused;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The refusal a coordinating repo returns for this error, so it crosses the repo protocol as an
|
|
53
|
+
* answer rather than as a reset stream. */
|
|
54
|
+
toFailure(): ShortfallFailure {
|
|
55
|
+
return {
|
|
56
|
+
success: false,
|
|
57
|
+
conflict: false,
|
|
58
|
+
reason: this.message,
|
|
59
|
+
shortfall: {
|
|
60
|
+
approvals: this.approvals,
|
|
61
|
+
rejections: this.rejections,
|
|
62
|
+
peerCount: this.peerCount,
|
|
63
|
+
needed: this.needed,
|
|
64
|
+
silentPeers: [...this.silentPeers],
|
|
65
|
+
...(this.soloRefused === undefined ? {} : { soloRefused: this.soloRefused }),
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Wraps `PendRequest.refuseSolo` and `CommitRequest.refuseSolo` so the field spreads onto a request
|
|
2
|
+
* only when the write refuses to be committed alone. A write that does not is omitted rather than
|
|
3
|
+
* sent as `false`: the request is hashed verbatim into every cohort signature preimage, so it must
|
|
4
|
+
* serialize exactly as it did before the field existed. Every producer of the field goes through here. */
|
|
5
|
+
export function refuseSoloField(refuse: boolean | undefined): { refuseSolo?: true } {
|
|
6
|
+
return refuse === true ? { refuseSolo: true } : {};
|
|
7
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ActionId, ActionRev } from "../collection/action.js";
|
|
2
|
-
import type { ExpiredFailure, StaleFailure, WriteFailure } from "./struct.js";
|
|
2
|
+
import type { ExpiredFailure, RejectedFailure, ShortfallFailure, StaleFailure, WriteFailure } from "./struct.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* The single rule for "is this non-success retryable after a re-read?" Both write paths and the
|
|
@@ -7,11 +7,12 @@ import type { ExpiredFailure, StaleFailure, WriteFailure } from "./struct.js";
|
|
|
7
7
|
*
|
|
8
8
|
* {@link StaleFailure.conflict} is authoritative when present. The `missing`/`pending` fallback
|
|
9
9
|
* covers producers that have not been taught the field, including a remote peer on an older build
|
|
10
|
-
* (the repo protocol is plain JSON, so an unset field simply arrives absent). An expiry
|
|
11
|
-
*
|
|
10
|
+
* (the repo protocol is plain JSON, so an unset field simply arrives absent). An expiry
|
|
11
|
+
* ({@link isExpiryFailure}), a promise shortfall ({@link isShortfallFailure}) and a validity
|
|
12
|
+
* rejection ({@link isRejectedFailure}) are never a conflict, whatever else arrived beside them.
|
|
12
13
|
*/
|
|
13
14
|
export function isConflictFailure(failure: WriteFailure): boolean {
|
|
14
|
-
if (isExpiryFailure(failure)) return false;
|
|
15
|
+
if (isExpiryFailure(failure) || isShortfallFailure(failure) || isRejectedFailure(failure)) return false;
|
|
15
16
|
return failure.conflict ?? Boolean(failure.missing?.length || failure.pending?.length);
|
|
16
17
|
}
|
|
17
18
|
|
|
@@ -26,6 +27,28 @@ export function isExpiryFailure(failure: WriteFailure): failure is ExpiredFailur
|
|
|
26
27
|
return failure.expired !== undefined;
|
|
27
28
|
}
|
|
28
29
|
|
|
30
|
+
/**
|
|
31
|
+
* The single rule for "did the cohort refuse this pend because too few of its members answered the
|
|
32
|
+
* promise round?" ({@link ShortfallFailure}). Every write path stops on it rather than retrying, since
|
|
33
|
+
* every coordinator of the cohort meets the same silent members. Decided by the structured field,
|
|
34
|
+
* never the reason text; a coordinator on a build that predates the field throws the shortfall
|
|
35
|
+
* instead of returning it, which a writer sees as it always did.
|
|
36
|
+
*/
|
|
37
|
+
export function isShortfallFailure(failure: WriteFailure): failure is ShortfallFailure {
|
|
38
|
+
return failure.shortfall !== undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The single rule for "did the cohort refuse this pend because the members that sank it judged the
|
|
43
|
+
* transaction invalid?" ({@link RejectedFailure}). Every write path stops on it rather than retrying,
|
|
44
|
+
* since re-sending the same content to the same checks is refused the same way. Decided by the
|
|
45
|
+
* structured field, never the reason text; a coordinator on a build that predates the field throws the
|
|
46
|
+
* rejection instead of returning it, which a writer sees as it always did.
|
|
47
|
+
*/
|
|
48
|
+
export function isRejectedFailure(failure: WriteFailure): failure is RejectedFailure {
|
|
49
|
+
return failure.rejected !== undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
29
52
|
/**
|
|
30
53
|
* The single rule for picking one {@link StaleFailure.staleAt} out of several candidates.
|
|
31
54
|
*
|
package/src/network/struct.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CollectionId, BlockId, IBlock, ActionId, Transform, Transforms } from "../index.js";
|
|
2
2
|
import type { ActionContext, ActionRev } from "../collection/action.js";
|
|
3
|
-
import type { Transaction } from "../transaction/transaction.js";
|
|
3
|
+
import type { Transaction, ValidityRejection } from "../transaction/transaction.js";
|
|
4
4
|
import type { DisputeResolutionProof } from "../log/struct.js";
|
|
5
5
|
import type { PeerId } from "./types.js";
|
|
6
6
|
|
|
@@ -83,18 +83,20 @@ export type PendRequest = ActionTransforms & {
|
|
|
83
83
|
*/
|
|
84
84
|
baseRevs?: BlockBaseRevs;
|
|
85
85
|
/**
|
|
86
|
-
* The writer's word that this pend
|
|
87
|
-
* is
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
86
|
+
* The writer's word that this pend, together with the other pends sent with it in one request
|
|
87
|
+
* (`IRepo.pendMany`), is the action's whole reservation — every block the action will commit is in
|
|
88
|
+
* that one request — and that the action's commit goes as one request to the coordinator this pend
|
|
89
|
+
* goes to. Only then may a coordinator hold back the pend record's final delivery to the other
|
|
90
|
+
* members and send it inside the commit's first request (`ClusterCoordinator.mayDeferPendDelivery`
|
|
91
|
+
* in db-p2p): a member that refuses any of the record's pends late refuses that one commit record,
|
|
92
|
+
* before anything of the action is durable. A send that pends in several requests before committing
|
|
93
|
+
* any — several collections one request each under one action id, or one collection split across
|
|
94
|
+
* coordinators — must never carry it: one request's late refusal would surface while another
|
|
95
|
+
* request's commit may already have landed, tearing the action.
|
|
95
96
|
*
|
|
96
|
-
* Set by `TransactorSource.transact
|
|
97
|
-
* one collection
|
|
97
|
+
* Set by `TransactorSource.transact`, and by `TransactionCoordinator` for a transaction of exactly
|
|
98
|
+
* one collection or for every request of a `pendMany` (whose commit goes through `commitMany`);
|
|
99
|
+
* `NetworkTransactor.pend` keeps it only on a request carrying the whole pend.
|
|
98
100
|
* `Collection.completeOwnEntry` re-sends one participant of a half-landed multi-collection action
|
|
99
101
|
* through `transact`, and carries it too: that re-send pends and commits its one collection on its
|
|
100
102
|
* own, so its late refusal stops only its own commit, as an on-time refusal would.
|
|
@@ -102,6 +104,25 @@ export type PendRequest = ActionTransforms & {
|
|
|
102
104
|
* an older build ignores it, and then delivers at once as it always did.
|
|
103
105
|
*/
|
|
104
106
|
soleReservation?: true;
|
|
107
|
+
/**
|
|
108
|
+
* The writer's word that this write must not be committed by a lone node that knows it is not
|
|
109
|
+
* alone (`SyncOptions.refuseSoloCommit`). A coordinator whose cohort for the write is only itself
|
|
110
|
+
* refuses the pend — before reserving anything — when it knows of cohort members it could not
|
|
111
|
+
* place in the cohort, is still waiting to hear from a configured bootstrap peer, or could not
|
|
112
|
+
* route the write to itself, answering with a {@link ShortfallFailure} whose
|
|
113
|
+
* {@link PromiseShortfall.soloRefused} names which (`CoordinatorRepo.pendSolo` in db-p2p). A
|
|
114
|
+
* coordinator that is genuinely alone ignores it and pends as it always did, and so does every
|
|
115
|
+
* coordinator whose cohort has more than one member.
|
|
116
|
+
*
|
|
117
|
+
* Carried by every send of the write's attempt cycle, the re-send a refresh makes to finish a torn
|
|
118
|
+
* attempt included (`Collection.completeOwnEntry`). Every per-coordinator rebuild of the request
|
|
119
|
+
* keeps it (`pendRequestForBatch` in `NetworkTransactor`), since a batch that dropped it would
|
|
120
|
+
* lose the protection for that coordinator's share. Omitted rather than `false`, for the same
|
|
121
|
+
* preimage reason as {@link baseRevs}: a write that does not set it serializes exactly as before.
|
|
122
|
+
* A coordinator on an older build ignores it and commits alone at `local`; in a group of mixed
|
|
123
|
+
* versions the offline queue's reachability gate is then the only protection.
|
|
124
|
+
*/
|
|
125
|
+
refuseSolo?: true;
|
|
105
126
|
};
|
|
106
127
|
|
|
107
128
|
export type BlockActionStatus = ActionBlocks & {
|
|
@@ -226,6 +247,12 @@ export type StaleFailure = {
|
|
|
226
247
|
staleAt?: { blockId: BlockId; rev: number };
|
|
227
248
|
/** Never set on a lost race — an expiry is its own failure shape, {@link ExpiredFailure}. */
|
|
228
249
|
expired?: never;
|
|
250
|
+
/** Never set on a lost race — a cohort too few of whose members answered is its own failure
|
|
251
|
+
* shape, {@link ShortfallFailure}. */
|
|
252
|
+
shortfall?: never;
|
|
253
|
+
/** Never set on a lost race — a write judged invalid is its own failure shape,
|
|
254
|
+
* {@link RejectedFailure}. */
|
|
255
|
+
rejected?: never;
|
|
229
256
|
};
|
|
230
257
|
|
|
231
258
|
/**
|
|
@@ -262,10 +289,124 @@ export type ExpiredFailure = {
|
|
|
262
289
|
missing?: never;
|
|
263
290
|
pending?: never;
|
|
264
291
|
staleAt?: never;
|
|
292
|
+
shortfall?: never;
|
|
293
|
+
rejected?: never;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Why a cohort's promise round fell short with no member refusing it as a lost race or a held block,
|
|
298
|
+
* and too few rejecting it for a rejection: too few members answered. Plain data, so it crosses the
|
|
299
|
+
* JSON repo protocol intact. `PromiseShortfallError` is built from it, wherever it is raised.
|
|
300
|
+
*/
|
|
301
|
+
export type PromiseShortfall = {
|
|
302
|
+
/** Approve votes the round received. */
|
|
303
|
+
approvals: number;
|
|
304
|
+
/** Reject votes the round received — fewer than would make the round a rejection. */
|
|
305
|
+
rejections: number;
|
|
306
|
+
/** How many members the cohort the round ran on has. */
|
|
307
|
+
peerCount: number;
|
|
308
|
+
/** The approvals the round needed: `ceil(peerCount × superMajorityThreshold)`. */
|
|
309
|
+
needed: number;
|
|
310
|
+
/** Peer-id strings of the cohort members that cast no promise vote at all. */
|
|
311
|
+
silentPeers: string[];
|
|
312
|
+
/**
|
|
313
|
+
* Set only when no consensus round ran: a coordinator whose cohort for the write was only itself
|
|
314
|
+
* refused to write alone, because the write asked it to (`PendRequest.refuseSolo`). The other
|
|
315
|
+
* fields then describe the cohort the coordinator expected rather than one it asked: one approval
|
|
316
|
+
* (its own), no rejections, the members it knows of and could not reach as `silentPeers`, and the
|
|
317
|
+
* bar a cohort of that size would set. Absent on a shortfall a consensus round produced, so a log
|
|
318
|
+
* or an application can tell "refused to write alone" from "a round heard too few".
|
|
319
|
+
*/
|
|
320
|
+
soloRefused?: SoloRefusalReason;
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Why a lone coordinator refused a write that asked not to be committed alone
|
|
325
|
+
* (`PendRequest.refuseSolo`, `CommitRequest.refuseSolo`):
|
|
326
|
+
* - `absent-members`: its cohort for the write was only itself, and it knows of cohort members it
|
|
327
|
+
* could not place in that cohort (`IKeyNetwork.absentCohortMembers`);
|
|
328
|
+
* - `awaiting-bootstrap`: its cohort was only itself and it is still waiting to hear from a peer it
|
|
329
|
+
* was configured to join through (`IKeyNetwork.awaitingBootstrapContact`);
|
|
330
|
+
* - `unrouted`: the cohort lookup failed, named nobody, or named one peer that is not this node.
|
|
331
|
+
*/
|
|
332
|
+
export type SoloRefusalReason = 'absent-members' | 'awaiting-bootstrap' | 'unrouted';
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* The cohort refused the pend because too few of its members ANSWERED the promise round for it to
|
|
336
|
+
* reach its bar — silence, not a judgement of the write and not a lost race. Not retryable by a write
|
|
337
|
+
* path: every coordinator of that cohort meets the same silent members, so a re-read and re-pend fails
|
|
338
|
+
* the same way until they come back. A sibling of {@link StaleFailure} and {@link ExpiredFailure},
|
|
339
|
+
* with the fields it can never carry typed `never`, so a consumer reads `reason`, `missing` or
|
|
340
|
+
* `staleAt` off any of them unnarrowed. Tell it apart with `isShortfallFailure`, never by reading
|
|
341
|
+
* `reason`. A pend answer only: a commit that falls short is still thrown (see
|
|
342
|
+
* `CoordinatorRepo.commit`).
|
|
343
|
+
*/
|
|
344
|
+
export type ShortfallFailure = {
|
|
345
|
+
success: false;
|
|
346
|
+
/** The coordinator's description of the refusal, for a log line. Never branch on it. */
|
|
347
|
+
reason?: string;
|
|
348
|
+
/** Always false: silence is not a conflict a re-read can win. */
|
|
349
|
+
conflict: false;
|
|
350
|
+
shortfall: PromiseShortfall;
|
|
351
|
+
missing?: never;
|
|
352
|
+
pending?: never;
|
|
353
|
+
staleAt?: never;
|
|
354
|
+
expired?: never;
|
|
355
|
+
rejected?: never;
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* One refusal behind a {@link TransactionInvalidity}: a cohort member's validity-marked reject vote
|
|
360
|
+
* (`Signature`'s `validity`), or the validity verdict of the coordinating node's own storage-tier check
|
|
361
|
+
* (`StorageRepo.pend`, for a cohort of one, where no vote is cast).
|
|
362
|
+
*/
|
|
363
|
+
export type ValidityRefusal = ValidityRejection & {
|
|
364
|
+
/** The refusing member's peer-id string. Absent for a refusal a node's storage made without a vote. */
|
|
365
|
+
peerId?: string;
|
|
366
|
+
/** The refuser's description, free-form, for a log line. Never branch on it. */
|
|
367
|
+
reason?: string;
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Why a write was refused as INVALID: the members that sank it judged the transaction itself wrong
|
|
372
|
+
* (see {@link ValidityRejection} for which refusals qualify), and no refusal of any other kind sank it
|
|
373
|
+
* beside them, bar expiry votes. Plain data, so it crosses the JSON repo protocol intact.
|
|
374
|
+
* `ValidityRejectedError` is built from it, wherever it is raised.
|
|
375
|
+
*/
|
|
376
|
+
export type TransactionInvalidity = {
|
|
377
|
+
/** Every validity refusal that sank the write. Never empty. */
|
|
378
|
+
refusals: ValidityRefusal[];
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* The cohort refused the write because the members that sank it judged it INVALID — the transaction
|
|
383
|
+
* failed its re-execution, its operations hash, its stamp or its client signature. Never retryable:
|
|
384
|
+
* re-sending the same content to the same checks is refused the same way. A sibling of
|
|
385
|
+
* {@link StaleFailure}, {@link ExpiredFailure} and {@link ShortfallFailure}, with the fields it can
|
|
386
|
+
* never carry typed `never`, so a consumer reads `reason`, `missing` or `staleAt` off any of them
|
|
387
|
+
* unnarrowed. Tell it apart with `isRejectedFailure`, never by reading `reason`.
|
|
388
|
+
*
|
|
389
|
+
* A pend answer only, and only when nothing else could explain the rejection: a reject vote with no
|
|
390
|
+
* validity marker beside the marked ones (a stale revision, a member on an older build) leaves the
|
|
391
|
+
* rejection unclassified, and a revision loss the coordinator's own storage confirms is returned as
|
|
392
|
+
* the {@link StaleFailure} it is, since the writer's rebase sends different content.
|
|
393
|
+
*/
|
|
394
|
+
export type RejectedFailure = {
|
|
395
|
+
success: false;
|
|
396
|
+
/** The coordinator's description of the refusal, for a log line. Never branch on it. */
|
|
397
|
+
reason?: string;
|
|
398
|
+
/** Always false: a verdict on the content is not a conflict a re-read can win. */
|
|
399
|
+
conflict: false;
|
|
400
|
+
rejected: TransactionInvalidity;
|
|
401
|
+
missing?: never;
|
|
402
|
+
pending?: never;
|
|
403
|
+
staleAt?: never;
|
|
404
|
+
expired?: never;
|
|
405
|
+
shortfall?: never;
|
|
265
406
|
};
|
|
266
407
|
|
|
267
408
|
/** Every way a pend or a commit can be refused with an answer rather than a throw. */
|
|
268
|
-
export type WriteFailure = StaleFailure | ExpiredFailure;
|
|
409
|
+
export type WriteFailure = StaleFailure | ExpiredFailure | ShortfallFailure | RejectedFailure;
|
|
269
410
|
|
|
270
411
|
export type PendResult = PendSuccess | WriteFailure;
|
|
271
412
|
|
|
@@ -310,8 +451,8 @@ export type CommitRequest = ActionBlocks & {
|
|
|
310
451
|
// cost: `StorageRepo.persistProofIfContentMatches` retains no `BlockCommitProof` for a block that
|
|
311
452
|
// declared no digest (`commit:proof-undeclared`), and a block with no proof is refused by any
|
|
312
453
|
// receiver running the default `requirePushCertificate: true` (`push:reject-uncertified
|
|
313
|
-
// reason=no-proof`). Such a block stays readable and
|
|
314
|
-
//
|
|
454
|
+
// reason=no-proof`). Such a block stays readable and still repairs by corroboration while two or
|
|
455
|
+
// more holders remain, but it can never GAIN a
|
|
315
456
|
// holder by push — so spread-on-churn and cohort-growth healing silently stop maintaining its
|
|
316
457
|
// replication factor. Kept optional anyway because: (a) required-but-nullable is the strongest form
|
|
317
458
|
// available and still permits `undefined`, so it does not make the bad state unrepresentable, only
|
|
@@ -322,6 +463,17 @@ export type CommitRequest = ActionBlocks & {
|
|
|
322
463
|
// by literally writing `undefined`. Revisit if undeclared commits ever become common enough to show
|
|
323
464
|
// up as replication-factor decay.
|
|
324
465
|
blockDigests?: BlockContentDigests;
|
|
466
|
+
/**
|
|
467
|
+
* {@link PendRequest.refuseSolo}, on the commit. A coordinator whose cohort for the commit is only
|
|
468
|
+
* itself refuses it on the same rule, before minting a proof or applying anything, with a
|
|
469
|
+
* conflict-shaped refusal (`SOLO_COMMIT_REFUSED_REASON` in db-p2p), since a commit cannot answer
|
|
470
|
+
* with a shortfall. That is reached when the pend ran on a real cohort and the partner dropped
|
|
471
|
+
* before the commit: the writer cancels and retries, and the retry's pend meets the pend-side
|
|
472
|
+
* refusal. Carried through every per-coordinator rebuild of the commit (`repoCommitRequest` in
|
|
473
|
+
* `NetworkTransactor`); omitted rather than `false` for the preimage reason
|
|
474
|
+
* {@link blockDigests} is.
|
|
475
|
+
*/
|
|
476
|
+
refuseSolo?: true;
|
|
325
477
|
};
|
|
326
478
|
|
|
327
479
|
/**
|
|
@@ -571,6 +723,8 @@ export type PendValidationResult = {
|
|
|
571
723
|
valid: boolean;
|
|
572
724
|
/** Reason for validation failure (if valid=false) */
|
|
573
725
|
reason?: string;
|
|
726
|
+
/** Set only on a failure that judges the transaction itself wrong; see {@link ValidityRejection}. */
|
|
727
|
+
validity?: ValidityRejection;
|
|
574
728
|
};
|
|
575
729
|
|
|
576
730
|
/**
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { ActionId } from "../collection/action.js";
|
|
2
|
+
import type { PendRequest } from "./struct.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The most bytes one transaction's pend may encode to: 7.5 MiB, measured by {@link jsonEncodedBytes}
|
|
6
|
+
* over the {@link PendRequest}s that travel together in one consensus record — transforms, validation
|
|
7
|
+
* payload and all. That is the sum of the batch when a transaction's collections travel together
|
|
8
|
+
* (`IRepo.pendMany`), and one collection's pend when they cannot and each is pended on its own.
|
|
9
|
+
*
|
|
10
|
+
* A pend's payload travels whole in one frame on every hop it takes: writer → coordinator on the repo
|
|
11
|
+
* protocol, then coordinator → each other cohort member on the cluster protocol, and back as the
|
|
12
|
+
* member's echoed answer. Both protocols decode under one frame cap (db-p2p's
|
|
13
|
+
* `MAX_BLOCK_MESSAGE_BYTES`), which is this limit plus a reserve for the envelope around the pend, so a
|
|
14
|
+
* transaction under this limit fits every frame it rides in. The limit is the same at every group
|
|
15
|
+
* size: a solo cohort takes no cluster hop, but is held to it all the same, so a write that commits
|
|
16
|
+
* on one machine does not start failing when a second joins.
|
|
17
|
+
*
|
|
18
|
+
* A writer with more to save than this splits it into several transactions.
|
|
19
|
+
*/
|
|
20
|
+
export const MAX_TRANSACTION_BYTES = 7.5 * 1024 * 1024;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The bytes `value` encodes to on the wire: the UTF-8 length of its `JSON.stringify`. JSON is the
|
|
24
|
+
* encoding every request and response frame of the repo and cluster protocols carries, so this is the
|
|
25
|
+
* measure a frame cap is judged by.
|
|
26
|
+
*/
|
|
27
|
+
export function jsonEncodedBytes(value: unknown): number {
|
|
28
|
+
return new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* A transaction too large to send: its pend encodes to more than {@link MAX_TRANSACTION_BYTES}.
|
|
33
|
+
* Raised before anything is sent, so nothing was reserved and nothing needs cancelling. Not a
|
|
34
|
+
* `StaleFailure` and never retried — the same transaction is the same size on every attempt. The
|
|
35
|
+
* writer has to split the change into smaller transactions.
|
|
36
|
+
*/
|
|
37
|
+
export class TransactionTooLargeError extends Error {
|
|
38
|
+
constructor(
|
|
39
|
+
readonly actionId: ActionId,
|
|
40
|
+
/** What the transaction's pend encodes to, by {@link jsonEncodedBytes}. */
|
|
41
|
+
readonly bytes: number,
|
|
42
|
+
/** The limit it exceeded: {@link MAX_TRANSACTION_BYTES}. */
|
|
43
|
+
readonly limit: number = MAX_TRANSACTION_BYTES,
|
|
44
|
+
) {
|
|
45
|
+
super(`transaction ${actionId} encodes to ${bytes} bytes, over the per-transaction limit MAX_TRANSACTION_BYTES (${limit} bytes); split it into smaller transactions`);
|
|
46
|
+
this.name = 'TransactionTooLargeError';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* What `requests` — one transaction's pends that ride in one consensus record — encode to together,
|
|
52
|
+
* the measure {@link MAX_TRANSACTION_BYTES} is judged by.
|
|
53
|
+
*
|
|
54
|
+
* NOTE: encodes every request once more than sending it does, on the writer and again on the
|
|
55
|
+
* coordinator. Negligible for an ordinary write; if pend encoding ever shows in a profile of large
|
|
56
|
+
* writes, measure once on the writer and hand the size along instead.
|
|
57
|
+
*/
|
|
58
|
+
export function transactionBytes(requests: readonly PendRequest[]): number {
|
|
59
|
+
let bytes = 0;
|
|
60
|
+
for (const request of requests) bytes += jsonEncodedBytes(request);
|
|
61
|
+
return bytes;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Throws {@link TransactionTooLargeError} when `requests` together encode to more than
|
|
66
|
+
* {@link MAX_TRANSACTION_BYTES} ({@link transactionBytes}). The one rule both the writer (before
|
|
67
|
+
* sending) and the coordinator (on receipt) apply.
|
|
68
|
+
*/
|
|
69
|
+
export function assertTransactionFits(requests: readonly PendRequest[]): void {
|
|
70
|
+
if (requests.length === 0) return;
|
|
71
|
+
const bytes = transactionBytes(requests);
|
|
72
|
+
if (bytes > MAX_TRANSACTION_BYTES) throw new TransactionTooLargeError(requests[0]!.actionId, bytes);
|
|
73
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { RejectedFailure, TransactionInvalidity, ValidityRefusal } from "./struct.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A write the cohort refused because the members that sank it judged the transaction INVALID: it failed
|
|
5
|
+
* its re-execution, its operations hash, its stamp or its client signature (`ValidityRejection` in
|
|
6
|
+
* `transaction/transaction.ts` says which refusals qualify). Not retryable — re-sending the same
|
|
7
|
+
* content to the same checks is refused the same way. Nothing was saved: the members that approved
|
|
8
|
+
* release their reservation when the writer cancels.
|
|
9
|
+
*
|
|
10
|
+
* ONE class for every place it is raised, as `TransactionExpiredError` is. The coordinating repo (db-p2p
|
|
11
|
+
* `CoordinatorRepo`) returns its facts as a {@link RejectedFailure} ({@link toFailure}) when every reject
|
|
12
|
+
* that sank a pend was validity-marked or an expiry vote and its own storage confirms no revision loss,
|
|
13
|
+
* and a cohort of one's storage tier returns the same for its own check; each write path
|
|
14
|
+
* (`Collection.sync`, `TransactionCoordinator.commit`) raises it again from that refusal.
|
|
15
|
+
*
|
|
16
|
+
* Not the dispute answer. With disputes enabled, a marked rejection beside approvals is held for a ruling
|
|
17
|
+
* before it ends the pend, and that writer is answered with the dispute's own refusal (backlog
|
|
18
|
+
* `dispute-commit-path-block`, `dispute-writer-sees-disputed-pend`); this error stays the answer for a
|
|
19
|
+
* rejection no approval contests.
|
|
20
|
+
*/
|
|
21
|
+
export class ValidityRejectedError extends Error {
|
|
22
|
+
/** Every validity refusal that sank the write. Never empty. */
|
|
23
|
+
readonly refusals: readonly ValidityRefusal[];
|
|
24
|
+
|
|
25
|
+
constructor(invalidity: TransactionInvalidity) {
|
|
26
|
+
// NOTE: `invalidity` off the wire is trusted as shaped (`isRejectedFailure` checks only presence),
|
|
27
|
+
// so a malformed coordinator answer throws a TypeError here (no `refusals` array) instead of this
|
|
28
|
+
// error. Either still ends the write; if hostile coordinators become a concern, validate the shape
|
|
29
|
+
// where the refusal is received, as for `TransactionExpiredError`.
|
|
30
|
+
super(validityRejectedMessage(invalidity.refusals));
|
|
31
|
+
this.name = 'ValidityRejectedError';
|
|
32
|
+
this.refusals = invalidity.refusals.map(refusal => ({ ...refusal }));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The refusal a coordinating repo returns for this error, so it crosses the repo protocol as an
|
|
36
|
+
* answer rather than as a reset stream. */
|
|
37
|
+
toFailure(): RejectedFailure {
|
|
38
|
+
return {
|
|
39
|
+
success: false,
|
|
40
|
+
conflict: false,
|
|
41
|
+
reason: this.message,
|
|
42
|
+
rejected: { refusals: this.refusals.map(refusal => ({ ...refusal })) },
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Written to be shown to a person. Names each refuser — the member's peer id, or the coordinating
|
|
48
|
+
* node's own check where no vote was cast — with the reason it gave. */
|
|
49
|
+
function validityRejectedMessage(refusals: readonly ValidityRefusal[]): string {
|
|
50
|
+
const details = refusals
|
|
51
|
+
.map(refusal => `${refusal.peerId ?? 'coordinating node'}: ${refusal.reason ?? 'no reason given'}`)
|
|
52
|
+
.join('; ');
|
|
53
|
+
return `The transaction was rejected as invalid by ${refusals.length} validator(s) (${details}). `
|
|
54
|
+
+ `This is a verdict on the transaction itself, not a conflict or an unreachable cohort, and sending it again is refused the same way.`;
|
|
55
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { ActionId, AlreadyAppliedTally } from '../collection/action.js';
|
|
2
|
+
import { addTallies, ZeroTally } from '../collection/action.js';
|
|
3
|
+
import type { Collection } from '../collection/collection.js';
|
|
4
|
+
import type { EntryId } from '../collection/segments.js';
|
|
5
|
+
import type { CollectionId, InFlightAttempt } from '../collection/struct.js';
|
|
6
|
+
import type { WriteDurability } from '../network/struct.js';
|
|
7
|
+
import type { CommittedEffect, EntryStatus, EntryWaitingFor, QueuedEntry } from './status.js';
|
|
8
|
+
import type { JournalDriveState } from './journal.js';
|
|
9
|
+
|
|
10
|
+
/** One queued entry, as `OfflineQueue` holds it, and the projections of it the queue reports. */
|
|
11
|
+
export type Entry = {
|
|
12
|
+
readonly id: EntryId;
|
|
13
|
+
readonly seq: number;
|
|
14
|
+
readonly queuedAt: number;
|
|
15
|
+
readonly expiresAt: number | undefined;
|
|
16
|
+
/** The collections the entry sealed a segment in — the ones `sealPending` answered `true` for. */
|
|
17
|
+
readonly collections: readonly Collection<any>[];
|
|
18
|
+
/** One action id for the entry's whole life, with one exception: a `torn-not-saved` outcome mints
|
|
19
|
+
* a fresh one (the queue's `applyFailure` says why). */
|
|
20
|
+
actionId: ActionId;
|
|
21
|
+
attempts: number;
|
|
22
|
+
/** Whether an attempt may have landed; sticky — see the NOTE at the queue's `recordAttempts`. */
|
|
23
|
+
inDoubt: boolean;
|
|
24
|
+
/** The attempts recorded under `actionId` and not yet resolved, per collection, handed back as
|
|
25
|
+
* `resume` when the entry is re-driven. Cleared with the action id. */
|
|
26
|
+
readonly retained: Map<CollectionId, InFlightAttempt>;
|
|
27
|
+
/** The collections a partial commit has already committed, with who holds each, accumulated
|
|
28
|
+
* across submissions (`recordCollectionDurability`); empty until one does. The committed half's
|
|
29
|
+
* segments are consumed, so the next submission drives only the rest, and the terminal status
|
|
30
|
+
* reports these beside it. */
|
|
31
|
+
readonly committed: Map<CollectionId, WriteDurability | undefined>;
|
|
32
|
+
/** What each collection's head segment accepted as already applied when it was committed, as the
|
|
33
|
+
* `onCommittedHead` hooks reported it — across submissions, so a partial commit's committed half is
|
|
34
|
+
* recorded before the error reaches the queue and a later submission's tallies accumulate beside
|
|
35
|
+
* it. A collection a restore found already committed records nothing. Folded into the `committed`
|
|
36
|
+
* status's `effect` ({@link effectOf}, which answers `applied` while any collection the entry wrote
|
|
37
|
+
* has no tally here). In memory only: a restart between a partial commit and the remainder's commit
|
|
38
|
+
* loses the committed half's tally, and the entry then reports `applied`, the conservative answer,
|
|
39
|
+
* whatever the remainder reports. */
|
|
40
|
+
readonly applied: Map<CollectionId, AlreadyAppliedTally>;
|
|
41
|
+
phase: 'queued' | 'submitting';
|
|
42
|
+
waitingFor: EntryWaitingFor | undefined;
|
|
43
|
+
lastError: unknown;
|
|
44
|
+
/** The last `queued` status emitted, so a re-check that changes nothing emits nothing. */
|
|
45
|
+
lastQueued: QueuedStatus | undefined;
|
|
46
|
+
/** The `offline:expiry-deferred` line is logged once per entry, not once per re-check. */
|
|
47
|
+
expiryDeferredLogged: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type QueuedStatus = Extract<EntryStatus, { kind: 'queued' }>;
|
|
51
|
+
|
|
52
|
+
export function queuedStatus(entry: Entry): QueuedStatus {
|
|
53
|
+
return {
|
|
54
|
+
kind: 'queued',
|
|
55
|
+
entryId: entry.id,
|
|
56
|
+
seq: entry.seq,
|
|
57
|
+
collections: entry.collections.map(collection => collection.id),
|
|
58
|
+
queuedAt: entry.queuedAt,
|
|
59
|
+
attempts: entry.attempts,
|
|
60
|
+
inDoubt: entry.inDoubt,
|
|
61
|
+
waitingFor: entry.waitingFor,
|
|
62
|
+
...(entry.lastError === undefined ? {} : { lastError: entry.lastError }),
|
|
63
|
+
...committedField(entry),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** `{ committedCollections }` once part of the entry has committed, `{}` before — the field the
|
|
68
|
+
* `queued` and `failed` statuses and {@link snapshot} carry only when it says something. */
|
|
69
|
+
export function committedField(entry: Entry): { committedCollections?: readonly CollectionId[] } {
|
|
70
|
+
return entry.committed.size === 0 ? {} : { committedCollections: [...entry.committed.keys()] };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function currentStatus(entry: Entry): EntryStatus {
|
|
74
|
+
return entry.phase === 'submitting'
|
|
75
|
+
? { kind: 'submitting', entryId: entry.id, attempt: entry.attempts }
|
|
76
|
+
: queuedStatus(entry);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The entry as `OfflineQueue.entries` lists it. */
|
|
80
|
+
export function snapshot(entry: Entry): QueuedEntry {
|
|
81
|
+
return {
|
|
82
|
+
id: entry.id,
|
|
83
|
+
seq: entry.seq,
|
|
84
|
+
queuedAt: entry.queuedAt,
|
|
85
|
+
collections: entry.collections.map(collection => collection.id),
|
|
86
|
+
...committedField(entry),
|
|
87
|
+
status: currentStatus(entry),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Whether two `queued` statuses differ in anything an application can see. */
|
|
92
|
+
export function queuedStatusChanged(previous: QueuedStatus | undefined, status: QueuedStatus): boolean {
|
|
93
|
+
return previous === undefined
|
|
94
|
+
|| previous.waitingFor !== status.waitingFor
|
|
95
|
+
|| previous.attempts !== status.attempts
|
|
96
|
+
|| previous.inDoubt !== status.inDoubt
|
|
97
|
+
|| previous.lastError !== status.lastError
|
|
98
|
+
// The committed set only grows, so its size says whether it changed.
|
|
99
|
+
|| previous.committedCollections?.length !== status.committedCollections?.length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The `effect` of a committed entry ({@link CommittedEffect}): `already-applied` exactly when EVERY
|
|
103
|
+
* collection the entry wrote reported a tally and the tallies sum to `alreadyApplied > 0` and
|
|
104
|
+
* `applied === 0`. A collection whose tally was never heard — one committed before a restart, or one
|
|
105
|
+
* a restore found already committed — may have changed rows, so its absence answers `applied`: a
|
|
106
|
+
* remainder that was wholly already applied must not speak for a half that was not. */
|
|
107
|
+
export function effectOf(entry: Entry): CommittedEffect {
|
|
108
|
+
let total = ZeroTally;
|
|
109
|
+
for (const collection of entry.collections) {
|
|
110
|
+
const tally = entry.applied.get(collection.id);
|
|
111
|
+
if (tally === undefined) return 'applied';
|
|
112
|
+
total = addTallies(total, tally);
|
|
113
|
+
}
|
|
114
|
+
return total.alreadyApplied > 0 && total.applied === 0 ? 'already-applied' : 'applied';
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The entry's drive state as the journal records it (`IOfflineJournal.driving`): its action id,
|
|
118
|
+
* the attempts retained under it — with `attempts`, the ones about to be sent, laid over them when
|
|
119
|
+
* given — and the collections already committed. Copies, so the journal serialises a state the
|
|
120
|
+
* drain cannot change under it. */
|
|
121
|
+
export function driveState(entry: Entry, attempts?: ReadonlyMap<CollectionId, InFlightAttempt>): JournalDriveState {
|
|
122
|
+
return {
|
|
123
|
+
actionId: entry.actionId,
|
|
124
|
+
attempts: new Map([...entry.retained, ...(attempts ?? [])]),
|
|
125
|
+
committed: new Map(entry.committed),
|
|
126
|
+
};
|
|
127
|
+
}
|