@needmoretruth/nmts-cli 0.45.0 → 0.46.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/AGENTS.md +2 -2
- package/CHANGELOG.md +61 -0
- package/README.ko.md +6 -3
- package/README.md +15 -3
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +8 -0
- package/dist/args.js +7 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +13 -0
- package/dist/commands/handover.js +328 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code.d.ts +6 -0
- package/dist/commands/public-code.js +46 -0
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +3 -3
- package/dist/commands/s3.js +44 -10
- package/dist/crypto-surface.d.ts +8 -0
- package/dist/crypto-surface.js +3 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.js +3 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/handover.d.ts +44 -0
- package/dist/handover.js +199 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +4 -2
- package/dist/help.js +16 -3
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +12 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +15 -0
- package/dist/portable.js +11 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +30 -0
- package/dist/risk.js +23 -2
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share.d.ts +5 -0
- package/dist/share.js +1 -1
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +9 -4
- package/dist/upload-wallet-put.js +16 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/handover.md +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +3 -1
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Read one NMTS Heavy part back from the storage companies that keep its copies.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
|
|
4
|
+
// Keep the comments self-contained English, with no pointer only we can follow.
|
|
5
|
+
//
|
|
6
|
+
// Each copy names a public address, `https://<company>/piece/<PieceCID>` — no credentials, open CORS,
|
|
7
|
+
// byte ranges allowed. The copies are asked in order; the first that answers with the bytes is used,
|
|
8
|
+
// and any failure moves on to the next one.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ ONLY ALLOWLISTED COMPANIES, ONLY HTTPS. An address comes from our server, and a browser page may
|
|
11
|
+
// only talk to hosts its security policy names — so a copy at a company outside the allowlist for
|
|
12
|
+
// this chain is skipped with a reason rather than asked (the request would be blocked anyway, as a
|
|
13
|
+
// bare "Failed to fetch"). A program with no such policy may pass `allowUnlisted` to ask any https
|
|
14
|
+
// company — somebody paying from their own wallet may have picked one the list does not name.
|
|
15
|
+
//
|
|
16
|
+
// ⛔ THE BYTES ARE NOT CHECKED HERE. They are an end-to-end sealed stream, and opening it is the check:
|
|
17
|
+
// a wrong or altered byte fails authentication when it is decrypted. Recomputing the PieceCID would
|
|
18
|
+
// cost a full hash of the part and tell us nothing the decryption does not.
|
|
19
|
+
import { providerForHost } from "../filecoin/providers.js";
|
|
20
|
+
/**
|
|
21
|
+
* Every copy failed. `notFound` is true only when every company actually asked answered 404 — one
|
|
22
|
+
* that was down or erred may still hold the piece, which is the same rule the Walrus reads keep.
|
|
23
|
+
*/
|
|
24
|
+
export class HeavyDownloadError extends Error {
|
|
25
|
+
pieceCid;
|
|
26
|
+
attempts;
|
|
27
|
+
notFound;
|
|
28
|
+
constructor(pieceCid, attempts) {
|
|
29
|
+
const asked = attempts.filter((a) => a.reason !== "not_https" && a.reason !== "host_not_allowed");
|
|
30
|
+
const notFound = asked.length > 0 && asked.every((a) => a.reason === "not_found");
|
|
31
|
+
super(attempts.length === 0
|
|
32
|
+
? `Piece ${pieceCid} has no recorded copies to read.`
|
|
33
|
+
: `No copy of piece ${pieceCid} could be read: ` +
|
|
34
|
+
attempts.map((a) => `${a.providerId} ${a.reason}${a.status ? ` ${a.status}` : ""}`).join(" | "));
|
|
35
|
+
// Two names, so a reader that sorts failures by name can tell "gone" from "unreachable".
|
|
36
|
+
this.name = notFound ? "HeavyPieceNotFoundError" : "HeavyDownloadError";
|
|
37
|
+
this.pieceCid = pieceCid;
|
|
38
|
+
this.attempts = attempts;
|
|
39
|
+
this.notFound = notFound;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Build the HTTP `Range` value from an exclusive-end range, or `null` for the whole part. */
|
|
43
|
+
function rangeHeader(range) {
|
|
44
|
+
if (range === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
const { start, end } = range;
|
|
47
|
+
if (!Number.isSafeInteger(start) || start < 0)
|
|
48
|
+
throw new RangeError(`Invalid range start ${start}.`);
|
|
49
|
+
if (end === undefined)
|
|
50
|
+
return `bytes=${start}-`;
|
|
51
|
+
if (!Number.isSafeInteger(end) || end <= start)
|
|
52
|
+
throw new RangeError(`Invalid range end ${end}.`);
|
|
53
|
+
return `bytes=${start}-${end - 1}`;
|
|
54
|
+
}
|
|
55
|
+
/** Why this copy may not be asked at all, or `null` when it may. */
|
|
56
|
+
function refusalFor(copy, chain, allowUnlisted) {
|
|
57
|
+
let url;
|
|
58
|
+
try {
|
|
59
|
+
url = new URL(copy.retrieval_url);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// An address that does not parse is refused the same way as one that is not https.
|
|
63
|
+
return "not_https";
|
|
64
|
+
}
|
|
65
|
+
if (url.protocol !== "https:")
|
|
66
|
+
return "not_https";
|
|
67
|
+
if (!allowUnlisted && providerForHost(chain, url.hostname) === null)
|
|
68
|
+
return "host_not_allowed";
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Open a streaming read of one Heavy part from the first copy that serves it.
|
|
73
|
+
*
|
|
74
|
+
* Resolves with the response (status 200, or 206 for a range) whose body is the sealed part;
|
|
75
|
+
* rejects with `HeavyDownloadError` when no copy serves it, or with the AbortError of `signal`.
|
|
76
|
+
* A company that ignores `Range` and answers 200 with the whole part is accepted — the caller reads
|
|
77
|
+
* only what it needs and cancels the rest.
|
|
78
|
+
*/
|
|
79
|
+
export async function fetchHeavyPart(input) {
|
|
80
|
+
const fetchImpl = input.fetchImpl ?? ((url, init) => fetch(url, init));
|
|
81
|
+
const range = rangeHeader(input.range);
|
|
82
|
+
const attempts = [];
|
|
83
|
+
for (const copy of input.copies) {
|
|
84
|
+
if (input.signal?.aborted)
|
|
85
|
+
throw new DOMException("The operation was aborted.", "AbortError");
|
|
86
|
+
const refused = refusalFor(copy, input.chain, input.allowUnlisted === true);
|
|
87
|
+
if (refused !== null) {
|
|
88
|
+
attempts.push({ providerId: copy.provider_id, reason: refused });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
let res;
|
|
92
|
+
try {
|
|
93
|
+
res = await fetchImpl(copy.retrieval_url, {
|
|
94
|
+
method: "GET",
|
|
95
|
+
...(range === null ? {} : { headers: { Range: range } }),
|
|
96
|
+
...(input.signal === undefined ? {} : { signal: input.signal }),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
if (input.signal?.aborted)
|
|
101
|
+
throw err;
|
|
102
|
+
attempts.push({ providerId: copy.provider_id, reason: "network_error" });
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if ((res.status === 200 || res.status === 206) && res.body !== null)
|
|
106
|
+
return res;
|
|
107
|
+
attempts.push({
|
|
108
|
+
providerId: copy.provider_id,
|
|
109
|
+
reason: res.status === 404 ? "not_found" : "http_error",
|
|
110
|
+
status: res.status,
|
|
111
|
+
});
|
|
112
|
+
// This answer is not going to be read; free the connection before asking the next company.
|
|
113
|
+
await res.body?.cancel().catch(() => undefined); // a failed cancel changes nothing we do next
|
|
114
|
+
}
|
|
115
|
+
throw new HeavyDownloadError(input.pieceCid, attempts);
|
|
116
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { HeavyPay, HeavyPrice, HeavySlotRequest } from "../api/types-heavy.ts";
|
|
2
|
+
/**
|
|
3
|
+
* How much plaintext one Heavy part carries at most: 512 MiB.
|
|
4
|
+
*
|
|
5
|
+
* WHY THIS NUMBER. A storage company takes at most 1,065,353,216 bytes in one piece. A 512 MiB part
|
|
6
|
+
* seals to 512 MiB plus about 2 KiB (a 72-byte header and a 16-byte tag per 4 MiB chunk), and size
|
|
7
|
+
* padding never grows a part past its own power of two, so a full part stays near half the limit
|
|
8
|
+
* whatever padding is chosen. It is also what a browser tab can hold comfortably while it hashes the
|
|
9
|
+
* part and sends it.
|
|
10
|
+
*/
|
|
11
|
+
export declare const HEAVY_PART_SIZE_BYTES: number;
|
|
12
|
+
/** A sealed part may be no smaller than this (a Filecoin piece's minimum)… */
|
|
13
|
+
export declare const HEAVY_PIECE_MIN_BYTES = 127;
|
|
14
|
+
/** …and no larger than this (the storage companies' limit per piece). */
|
|
15
|
+
export declare const HEAVY_PIECE_MAX_BYTES = 1065353216;
|
|
16
|
+
/**
|
|
17
|
+
* The least plaintext that seals to a legal piece: 127 − the 72-byte header − one 16-byte tag. A
|
|
18
|
+
* part shorter than this is padded (the same size padding the format already has) before sealing.
|
|
19
|
+
*/
|
|
20
|
+
export declare const HEAVY_MIN_SEAL_FROM_BYTES: number;
|
|
21
|
+
/** Slots one order may hold. */
|
|
22
|
+
export declare const HEAVY_MAX_SLOTS = 256;
|
|
23
|
+
/** One part of one file, before sealing. */
|
|
24
|
+
export interface HeavyRunPart {
|
|
25
|
+
/** Its position in its file (0-based). */
|
|
26
|
+
partIndex: number;
|
|
27
|
+
/** The exact size it will seal to — the order is opened with this before anything is sealed. */
|
|
28
|
+
sealedLen: number;
|
|
29
|
+
}
|
|
30
|
+
/** One file of the upload. */
|
|
31
|
+
export interface HeavyRunFile {
|
|
32
|
+
/** The caller's own name for the file; it comes back on the result unchanged. */
|
|
33
|
+
key: string;
|
|
34
|
+
parts: readonly HeavyRunPart[];
|
|
35
|
+
}
|
|
36
|
+
/** How the upload is paid. */
|
|
37
|
+
export type HeavyPayment = {
|
|
38
|
+
pay: "credits";
|
|
39
|
+
} | {
|
|
40
|
+
pay: "wallet";
|
|
41
|
+
/** `0x` + 64 hex — the Sui address that will send the WAL. */
|
|
42
|
+
payer: string;
|
|
43
|
+
/** 1..=365 days. */
|
|
44
|
+
termDays: number;
|
|
45
|
+
/** Send `price.wal_frost` FROST to `price.treasury`; resolve with the Sui transaction digest. */
|
|
46
|
+
payWallet: (price: HeavyPrice, orderId: string) => Promise<string>;
|
|
47
|
+
};
|
|
48
|
+
/** One slot of the order: which part of which file it carries. */
|
|
49
|
+
export interface SlotPlan {
|
|
50
|
+
slot: number;
|
|
51
|
+
fileIndex: number;
|
|
52
|
+
fileKey: string;
|
|
53
|
+
partIndex: number;
|
|
54
|
+
sealedLen: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Number the slots across the files (file by file, part by part), and refuse any part an order
|
|
58
|
+
* could never take — before anything is opened, sealed or charged.
|
|
59
|
+
*/
|
|
60
|
+
export declare function planSlots(files: readonly HeavyRunFile[]): SlotPlan[];
|
|
61
|
+
/** The `POST /v1/heavy/orders` body for an upload — exactly what `runHeavyOrder` sends. */
|
|
62
|
+
export interface HeavyOrderRequestBody {
|
|
63
|
+
idempotency_key: string;
|
|
64
|
+
slots: HeavySlotRequest[];
|
|
65
|
+
pay: HeavyPay;
|
|
66
|
+
term_days?: number;
|
|
67
|
+
payer?: string;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Build the order request for an upload.
|
|
71
|
+
*
|
|
72
|
+
* Exposed so a screen can open the order early — a wallet order's answer carries the price to show
|
|
73
|
+
* — and then run the upload under the SAME idempotency key, which reaches the same order again.
|
|
74
|
+
* ⚠ A different body under the same key is refused (`idempotency_conflict`): if the person changes
|
|
75
|
+
* the term, make a new key.
|
|
76
|
+
*/
|
|
77
|
+
export declare function heavyOrderRequest(files: readonly HeavyRunFile[], payment: HeavyPayment, idempotencyKey: string): HeavyOrderRequestBody;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// The sizes an NMTS Heavy part may have, and how an upload's parts become one order's slots.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
|
|
4
|
+
// Keep the comments self-contained English, with no pointer only we can follow.
|
|
5
|
+
import { HeavyOrderError } from "./order-wire.js";
|
|
6
|
+
/**
|
|
7
|
+
* How much plaintext one Heavy part carries at most: 512 MiB.
|
|
8
|
+
*
|
|
9
|
+
* WHY THIS NUMBER. A storage company takes at most 1,065,353,216 bytes in one piece. A 512 MiB part
|
|
10
|
+
* seals to 512 MiB plus about 2 KiB (a 72-byte header and a 16-byte tag per 4 MiB chunk), and size
|
|
11
|
+
* padding never grows a part past its own power of two, so a full part stays near half the limit
|
|
12
|
+
* whatever padding is chosen. It is also what a browser tab can hold comfortably while it hashes the
|
|
13
|
+
* part and sends it.
|
|
14
|
+
*/
|
|
15
|
+
export const HEAVY_PART_SIZE_BYTES = 512 * 1024 * 1024;
|
|
16
|
+
/** A sealed part may be no smaller than this (a Filecoin piece's minimum)… */
|
|
17
|
+
export const HEAVY_PIECE_MIN_BYTES = 127;
|
|
18
|
+
/** …and no larger than this (the storage companies' limit per piece). */
|
|
19
|
+
export const HEAVY_PIECE_MAX_BYTES = 1_065_353_216;
|
|
20
|
+
/**
|
|
21
|
+
* The least plaintext that seals to a legal piece: 127 − the 72-byte header − one 16-byte tag. A
|
|
22
|
+
* part shorter than this is padded (the same size padding the format already has) before sealing.
|
|
23
|
+
*/
|
|
24
|
+
export const HEAVY_MIN_SEAL_FROM_BYTES = HEAVY_PIECE_MIN_BYTES - 72 - 16;
|
|
25
|
+
/** Slots one order may hold. */
|
|
26
|
+
export const HEAVY_MAX_SLOTS = 256;
|
|
27
|
+
/**
|
|
28
|
+
* Number the slots across the files (file by file, part by part), and refuse any part an order
|
|
29
|
+
* could never take — before anything is opened, sealed or charged.
|
|
30
|
+
*/
|
|
31
|
+
export function planSlots(files) {
|
|
32
|
+
const plans = [];
|
|
33
|
+
files.forEach((file, fileIndex) => {
|
|
34
|
+
const parts = [...file.parts].sort((a, b) => a.partIndex - b.partIndex);
|
|
35
|
+
if (parts.length === 0) {
|
|
36
|
+
throw new HeavyOrderError("slot_count", `File "${file.key}" has no parts.`);
|
|
37
|
+
}
|
|
38
|
+
parts.forEach((part, position) => {
|
|
39
|
+
if (part.partIndex !== position) {
|
|
40
|
+
throw new HeavyOrderError("slot_count", `File "${file.key}" has no part ${position}; its parts must be numbered 0..n-1.`);
|
|
41
|
+
}
|
|
42
|
+
if (!Number.isSafeInteger(part.sealedLen) || part.sealedLen < HEAVY_PIECE_MIN_BYTES) {
|
|
43
|
+
throw new HeavyOrderError("piece_too_small", `Part ${part.partIndex} of "${file.key}" seals to ${part.sealedLen} bytes; a piece needs ` +
|
|
44
|
+
`${HEAVY_PIECE_MIN_BYTES}. Pad it to at least ${HEAVY_MIN_SEAL_FROM_BYTES} bytes of plaintext.`);
|
|
45
|
+
}
|
|
46
|
+
if (part.sealedLen > HEAVY_PIECE_MAX_BYTES) {
|
|
47
|
+
throw new HeavyOrderError("piece_too_large", `Part ${part.partIndex} of "${file.key}" seals to ${part.sealedLen} bytes; a piece holds ` +
|
|
48
|
+
`at most ${HEAVY_PIECE_MAX_BYTES}.`);
|
|
49
|
+
}
|
|
50
|
+
plans.push({
|
|
51
|
+
slot: plans.length,
|
|
52
|
+
fileIndex,
|
|
53
|
+
fileKey: file.key,
|
|
54
|
+
partIndex: part.partIndex,
|
|
55
|
+
sealedLen: part.sealedLen,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
if (plans.length === 0 || plans.length > HEAVY_MAX_SLOTS) {
|
|
60
|
+
throw new HeavyOrderError("slot_count", `An order holds 1..${HEAVY_MAX_SLOTS} parts; this upload has ${plans.length}.`);
|
|
61
|
+
}
|
|
62
|
+
return plans;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Build the order request for an upload.
|
|
66
|
+
*
|
|
67
|
+
* Exposed so a screen can open the order early — a wallet order's answer carries the price to show
|
|
68
|
+
* — and then run the upload under the SAME idempotency key, which reaches the same order again.
|
|
69
|
+
* ⚠ A different body under the same key is refused (`idempotency_conflict`): if the person changes
|
|
70
|
+
* the term, make a new key.
|
|
71
|
+
*/
|
|
72
|
+
export function heavyOrderRequest(files, payment, idempotencyKey) {
|
|
73
|
+
const slots = planSlots(files).map((p) => ({ slot: p.slot, sealed_len: p.sealedLen }));
|
|
74
|
+
return payment.pay === "credits"
|
|
75
|
+
? { idempotency_key: idempotencyKey, slots, pay: "credits" }
|
|
76
|
+
: {
|
|
77
|
+
idempotency_key: idempotencyKey,
|
|
78
|
+
slots,
|
|
79
|
+
pay: "wallet",
|
|
80
|
+
term_days: payment.termDays,
|
|
81
|
+
payer: payment.payer,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HeavyRunDeps, HeavyRunOptions, HeavyRunResult } from "./order-types.ts";
|
|
2
|
+
export { HEAVY_MAX_SLOTS, HEAVY_MIN_SEAL_FROM_BYTES, HEAVY_PART_SIZE_BYTES, HEAVY_PIECE_MAX_BYTES, HEAVY_PIECE_MIN_BYTES, heavyOrderRequest, type HeavyOrderRequestBody, type HeavyPayment, type HeavyRunFile, type HeavyRunPart, } from "./order-plan.ts";
|
|
3
|
+
export { HeavyOrderError, type HeavyFailure } from "./order-wire.ts";
|
|
4
|
+
export type { HeavyProgress, HeavyRunDeps, HeavyRunOptions, HeavyRunResult, HeavySealJob, SealedBytes, } from "./order-types.ts";
|
|
5
|
+
/**
|
|
6
|
+
* Run one Heavy upload to the end. Resolves with the parts to commit; rejects with a
|
|
7
|
+
* `HeavyOrderError`, or with an AbortError when `options.signal` aborts.
|
|
8
|
+
*/
|
|
9
|
+
export declare function runHeavyOrder(options: HeavyRunOptions, deps: HeavyRunDeps): Promise<HeavyRunResult>;
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// Drive one NMTS Heavy upload from start to finish: open the order, place and upload every sealed
|
|
2
|
+
// part, wait until each is stored, pay if the wallet pays, and hand back the parts to commit.
|
|
3
|
+
//
|
|
4
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
|
|
5
|
+
// Keep the comments self-contained English, with no pointer only we can follow.
|
|
6
|
+
//
|
|
7
|
+
// EVERY EDGE IS INJECTED — the order routes, the sealer, the PieceCID, the upload, the clock and the
|
|
8
|
+
// wait. The browser and the command line plug in their own; a test plugs in fakes. Nothing in here
|
|
9
|
+
// touches the network or a key on its own.
|
|
10
|
+
//
|
|
11
|
+
// THE ORDER OF THINGS (one upload = one order, however many files):
|
|
12
|
+
// 1. open `createOrder` with one slot per sealed part, numbered across the files in order.
|
|
13
|
+
// Retried under the SAME idempotency key when the answer is lost, so it is charged once.
|
|
14
|
+
// 2. per slot, a few at a time (default 2):
|
|
15
|
+
// seal → check the sealed size is the one the order was opened with → PieceCID →
|
|
16
|
+
// `targetSlot` (where to upload) → upload to that company → `slotUploaded` →
|
|
17
|
+
// read the order until the slot is `stored` (bounded, with backoff) or `failed`.
|
|
18
|
+
// 3. pay wallet orders only, after EVERY slot is stored: the injected `payWallet` sends the WAL
|
|
19
|
+
// and returns the Sui transaction digest, which is reported with `markPaid`.
|
|
20
|
+
// 4. return per file, the parts to commit (`network` 1, `blob_id` = PieceCID, the order and slot,
|
|
21
|
+
// the copies, and the Filecoin epoch they are kept until).
|
|
22
|
+
//
|
|
23
|
+
// ⛔ A FAILURE STOPS EVERYTHING AND SAYS WHY IN A WORD (`HeavyOrderError.reason`). Nothing is cleaned
|
|
24
|
+
// up from here: the server removes whatever an order stored if no file is committed from it within
|
|
25
|
+
// a day, and gives back the credits for slots that were never stored.
|
|
26
|
+
import { heavyRefusalOf } from "../api/types-heavy.js";
|
|
27
|
+
import { heavyOrderRequest, planSlots } from "./order-plan.js";
|
|
28
|
+
import { abortError, codeOf, defaultSleep, HeavyOrderError, isAbort, isHttpsUrl, isTransient, readCopies, } from "./order-wire.js";
|
|
29
|
+
export { HEAVY_MAX_SLOTS, HEAVY_MIN_SEAL_FROM_BYTES, HEAVY_PART_SIZE_BYTES, HEAVY_PIECE_MAX_BYTES, HEAVY_PIECE_MIN_BYTES, heavyOrderRequest, } from "./order-plan.js";
|
|
30
|
+
export { HeavyOrderError } from "./order-wire.js";
|
|
31
|
+
function sizeOf(bytes) {
|
|
32
|
+
return bytes instanceof Uint8Array ? bytes.byteLength : bytes.size;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Run one Heavy upload to the end. Resolves with the parts to commit; rejects with a
|
|
36
|
+
* `HeavyOrderError`, or with an AbortError when `options.signal` aborts.
|
|
37
|
+
*/
|
|
38
|
+
export async function runHeavyOrder(options, deps) {
|
|
39
|
+
const plans = planSlots(options.files);
|
|
40
|
+
const { api } = deps;
|
|
41
|
+
const sleep = deps.sleep ?? defaultSleep;
|
|
42
|
+
const now = deps.now ?? (() => Date.now());
|
|
43
|
+
const concurrency = Math.max(1, options.concurrency ?? 2);
|
|
44
|
+
const pollStart = options.pollIntervalMs ?? 3_000;
|
|
45
|
+
const pollMax = options.pollMaxIntervalMs ?? 30_000;
|
|
46
|
+
const storeTimeout = options.storeTimeoutMs ?? 60 * 60 * 1000;
|
|
47
|
+
const uploadAttempts = Math.max(1, options.uploadAttempts ?? 3);
|
|
48
|
+
const idempotencyKey = options.idempotencyKey ?? deps.newIdempotencyKey?.() ?? globalThis.crypto.randomUUID();
|
|
49
|
+
const emit = (event) => options.onProgress?.(event);
|
|
50
|
+
// One controller for the run: the caller's abort, or the first slot that fails, stops the rest.
|
|
51
|
+
if (options.signal?.aborted)
|
|
52
|
+
throw abortError();
|
|
53
|
+
const run = new AbortController();
|
|
54
|
+
const onOuterAbort = () => run.abort();
|
|
55
|
+
options.signal?.addEventListener("abort", onOuterAbort, { once: true });
|
|
56
|
+
const signal = run.signal;
|
|
57
|
+
let orderId = null;
|
|
58
|
+
/** One api call, made again after 1, 2, 4… s when it failed for a reason unrelated to the request. */
|
|
59
|
+
async function call(what, fn, slot) {
|
|
60
|
+
const attempts = 5;
|
|
61
|
+
for (let attempt = 1;; attempt += 1) {
|
|
62
|
+
if (signal.aborted)
|
|
63
|
+
throw abortError();
|
|
64
|
+
try {
|
|
65
|
+
return await fn();
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
if (isAbort(err))
|
|
69
|
+
throw err;
|
|
70
|
+
const code = codeOf(err);
|
|
71
|
+
if (!isTransient(err)) {
|
|
72
|
+
throw new HeavyOrderError("refused", `${what} was refused (${code ?? "no code"}).`, {
|
|
73
|
+
refusal: code === null ? null : heavyRefusalOf(code),
|
|
74
|
+
orderId,
|
|
75
|
+
slot,
|
|
76
|
+
cause: err,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (attempt >= attempts) {
|
|
80
|
+
throw new HeavyOrderError("unreachable", `${what} failed ${attempts} times.`, {
|
|
81
|
+
orderId,
|
|
82
|
+
slot,
|
|
83
|
+
cause: err,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
await sleep(1000 * 2 ** (attempt - 1), signal);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const readOrder = (id, slot) => call("Reading the order", () => api.getOrder(id, { signal }), slot);
|
|
91
|
+
/**
|
|
92
|
+
* Report the payment. A just-sent transaction may not be readable yet where the server looks, so
|
|
93
|
+
* "not found" is asked again a few times; "already used" means an earlier report landed and its
|
|
94
|
+
* answer was lost, which the order's own state confirms.
|
|
95
|
+
*/
|
|
96
|
+
async function reportPayment(id, digest) {
|
|
97
|
+
for (let attempt = 1;; attempt += 1) {
|
|
98
|
+
try {
|
|
99
|
+
await call("Reporting the payment", () => api.markPaid(id, { tx_digest: digest }, { signal }), null);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
const refusal = err instanceof HeavyOrderError ? err.refusal : null;
|
|
104
|
+
if (refusal === "payment_not_found" && attempt < 6) {
|
|
105
|
+
await sleep(5000 * attempt, signal);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (refusal === "digest_used") {
|
|
109
|
+
const view = await readOrder(id, null);
|
|
110
|
+
if (view.state === "paid" || view.state === "bound")
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (err instanceof HeavyOrderError && err.reason === "refused") {
|
|
114
|
+
throw new HeavyOrderError("payment_refused", `The payment ${digest} was not accepted.`, {
|
|
115
|
+
refusal,
|
|
116
|
+
orderId: id,
|
|
117
|
+
cause: err,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
// 1. Open (or reach again) the order.
|
|
126
|
+
const request = heavyOrderRequest(options.files, options.payment, idempotencyKey);
|
|
127
|
+
const created = await call("Opening the order", () => api.createOrder(request, { signal }), null);
|
|
128
|
+
if (typeof created.order_id !== "string" || created.slots?.length !== plans.length) {
|
|
129
|
+
throw new HeavyOrderError("bad_answer", "The order came back without the slots it was opened with.");
|
|
130
|
+
}
|
|
131
|
+
const id = created.order_id;
|
|
132
|
+
orderId = id;
|
|
133
|
+
if (options.payment.pay === "wallet" && created.price === undefined) {
|
|
134
|
+
throw new HeavyOrderError("bad_answer", "A wallet order came back without a price.", { orderId: id });
|
|
135
|
+
}
|
|
136
|
+
emit({ phase: "opened", orderId: id, order: created });
|
|
137
|
+
// What the order already holds: a run that reaches an existing order does not redo stored slots.
|
|
138
|
+
const before = await readOrder(id, null);
|
|
139
|
+
let latest = before;
|
|
140
|
+
const results = new Map();
|
|
141
|
+
/** Read the order until this slot is stored; fail on a failed slot, a failed order or the deadline. */
|
|
142
|
+
async function waitStored(plan, cid) {
|
|
143
|
+
const deadline = now() + storeTimeout;
|
|
144
|
+
const where = { orderId: id, slot: plan.slot };
|
|
145
|
+
for (let interval = pollStart;; interval = Math.min(pollMax, Math.round(interval * 1.5))) {
|
|
146
|
+
const view = await readOrder(id, plan.slot);
|
|
147
|
+
latest = view;
|
|
148
|
+
if (view.state === "failed" || view.state === "expired") {
|
|
149
|
+
throw new HeavyOrderError("order_failed", `The order is ${view.state}.`, where);
|
|
150
|
+
}
|
|
151
|
+
const slot = view.slots?.find((s) => s.slot === plan.slot);
|
|
152
|
+
if (slot?.state === "failed" || slot?.state === "removed") {
|
|
153
|
+
const why = slot.error ? `: ${slot.error}` : "";
|
|
154
|
+
throw new HeavyOrderError("slot_failed", `Part ${plan.partIndex} of "${plan.fileKey}" was not stored (${slot.state}${why}).`, where);
|
|
155
|
+
}
|
|
156
|
+
if (slot?.state === "stored") {
|
|
157
|
+
const copies = readCopies(slot.copies);
|
|
158
|
+
const stored = slot.piece_cid ?? cid;
|
|
159
|
+
if (copies === null || typeof stored !== "string" || (cid !== null && stored !== cid)) {
|
|
160
|
+
throw new HeavyOrderError("bad_answer", `Slot ${plan.slot} is stored, but its copies or piece id do not add up.`, where);
|
|
161
|
+
}
|
|
162
|
+
return { cid: stored, copies };
|
|
163
|
+
}
|
|
164
|
+
if (now() >= deadline) {
|
|
165
|
+
const minutes = Math.round(storeTimeout / 60_000);
|
|
166
|
+
throw new HeavyOrderError("store_timeout", `Part ${plan.partIndex} of "${plan.fileKey}" was not stored within ${minutes} minutes.`, where);
|
|
167
|
+
}
|
|
168
|
+
await sleep(interval, signal);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async function processSlot(plan) {
|
|
172
|
+
const at = { slot: plan.slot, fileKey: plan.fileKey, partIndex: plan.partIndex };
|
|
173
|
+
const where = { orderId: id, slot: plan.slot };
|
|
174
|
+
const known = before.slots?.find((s) => s.slot === plan.slot);
|
|
175
|
+
if (known?.state === "stored" || known?.state === "committing") {
|
|
176
|
+
results.set(plan.slot, await waitStored(plan, known.piece_cid ?? null));
|
|
177
|
+
emit({ phase: "stored", ...at });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
emit({ phase: "sealing", ...at });
|
|
181
|
+
const job = { fileKey: plan.fileKey, partIndex: plan.partIndex, slot: plan.slot, sealedLen: plan.sealedLen };
|
|
182
|
+
const bytes = await deps.seal(job, signal);
|
|
183
|
+
if (signal.aborted)
|
|
184
|
+
throw abortError();
|
|
185
|
+
if (sizeOf(bytes) !== plan.sealedLen) {
|
|
186
|
+
throw new HeavyOrderError("sealed_len_mismatch", `Part ${plan.partIndex} of "${plan.fileKey}" sealed to ${sizeOf(bytes)} bytes, ` +
|
|
187
|
+
`not the ${plan.sealedLen} the order was opened for.`, where);
|
|
188
|
+
}
|
|
189
|
+
emit({ phase: "hashing", ...at });
|
|
190
|
+
const cid = await deps.pieceCid(bytes);
|
|
191
|
+
emit({ phase: "placing", ...at });
|
|
192
|
+
const target = await call("Placing the part", () => api.targetSlot(id, plan.slot, { piece_cid: cid }, { signal }), plan.slot);
|
|
193
|
+
if (!isHttpsUrl(target.service_url)) {
|
|
194
|
+
throw new HeavyOrderError("bad_answer", "The server named no https storage service for this part.", where);
|
|
195
|
+
}
|
|
196
|
+
for (let attempt = 1;; attempt += 1) {
|
|
197
|
+
try {
|
|
198
|
+
await deps.upload({
|
|
199
|
+
serviceUrl: target.service_url,
|
|
200
|
+
bytes,
|
|
201
|
+
pieceCid: cid,
|
|
202
|
+
signal,
|
|
203
|
+
onProgress: (sentBytes, totalBytes) => emit({ phase: "uploading", ...at, sentBytes, totalBytes }),
|
|
204
|
+
});
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
if (isAbort(err) || signal.aborted)
|
|
209
|
+
throw err;
|
|
210
|
+
if (attempt >= uploadAttempts) {
|
|
211
|
+
throw new HeavyOrderError("upload_failed", `Uploading part ${plan.partIndex} of "${plan.fileKey}" failed ${uploadAttempts} times.`, { ...where, cause: err });
|
|
212
|
+
}
|
|
213
|
+
await sleep(2000 * 2 ** (attempt - 1), signal);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
emit({ phase: "committing", ...at });
|
|
217
|
+
await call("Reporting the upload", () => api.slotUploaded(id, plan.slot, { signal }), plan.slot);
|
|
218
|
+
results.set(plan.slot, await waitStored(plan, cid));
|
|
219
|
+
emit({ phase: "stored", ...at });
|
|
220
|
+
}
|
|
221
|
+
// 2. The slots, a few at a time. The first failure aborts the others and is the one reported.
|
|
222
|
+
let firstFailure = null;
|
|
223
|
+
let next = 0;
|
|
224
|
+
const worker = async () => {
|
|
225
|
+
for (let plan = plans[next]; firstFailure === null && plan !== undefined; plan = plans[next]) {
|
|
226
|
+
next += 1;
|
|
227
|
+
try {
|
|
228
|
+
await processSlot(plan);
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
if (firstFailure === null)
|
|
232
|
+
firstFailure = err;
|
|
233
|
+
run.abort();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, plans.length) }, worker));
|
|
239
|
+
if (firstFailure !== null) {
|
|
240
|
+
// When the caller aborted, every slot reports an abort; say so rather than the first of them.
|
|
241
|
+
if (options.signal?.aborted)
|
|
242
|
+
throw abortError();
|
|
243
|
+
throw firstFailure;
|
|
244
|
+
}
|
|
245
|
+
// The epoch the copies are kept until is set once every slot is stored: the last read has it,
|
|
246
|
+
// or a read or two later does.
|
|
247
|
+
let view = latest;
|
|
248
|
+
for (let tries = 0; typeof view.expiry_epoch !== "number" && tries < 5; tries += 1) {
|
|
249
|
+
await sleep(pollStart, signal);
|
|
250
|
+
view = await readOrder(id, null);
|
|
251
|
+
}
|
|
252
|
+
const expiryEpoch = view.expiry_epoch;
|
|
253
|
+
if (typeof expiryEpoch !== "number") {
|
|
254
|
+
throw new HeavyOrderError("bad_answer", "Every part is stored but the order names no end epoch.", {
|
|
255
|
+
orderId: id,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
// 3. Pay, when the wallet pays — once, after everything is stored, and not again on a rerun.
|
|
259
|
+
let txDigest;
|
|
260
|
+
const price = created.price;
|
|
261
|
+
if (options.payment.pay === "wallet" && price && view.state !== "paid" && view.state !== "bound") {
|
|
262
|
+
emit({ phase: "paying", orderId: id, price });
|
|
263
|
+
txDigest = await options.payment.payWallet(price, id);
|
|
264
|
+
await reportPayment(id, txDigest);
|
|
265
|
+
emit({ phase: "paid", orderId: id, txDigest });
|
|
266
|
+
}
|
|
267
|
+
// 4. The parts to commit, per file.
|
|
268
|
+
const files = options.files.map((file) => ({ key: file.key, parts: [] }));
|
|
269
|
+
for (const plan of plans) {
|
|
270
|
+
const done = results.get(plan.slot);
|
|
271
|
+
const file = files[plan.fileIndex];
|
|
272
|
+
if (done === undefined || file === undefined) {
|
|
273
|
+
throw new HeavyOrderError("bad_answer", `Slot ${plan.slot} finished without a result.`, { orderId: id });
|
|
274
|
+
}
|
|
275
|
+
file.parts.push({
|
|
276
|
+
part_index: plan.partIndex,
|
|
277
|
+
storage_kind: 0,
|
|
278
|
+
network: 1,
|
|
279
|
+
blob_id: done.cid,
|
|
280
|
+
sealed_len: plan.sealedLen,
|
|
281
|
+
owner_kind: 1,
|
|
282
|
+
expiry_epoch: expiryEpoch,
|
|
283
|
+
heavy_order_id: id,
|
|
284
|
+
heavy_slot: plan.slot,
|
|
285
|
+
copies: done.copies,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
orderId: id,
|
|
290
|
+
idempotencyKey,
|
|
291
|
+
expiryEpoch,
|
|
292
|
+
...(created.credits === undefined ? {} : { credits: created.credits }),
|
|
293
|
+
...(txDigest === undefined ? {} : { txDigest }),
|
|
294
|
+
files,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
options.signal?.removeEventListener("abort", onOuterAbort);
|
|
299
|
+
}
|
|
300
|
+
}
|