@needmoretruth/nmts-cli 0.54.1 → 0.55.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/CHANGELOG.md +20 -0
- package/README.ko.md +1 -1
- package/README.md +1 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/client-id.d.ts +1 -1
- package/dist/client-id.js +1 -1
- package/dist/commands/push.js +5 -1
- package/dist/commands/put-wallet.js +5 -1
- package/dist/commands/put.js +5 -1
- package/dist/manifest.js +3 -0
- package/dist/node-read.d.ts +2 -0
- package/dist/node-read.js +24 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/read-race.d.ts +35 -0
- package/dist/read-race.js +91 -0
- package/dist/road-hosts.generated.d.ts +4 -0
- package/dist/road-hosts.generated.js +284 -0
- package/dist/shared/lib/upload/wallet-storage-estimate.d.ts +16 -0
- package/dist/shared/lib/upload/wallet-storage-estimate.js +44 -0
- package/dist/shared/lib/wallet/graphql-history.d.ts +7 -2
- package/dist/shared/lib/wallet/graphql-history.js +7 -2
- package/dist/shared/lib/walrus/credit-tip.d.ts +24 -0
- package/dist/shared/lib/walrus/credit-tip.js +61 -0
- package/dist/shared/lib/walrus/node-certificate.d.ts +18 -0
- package/dist/shared/lib/walrus/node-certificate.js +77 -0
- package/dist/shared/lib/walrus/node-quorum.d.ts +17 -0
- package/dist/shared/lib/walrus/node-quorum.js +103 -0
- package/dist/shared/lib/walrus/tip-config.d.ts +33 -0
- package/dist/shared/lib/walrus/tip-config.js +68 -0
- package/dist/upload-road.d.ts +55 -0
- package/dist/upload-road.js +155 -0
- package/dist/upload-steps.js +1 -0
- package/dist/upload-wallet-chain.d.ts +1 -1
- package/dist/upload-wallet-chain.js +17 -4
- package/dist/upload-wallet.js +1 -0
- package/dist/upload-wire.d.ts +7 -1
- package/dist/upload.js +5 -5
- package/dist/walrus-write.js +73 -6
- package/dist/walrus.d.ts +21 -2
- package/dist/walrus.js +116 -34
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Shards on the Walrus networks NMTS uses (mainnet and testnet both, measured 2026-09-03). */
|
|
2
|
+
export declare const N_SHARDS = 1000;
|
|
3
|
+
/** `encodedBlobLength` as the SDK computes it for RS2 encoding. */
|
|
4
|
+
export declare function encodedBlobLength(unencodedLength: number, nShards?: number): number;
|
|
5
|
+
export interface WalletStorageEstimate {
|
|
6
|
+
/** Raw bytes of the files in the batch. */
|
|
7
|
+
rawBytes: number;
|
|
8
|
+
/** What an empty blob already costs — the part every batch carries whatever is in it. */
|
|
9
|
+
fixedBytes: number;
|
|
10
|
+
/** Bytes the batch buys in total, coding and fixed part included. */
|
|
11
|
+
boughtBytes: number;
|
|
12
|
+
/** How much of what is bought is the fixed part — near 1 for one small file. */
|
|
13
|
+
fixedShare: number;
|
|
14
|
+
}
|
|
15
|
+
/** What one batch (one blob) buys for `rawBytes` of files. */
|
|
16
|
+
export declare function estimateWalletStorage(rawBytes: number): WalletStorageEstimate;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// What a small-file batch actually BUYS on the storage network when the person's own wallet pays
|
|
2
|
+
// for it: a file uploaded alone pays the fixed part of a blob by itself.
|
|
3
|
+
//
|
|
4
|
+
// THE PHYSICS, FROM THE SDK'S OWN ARITHMETIC. `@mysten/walrus` computes a blob's encoded length as
|
|
5
|
+
// metadata (n_shards × (n_shards × 32 × 2 + 32)) plus the slivers (RS2: (primary + secondary
|
|
6
|
+
// symbols) × symbol size × n_shards), and that function is not exported from the package index, so
|
|
7
|
+
// the same arithmetic is written here and pinned by test to the SDK's numbers at 1,000 shards:
|
|
8
|
+
// 0 B → 66,034,000 · 500 KiB → 68,036,000 · 1 MiB → 70,038,000 · 1 GiB → 4,888,852,000.
|
|
9
|
+
// So an empty blob already costs 63.0 MiB, a 500 KiB file alone 64.9 MiB, and 400 such files in
|
|
10
|
+
// one batch about 2.3 MiB each. The batch on the pending panel is ONE blob (a quilt), so the fixed
|
|
11
|
+
// part is paid once per press of the upload button, however many files are in it.
|
|
12
|
+
//
|
|
13
|
+
// ⛔ WALLET RAIL ONLY. On the credit rail the person pays for encrypted bytes at 1 credit per
|
|
14
|
+
// MiB·28 days and the treasury carries the coding and the fixed part, so this number
|
|
15
|
+
// would be false there. The panel shows this line only when the wallet is the chosen purse.
|
|
16
|
+
//
|
|
17
|
+
// ⚠ AN ESTIMATE, NOT THE QUOTE. The signed quote at register time is the price; this is the same
|
|
18
|
+
// arithmetic ahead of it so the person can change their mind before pressing. Both networks run
|
|
19
|
+
// 1,000 shards today; the count is a parameter so a move shows up as one number, not a rewrite.
|
|
20
|
+
const DIGEST_LEN = 32;
|
|
21
|
+
const BLOB_ID_LEN = 32;
|
|
22
|
+
/** Shards on the Walrus networks NMTS uses (mainnet and testnet both, measured 2026-09-03). */
|
|
23
|
+
export const N_SHARDS = 1_000;
|
|
24
|
+
/** `encodedBlobLength` as the SDK computes it for RS2 encoding. */
|
|
25
|
+
export function encodedBlobLength(unencodedLength, nShards = N_SHARDS) {
|
|
26
|
+
if (!Number.isFinite(unencodedLength) || unencodedLength < 0) {
|
|
27
|
+
throw new RangeError(`unencodedLength must be a non-negative finite number, got ${String(unencodedLength)}`);
|
|
28
|
+
}
|
|
29
|
+
const maxFaulty = Math.floor((nShards - 1) / 3);
|
|
30
|
+
const minCorrect = nShards - maxFaulty;
|
|
31
|
+
const primary = minCorrect - maxFaulty;
|
|
32
|
+
const secondary = minCorrect;
|
|
33
|
+
let symbolSize = Math.floor((Math.max(unencodedLength, 1) - 1) / (primary * secondary)) + 1;
|
|
34
|
+
if (symbolSize % 2 === 1)
|
|
35
|
+
symbolSize += 1;
|
|
36
|
+
const slivers = (primary + secondary) * symbolSize * nShards;
|
|
37
|
+
return nShards * (nShards * DIGEST_LEN * 2 + BLOB_ID_LEN) + slivers;
|
|
38
|
+
}
|
|
39
|
+
/** What one batch (one blob) buys for `rawBytes` of files. */
|
|
40
|
+
export function estimateWalletStorage(rawBytes) {
|
|
41
|
+
const fixedBytes = encodedBlobLength(0);
|
|
42
|
+
const boughtBytes = encodedBlobLength(rawBytes);
|
|
43
|
+
return { rawBytes, fixedBytes, boughtBytes, fixedShare: fixedBytes / boughtBytes };
|
|
44
|
+
}
|
|
@@ -8,9 +8,14 @@ import type { DeeperHistory } from "./address-history.ts";
|
|
|
8
8
|
* failure there left every wallet's older history unread. 01NODE Funding SRL (Bucharest, Romania,
|
|
9
9
|
* its own network) runs a public mainnet GraphQL at `sui.01.ro`; measured that day from this machine:
|
|
10
10
|
* the same query and the same rows as Mysten's (the treasury's oldest transaction, 2026-08-02),
|
|
11
|
-
* checkpoints back to 2023, `access-control-allow-origin: *`, 1.3 s.
|
|
12
|
-
* too; it is not used because its company is not one the Privacy Policy names. Testnet keeps one
|
|
11
|
+
* checkpoints back to 2023, `access-control-allow-origin: *`, 1.3 s. Testnet keeps one
|
|
13
12
|
* operator: development has no second public GraphQL and no one's activity depends on it.
|
|
13
|
+
* ⭐ NO THIRD (2026-11-10, D51). Triton One Limited's `mainnet.sui.rpcpool.com/graphql` answered this
|
|
14
|
+
* file's exact query from real Chromium on an `https://nmts.me` page, with CORS and the same first
|
|
15
|
+
* rows, but Triton's documentation calls that endpoint "for development and evaluation only, not
|
|
16
|
+
* production traffic" and strictly rate limited: the day Mysten and 01NODE fail it would carry
|
|
17
|
+
* every wallet and refuse them. Suiscan, SuiVision, PublicNode, Ankr, Tatum, Pocket, BlockPI and
|
|
18
|
+
* Nodeinfra did not answer it (no service, no CORS, 403, an error body, 500, 404, nothing in 15 s).
|
|
14
19
|
* Still the second tier and never the only one: when every service fails the answer is
|
|
15
20
|
* `complete: false`, never "nothing there".
|
|
16
21
|
*/
|
|
@@ -15,9 +15,14 @@ import { GrpcTypes, parseGrpcTransactionResponse } from "@mysten/sui/grpc";
|
|
|
15
15
|
* failure there left every wallet's older history unread. 01NODE Funding SRL (Bucharest, Romania,
|
|
16
16
|
* its own network) runs a public mainnet GraphQL at `sui.01.ro`; measured that day from this machine:
|
|
17
17
|
* the same query and the same rows as Mysten's (the treasury's oldest transaction, 2026-08-02),
|
|
18
|
-
* checkpoints back to 2023, `access-control-allow-origin: *`, 1.3 s.
|
|
19
|
-
* too; it is not used because its company is not one the Privacy Policy names. Testnet keeps one
|
|
18
|
+
* checkpoints back to 2023, `access-control-allow-origin: *`, 1.3 s. Testnet keeps one
|
|
20
19
|
* operator: development has no second public GraphQL and no one's activity depends on it.
|
|
20
|
+
* ⭐ NO THIRD (2026-11-10, D51). Triton One Limited's `mainnet.sui.rpcpool.com/graphql` answered this
|
|
21
|
+
* file's exact query from real Chromium on an `https://nmts.me` page, with CORS and the same first
|
|
22
|
+
* rows, but Triton's documentation calls that endpoint "for development and evaluation only, not
|
|
23
|
+
* production traffic" and strictly rate limited: the day Mysten and 01NODE fail it would carry
|
|
24
|
+
* every wallet and refuse them. Suiscan, SuiVision, PublicNode, Ankr, Tatum, Pocket, BlockPI and
|
|
25
|
+
* Nodeinfra did not answer it (no service, no CORS, 403, an error body, 500, 404, nothing in 15 s).
|
|
21
26
|
* Still the second tier and never the only one: when every service fails the answer is
|
|
22
27
|
* `complete: false`, never "nothing there".
|
|
23
28
|
*/
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { RelayTip } from "./tip-config.ts";
|
|
2
|
+
/** Mysten Labs' relay tip per KiB of encoded size, MIST (base 0). ⚠ MUST match `chain/src/limits.ts::REFERENCE_TIP_PER_KIB_MIST`. */
|
|
3
|
+
export declare const REFERENCE_TIP_PER_KIB_MIST = 40;
|
|
4
|
+
/** How many times Mysten's formula a credit upload may tip. ⚠ MUST match `chain/src/limits.ts::CREDIT_TIP_RATIO`. */
|
|
5
|
+
export declare const CREDIT_TIP_RATIO = 3;
|
|
6
|
+
/** The largest blob the credit rail registers: 2,048 credits of 1 MiB (`chain/src/limits.ts` says why). */
|
|
7
|
+
export declare const LARGEST_CREDIT_BLOB_BYTES: number;
|
|
8
|
+
/** A relay's tip as a line: `baseMist + mulPerKibMist × encoded KiB` (a constant tip has no slope). */
|
|
9
|
+
export interface TipLine {
|
|
10
|
+
baseMist: bigint;
|
|
11
|
+
mulPerKibMist: bigint;
|
|
12
|
+
}
|
|
13
|
+
/** What Mysten's formula charges for a blob of `blobBytes` unencoded bytes, MIST. */
|
|
14
|
+
export declare function referenceTipMist(blobBytes: number): bigint;
|
|
15
|
+
/** True when this tip for a blob of `blobBytes` is one the treasury pays. */
|
|
16
|
+
export declare function creditTipFits(tip: TipLine, blobBytes: number): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* True when the tip fits at every blob size the credit rail registers. Both tips are straight lines in
|
|
19
|
+
* the encoded size, so their ratio only rises or falls between the ends: checking the two ends is
|
|
20
|
+
* checking every size. For a caller that chooses its road before it knows the size.
|
|
21
|
+
*/
|
|
22
|
+
export declare function creditTipFitsEverySize(tip: TipLine): boolean;
|
|
23
|
+
/** A tip as a line in the encoded size, or null when the relay's price could not be read. */
|
|
24
|
+
export declare function tipLine(tip: RelayTip): TipLine | null;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Which company relay the treasury may tip on a credit upload (2026-11-10).
|
|
2
|
+
//
|
|
3
|
+
// WHY. Until this day a credit upload went through Mysten Labs' relay or, when it did not answer,
|
|
4
|
+
// straight to the storage nodes: the treasury tipped only Mysten. The owner asked for every company
|
|
5
|
+
// that answers on every road, so that a second closure still leaves a road, and Studio
|
|
6
|
+
// Mirai's relay now carries credit uploads too, after Mysten and before the storage nodes.
|
|
7
|
+
//
|
|
8
|
+
// ⛔ BUT ANOTHER COMPANY NAMES THE PRICE THE TREASURY PAYS. So a relay is a credit road only while its
|
|
9
|
+
// tip for THIS upload is at most `CREDIT_TIP_RATIO` times what Mysten's formula would charge for it;
|
|
10
|
+
// above that the upload goes on to the next road (the storage nodes, no tip). Mysten's formula is
|
|
11
|
+
// pinned, not read live: the relay is needed exactly when Mysten does not answer, and a reference
|
|
12
|
+
// that has to be fetched from the company that is down is no reference.
|
|
13
|
+
//
|
|
14
|
+
// MEASURED 2026-11-10, `GET /v1/tip-config` on mainnet:
|
|
15
|
+
// Mysten Labs {"linear":{"base":0,"encoded_size_mul_per_kib":40}}
|
|
16
|
+
// Studio Mirai {"linear":{"base":3000000,"encoded_size_mul_per_kib":60}}
|
|
17
|
+
// Every blob carries 66,034,000 encoded bytes even when empty (1,000 shards), so Mirai's tip is
|
|
18
|
+
// 2.7 times Mysten's for an empty blob and falls toward 1.5 times as the blob grows: it fits.
|
|
19
|
+
//
|
|
20
|
+
// ⚠ The chain service holds the same rule (`chain/src/limits.ts::referenceTipCeilingMist`) and refuses a
|
|
21
|
+
// tip above it with 403, so a relay the browser picks and the chain refuses is a repricing between
|
|
22
|
+
// the two reads, not a disagreement between two programs.
|
|
23
|
+
import { encodedBlobLength } from "../upload/wallet-storage-estimate.js";
|
|
24
|
+
/** Mysten Labs' relay tip per KiB of encoded size, MIST (base 0). ⚠ MUST match `chain/src/limits.ts::REFERENCE_TIP_PER_KIB_MIST`. */
|
|
25
|
+
export const REFERENCE_TIP_PER_KIB_MIST = 40;
|
|
26
|
+
/** How many times Mysten's formula a credit upload may tip. ⚠ MUST match `chain/src/limits.ts::CREDIT_TIP_RATIO`. */
|
|
27
|
+
export const CREDIT_TIP_RATIO = 3;
|
|
28
|
+
/** The largest blob the credit rail registers: 2,048 credits of 1 MiB (`chain/src/limits.ts` says why). */
|
|
29
|
+
export const LARGEST_CREDIT_BLOB_BYTES = 2 * 1024 * 1024 * 1024;
|
|
30
|
+
function encodedKib(blobBytes) {
|
|
31
|
+
return (BigInt(encodedBlobLength(blobBytes)) + 1023n) / 1024n;
|
|
32
|
+
}
|
|
33
|
+
/** What Mysten's formula charges for a blob of `blobBytes` unencoded bytes, MIST. */
|
|
34
|
+
export function referenceTipMist(blobBytes) {
|
|
35
|
+
return BigInt(REFERENCE_TIP_PER_KIB_MIST) * encodedKib(blobBytes);
|
|
36
|
+
}
|
|
37
|
+
/** True when this tip for a blob of `blobBytes` is one the treasury pays. */
|
|
38
|
+
export function creditTipFits(tip, blobBytes) {
|
|
39
|
+
return tip.baseMist + tip.mulPerKibMist * encodedKib(blobBytes) <= BigInt(CREDIT_TIP_RATIO) * referenceTipMist(blobBytes);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* True when the tip fits at every blob size the credit rail registers. Both tips are straight lines in
|
|
43
|
+
* the encoded size, so their ratio only rises or falls between the ends: checking the two ends is
|
|
44
|
+
* checking every size. For a caller that chooses its road before it knows the size.
|
|
45
|
+
*/
|
|
46
|
+
export function creditTipFitsEverySize(tip) {
|
|
47
|
+
return creditTipFits(tip, 0) && creditTipFits(tip, LARGEST_CREDIT_BLOB_BYTES);
|
|
48
|
+
}
|
|
49
|
+
/** A tip as a line in the encoded size, or null when the relay's price could not be read. */
|
|
50
|
+
export function tipLine(tip) {
|
|
51
|
+
switch (tip.kind) {
|
|
52
|
+
case "none":
|
|
53
|
+
return { baseMist: 0n, mulPerKibMist: 0n };
|
|
54
|
+
case "const":
|
|
55
|
+
return { baseMist: tip.baseMist, mulPerKibMist: 0n };
|
|
56
|
+
case "linear":
|
|
57
|
+
return { baseMist: tip.baseMist, mulPerKibMist: tip.mulPerKibMist };
|
|
58
|
+
case "unknown":
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type WalrusClient } from "@mysten/walrus";
|
|
2
|
+
type CertArgs = Parameters<WalrusClient["certificateFromConfirmations"]>[0];
|
|
3
|
+
type Certificate = Awaited<ReturnType<WalrusClient["certificateFromConfirmations"]>>;
|
|
4
|
+
type SystemState = Awaited<ReturnType<WalrusClient["systemState"]>>;
|
|
5
|
+
type Member = SystemState["committee"]["members"][number];
|
|
6
|
+
/** The chain's quorum (`walrus::bls_aggregate::is_quorum`). */
|
|
7
|
+
export declare const chainQuorum: (weight: number, nShards: number) => boolean;
|
|
8
|
+
/** The message every confirming node signs for this blob in this epoch. */
|
|
9
|
+
export declare function confirmationMessage(epoch: number, blobId: string, deletable: boolean, blobObjectId: string): Uint8Array;
|
|
10
|
+
/**
|
|
11
|
+
* Combine the confirmations whose message and signature hold into a certificate, or throw when their
|
|
12
|
+
* shard weight is short of the chain's quorum. `members` is the BLS committee in signer-index order.
|
|
13
|
+
*/
|
|
14
|
+
export declare function certificateByShardWeight(confirmations: CertArgs["confirmations"], members: readonly Pick<Member, "public_key" | "weight">[], nShards: number, expected: Uint8Array): Certificate;
|
|
15
|
+
type Certifier = Pick<WalrusClient, "certificateFromConfirmations" | "systemState">;
|
|
16
|
+
/** Replace the client's node-count certificate with the chain's shard-weight one. */
|
|
17
|
+
export declare function certifyByShardWeight<T extends Certifier>(walrus: T): T;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// The certificate from node confirmations, by the rule the chain applies (D51, 2026-11-10).
|
|
2
|
+
// ⛔ WHY NOT THE SDK'S. `WalrusClient.certificateFromConfirmations` (1.2.36) refuses unless more than two
|
|
3
|
+
// thirds of the committee's NODES confirmed. The chain counts no nodes: `bls_aggregate::
|
|
4
|
+
// verify_certificate_and_weight` sums the signers' weight (each member's shard count) and asserts
|
|
5
|
+
// `is_quorum`, `3 * weight >= 2 * n_shards + 1`. Members hold uneven shard counts (mainnet and testnet),
|
|
6
|
+
// so a node count waits for confirmations the chain does not need, and on testnet on 2026-11-10 it
|
|
7
|
+
// refused certificates the chain would have taken (63 of 97 nodes confirmed). The writes stop on the
|
|
8
|
+
// same shard rule (node-quorum.ts), so the stop and the certificate cannot disagree.
|
|
9
|
+
// ⛔ WHAT IS CHECKED. Each confirmation must carry the exact message the chain will rebuild (intent,
|
|
10
|
+
// epoch, blob id, persistence) and verify under its member's key; one aggregate check covers the honest
|
|
11
|
+
// case, and only when it fails is each signature checked alone to drop the bad ones.
|
|
12
|
+
import { bls12_381 } from "@noble/curves/bls12-381.js";
|
|
13
|
+
import { bcs } from "@mysten/sui/bcs";
|
|
14
|
+
import { fromBase64 } from "@mysten/sui/utils";
|
|
15
|
+
import { blobIdToInt, NotEnoughBlobConfirmationsError } from "@mysten/walrus";
|
|
16
|
+
/** The chain's quorum (`walrus::bls_aggregate::is_quorum`). */
|
|
17
|
+
export const chainQuorum = (weight, nShards) => 3 * weight >= 2 * nShards + 1;
|
|
18
|
+
const BLOB_CERT_MSG = 1;
|
|
19
|
+
const WALRUS_APP_ID = 3;
|
|
20
|
+
const StorageConfirmation = bcs.struct("ProtocolMessage<StorageConfirmationBody>", {
|
|
21
|
+
intent: bcs.struct("Intent", { type: bcs.u8(), version: bcs.u8(), appId: bcs.u8() }),
|
|
22
|
+
epoch: bcs.u32(),
|
|
23
|
+
messageContents: bcs.struct("StorageConfirmationBody", {
|
|
24
|
+
blobId: bcs.u256(),
|
|
25
|
+
blobType: bcs.enum("BlobPersistenceType", { Permanent: null, Deletable: bcs.struct("Deletable", { objectId: bcs.Address }) }),
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
/** The message every confirming node signs for this blob in this epoch. */
|
|
29
|
+
export function confirmationMessage(epoch, blobId, deletable, blobObjectId) {
|
|
30
|
+
return StorageConfirmation.serialize({
|
|
31
|
+
intent: { type: BLOB_CERT_MSG, version: 0, appId: WALRUS_APP_ID },
|
|
32
|
+
epoch,
|
|
33
|
+
messageContents: {
|
|
34
|
+
blobId: blobIdToInt(blobId),
|
|
35
|
+
blobType: deletable ? { Deletable: { objectId: blobObjectId } } : { Permanent: null },
|
|
36
|
+
},
|
|
37
|
+
}).toBytes();
|
|
38
|
+
}
|
|
39
|
+
const sameBytes = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
|
|
40
|
+
/**
|
|
41
|
+
* Combine the confirmations whose message and signature hold into a certificate, or throw when their
|
|
42
|
+
* shard weight is short of the chain's quorum. `members` is the BLS committee in signer-index order.
|
|
43
|
+
*/
|
|
44
|
+
export function certificateByShardWeight(confirmations, members, nShards, expected) {
|
|
45
|
+
const sigs = bls12_381.longSignatures;
|
|
46
|
+
const hashed = sigs.hash(expected);
|
|
47
|
+
const signed = confirmations.flatMap((confirmation, index) => {
|
|
48
|
+
const member = members[index];
|
|
49
|
+
if (!confirmation || !member || !sameBytes(fromBase64(confirmation.serializedMessage), expected))
|
|
50
|
+
return [];
|
|
51
|
+
return [{ index, weight: member.weight, key: new Uint8Array(member.public_key.bytes), signature: fromBase64(confirmation.signature) }];
|
|
52
|
+
});
|
|
53
|
+
const aggregateHolds = (set) => set.length > 0 &&
|
|
54
|
+
sigs.verify(sigs.aggregateSignatures(set.map((s) => s.signature)), hashed, sigs.aggregatePublicKeys(set.map((s) => s.key)));
|
|
55
|
+
const valid = aggregateHolds(signed) ? signed : signed.filter((s) => sigs.verify(s.signature, hashed, s.key));
|
|
56
|
+
const weight = valid.reduce((sum, s) => sum + s.weight, 0);
|
|
57
|
+
if (!chainQuorum(weight, nShards)) {
|
|
58
|
+
throw new NotEnoughBlobConfirmationsError(`Too few confirmed shards for blob (${weight} of ${nShards} from ${valid.length} nodes; the chain needs more than two thirds)`);
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
signers: valid.map((s) => s.index),
|
|
62
|
+
serializedMessage: expected,
|
|
63
|
+
signature: sigs.Signature.toBytes(sigs.aggregateSignatures(valid.map((s) => s.signature))),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** Replace the client's node-count certificate with the chain's shard-weight one. */
|
|
67
|
+
export function certifyByShardWeight(walrus) {
|
|
68
|
+
walrus.certificateFromConfirmations = async ({ confirmations, blobId, deletable, blobObjectId }) => {
|
|
69
|
+
const { committee } = await walrus.systemState();
|
|
70
|
+
if (confirmations.length !== committee.members.length) {
|
|
71
|
+
throw new NotEnoughBlobConfirmationsError("Invalid number of confirmations. Confirmations array must contain an entry for each node");
|
|
72
|
+
}
|
|
73
|
+
const expected = confirmationMessage(committee.epoch, blobId, deletable, blobObjectId);
|
|
74
|
+
return certificateByShardWeight(confirmations, committee.members, committee.n_shards, expected);
|
|
75
|
+
};
|
|
76
|
+
return walrus;
|
|
77
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { WalrusClient } from "@mysten/walrus";
|
|
2
|
+
type WriteAll = WalrusClient["writeEncodedBlobToNodes"];
|
|
3
|
+
type Writer = Pick<WalrusClient, "writeEncodedBlobToNode" | "systemState"> & {
|
|
4
|
+
writeEncodedBlobToNodes: WriteAll;
|
|
5
|
+
};
|
|
6
|
+
export declare const QUORUM_GRACE_MIN_MS = 2000;
|
|
7
|
+
export declare const QUORUM_GRACE_MAX_MS = 20000;
|
|
8
|
+
export declare const STALL_FLOOR_MS = 90000;
|
|
9
|
+
export declare const RETRY_WAITS_MS: readonly number[];
|
|
10
|
+
export interface QuorumTiming {
|
|
11
|
+
now?: () => number;
|
|
12
|
+
stallFloorMs?: number;
|
|
13
|
+
retryWaitsMs?: readonly number[];
|
|
14
|
+
}
|
|
15
|
+
/** Replace the client's all-nodes wait with quorum-then-grace. Returns the same client. */
|
|
16
|
+
export declare function stopAtQuorum<T extends Writer>(walrus: T, timing?: QuorumTiming): T;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { chainQuorum } from "./node-certificate.js";
|
|
2
|
+
export const QUORUM_GRACE_MIN_MS = 2_000;
|
|
3
|
+
export const QUORUM_GRACE_MAX_MS = 20_000;
|
|
4
|
+
export const STALL_FLOOR_MS = 90_000;
|
|
5
|
+
export const RETRY_WAITS_MS = [3_000, 6_000, 12_000, 24_000];
|
|
6
|
+
/** So many shards lost that the rest can never reach the chain's quorum. */
|
|
7
|
+
const beyondHope = (lostShards, nShards) => !chainQuorum(nShards - lostShards, nShards);
|
|
8
|
+
/** Wait `ms`, or less when the signal aborts. */
|
|
9
|
+
const pause = (ms, signal) => new Promise((resolve) => {
|
|
10
|
+
const timer = setTimeout(resolve, ms);
|
|
11
|
+
signal.addEventListener("abort", () => (clearTimeout(timer), resolve()), { once: true });
|
|
12
|
+
});
|
|
13
|
+
/** Replace the client's all-nodes wait with quorum-then-grace. Returns the same client. */
|
|
14
|
+
export function stopAtQuorum(walrus, timing = {}) {
|
|
15
|
+
const now = timing.now ?? Date.now;
|
|
16
|
+
const stallFloorMs = timing.stallFloorMs ?? STALL_FLOOR_MS;
|
|
17
|
+
const waits = timing.retryWaitsMs ?? RETRY_WAITS_MS;
|
|
18
|
+
/** One node's write, asked again after a failure while the write is open. */
|
|
19
|
+
const writeOne = async (input, signal) => {
|
|
20
|
+
for (let attempt = 0;; attempt++) {
|
|
21
|
+
try {
|
|
22
|
+
return await walrus.writeEncodedBlobToNode(input);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
const wait = waits[attempt];
|
|
26
|
+
if (wait === undefined || signal.aborted)
|
|
27
|
+
throw error;
|
|
28
|
+
await pause(wait, signal);
|
|
29
|
+
if (signal.aborted)
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const writeAll = async ({ blobId, metadata, sliversByNode, signal, ...options }) => {
|
|
35
|
+
const { committee } = await walrus.systemState();
|
|
36
|
+
const nShards = committee.n_shards;
|
|
37
|
+
const nNodes = sliversByNode.length;
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const linked = signal ? AbortSignal.any([controller.signal, signal]) : controller.signal;
|
|
40
|
+
const results = sliversByNode.map(() => null);
|
|
41
|
+
const started = now();
|
|
42
|
+
let okShards = 0;
|
|
43
|
+
let lostShards = 0;
|
|
44
|
+
let settled = 0;
|
|
45
|
+
return await new Promise((resolve, reject) => {
|
|
46
|
+
let graceTimer = null;
|
|
47
|
+
let stallTimer = null;
|
|
48
|
+
let stallMs = null;
|
|
49
|
+
let done = false;
|
|
50
|
+
const tooMany = (why) => new Error(`Too many failures while writing blob ${blobId} to nodes (${why})`);
|
|
51
|
+
const quorum = () => chainQuorum(okShards, nShards);
|
|
52
|
+
const finish = (error) => {
|
|
53
|
+
if (done)
|
|
54
|
+
return;
|
|
55
|
+
done = true;
|
|
56
|
+
if (graceTimer !== null)
|
|
57
|
+
clearTimeout(graceTimer);
|
|
58
|
+
if (stallTimer !== null)
|
|
59
|
+
clearTimeout(stallTimer);
|
|
60
|
+
controller.abort(error ?? new Error("quorum reached; the remaining node writes were stopped"));
|
|
61
|
+
if (error)
|
|
62
|
+
reject(error);
|
|
63
|
+
else
|
|
64
|
+
resolve([...results]);
|
|
65
|
+
};
|
|
66
|
+
const check = () => {
|
|
67
|
+
if (settled === nNodes)
|
|
68
|
+
return finish(quorum() ? undefined : tooMany("no quorum"));
|
|
69
|
+
if (graceTimer === null && quorum()) {
|
|
70
|
+
const grace = Math.min(Math.max(now() - started, QUORUM_GRACE_MIN_MS), QUORUM_GRACE_MAX_MS);
|
|
71
|
+
graceTimer = setTimeout(() => finish(), grace);
|
|
72
|
+
}
|
|
73
|
+
if (stallMs === null && okShards * 2 > nShards)
|
|
74
|
+
stallMs = Math.max((now() - started) * 4, stallFloorMs);
|
|
75
|
+
if (stallMs !== null) {
|
|
76
|
+
if (stallTimer !== null)
|
|
77
|
+
clearTimeout(stallTimer);
|
|
78
|
+
stallTimer = setTimeout(() => finish(quorum() ? undefined : tooMany(`${nNodes - settled} nodes did not answer`)), stallMs);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
sliversByNode.forEach((slivers, nodeIndex) => {
|
|
82
|
+
const shards = slivers.primary.length;
|
|
83
|
+
writeOne({ blobId, nodeIndex, metadata, slivers, signal: linked, ...options }, linked)
|
|
84
|
+
.then((confirmation) => {
|
|
85
|
+
results[nodeIndex] = confirmation;
|
|
86
|
+
okShards += shards;
|
|
87
|
+
}, () => {
|
|
88
|
+
lostShards += shards;
|
|
89
|
+
})
|
|
90
|
+
.finally(() => {
|
|
91
|
+
settled += 1;
|
|
92
|
+
if (done)
|
|
93
|
+
return;
|
|
94
|
+
if (beyondHope(lostShards, nShards))
|
|
95
|
+
return finish(tooMany("refused"));
|
|
96
|
+
check();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
walrus.writeEncodedBlobToNodes = writeAll;
|
|
102
|
+
return walrus;
|
|
103
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tip a relay charges, as parsed from its `GET /v1/tip-config`. A relay is free (`none`),
|
|
3
|
+
* flat (`const`), or size-linear (`linear`). `unknown` means the endpoint answered but we could
|
|
4
|
+
* not understand its tip shape — REACHABLE BUT UNUSABLE (scoring gates it out, never guesses a
|
|
5
|
+
* price). All MIST amounts are bigint.
|
|
6
|
+
*/
|
|
7
|
+
export type RelayTip = {
|
|
8
|
+
readonly kind: "none";
|
|
9
|
+
} | {
|
|
10
|
+
readonly kind: "const";
|
|
11
|
+
readonly address: string;
|
|
12
|
+
readonly baseMist: bigint;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: "linear";
|
|
15
|
+
readonly address: string;
|
|
16
|
+
readonly baseMist: bigint;
|
|
17
|
+
/** MIST added per KiB of ENCODED size (Walrus config `encoded_size_mul_per_kib`). */
|
|
18
|
+
readonly mulPerKibMist: bigint;
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: "unknown";
|
|
21
|
+
readonly detail: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Parse a relay's `/v1/tip-config` JSON into a RelayTip, DEFENSIVELY (an unrecognized/garbled shape
|
|
25
|
+
* becomes `unknown`, never a guessed price). Recognized shapes:
|
|
26
|
+
* no-tip: { "no_tip": ... } → none
|
|
27
|
+
* const: { "send_tip": { "address", "kind": { "const": N } } } → const
|
|
28
|
+
* linear: { "send_tip": { "address",
|
|
29
|
+
* "kind": { "linear": { "base": N,
|
|
30
|
+
* "encoded_size_mul_per_kib": M } } } } → linear
|
|
31
|
+
* Tolerates a missing `send_tip` wrapper (reads `kind` off the root) and camelCase `sendTip`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseTipConfig(raw: unknown): RelayTip;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// A relay's tip, read from its `GET /v1/tip-config`. Kept apart from the relay pool so the
|
|
2
|
+
// command-line tool reads a tip with the same parser the browser uses (it copies this file).
|
|
3
|
+
/** Coerce a JSON tip amount (number | numeric string | bigint) to a non-negative bigint, or null. */
|
|
4
|
+
function toMist(value) {
|
|
5
|
+
if (typeof value === "number")
|
|
6
|
+
return Number.isInteger(value) && value >= 0 ? BigInt(value) : null;
|
|
7
|
+
if (typeof value === "bigint")
|
|
8
|
+
return value >= 0n ? value : null;
|
|
9
|
+
if (typeof value === "string")
|
|
10
|
+
return /^\d+$/.test(value) ? BigInt(value) : null;
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
/** The value under `key` when `value` is a JSON object that has it; `undefined` otherwise. */
|
|
14
|
+
function field(value, key) {
|
|
15
|
+
return typeof value === "object" && value !== null && key in value ? Reflect.get(value, key) : undefined;
|
|
16
|
+
}
|
|
17
|
+
function isObject(value) {
|
|
18
|
+
return typeof value === "object" && value !== null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Parse a relay's `/v1/tip-config` JSON into a RelayTip, DEFENSIVELY (an unrecognized/garbled shape
|
|
22
|
+
* becomes `unknown`, never a guessed price). Recognized shapes:
|
|
23
|
+
* no-tip: { "no_tip": ... } → none
|
|
24
|
+
* const: { "send_tip": { "address", "kind": { "const": N } } } → const
|
|
25
|
+
* linear: { "send_tip": { "address",
|
|
26
|
+
* "kind": { "linear": { "base": N,
|
|
27
|
+
* "encoded_size_mul_per_kib": M } } } } → linear
|
|
28
|
+
* Tolerates a missing `send_tip` wrapper (reads `kind` off the root) and camelCase `sendTip`.
|
|
29
|
+
*/
|
|
30
|
+
export function parseTipConfig(raw) {
|
|
31
|
+
// A no-tip relay answers the bare JSON string "no_tip" (measured on Mysten's relay image v1.60.1,
|
|
32
|
+
// 2026-11-09; the SDK reads any string as "no tip"). Read as an object, a free relay would be
|
|
33
|
+
// "unknown" and could never be chosen.
|
|
34
|
+
if (raw === "no_tip")
|
|
35
|
+
return { kind: "none" };
|
|
36
|
+
if (!isObject(raw)) {
|
|
37
|
+
return { kind: "unknown", detail: "tip-config was not a JSON object" };
|
|
38
|
+
}
|
|
39
|
+
if ("no_tip" in raw || field(raw, "tip") === "no_tip")
|
|
40
|
+
return { kind: "none" };
|
|
41
|
+
const sendTip = field(raw, "send_tip") ?? field(raw, "sendTip");
|
|
42
|
+
const node = isObject(sendTip) ? sendTip : raw;
|
|
43
|
+
const addressValue = field(node, "address");
|
|
44
|
+
const address = typeof addressValue === "string" ? addressValue : "";
|
|
45
|
+
const kind = field(node, "kind");
|
|
46
|
+
if (!isObject(kind)) {
|
|
47
|
+
return { kind: "unknown", detail: "tip-config had no recognizable send_tip.kind" };
|
|
48
|
+
}
|
|
49
|
+
if ("const" in kind) {
|
|
50
|
+
const baseMist = toMist(field(kind, "const"));
|
|
51
|
+
if (baseMist === null)
|
|
52
|
+
return { kind: "unknown", detail: "const tip amount was not a non-negative integer" };
|
|
53
|
+
return { kind: "const", address, baseMist };
|
|
54
|
+
}
|
|
55
|
+
if ("linear" in kind) {
|
|
56
|
+
const linear = field(kind, "linear");
|
|
57
|
+
if (!isObject(linear)) {
|
|
58
|
+
return { kind: "unknown", detail: "linear tip had no body" };
|
|
59
|
+
}
|
|
60
|
+
const baseMist = toMist(field(linear, "base") ?? 0);
|
|
61
|
+
const mulPerKibMist = toMist(field(linear, "encoded_size_mul_per_kib"));
|
|
62
|
+
if (baseMist === null || mulPerKibMist === null) {
|
|
63
|
+
return { kind: "unknown", detail: "linear tip base/encoded_size_mul_per_kib were not non-negative integers" };
|
|
64
|
+
}
|
|
65
|
+
return { kind: "linear", address, baseMist, mulPerKibMist };
|
|
66
|
+
}
|
|
67
|
+
return { kind: "unknown", detail: "unrecognized tip kind (expected const | linear | no_tip)" };
|
|
68
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** Where the own relay answers, after the NMTS server's address. */
|
|
2
|
+
export declare const OWN_RELAY_PATH = "/v1/relay";
|
|
3
|
+
/** The part size on the own road: an existing preset, the size whose relay peak fits the server. */
|
|
4
|
+
export declare const OWN_RELAY_PART_BYTES: number;
|
|
5
|
+
/** How long Mysten's relay gets to answer before this run takes the own road. */
|
|
6
|
+
export declare const ROAD_PROBE_TIMEOUT_MS = 10000;
|
|
7
|
+
/**
|
|
8
|
+
* The "relay address" of the direct road: no host, the storage nodes themselves (D51). The same
|
|
9
|
+
* value as the browser's (`web/src/lib/walrus/node-road.ts`), and it is what a resumable upload's
|
|
10
|
+
* record keeps, so a resumed part goes back to the nodes.
|
|
11
|
+
*/
|
|
12
|
+
export declare const NODE_ROAD = "walrus:storage-nodes";
|
|
13
|
+
/** True for the direct road. */
|
|
14
|
+
export declare function isNodeRoad(relayUrl: string): boolean;
|
|
15
|
+
/** True for a road that takes no tip: the own relay and the storage nodes. */
|
|
16
|
+
export declare function isTipFree(relayUrl: string): boolean;
|
|
17
|
+
/** True for the own relay's address on any NMTS server. */
|
|
18
|
+
export declare function isOwnRelay(relayUrl: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Decide this run's road, once, before anything is priced. True when a small road (the storage
|
|
21
|
+
* nodes, or the own relay) was taken.
|
|
22
|
+
*
|
|
23
|
+
* `probe` is the request that asks Mysten's relay (tests pass a fake one). `nodeRoad: false` is a
|
|
24
|
+
* machine that cannot write to the storage nodes itself; it goes to the own relay, as before D51.
|
|
25
|
+
*/
|
|
26
|
+
export declare function chooseUploadRoad(input: {
|
|
27
|
+
network: string;
|
|
28
|
+
server: string;
|
|
29
|
+
apiKey: string;
|
|
30
|
+
probe?: typeof fetch;
|
|
31
|
+
timeoutMs?: number;
|
|
32
|
+
nodeRoad?: boolean;
|
|
33
|
+
/** A wallet upload: the person pays the tip, so the other company relays may carry it. */
|
|
34
|
+
otherCompanies?: boolean;
|
|
35
|
+
/** A credit upload: the other company relays may carry it while their tip fits the credit ceiling. */
|
|
36
|
+
credit?: boolean;
|
|
37
|
+
}): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* The own relay to try when a direct write failed, with the fetch that carries this account's key,
|
|
40
|
+
* or null when this run did not choose the small roads (a named relay, or Mysten's).
|
|
41
|
+
*/
|
|
42
|
+
export declare function ownRelayFallback(): string | null;
|
|
43
|
+
/** Forget the choice (tests; a library caller between runs). */
|
|
44
|
+
export declare function resetUploadRoad(): void;
|
|
45
|
+
/** The road this run writes through: the storage nodes or the own relay when chosen, else the usual relay. */
|
|
46
|
+
export declare function uploadRelay(network: string): string;
|
|
47
|
+
/** The part size a fresh upload uses: unchanged on Mysten's road, at most 64 MiB on the small roads. */
|
|
48
|
+
export declare function partSizeForRoad(partSize: number): number;
|
|
49
|
+
/**
|
|
50
|
+
* The fetch the relay client uses. On the own road it carries this account's key, and while another
|
|
51
|
+
* upload holds the relay (503 with `Retry-After`) it waits and asks again until the SDK's own
|
|
52
|
+
* timeout ends it. On Mysten's road it is `inner` unchanged.
|
|
53
|
+
*/
|
|
54
|
+
export type RelayFetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
55
|
+
export declare function relayFetch(relayUrl: string, inner: RelayFetch, sleep?: (ms: number, signal?: AbortSignal | null) => Promise<void>): RelayFetch;
|