@mysten/walrus 0.2.2 → 0.3.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 +12 -0
- package/README.md +83 -0
- package/dist/cjs/client.d.ts +6 -6
- package/dist/cjs/client.js +29 -29
- package/dist/cjs/client.js.map +2 -2
- package/dist/{esm/read/quilt.d.ts → cjs/quilt/reader.d.ts} +7 -3
- package/dist/cjs/{read/quilt.js → quilt/reader.js} +32 -18
- package/dist/cjs/quilt/reader.js.map +7 -0
- package/dist/cjs/quilt/write.d.ts +21 -0
- package/dist/cjs/quilt/write.js +167 -0
- package/dist/cjs/quilt/write.js.map +7 -0
- package/dist/cjs/types.d.ts +7 -7
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/{fan-out-proxy → upload-relay}/client.d.ts +7 -7
- package/dist/cjs/{fan-out-proxy → upload-relay}/client.js +9 -9
- package/dist/cjs/upload-relay/client.js.map +7 -0
- package/dist/cjs/utils/bcs.d.ts +11 -5
- package/dist/cjs/utils/bcs.js +10 -6
- package/dist/cjs/utils/bcs.js.map +2 -2
- package/dist/cjs/utils/quilts.d.ts +6 -1
- package/dist/cjs/utils/quilts.js +12 -2
- package/dist/cjs/utils/quilts.js.map +2 -2
- package/dist/esm/client.d.ts +6 -6
- package/dist/esm/client.js +29 -29
- package/dist/esm/client.js.map +2 -2
- package/dist/{cjs/read/quilt.d.ts → esm/quilt/reader.d.ts} +7 -3
- package/dist/esm/{read/quilt.js → quilt/reader.js} +31 -17
- package/dist/esm/quilt/reader.js.map +7 -0
- package/dist/esm/quilt/write.d.ts +21 -0
- package/dist/esm/quilt/write.js +156 -0
- package/dist/esm/quilt/write.js.map +7 -0
- package/dist/esm/types.d.ts +7 -7
- package/dist/esm/{fan-out-proxy → upload-relay}/client.d.ts +7 -7
- package/dist/esm/{fan-out-proxy → upload-relay}/client.js +9 -9
- package/dist/esm/upload-relay/client.js.map +7 -0
- package/dist/esm/utils/bcs.d.ts +11 -5
- package/dist/esm/utils/bcs.js +10 -6
- package/dist/esm/utils/bcs.js.map +2 -2
- package/dist/esm/utils/quilts.d.ts +6 -1
- package/dist/esm/utils/quilts.js +12 -2
- package/dist/esm/utils/quilts.js.map +2 -2
- package/dist/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/dist/cjs/fan-out-proxy/client.js.map +0 -7
- package/dist/cjs/read/quilt.js.map +0 -7
- package/dist/esm/fan-out-proxy/client.js.map +0 -7
- package/dist/esm/read/quilt.js.map +0 -7
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EncodingType } from '../types.js';
|
|
2
|
+
import type { QuiltPatchV1 } from '../utils/bcs.js';
|
|
3
|
+
interface QuiltBlob {
|
|
4
|
+
contents: Uint8Array;
|
|
5
|
+
identifier: string;
|
|
6
|
+
tags?: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
interface EncodeQuiltOptions {
|
|
9
|
+
blobs: QuiltBlob[];
|
|
10
|
+
numShards: number;
|
|
11
|
+
encodingType?: EncodingType;
|
|
12
|
+
}
|
|
13
|
+
export declare function encodeQuilt({ blobs, numShards, encodingType }: EncodeQuiltOptions): {
|
|
14
|
+
quilt: Uint8Array<ArrayBuffer>;
|
|
15
|
+
index: {
|
|
16
|
+
patches: (typeof QuiltPatchV1.$inferInput & {
|
|
17
|
+
startIndex: number;
|
|
18
|
+
})[];
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { bcs } from "@mysten/bcs";
|
|
2
|
+
import { QuiltIndexV1, QuiltPatchBlobHeader, QuiltPatchTags } from "../utils/bcs.js";
|
|
3
|
+
import { getSourceSymbols } from "../utils/index.js";
|
|
4
|
+
import {
|
|
5
|
+
BLOB_IDENTIFIER_SIZE_BYTES_LENGTH,
|
|
6
|
+
computeSymbolSize,
|
|
7
|
+
HAS_TAGS_FLAG,
|
|
8
|
+
MAX_BLOB_IDENTIFIER_BYTES_LENGTH,
|
|
9
|
+
MAX_NUM_SLIVERS_FOR_QUILT_INDEX,
|
|
10
|
+
QUILT_INDEX_PREFIX_SIZE,
|
|
11
|
+
QUILT_PATCH_BLOB_HEADER_SIZE,
|
|
12
|
+
TAGS_SIZE_BYTES_LENGTH
|
|
13
|
+
} from "../utils/quilts.js";
|
|
14
|
+
function encodeQuilt({ blobs, numShards, encodingType }) {
|
|
15
|
+
const { primarySymbols: nRows, secondarySymbols: nCols } = getSourceSymbols(
|
|
16
|
+
numShards,
|
|
17
|
+
encodingType
|
|
18
|
+
);
|
|
19
|
+
const sortedBlobs = blobs.sort((a, b) => a.identifier < b.identifier ? -1 : 1);
|
|
20
|
+
const identifiers = /* @__PURE__ */ new Set();
|
|
21
|
+
const index = {
|
|
22
|
+
patches: []
|
|
23
|
+
};
|
|
24
|
+
const tags = sortedBlobs.map(
|
|
25
|
+
(blob) => blob.tags && Object.keys(blob.tags).length > 0 ? QuiltPatchTags.serialize(blob.tags).toBytes() : null
|
|
26
|
+
);
|
|
27
|
+
for (const blob of sortedBlobs) {
|
|
28
|
+
if (identifiers.has(blob.identifier)) {
|
|
29
|
+
throw new Error(`Duplicate blob identifier: ${blob.identifier}`);
|
|
30
|
+
}
|
|
31
|
+
identifiers.add(blob.identifier);
|
|
32
|
+
index.patches.push({
|
|
33
|
+
startIndex: 0,
|
|
34
|
+
endIndex: 0,
|
|
35
|
+
identifier: blob.identifier,
|
|
36
|
+
tags: blob.tags ?? {}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
const indexSize = QUILT_INDEX_PREFIX_SIZE + QuiltIndexV1.serialize(index).toBytes().length;
|
|
40
|
+
const blobMetadata = sortedBlobs.map((blob, i) => {
|
|
41
|
+
const identifierBytes = bcs.string().serialize(blob.identifier).toBytes();
|
|
42
|
+
let metadataSize = QUILT_PATCH_BLOB_HEADER_SIZE + BLOB_IDENTIFIER_SIZE_BYTES_LENGTH + identifierBytes.length;
|
|
43
|
+
let mask = 0;
|
|
44
|
+
let offset = 0;
|
|
45
|
+
if (tags[i]) {
|
|
46
|
+
metadataSize += TAGS_SIZE_BYTES_LENGTH + tags[i].length;
|
|
47
|
+
mask |= HAS_TAGS_FLAG << 0;
|
|
48
|
+
}
|
|
49
|
+
const metadata = new Uint8Array(metadataSize);
|
|
50
|
+
const metadataView = new DataView(metadata.buffer);
|
|
51
|
+
const header = QuiltPatchBlobHeader.serialize({
|
|
52
|
+
version: 1,
|
|
53
|
+
length: metadataSize - QUILT_PATCH_BLOB_HEADER_SIZE + blob.contents.length,
|
|
54
|
+
mask
|
|
55
|
+
}).toBytes();
|
|
56
|
+
metadata.set(header, offset);
|
|
57
|
+
offset += header.length;
|
|
58
|
+
metadataView.setUint16(offset, identifierBytes.length, true);
|
|
59
|
+
offset += BLOB_IDENTIFIER_SIZE_BYTES_LENGTH;
|
|
60
|
+
metadata.set(identifierBytes, offset);
|
|
61
|
+
offset += identifierBytes.length;
|
|
62
|
+
if (tags[i]) {
|
|
63
|
+
metadataView.setUint16(offset, tags[i].length, true);
|
|
64
|
+
offset += TAGS_SIZE_BYTES_LENGTH;
|
|
65
|
+
metadata.set(tags[i], offset);
|
|
66
|
+
offset += tags[i].length;
|
|
67
|
+
}
|
|
68
|
+
return metadata;
|
|
69
|
+
});
|
|
70
|
+
const blobSizes = [
|
|
71
|
+
indexSize,
|
|
72
|
+
...sortedBlobs.map((blob, i) => {
|
|
73
|
+
if (blob.identifier.length > MAX_BLOB_IDENTIFIER_BYTES_LENGTH) {
|
|
74
|
+
throw new Error(`Blob identifier too long: ${blob.identifier}`);
|
|
75
|
+
}
|
|
76
|
+
return blobMetadata[i].length + blob.contents.length;
|
|
77
|
+
})
|
|
78
|
+
];
|
|
79
|
+
const symbolSize = computeSymbolSize(
|
|
80
|
+
blobSizes,
|
|
81
|
+
nCols,
|
|
82
|
+
nRows,
|
|
83
|
+
MAX_NUM_SLIVERS_FOR_QUILT_INDEX,
|
|
84
|
+
encodingType
|
|
85
|
+
);
|
|
86
|
+
const rowSize = symbolSize * nCols;
|
|
87
|
+
const columnSize = symbolSize * nRows;
|
|
88
|
+
const indexColumnsNeeded = Math.ceil(indexSize / columnSize);
|
|
89
|
+
if (indexColumnsNeeded > MAX_NUM_SLIVERS_FOR_QUILT_INDEX) {
|
|
90
|
+
throw new Error("Index too large");
|
|
91
|
+
}
|
|
92
|
+
const quilt = new Uint8Array(rowSize * nRows);
|
|
93
|
+
let currentColumn = indexColumnsNeeded;
|
|
94
|
+
for (let i = 0; i < sortedBlobs.length; i++) {
|
|
95
|
+
const blob = sortedBlobs[i];
|
|
96
|
+
index.patches[i].startIndex = currentColumn;
|
|
97
|
+
currentColumn += writeBlobToQuilt(
|
|
98
|
+
quilt,
|
|
99
|
+
blob.contents,
|
|
100
|
+
rowSize,
|
|
101
|
+
columnSize,
|
|
102
|
+
symbolSize,
|
|
103
|
+
currentColumn,
|
|
104
|
+
blobMetadata[i]
|
|
105
|
+
);
|
|
106
|
+
index.patches[i].endIndex = currentColumn;
|
|
107
|
+
}
|
|
108
|
+
const indexBytes = QuiltIndexV1.serialize(index).toBytes();
|
|
109
|
+
const quiltIndex = new Uint8Array(QUILT_INDEX_PREFIX_SIZE + indexBytes.length);
|
|
110
|
+
quiltIndex.set([1], 0);
|
|
111
|
+
quiltIndex.set(new Uint32Array([indexBytes.length]), 1);
|
|
112
|
+
quiltIndex.set(indexBytes, QUILT_INDEX_PREFIX_SIZE);
|
|
113
|
+
writeBlobToQuilt(quilt, quiltIndex, rowSize, columnSize, symbolSize, 0);
|
|
114
|
+
return { quilt, index };
|
|
115
|
+
}
|
|
116
|
+
function writeBlobToQuilt(quilt, blob, rowSize, columnSize, symbolSize, startColumn, prefix) {
|
|
117
|
+
const nRows = rowSize / symbolSize;
|
|
118
|
+
let bytesWritten = 0;
|
|
119
|
+
if (rowSize % symbolSize !== 0) {
|
|
120
|
+
throw new Error("Row size must be divisible by symbol size");
|
|
121
|
+
}
|
|
122
|
+
if (columnSize % symbolSize !== 0) {
|
|
123
|
+
throw new Error("Column size must be divisible by symbol size");
|
|
124
|
+
}
|
|
125
|
+
if (prefix) {
|
|
126
|
+
writeBytes(prefix);
|
|
127
|
+
}
|
|
128
|
+
writeBytes(blob);
|
|
129
|
+
return Math.ceil(bytesWritten / columnSize);
|
|
130
|
+
function writeBytes(bytes) {
|
|
131
|
+
const offset = bytesWritten;
|
|
132
|
+
const symbolsToSkip = Math.floor(offset / symbolSize);
|
|
133
|
+
let remainingOffset = offset % symbolSize;
|
|
134
|
+
let currentCol = startColumn + Math.floor(symbolsToSkip / nRows);
|
|
135
|
+
let currentRow = symbolsToSkip % nRows;
|
|
136
|
+
let index = 0;
|
|
137
|
+
while (index < bytes.length) {
|
|
138
|
+
const baseIndex = currentRow * rowSize + currentCol * symbolSize;
|
|
139
|
+
const startIndex = baseIndex + remainingOffset;
|
|
140
|
+
const len = Math.min(symbolSize - remainingOffset, bytes.length - index);
|
|
141
|
+
const subarray = bytes.subarray(index, index + len);
|
|
142
|
+
quilt.set(subarray, startIndex);
|
|
143
|
+
index += len;
|
|
144
|
+
remainingOffset = 0;
|
|
145
|
+
currentRow = (currentRow + 1) % nRows;
|
|
146
|
+
if (currentRow === 0) {
|
|
147
|
+
currentCol++;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
bytesWritten += bytes.length;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export {
|
|
154
|
+
encodeQuilt
|
|
155
|
+
};
|
|
156
|
+
//# sourceMappingURL=write.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/quilt/write.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/bcs';\nimport type { EncodingType } from '../types.js';\nimport type { QuiltPatchV1 } from '../utils/bcs.js';\nimport { QuiltIndexV1, QuiltPatchBlobHeader, QuiltPatchTags } from '../utils/bcs.js';\nimport { getSourceSymbols } from '../utils/index.js';\nimport {\n\tBLOB_IDENTIFIER_SIZE_BYTES_LENGTH,\n\tcomputeSymbolSize,\n\tHAS_TAGS_FLAG,\n\tMAX_BLOB_IDENTIFIER_BYTES_LENGTH,\n\tMAX_NUM_SLIVERS_FOR_QUILT_INDEX,\n\tQUILT_INDEX_PREFIX_SIZE,\n\tQUILT_PATCH_BLOB_HEADER_SIZE,\n\tTAGS_SIZE_BYTES_LENGTH,\n} from '../utils/quilts.js';\n\ninterface QuiltBlob {\n\tcontents: Uint8Array;\n\tidentifier: string;\n\ttags?: Record<string, string>;\n}\n\ninterface EncodeQuiltOptions {\n\tblobs: QuiltBlob[];\n\tnumShards: number;\n\tencodingType?: EncodingType;\n}\n\nexport function encodeQuilt({ blobs, numShards, encodingType }: EncodeQuiltOptions) {\n\tconst { primarySymbols: nRows, secondarySymbols: nCols } = getSourceSymbols(\n\t\tnumShards,\n\t\tencodingType,\n\t);\n\n\tconst sortedBlobs = blobs.sort((a, b) => (a.identifier < b.identifier ? -1 : 1));\n\tconst identifiers = new Set<string>();\n\tconst index = {\n\t\tpatches: [] as (typeof QuiltPatchV1.$inferInput & { startIndex: number })[],\n\t};\n\tconst tags = sortedBlobs.map((blob) =>\n\t\tblob.tags && Object.keys(blob.tags).length > 0\n\t\t\t? QuiltPatchTags.serialize(blob.tags).toBytes()\n\t\t\t: null,\n\t);\n\n\tfor (const blob of sortedBlobs) {\n\t\tif (identifiers.has(blob.identifier)) {\n\t\t\tthrow new Error(`Duplicate blob identifier: ${blob.identifier}`);\n\t\t}\n\t\tidentifiers.add(blob.identifier);\n\t\tindex.patches.push({\n\t\t\tstartIndex: 0,\n\t\t\tendIndex: 0,\n\t\t\tidentifier: blob.identifier,\n\t\t\ttags: blob.tags ?? {},\n\t\t});\n\t}\n\n\tconst indexSize = QUILT_INDEX_PREFIX_SIZE + QuiltIndexV1.serialize(index).toBytes().length;\n\tconst blobMetadata = sortedBlobs.map((blob, i) => {\n\t\tconst identifierBytes = bcs.string().serialize(blob.identifier).toBytes();\n\t\tlet metadataSize =\n\t\t\tQUILT_PATCH_BLOB_HEADER_SIZE + BLOB_IDENTIFIER_SIZE_BYTES_LENGTH + identifierBytes.length;\n\n\t\tlet mask = 0;\n\t\tlet offset = 0;\n\n\t\tif (tags[i]) {\n\t\t\tmetadataSize += TAGS_SIZE_BYTES_LENGTH + tags[i].length;\n\t\t\tmask |= HAS_TAGS_FLAG << 0;\n\t\t}\n\n\t\tconst metadata = new Uint8Array(metadataSize);\n\t\tconst metadataView = new DataView(metadata.buffer);\n\n\t\tconst header = QuiltPatchBlobHeader.serialize({\n\t\t\tversion: 1,\n\t\t\tlength: metadataSize - QUILT_PATCH_BLOB_HEADER_SIZE + blob.contents.length,\n\t\t\tmask,\n\t\t}).toBytes();\n\n\t\tmetadata.set(header, offset);\n\t\toffset += header.length;\n\n\t\tmetadataView.setUint16(offset, identifierBytes.length, true);\n\t\toffset += BLOB_IDENTIFIER_SIZE_BYTES_LENGTH;\n\t\tmetadata.set(identifierBytes, offset);\n\t\toffset += identifierBytes.length;\n\n\t\tif (tags[i]) {\n\t\t\tmetadataView.setUint16(offset, tags[i].length, true);\n\t\t\toffset += TAGS_SIZE_BYTES_LENGTH;\n\t\t\tmetadata.set(tags[i], offset);\n\t\t\toffset += tags[i].length;\n\t\t}\n\n\t\treturn metadata;\n\t});\n\n\tconst blobSizes = [\n\t\tindexSize,\n\t\t...sortedBlobs.map((blob, i) => {\n\t\t\tif (blob.identifier.length > MAX_BLOB_IDENTIFIER_BYTES_LENGTH) {\n\t\t\t\tthrow new Error(`Blob identifier too long: ${blob.identifier}`);\n\t\t\t}\n\n\t\t\treturn blobMetadata[i].length + blob.contents.length;\n\t\t}),\n\t];\n\n\tconst symbolSize = computeSymbolSize(\n\t\tblobSizes,\n\t\tnCols,\n\t\tnRows,\n\t\tMAX_NUM_SLIVERS_FOR_QUILT_INDEX,\n\t\tencodingType,\n\t);\n\n\tconst rowSize = symbolSize * nCols;\n\tconst columnSize = symbolSize * nRows;\n\tconst indexColumnsNeeded = Math.ceil(indexSize / columnSize);\n\n\tif (indexColumnsNeeded > MAX_NUM_SLIVERS_FOR_QUILT_INDEX) {\n\t\tthrow new Error('Index too large');\n\t}\n\n\tconst quilt = new Uint8Array(rowSize * nRows);\n\tlet currentColumn = indexColumnsNeeded;\n\n\tfor (let i = 0; i < sortedBlobs.length; i++) {\n\t\tconst blob = sortedBlobs[i];\n\t\tindex.patches[i].startIndex = currentColumn;\n\t\tcurrentColumn += writeBlobToQuilt(\n\t\t\tquilt,\n\t\t\tblob.contents,\n\t\t\trowSize,\n\t\t\tcolumnSize,\n\t\t\tsymbolSize,\n\t\t\tcurrentColumn,\n\t\t\tblobMetadata[i],\n\t\t);\n\t\tindex.patches[i].endIndex = currentColumn;\n\t}\n\n\tconst indexBytes = QuiltIndexV1.serialize(index).toBytes();\n\tconst quiltIndex = new Uint8Array(QUILT_INDEX_PREFIX_SIZE + indexBytes.length);\n\tquiltIndex.set([1], 0); // version\n\tquiltIndex.set(new Uint32Array([indexBytes.length]), 1);\n\tquiltIndex.set(indexBytes, QUILT_INDEX_PREFIX_SIZE);\n\n\twriteBlobToQuilt(quilt, quiltIndex, rowSize, columnSize, symbolSize, 0);\n\n\treturn { quilt, index };\n}\n\nfunction writeBlobToQuilt(\n\tquilt: Uint8Array,\n\tblob: Uint8Array,\n\trowSize: number,\n\tcolumnSize: number,\n\tsymbolSize: number,\n\tstartColumn: number,\n\tprefix?: Uint8Array,\n) {\n\tconst nRows = rowSize / symbolSize;\n\tlet bytesWritten = 0;\n\n\tif (rowSize % symbolSize !== 0) {\n\t\tthrow new Error('Row size must be divisible by symbol size');\n\t}\n\n\tif (columnSize % symbolSize !== 0) {\n\t\tthrow new Error('Column size must be divisible by symbol size');\n\t}\n\n\tif (prefix) {\n\t\twriteBytes(prefix);\n\t}\n\n\twriteBytes(blob);\n\n\treturn Math.ceil(bytesWritten / columnSize);\n\n\tfunction writeBytes(bytes: Uint8Array) {\n\t\tconst offset = bytesWritten;\n\t\tconst symbolsToSkip = Math.floor(offset / symbolSize);\n\t\tlet remainingOffset = offset % symbolSize;\n\t\tlet currentCol = startColumn + Math.floor(symbolsToSkip / nRows);\n\t\tlet currentRow = symbolsToSkip % nRows;\n\n\t\tlet index = 0;\n\t\twhile (index < bytes.length) {\n\t\t\tconst baseIndex = currentRow * rowSize + currentCol * symbolSize;\n\t\t\tconst startIndex = baseIndex + remainingOffset;\n\t\t\tconst len = Math.min(symbolSize - remainingOffset, bytes.length - index);\n\n\t\t\tconst subarray = bytes.subarray(index, index + len);\n\t\t\tquilt.set(subarray, startIndex);\n\t\t\tindex += len;\n\t\t\tremainingOffset = 0;\n\t\t\tcurrentRow = (currentRow + 1) % nRows;\n\t\t\tif (currentRow === 0) {\n\t\t\t\tcurrentCol++;\n\t\t\t}\n\t\t}\n\n\t\tbytesWritten += bytes.length;\n\t}\n}\n"],
|
|
5
|
+
"mappings": "AAGA,SAAS,WAAW;AAGpB,SAAS,cAAc,sBAAsB,sBAAsB;AACnE,SAAS,wBAAwB;AACjC;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAcA,SAAS,YAAY,EAAE,OAAO,WAAW,aAAa,GAAuB;AACnF,QAAM,EAAE,gBAAgB,OAAO,kBAAkB,MAAM,IAAI;AAAA,IAC1D;AAAA,IACA;AAAA,EACD;AAEA,QAAM,cAAc,MAAM,KAAK,CAAC,GAAG,MAAO,EAAE,aAAa,EAAE,aAAa,KAAK,CAAE;AAC/E,QAAM,cAAc,oBAAI,IAAY;AACpC,QAAM,QAAQ;AAAA,IACb,SAAS,CAAC;AAAA,EACX;AACA,QAAM,OAAO,YAAY;AAAA,IAAI,CAAC,SAC7B,KAAK,QAAQ,OAAO,KAAK,KAAK,I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AuB;AAElD,mBAAiB,OAAO,YAAY,SAAS,YAAY,YAAY,CAAC;AAEtE,SAAO,EAAE,OAAO,MAAM;AACvB;AAEA,SAAS,iBACR,OACA,MACA,SACA,YACA,YACA,aACA,QACC;AACD,QAAM,QAAQ,UAAU;AACxB,MAAI,eAAe;AAEnB,MAAI,UAAU,eAAe,GAAG;AAC/B,UAAM,IAAI,MAAM,2CAA2C;AAAA,EAC5D;AAEA,MAAI,aAAa,eAAe,GAAG;AAClC,UAAM,IAAI,MAAM,8CAA8C;AAAA,EAC/D;AAEA,MAAI,QAAQ;AACX,eAAW,MAAM;AAAA,EAClB;AAEA,aAAW,IAAI;AAEf,SAAO,KAAK,KAAK,eAAe,UAAU;AAE1C,WAAS,WAAW,OAAmB;AACtC,UAAM,SAAS;AACf,UAAM,gBAAgB,KAAK,MAAM,SAAS,UAAU;AACpD,QAAI,kBAAkB,SAAS;AAC/B,QAAI,aAAa,cAAc,KAAK,MAAM,gBAAgB,KAAK;AAC/D,QAAI,aAAa,gBAAgB;AAEjC,QAAI,QAAQ;AACZ,WAAO,QAAQ,MAAM,QAAQ;AAC5B,YAAM,YAAY,aAAa,UAAU,aAAa;AACtD,YAAM,aAAa,YAAY;AAC/B,YAAM,MAAM,KAAK,IAAI,aAAa,iBAAiB,MAAM,SAAS,KAAK;AAEvE,YAAM,WAAW,MAAM,SAAS,OAAO,QAAQ,GAAG;AAClD,YAAM,IAAI,UAAU,UAAU;AAC9B,eAAS;AACT,wBAAkB;AAClB,oBAAc,aAAa,KAAK;AAChC,UAAI,eAAe,GAAG;AACrB;AAAA,MACD;AAAA,IACD;AAEA,oBAAgB,MAAM;AAAA,EACvB;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type { StorageNodeInfo } from './contracts/walrus/storage_node.js';
|
|
|
6
6
|
import type { RequestOptions, StorageNodeClientOptions } from './storage-node/client.js';
|
|
7
7
|
import type { StorageConfirmation, StoreBlobMetadataRequestInput, StoreSliverRequestInput, Uploadable } from './storage-node/types.js';
|
|
8
8
|
import type { BlobMetadata, EncodingType } from './utils/bcs.js';
|
|
9
|
-
import type {
|
|
9
|
+
import type { UploadRelayClientOptions } from './upload-relay/client.js';
|
|
10
10
|
/**
|
|
11
11
|
* Configuration for the Walrus package on sui
|
|
12
12
|
*
|
|
@@ -41,23 +41,23 @@ export type TipStrategy = {
|
|
|
41
41
|
} | {
|
|
42
42
|
linear: {
|
|
43
43
|
base: number | bigint;
|
|
44
|
-
|
|
44
|
+
perEncodedKib: number | bigint;
|
|
45
45
|
};
|
|
46
46
|
};
|
|
47
|
-
export type
|
|
47
|
+
export type UploadRelayTipConfig = {
|
|
48
48
|
address: string;
|
|
49
49
|
max?: number;
|
|
50
50
|
kind: TipStrategy;
|
|
51
51
|
};
|
|
52
|
-
export interface
|
|
53
|
-
sendTip?: null |
|
|
52
|
+
export interface UploadRelayConfig extends UploadRelayClientOptions {
|
|
53
|
+
sendTip?: null | UploadRelayTipConfig | {
|
|
54
54
|
max: number;
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
57
|
interface BaseWalrusClientConfig {
|
|
58
58
|
storageNodeClientOptions?: StorageNodeClientOptions;
|
|
59
59
|
wasmUrl?: string;
|
|
60
|
-
|
|
60
|
+
uploadRelay?: UploadRelayConfig;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* Configuration for the Walrus client.
|
|
@@ -175,7 +175,7 @@ export type WriteEncodedBlobToNodesOptions = {
|
|
|
175
175
|
metadata: Uploadable | typeof BlobMetadata.$inferInput;
|
|
176
176
|
sliversByNode: SliversForNode[];
|
|
177
177
|
} & DeletableConfirmationOptions & WalrusClientRequestOptions;
|
|
178
|
-
export type
|
|
178
|
+
export type WriteBlobToUploadRelayOptions = {
|
|
179
179
|
blobId: string;
|
|
180
180
|
blob: Uint8Array;
|
|
181
181
|
nonce: Uint8Array;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { mergeHeaders } from '../storage-node/utils.js';
|
|
2
|
-
import type { EncodingType,
|
|
2
|
+
import type { EncodingType, UploadRelayTipConfig, ProtocolMessageCertificate, WalrusClientRequestOptions } from '../types.js';
|
|
3
3
|
export type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
4
|
-
export type
|
|
4
|
+
export type UploadRelayClientOptions = {
|
|
5
5
|
host: string;
|
|
6
6
|
/**
|
|
7
7
|
* An optional custom fetch function.
|
|
@@ -26,7 +26,7 @@ export type RequestOptions = {
|
|
|
26
26
|
timeout?: number;
|
|
27
27
|
headers?: ReturnType<typeof mergeHeaders>;
|
|
28
28
|
} & Omit<RequestInit, 'headers'>;
|
|
29
|
-
export type
|
|
29
|
+
export type WriteBlobToUploadRelayOptions = {
|
|
30
30
|
blobId: string;
|
|
31
31
|
nonce: Uint8Array;
|
|
32
32
|
txDigest: string;
|
|
@@ -36,12 +36,12 @@ export type WriteBlobToFanOutProxyOptions = {
|
|
|
36
36
|
requiresTip: boolean;
|
|
37
37
|
encodingType?: EncodingType;
|
|
38
38
|
} & WalrusClientRequestOptions;
|
|
39
|
-
export declare class
|
|
39
|
+
export declare class UploadRelayClient {
|
|
40
40
|
#private;
|
|
41
41
|
host: string;
|
|
42
|
-
constructor({ host, fetch: overriddenFetch, timeout, onError }:
|
|
43
|
-
tipConfig(): Promise<
|
|
44
|
-
writeBlob({ blobId, nonce, txDigest, blob, deletable, blobObjectId, requiresTip, encodingType, ...options }:
|
|
42
|
+
constructor({ host, fetch: overriddenFetch, timeout, onError }: UploadRelayClientOptions);
|
|
43
|
+
tipConfig(): Promise<UploadRelayTipConfig>;
|
|
44
|
+
writeBlob({ blobId, nonce, txDigest, blob, deletable, blobObjectId, requiresTip, encodingType, ...options }: WriteBlobToUploadRelayOptions): Promise<{
|
|
45
45
|
blobId: string;
|
|
46
46
|
certificate: ProtocolMessageCertificate;
|
|
47
47
|
}>;
|
|
@@ -6,13 +6,13 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
|
|
|
6
6
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
7
|
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
8
8
|
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
9
|
-
var _fetch, _timeout, _onError,
|
|
9
|
+
var _fetch, _timeout, _onError, _UploadRelayClient_instances, request_fn;
|
|
10
10
|
import { ConnectionTimeoutError, StorageNodeAPIError } from "../storage-node/error.js";
|
|
11
11
|
import { UserAbortError } from "../storage-node/error.js";
|
|
12
12
|
import { fromUrlSafeBase64, urlSafeBase64 } from "../utils/index.js";
|
|
13
|
-
class
|
|
13
|
+
class UploadRelayClient {
|
|
14
14
|
constructor({ host, fetch: overriddenFetch, timeout, onError }) {
|
|
15
|
-
__privateAdd(this,
|
|
15
|
+
__privateAdd(this, _UploadRelayClient_instances);
|
|
16
16
|
__privateAdd(this, _fetch);
|
|
17
17
|
__privateAdd(this, _timeout);
|
|
18
18
|
__privateAdd(this, _onError);
|
|
@@ -22,7 +22,7 @@ class FanOutProxyClient {
|
|
|
22
22
|
__privateSet(this, _onError, onError);
|
|
23
23
|
}
|
|
24
24
|
async tipConfig() {
|
|
25
|
-
const response = await __privateMethod(this,
|
|
25
|
+
const response = await __privateMethod(this, _UploadRelayClient_instances, request_fn).call(this, {
|
|
26
26
|
method: "GET",
|
|
27
27
|
path: "/v1/tip-config"
|
|
28
28
|
});
|
|
@@ -40,7 +40,7 @@ class FanOutProxyClient {
|
|
|
40
40
|
kind: {
|
|
41
41
|
linear: {
|
|
42
42
|
base: data.send_tip.kind.base,
|
|
43
|
-
|
|
43
|
+
perEncodedKib: data.send_tip.kind.encoded_size_mul_per_kib
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
};
|
|
@@ -69,9 +69,9 @@ class FanOutProxyClient {
|
|
|
69
69
|
if (encodingType) {
|
|
70
70
|
query.set("encoding_type", encodingType);
|
|
71
71
|
}
|
|
72
|
-
const response = await __privateMethod(this,
|
|
72
|
+
const response = await __privateMethod(this, _UploadRelayClient_instances, request_fn).call(this, {
|
|
73
73
|
method: "POST",
|
|
74
|
-
path: `/v1/blob-
|
|
74
|
+
path: `/v1/blob-upload-relay?${query.toString()}`,
|
|
75
75
|
body: blob,
|
|
76
76
|
...options
|
|
77
77
|
});
|
|
@@ -89,7 +89,7 @@ class FanOutProxyClient {
|
|
|
89
89
|
_fetch = new WeakMap();
|
|
90
90
|
_timeout = new WeakMap();
|
|
91
91
|
_onError = new WeakMap();
|
|
92
|
-
|
|
92
|
+
_UploadRelayClient_instances = new WeakSet();
|
|
93
93
|
request_fn = async function(options) {
|
|
94
94
|
var _a, _b, _c;
|
|
95
95
|
const { signal, timeout, ...init } = options;
|
|
@@ -133,6 +133,6 @@ function safeParseJSON(value) {
|
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
135
|
export {
|
|
136
|
-
|
|
136
|
+
UploadRelayClient
|
|
137
137
|
};
|
|
138
138
|
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/upload-relay/client.ts"],
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { ConnectionTimeoutError, StorageNodeAPIError } from '../storage-node/error.js';\nimport { UserAbortError } from '../storage-node/error.js';\nimport type { mergeHeaders } from '../storage-node/utils.js';\nimport type {\n\tEncodingType,\n\tUploadRelayTipConfig,\n\tProtocolMessageCertificate,\n\tWalrusClientRequestOptions,\n} from '../types.js';\nimport { fromUrlSafeBase64, urlSafeBase64 } from '../utils/index.js';\n\nexport type Fetch = (url: RequestInfo, init?: RequestInit) => Promise<Response>;\n\nexport type UploadRelayClientOptions = {\n\thost: string;\n\t/**\n\t * An optional custom fetch function.\n\t *\n\t * If not provided, defaults to the global `fetch` function (`globalThis.fetch`).\n\t *\n\t * @default globalThis.fetch\n\t */\n\tfetch?: Fetch;\n\n\t/**\n\t * An optional timeout for requests.\n\t * @default 30_000ms (30 seconds)\n\t */\n\ttimeout?: number;\n\n\t/**\n\t * Callback for individual network errors.\n\t */\n\tonError?: (error: Error) => void;\n};\n\nexport type RequestOptions = {\n\tpath: string;\n\ttimeout?: number;\n\theaders?: ReturnType<typeof mergeHeaders>;\n} & Omit<RequestInit, 'headers'>;\n\nexport type WriteBlobToUploadRelayOptions = {\n\tblobId: string;\n\tnonce: Uint8Array;\n\ttxDigest: string;\n\tblob: Uint8Array;\n\tblobObjectId: string;\n\tdeletable: boolean;\n\trequiresTip: boolean;\n\tencodingType?: EncodingType;\n} & WalrusClientRequestOptions;\n\nexport class UploadRelayClient {\n\thost: string;\n\t#fetch: Fetch;\n\t#timeout: number;\n\t#onError?: (error: Error) => void;\n\tconstructor({ host, fetch: overriddenFetch, timeout, onError }: UploadRelayClientOptions) {\n\t\tthis.host = host;\n\t\tthis.#fetch = overriddenFetch ?? globalThis.fetch;\n\t\tthis.#timeout = timeout ?? 30_000;\n\t\tthis.#onError = onError;\n\t}\n\n\tasync tipConfig(): Promise<UploadRelayTipConfig> {\n\t\tconst response = await this.#request({\n\t\t\tmethod: 'GET',\n\t\t\tpath: '/v1/tip-config',\n\t\t});\n\n\t\tconst data = (await response.json()) as {\n\t\t\tsend_tip: {\n\t\t\t\taddress: string;\n\t\t\t\tkind:\n\t\t\t\t\t| {\n\t\t\t\t\t\t\tconst: number;\n\t\t\t\t\t }\n\t\t\t\t\t| {\n\t\t\t\t\t\t\tbase: number;\n\t\t\t\t\t\t\tencoded_size_mul_per_kib: number;\n\t\t\t\t\t };\n\t\t\t};\n\t\t};\n\n\t\tif ('const' in data.send_tip.kind) {\n\t\t\treturn {\n\t\t\t\taddress: data.send_tip.address,\n\t\t\t\tkind: {\n\t\t\t\t\tconst: data.send_tip.kind.const,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\treturn {\n\t\t\taddress: data.send_tip.address,\n\t\t\tkind: {\n\t\t\t\tlinear: {\n\t\t\t\t\tbase: data.send_tip.kind.base,\n\t\t\t\t\tperEncodedKib: data.send_tip.kind.encoded_size_mul_per_kib,\n\t\t\t\t},\n\t\t\t},\n\t\t};\n\t}\n\n\tasync writeBlob({\n\t\tblobId,\n\t\tnonce,\n\t\ttxDigest,\n\t\tblob,\n\t\tdeletable,\n\t\tblobObjectId,\n\t\trequiresTip,\n\t\tencodingType,\n\t\t...options\n\t}: WriteBlobToUploadRelayOptions): Promise<{\n\t\tblobId: string;\n\t\tcertificate: ProtocolMessageCertificate;\n\t}> {\n\t\tconst query = new URLSearchParams({\n\t\t\tblob_id: blobId,\n\t\t});\n\n\t\tif (requiresTip) {\n\t\t\tquery.set('nonce', urlSafeBase64(nonce));\n\t\t\tquery.set('tx_id', txDigest);\n\t\t}\n\n\t\tif (deletable) {\n\t\t\tquery.set('deletable_blob_object', blobObjectId);\n\t\t}\n\n\t\tif (encodingType) {\n\t\t\tquery.set('encoding_type', encodingType);\n\t\t}\n\n\t\tconst response = await this.#request({\n\t\t\tmethod: 'POST',\n\t\t\tpath: `/v1/blob-upload-relay?${query.toString()}`,\n\t\t\tbody: blob,\n\t\t\t...options,\n\t\t});\n\n\t\tconst data: {\n\t\t\tblob_id: number[];\n\t\t\tconfirmation_certificate: {\n\t\t\t\tsigners: number[];\n\t\t\t\tserialized_message: number[];\n\t\t\t\tsignature: string;\n\t\t\t};\n\t\t} = await response.json();\n\n\t\treturn {\n\t\t\tblobId,\n\t\t\tcertificate: {\n\t\t\t\tsigners: data.confirmation_certificate.signers,\n\t\t\t\tserializedMessage: new Uint8Array(data.confirmation_certificate.serialized_message),\n\t\t\t\tsignature: fromUrlSafeBase64(data.confirmation_certificate.signature),\n\t\t\t},\n\t\t};\n\t}\n\n\tasync #request(options: RequestOptions) {\n\t\tconst { signal, timeout, ...init } = options;\n\n\t\tif (signal?.aborted) {\n\t\t\tthrow new UserAbortError();\n\t\t}\n\n\t\tconst timeoutSignal = AbortSignal.timeout(timeout ?? this.#timeout);\n\n\t\tlet response: Response | undefined;\n\n\t\ttry {\n\t\t\tresponse = await (0, this.#fetch)(`${this.host}${options.path}`, {\n\t\t\t\t...init,\n\t\t\t\tsignal: signal ? AbortSignal.any([timeoutSignal, signal]) : timeoutSignal,\n\t\t\t});\n\t\t} catch (error) {\n\t\t\tif (signal?.aborted) {\n\t\t\t\tthrow new UserAbortError();\n\t\t\t}\n\n\t\t\tif (error instanceof Error && error.name === 'AbortError') {\n\t\t\t\tconst error = new ConnectionTimeoutError();\n\t\t\t\tthis.#onError?.(error);\n\t\t\t\tthrow error;\n\t\t\t}\n\n\t\t\tthis.#onError?.(error as Error);\n\n\t\t\tthrow error;\n\t\t}\n\n\t\tif (!response.ok) {\n\t\t\tconst errorText = await response.text().catch((reason) => reason);\n\t\t\tconst errorJSON = safeParseJSON(errorText);\n\t\t\tconst errorMessage = errorJSON ? undefined : errorText;\n\t\t\tconst error = StorageNodeAPIError.generate(response.status, errorJSON, errorMessage);\n\t\t\tthis.#onError?.(error);\n\t\t\tthrow error;\n\t\t}\n\n\t\treturn response;\n\t}\n}\n\nfunction safeParseJSON(value: string) {\n\ttry {\n\t\treturn JSON.parse(value);\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;AAAA;AAGA,SAAS,wBAAwB,2BAA2B;AAC5D,SAAS,sBAAsB;AAQ/B,SAAS,mBAAmB,qBAAqB;AA4C1C,MAAM,kBAAkB;AAAA,EAK9B,YAAY,EAAE,MAAM,OAAO,iBAAiB,SAAS,QAAQ,GAA6B;AALpF;AAEN;AACA;AACA;AAEC,SAAK,OAAO;AACZ,uBAAK,QAAS,mBAAmB,WAAW;AAC5C,uBAAK,UAAW,WAAW;AAC3B,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O;AAAA,EACR;AACD;",
|
|
6
|
+
"names": ["error"]
|
|
7
|
+
}
|
package/dist/esm/utils/bcs.d.ts
CHANGED
|
@@ -298,27 +298,33 @@ export declare function Field<T0 extends BcsType<any>, T1 extends BcsType<any>>(
|
|
|
298
298
|
name: T0 extends BcsType<any, infer U_1> ? U_1 : never;
|
|
299
299
|
value: T1 extends BcsType<any, infer U_1> ? U_1 : never;
|
|
300
300
|
}>;
|
|
301
|
-
export declare const QuiltPatchTags: BcsType<
|
|
301
|
+
export declare const QuiltPatchTags: BcsType<{
|
|
302
|
+
[k: string]: string;
|
|
303
|
+
}, Map<string, string> | Record<string, string>>;
|
|
302
304
|
export declare const QuiltPatchV1: BcsType<{
|
|
303
305
|
endIndex: number;
|
|
304
306
|
identifier: string;
|
|
305
|
-
tags:
|
|
307
|
+
tags: {
|
|
308
|
+
[k: string]: string;
|
|
309
|
+
};
|
|
306
310
|
}, {
|
|
307
311
|
endIndex: number;
|
|
308
312
|
identifier: string;
|
|
309
|
-
tags: Map<string, string>;
|
|
313
|
+
tags: Map<string, string> | Record<string, string>;
|
|
310
314
|
}>;
|
|
311
315
|
export declare const QuiltIndexV1: BcsType<{
|
|
312
316
|
patches: {
|
|
313
317
|
endIndex: number;
|
|
314
318
|
identifier: string;
|
|
315
|
-
tags:
|
|
319
|
+
tags: {
|
|
320
|
+
[k: string]: string;
|
|
321
|
+
};
|
|
316
322
|
}[];
|
|
317
323
|
}, {
|
|
318
324
|
patches: Iterable<{
|
|
319
325
|
endIndex: number;
|
|
320
326
|
identifier: string;
|
|
321
|
-
tags: Map<string, string>;
|
|
327
|
+
tags: Map<string, string> | Record<string, string>;
|
|
322
328
|
}> & {
|
|
323
329
|
length: number;
|
|
324
330
|
};
|
package/dist/esm/utils/bcs.js
CHANGED
|
@@ -105,10 +105,14 @@ function Field(...typeParameters) {
|
|
|
105
105
|
const QuiltPatchTags = bcs.map(bcs.string(), bcs.string()).transform({
|
|
106
106
|
// tags is a BTreeMap, so we need to sort entries before serializing
|
|
107
107
|
input: (tags) => new Map(
|
|
108
|
-
[...tags.entries()].sort(
|
|
109
|
-
([a], [b]) =>
|
|
108
|
+
[...tags instanceof Map ? tags : Object.entries(tags)].sort(
|
|
109
|
+
([a], [b]) => (
|
|
110
|
+
// TODO: sorting for map keys should be moved into @mysten/bcs
|
|
111
|
+
compareBcsBytes(bcs.string().serialize(a).toBytes(), bcs.string().serialize(b).toBytes())
|
|
112
|
+
)
|
|
110
113
|
)
|
|
111
|
-
)
|
|
114
|
+
),
|
|
115
|
+
output: (tags) => Object.fromEntries(tags)
|
|
112
116
|
});
|
|
113
117
|
const QuiltPatchV1 = bcs.struct("QuiltPatchV1", {
|
|
114
118
|
endIndex: bcs.u16(),
|
|
@@ -116,10 +120,10 @@ const QuiltPatchV1 = bcs.struct("QuiltPatchV1", {
|
|
|
116
120
|
tags: QuiltPatchTags
|
|
117
121
|
});
|
|
118
122
|
function compareBcsBytes(a, b) {
|
|
123
|
+
if (a.length !== b.length) {
|
|
124
|
+
return a.length - b.length;
|
|
125
|
+
}
|
|
119
126
|
for (let i = 0; i < a.length; i++) {
|
|
120
|
-
if (i >= b.length) {
|
|
121
|
-
return 1;
|
|
122
|
-
}
|
|
123
127
|
if (a[i] !== b[i]) {
|
|
124
128
|
return a[i] - b[i];
|
|
125
129
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/utils/bcs.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsType } from '@mysten/sui/bcs';\nimport { bcs } from '@mysten/sui/bcs';\n\nconst MerkleNode = bcs.enum('MerkleNode', {\n\tEmpty: null,\n\tDigest: bcs.bytes(32),\n});\n\nconst SliverPairMetadata = bcs.struct('SliverPairMetadata', {\n\tprimary_hash: MerkleNode,\n\tsecondary_hash: MerkleNode,\n});\n\nexport const EncodingType = bcs\n\t.enum('EncodingType', {\n\t\tRedStuff: null,\n\t\tRS2: null,\n\t})\n\t.transform({\n\t\tinput: (\n\t\t\tencodingType:\n\t\t\t\t| { RedStuff: boolean | object | null }\n\t\t\t\t| { RS2: boolean | object | null }\n\t\t\t\t| 'RedStuff'\n\t\t\t\t| 'RS2',\n\t\t) =>\n\t\t\ttypeof encodingType === 'string'\n\t\t\t\t? ({ [encodingType]: null } as Exclude<typeof encodingType, string>)\n\t\t\t\t: encodingType,\n\t\toutput: (encodingType) => encodingType,\n\t});\n\nexport const BlobMetadataV1 = bcs.struct('BlobMetadataV1', {\n\tencoding_type: EncodingType,\n\tunencoded_length: bcs.u64(),\n\thashes: bcs.vector(SliverPairMetadata),\n});\n\nexport const BlobMetadata = bcs.enum('BlobMetadata', {\n\tV1: BlobMetadataV1,\n});\n\nexport const BlobId = bcs.u256().transform({\n\tinput: (blobId: string | bigint) => (typeof blobId === 'string' ? blobIdToInt(blobId) : blobId),\n\toutput: (id: string) => blobIdFromInt(id),\n});\n\nexport function blobIdFromInt(blobId: bigint | string): string {\n\treturn bcs\n\t\t.u256()\n\t\t.serialize(blobId)\n\t\t.toBase64()\n\t\t.replace(/=*$/, '')\n\t\t.replaceAll('+', '-')\n\t\t.replaceAll('/', '_');\n}\n\nexport function blobIdFromBytes(blobId: Uint8Array): string {\n\treturn blobIdFromInt(bcs.u256().parse(blobId));\n}\n\nexport function blobIdToInt(blobId: string): bigint {\n\treturn BigInt(bcs.u256().fromBase64(blobId.replaceAll('-', '+').replaceAll('_', '/')));\n}\n\nexport const BlobMetadataWithId = bcs.struct('BlobMetadataWithId', {\n\tblobId: BlobId,\n\tmetadata: BlobMetadata,\n});\n\nconst Symbols = bcs.struct('Symbols', {\n\tdata: bcs.vector(bcs.u8()),\n\tsymbol_size: bcs.u16(),\n});\n\nexport const SliverData = bcs.struct('SliverData', {\n\tsymbols: Symbols,\n\tindex: bcs.u16(),\n});\n\nexport const Sliver = bcs.enum('Sliver', {\n\tPrimary: SliverData,\n\tSecondary: SliverData,\n});\n\nexport const SliverPair = bcs.struct('SliverPair', {\n\tprimary: SliverData,\n\tsecondary: SliverData,\n});\n\nexport enum IntentType {\n\tPROOF_OF_POSSESSION_MSG = 0,\n\tBLOB_CERT_MSG = 1,\n\tINVALID_BLOB_ID_MSG = 2,\n\tSYNC_SHARD_MSG = 3,\n}\n\nexport const Intent = bcs\n\t.struct('Intent', {\n\t\ttype: bcs.u8().transform({\n\t\t\tinput: (type: IntentType) => type,\n\t\t\toutput: (type: number) => type as IntentType,\n\t\t}),\n\t\tversion: bcs.u8(),\n\t\tappId: bcs.u8(),\n\t})\n\t.transform({\n\t\tinput: (intent: IntentType) => ({\n\t\t\ttype: intent,\n\t\t\tversion: 0,\n\t\t\tappId: 3,\n\t\t}),\n\t\toutput: (intent) => intent.type,\n\t});\n\nexport function ProtocolMessage<T extends BcsType<any>>(messageContents: T) {\n\treturn bcs.struct(`ProtocolMessage<${messageContents.name}>`, {\n\t\tintent: Intent,\n\t\tepoch: bcs.u32(),\n\t\tmessageContents,\n\t});\n}\n\nexport const BlobPersistenceType = bcs.enum('BlobPersistenceType', {\n\tPermanent: null,\n\tDeletable: bcs.struct('Deletable', {\n\t\tobjectId: bcs.Address,\n\t}),\n});\nexport const StorageConfirmationBody = bcs.struct('StorageConfirmationBody', {\n\tblobId: BlobId,\n\tblobType: BlobPersistenceType,\n});\n\nexport const StorageConfirmation = ProtocolMessage(StorageConfirmationBody);\n\nexport function Field<T0 extends BcsType<any>, T1 extends BcsType<any>>(\n\t...typeParameters: [T0, T1]\n) {\n\treturn bcs.struct('Field', {\n\t\tid: bcs.Address,\n\t\tname: typeParameters[0],\n\t\tvalue: typeParameters[1],\n\t});\n}\n\nexport const QuiltPatchTags = bcs.map(bcs.string(), bcs.string()).transform({\n\t// tags is a BTreeMap, so we need to sort entries before serializing\n\tinput: (tags: Map<string, string>) =>\n\t\tnew Map(\n\t\t\t[...tags.entries()].sort(([a], [b]) =>\n\t\t\t\tcompareBcsBytes(bcs.string().serialize(a).toBytes(), bcs.string().serialize(b).toBytes()),\n\t\t\t),\n\t\t),\n});\n\nexport const QuiltPatchV1 = bcs.struct('QuiltPatchV1', {\n\tendIndex: bcs.u16(),\n\tidentifier: bcs.string(),\n\ttags: QuiltPatchTags,\n});\n\nfunction compareBcsBytes(a: Uint8Array, b: Uint8Array) {\n\
|
|
5
|
-
"mappings": "AAIA,SAAS,WAAW;AAEpB,MAAM,aAAa,IAAI,KAAK,cAAc;AAAA,EACzC,OAAO;AAAA,EACP,QAAQ,IAAI,MAAM,EAAE;AACrB,CAAC;AAED,MAAM,qBAAqB,IAAI,OAAO,sBAAsB;AAAA,EAC3D,cAAc;AAAA,EACd,gBAAgB;AACjB,CAAC;AAEM,MAAM,eAAe,IAC1B,KAAK,gBAAgB;AAAA,EACrB,UAAU;AAAA,EACV,KAAK;AACN,CAAC,EACA,UAAU;AAAA,EACV,OAAO,CACN,iBAMA,OAAO,iBAAiB,WACpB,EAAE,CAAC,YAAY,GAAG,KAAK,IACxB;AAAA,EACJ,QAAQ,CAAC,iBAAiB;AAC3B,CAAC;AAEK,MAAM,iBAAiB,IAAI,OAAO,kBAAkB;AAAA,EAC1D,eAAe;AAAA,EACf,kBAAkB,IAAI,IAAI;AAAA,EAC1B,QAAQ,IAAI,OAAO,kBAAkB;AACtC,CAAC;AAEM,MAAM,eAAe,IAAI,KAAK,gBAAgB;AAAA,EACpD,IAAI;AACL,CAAC;AAEM,MAAM,SAAS,IAAI,KAAK,EAAE,UAAU;AAAA,EAC1C,OAAO,CAAC,WAA6B,OAAO,WAAW,WAAW,YAAY,MAAM,IAAI;AAAA,EACxF,QAAQ,CAAC,OAAe,cAAc,EAAE;AACzC,CAAC;AAEM,SAAS,cAAc,QAAiC;AAC9D,SAAO,IACL,KAAK,EACL,UAAU,MAAM,EAChB,SAAS,EACT,QAAQ,OAAO,EAAE,EACjB,WAAW,KAAK,GAAG,EACnB,WAAW,KAAK,GAAG;AACtB;AAE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AA,EACA,QAAQ,CAAC,WAAW,OAAO;AAC5B,CAAC;AAEK,SAAS,gBAAwC,iBAAoB;AAC3E,SAAO,IAAI,OAAO,mBAAmB,gBAAgB,IAAI,KAAK;AAAA,IAC7D,QAAQ;AAAA,IACR,OAAO,IAAI,IAAI;AAAA,IACf;AAAA,EACD,CAAC;AACF;AAEO,MAAM,sBAAsB,IAAI,KAAK,uBAAuB;AAAA,EAClE,WAAW;AAAA,EACX,WAAW,IAAI,OAAO,aAAa;AAAA,IAClC,UAAU,IAAI;AAAA,EACf,CAAC;AACF,CAAC;AACM,MAAM,0BAA0B,IAAI,OAAO,2BAA2B;AAAA,EAC5E,QAAQ;AAAA,EACR,UAAU;AACX,CAAC;AAEM,MAAM,sBAAsB,gBAAgB,uBAAuB;AAEnE,SAAS,SACZ,gBACF;AACD,SAAO,IAAI,OAAO,SAAS;AAAA,IAC1B,IAAI,IAAI;AAAA,IACR,MAAM,eAAe,CAAC;AAAA,IACtB,OAAO,eAAe,CAAC;AAAA,EACxB,CAAC;AACF;AAEO,MAAM,iBAAiB,IAAI,IAAI,IAAI,OAAO,GAAG,IAAI,OAAO,CAAC,EAAE,UAAU;AAAA;AAAA,EAE3E,OAAO,CAAC,SACP,IAAI;AAAA,IACH,CAAC,
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { BcsType } from '@mysten/sui/bcs';\nimport { bcs } from '@mysten/sui/bcs';\n\nconst MerkleNode = bcs.enum('MerkleNode', {\n\tEmpty: null,\n\tDigest: bcs.bytes(32),\n});\n\nconst SliverPairMetadata = bcs.struct('SliverPairMetadata', {\n\tprimary_hash: MerkleNode,\n\tsecondary_hash: MerkleNode,\n});\n\nexport const EncodingType = bcs\n\t.enum('EncodingType', {\n\t\tRedStuff: null,\n\t\tRS2: null,\n\t})\n\t.transform({\n\t\tinput: (\n\t\t\tencodingType:\n\t\t\t\t| { RedStuff: boolean | object | null }\n\t\t\t\t| { RS2: boolean | object | null }\n\t\t\t\t| 'RedStuff'\n\t\t\t\t| 'RS2',\n\t\t) =>\n\t\t\ttypeof encodingType === 'string'\n\t\t\t\t? ({ [encodingType]: null } as Exclude<typeof encodingType, string>)\n\t\t\t\t: encodingType,\n\t\toutput: (encodingType) => encodingType,\n\t});\n\nexport const BlobMetadataV1 = bcs.struct('BlobMetadataV1', {\n\tencoding_type: EncodingType,\n\tunencoded_length: bcs.u64(),\n\thashes: bcs.vector(SliverPairMetadata),\n});\n\nexport const BlobMetadata = bcs.enum('BlobMetadata', {\n\tV1: BlobMetadataV1,\n});\n\nexport const BlobId = bcs.u256().transform({\n\tinput: (blobId: string | bigint) => (typeof blobId === 'string' ? blobIdToInt(blobId) : blobId),\n\toutput: (id: string) => blobIdFromInt(id),\n});\n\nexport function blobIdFromInt(blobId: bigint | string): string {\n\treturn bcs\n\t\t.u256()\n\t\t.serialize(blobId)\n\t\t.toBase64()\n\t\t.replace(/=*$/, '')\n\t\t.replaceAll('+', '-')\n\t\t.replaceAll('/', '_');\n}\n\nexport function blobIdFromBytes(blobId: Uint8Array): string {\n\treturn blobIdFromInt(bcs.u256().parse(blobId));\n}\n\nexport function blobIdToInt(blobId: string): bigint {\n\treturn BigInt(bcs.u256().fromBase64(blobId.replaceAll('-', '+').replaceAll('_', '/')));\n}\n\nexport const BlobMetadataWithId = bcs.struct('BlobMetadataWithId', {\n\tblobId: BlobId,\n\tmetadata: BlobMetadata,\n});\n\nconst Symbols = bcs.struct('Symbols', {\n\tdata: bcs.vector(bcs.u8()),\n\tsymbol_size: bcs.u16(),\n});\n\nexport const SliverData = bcs.struct('SliverData', {\n\tsymbols: Symbols,\n\tindex: bcs.u16(),\n});\n\nexport const Sliver = bcs.enum('Sliver', {\n\tPrimary: SliverData,\n\tSecondary: SliverData,\n});\n\nexport const SliverPair = bcs.struct('SliverPair', {\n\tprimary: SliverData,\n\tsecondary: SliverData,\n});\n\nexport enum IntentType {\n\tPROOF_OF_POSSESSION_MSG = 0,\n\tBLOB_CERT_MSG = 1,\n\tINVALID_BLOB_ID_MSG = 2,\n\tSYNC_SHARD_MSG = 3,\n}\n\nexport const Intent = bcs\n\t.struct('Intent', {\n\t\ttype: bcs.u8().transform({\n\t\t\tinput: (type: IntentType) => type,\n\t\t\toutput: (type: number) => type as IntentType,\n\t\t}),\n\t\tversion: bcs.u8(),\n\t\tappId: bcs.u8(),\n\t})\n\t.transform({\n\t\tinput: (intent: IntentType) => ({\n\t\t\ttype: intent,\n\t\t\tversion: 0,\n\t\t\tappId: 3,\n\t\t}),\n\t\toutput: (intent) => intent.type,\n\t});\n\nexport function ProtocolMessage<T extends BcsType<any>>(messageContents: T) {\n\treturn bcs.struct(`ProtocolMessage<${messageContents.name}>`, {\n\t\tintent: Intent,\n\t\tepoch: bcs.u32(),\n\t\tmessageContents,\n\t});\n}\n\nexport const BlobPersistenceType = bcs.enum('BlobPersistenceType', {\n\tPermanent: null,\n\tDeletable: bcs.struct('Deletable', {\n\t\tobjectId: bcs.Address,\n\t}),\n});\nexport const StorageConfirmationBody = bcs.struct('StorageConfirmationBody', {\n\tblobId: BlobId,\n\tblobType: BlobPersistenceType,\n});\n\nexport const StorageConfirmation = ProtocolMessage(StorageConfirmationBody);\n\nexport function Field<T0 extends BcsType<any>, T1 extends BcsType<any>>(\n\t...typeParameters: [T0, T1]\n) {\n\treturn bcs.struct('Field', {\n\t\tid: bcs.Address,\n\t\tname: typeParameters[0],\n\t\tvalue: typeParameters[1],\n\t});\n}\n\nexport const QuiltPatchTags = bcs.map(bcs.string(), bcs.string()).transform({\n\t// tags is a BTreeMap, so we need to sort entries before serializing\n\tinput: (tags: Record<string, string> | Map<string, string>) =>\n\t\tnew Map(\n\t\t\t[...(tags instanceof Map ? tags : Object.entries(tags))].sort(([a], [b]) =>\n\t\t\t\t// TODO: sorting for map keys should be moved into @mysten/bcs\n\t\t\t\tcompareBcsBytes(bcs.string().serialize(a).toBytes(), bcs.string().serialize(b).toBytes()),\n\t\t\t),\n\t\t),\n\toutput: (tags: Map<string, string>) => Object.fromEntries(tags),\n});\n\nexport const QuiltPatchV1 = bcs.struct('QuiltPatchV1', {\n\tendIndex: bcs.u16(),\n\tidentifier: bcs.string(),\n\ttags: QuiltPatchTags,\n});\n\nfunction compareBcsBytes(a: Uint8Array, b: Uint8Array) {\n\t// sort by length first, because bcs bytes prefix length\n\tif (a.length !== b.length) {\n\t\treturn a.length - b.length;\n\t}\n\n\tfor (let i = 0; i < a.length; i++) {\n\t\tif (a[i] !== b[i]) {\n\t\t\treturn a[i] - b[i];\n\t\t}\n\t}\n\n\treturn 0;\n}\n\nexport const QuiltIndexV1 = bcs.struct('QuiltIndexV1', {\n\tpatches: bcs.vector(QuiltPatchV1),\n});\n\nexport const QuiltPatchId = bcs.struct('QuiltPatchId', {\n\tquiltId: BlobId,\n\tpatchId: bcs.struct('InternalQuiltPatchId', {\n\t\tversion: bcs.u8(),\n\t\tstartIndex: bcs.u16(),\n\t\tendIndex: bcs.u16(),\n\t}),\n});\n\nexport const QuiltPatchBlobHeader = bcs.struct('QuiltPatchBlobHeader', {\n\tversion: bcs.u8(),\n\tlength: bcs.u32(),\n\tmask: bcs.u8(),\n});\n"],
|
|
5
|
+
"mappings": "AAIA,SAAS,WAAW;AAEpB,MAAM,aAAa,IAAI,KAAK,cAAc;AAAA,EACzC,OAAO;AAAA,EACP,QAAQ,IAAI,MAAM,EAAE;AACrB,CAAC;AAED,MAAM,qBAAqB,IAAI,OAAO,sBAAsB;AAAA,EAC3D,cAAc;AAAA,EACd,gBAAgB;AACjB,CAAC;AAEM,MAAM,eAAe,IAC1B,KAAK,gBAAgB;AAAA,EACrB,UAAU;AAAA,EACV,KAAK;AACN,CAAC,EACA,UAAU;AAAA,EACV,OAAO,CACN,iBAMA,OAAO,iBAAiB,WACpB,EAAE,CAAC,YAAY,GAAG,KAAK,IACxB;AAAA,EACJ,QAAQ,CAAC,iBAAiB;AAC3B,CAAC;AAEK,MAAM,iBAAiB,IAAI,O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AAG;AACd,CAAC;",
|
|
6
6
|
"names": ["IntentType"]
|
|
7
7
|
}
|
|
@@ -3,6 +3,11 @@ export declare const QUILT_INDEX_SIZE_BYTES_LENGTH = 4;
|
|
|
3
3
|
export declare const QUILT_VERSION_BYTES_LENGTH = 1;
|
|
4
4
|
export declare const QUILT_INDEX_PREFIX_SIZE: number;
|
|
5
5
|
export declare const QUILT_PATCH_BLOB_HEADER_SIZE: number;
|
|
6
|
+
export declare const BLOB_IDENTIFIER_SIZE_BYTES_LENGTH = 2;
|
|
7
|
+
export declare const TAGS_SIZE_BYTES_LENGTH = 2;
|
|
8
|
+
export declare const MAX_BLOB_IDENTIFIER_BYTES_LENGTH: number;
|
|
9
|
+
export declare const MAX_NUM_SLIVERS_FOR_QUILT_INDEX = 10;
|
|
10
|
+
export declare const HAS_TAGS_FLAG: number;
|
|
6
11
|
/**
|
|
7
12
|
* Finds the minimum symbol size needed to store blobs in a fixed number of columns.
|
|
8
13
|
* Each blob must be stored in consecutive columns exclusively.
|
|
@@ -22,7 +27,7 @@ export declare const QUILT_PATCH_BLOB_HEADER_SIZE: number;
|
|
|
22
27
|
*
|
|
23
28
|
* @returns The minimum symbol size needed.
|
|
24
29
|
**/
|
|
25
|
-
export declare function computeSymbolSize(blobsSizes: number[], nColumns: number, nRows: number, maxNumColumnsForQuiltIndex: number, encodingType
|
|
30
|
+
export declare function computeSymbolSize(blobsSizes: number[], nColumns: number, nRows: number, maxNumColumnsForQuiltIndex: number, encodingType?: EncodingType): number;
|
|
26
31
|
export declare function parseQuiltPatchId(id: string): {
|
|
27
32
|
quiltId: string;
|
|
28
33
|
patchId: {
|
package/dist/esm/utils/quilts.js
CHANGED
|
@@ -4,6 +4,11 @@ const QUILT_INDEX_SIZE_BYTES_LENGTH = 4;
|
|
|
4
4
|
const QUILT_VERSION_BYTES_LENGTH = 1;
|
|
5
5
|
const QUILT_INDEX_PREFIX_SIZE = QUILT_VERSION_BYTES_LENGTH + QUILT_INDEX_SIZE_BYTES_LENGTH;
|
|
6
6
|
const QUILT_PATCH_BLOB_HEADER_SIZE = 1 + 4 + 1;
|
|
7
|
+
const BLOB_IDENTIFIER_SIZE_BYTES_LENGTH = 2;
|
|
8
|
+
const TAGS_SIZE_BYTES_LENGTH = 2;
|
|
9
|
+
const MAX_BLOB_IDENTIFIER_BYTES_LENGTH = (1 << 8 * BLOB_IDENTIFIER_SIZE_BYTES_LENGTH) - 1;
|
|
10
|
+
const MAX_NUM_SLIVERS_FOR_QUILT_INDEX = 10;
|
|
11
|
+
const HAS_TAGS_FLAG = 1 << 0;
|
|
7
12
|
const REQUIRED_ALIGNMENT_BY_ENCODING_TYPE = {
|
|
8
13
|
RS2: 2,
|
|
9
14
|
RedStuff: 2
|
|
@@ -12,7 +17,7 @@ const MAX_SYMBOL_SIZE_BY_ENCODING_TYPE = {
|
|
|
12
17
|
RS2: 2 ** 16 - 1,
|
|
13
18
|
RedStuff: 2 ** 16 - 1
|
|
14
19
|
};
|
|
15
|
-
function computeSymbolSize(blobsSizes, nColumns, nRows, maxNumColumnsForQuiltIndex, encodingType) {
|
|
20
|
+
function computeSymbolSize(blobsSizes, nColumns, nRows, maxNumColumnsForQuiltIndex, encodingType = "RS2") {
|
|
16
21
|
if (blobsSizes.length > nColumns) {
|
|
17
22
|
throw new Error("Too many blobs, the number of blobs must be less than the number of columns");
|
|
18
23
|
}
|
|
@@ -33,7 +38,7 @@ function computeSymbolSize(blobsSizes, nColumns, nRows, maxNumColumnsForQuiltInd
|
|
|
33
38
|
minVal = mid + 1;
|
|
34
39
|
}
|
|
35
40
|
}
|
|
36
|
-
const symbolSize = Math.ceil(minVal / REQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType]);
|
|
41
|
+
const symbolSize = Math.ceil(minVal / REQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType]) * REQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType];
|
|
37
42
|
if (!canBlobsFitIntoMatrix(blobsSizes, nColumns, symbolSize * nRows)) {
|
|
38
43
|
throw new Error("Quilt oversize");
|
|
39
44
|
}
|
|
@@ -51,10 +56,15 @@ function parseQuiltPatchId(id) {
|
|
|
51
56
|
return QuiltPatchId.parse(fromUrlSafeBase64(id));
|
|
52
57
|
}
|
|
53
58
|
export {
|
|
59
|
+
BLOB_IDENTIFIER_SIZE_BYTES_LENGTH,
|
|
60
|
+
HAS_TAGS_FLAG,
|
|
61
|
+
MAX_BLOB_IDENTIFIER_BYTES_LENGTH,
|
|
62
|
+
MAX_NUM_SLIVERS_FOR_QUILT_INDEX,
|
|
54
63
|
QUILT_INDEX_PREFIX_SIZE,
|
|
55
64
|
QUILT_INDEX_SIZE_BYTES_LENGTH,
|
|
56
65
|
QUILT_PATCH_BLOB_HEADER_SIZE,
|
|
57
66
|
QUILT_VERSION_BYTES_LENGTH,
|
|
67
|
+
TAGS_SIZE_BYTES_LENGTH,
|
|
58
68
|
computeSymbolSize,
|
|
59
69
|
parseQuiltPatchId
|
|
60
70
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/utils/quilts.ts"],
|
|
4
|
-
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { EncodingType } from '../types.js';\nimport { QuiltPatchId } from './bcs.js';\nimport { fromUrlSafeBase64 } from './index.js';\n\nexport const QUILT_INDEX_SIZE_BYTES_LENGTH = 4;\nexport const QUILT_VERSION_BYTES_LENGTH = 1;\nexport const QUILT_INDEX_PREFIX_SIZE = QUILT_VERSION_BYTES_LENGTH + QUILT_INDEX_SIZE_BYTES_LENGTH;\nexport const QUILT_PATCH_BLOB_HEADER_SIZE = 1 + 4 + 1; // bcs length of QuiltPatchBlobHeader\n\nconst REQUIRED_ALIGNMENT_BY_ENCODING_TYPE = {\n\tRS2: 2,\n\tRedStuff: 2,\n};\n\nconst MAX_SYMBOL_SIZE_BY_ENCODING_TYPE = {\n\tRS2: 2 ** 16 - 1,\n\tRedStuff: 2 ** 16 - 1,\n};\n\n/**\n * Finds the minimum symbol size needed to store blobs in a fixed number of columns.\n * Each blob must be stored in consecutive columns exclusively.\n *\n * A binary search is used to find the minimum symbol size:\n * 1. Compute the upper and lower bounds for the symbol size.\n * 2. Check if the all the blobs can be fit into the quilt with the current symbol size.\n * 3. Adjust the bounds based on the result and repeat until the symbol size is found.\n *\n * @param blobsSizes - Slice of blob lengths, including the index size as the first element.\n * Note that the len of the blob_size should be between 1 and n_columns.\n * @param nColumns - Number of columns available.\n * @param nRows - Number of rows available.\n * @param maxNumColumnsForQuiltIndex - The maximum number of columns that can be used to\n * store the quilt index.\n * @param encodingType - The encoding type to use.\n *\n * @returns The minimum symbol size needed.\n **/\nexport function computeSymbolSize(\n\tblobsSizes: number[],\n\tnColumns: number,\n\tnRows: number,\n\tmaxNumColumnsForQuiltIndex: number,\n\tencodingType: EncodingType,\n): number {\n\tif (blobsSizes.length > nColumns) {\n\t\tthrow new Error('Too many blobs, the number of blobs must be less than the number of columns');\n\t}\n\n\tif (blobsSizes.length === 0) {\n\t\tthrow new Error('No blobs provided');\n\t}\n\n\tlet minVal = Math.max(\n\t\tblobsSizes.reduce((acc, size) => acc + size, 0) / (nColumns * nRows),\n\t\tblobsSizes[0] / (nRows * maxNumColumnsForQuiltIndex),\n\t\tMath.ceil(QUILT_INDEX_PREFIX_SIZE / nRows),\n\t);\n\n\tlet maxVal = Math.ceil((Math.max(...blobsSizes) / (nColumns / blobsSizes.length)) * nRows);\n\n\twhile (minVal < maxVal) {\n\t\tconst mid = (minVal + maxVal) / 2;\n\t\tif (canBlobsFitIntoMatrix(blobsSizes, nColumns, mid * nRows)) {\n\t\t\tmaxVal = mid;\n\t\t} else {\n\t\t\tminVal = mid + 1;\n\t\t}\n\t}\n\n\tconst symbolSize
|
|
5
|
-
"mappings": "AAIA,SAAS,oBAAoB;AAC7B,SAAS,yBAAyB;AAE3B,MAAM,gCAAgC;AACtC,MAAM,6BAA6B;AACnC,MAAM,0BAA0B,6BAA6B;AAC7D,MAAM,+BAA+B,IAAI,IAAI;
|
|
4
|
+
"sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { EncodingType } from '../types.js';\nimport { QuiltPatchId } from './bcs.js';\nimport { fromUrlSafeBase64 } from './index.js';\n\nexport const QUILT_INDEX_SIZE_BYTES_LENGTH = 4;\nexport const QUILT_VERSION_BYTES_LENGTH = 1;\nexport const QUILT_INDEX_PREFIX_SIZE = QUILT_VERSION_BYTES_LENGTH + QUILT_INDEX_SIZE_BYTES_LENGTH;\nexport const QUILT_PATCH_BLOB_HEADER_SIZE = 1 + 4 + 1; // bcs length of QuiltPatchBlobHeader\n\nexport const BLOB_IDENTIFIER_SIZE_BYTES_LENGTH = 2;\nexport const TAGS_SIZE_BYTES_LENGTH = 2;\nexport const MAX_BLOB_IDENTIFIER_BYTES_LENGTH = (1 << (8 * BLOB_IDENTIFIER_SIZE_BYTES_LENGTH)) - 1;\nexport const MAX_NUM_SLIVERS_FOR_QUILT_INDEX = 10;\n\nexport const HAS_TAGS_FLAG = 1 << 0;\n\nconst REQUIRED_ALIGNMENT_BY_ENCODING_TYPE = {\n\tRS2: 2,\n\tRedStuff: 2,\n};\n\nconst MAX_SYMBOL_SIZE_BY_ENCODING_TYPE = {\n\tRS2: 2 ** 16 - 1,\n\tRedStuff: 2 ** 16 - 1,\n};\n\n/**\n * Finds the minimum symbol size needed to store blobs in a fixed number of columns.\n * Each blob must be stored in consecutive columns exclusively.\n *\n * A binary search is used to find the minimum symbol size:\n * 1. Compute the upper and lower bounds for the symbol size.\n * 2. Check if the all the blobs can be fit into the quilt with the current symbol size.\n * 3. Adjust the bounds based on the result and repeat until the symbol size is found.\n *\n * @param blobsSizes - Slice of blob lengths, including the index size as the first element.\n * Note that the len of the blob_size should be between 1 and n_columns.\n * @param nColumns - Number of columns available.\n * @param nRows - Number of rows available.\n * @param maxNumColumnsForQuiltIndex - The maximum number of columns that can be used to\n * store the quilt index.\n * @param encodingType - The encoding type to use.\n *\n * @returns The minimum symbol size needed.\n **/\nexport function computeSymbolSize(\n\tblobsSizes: number[],\n\tnColumns: number,\n\tnRows: number,\n\tmaxNumColumnsForQuiltIndex: number,\n\tencodingType: EncodingType = 'RS2',\n): number {\n\tif (blobsSizes.length > nColumns) {\n\t\tthrow new Error('Too many blobs, the number of blobs must be less than the number of columns');\n\t}\n\n\tif (blobsSizes.length === 0) {\n\t\tthrow new Error('No blobs provided');\n\t}\n\n\tlet minVal = Math.max(\n\t\tblobsSizes.reduce((acc, size) => acc + size, 0) / (nColumns * nRows),\n\t\tblobsSizes[0] / (nRows * maxNumColumnsForQuiltIndex),\n\t\tMath.ceil(QUILT_INDEX_PREFIX_SIZE / nRows),\n\t);\n\n\tlet maxVal = Math.ceil((Math.max(...blobsSizes) / (nColumns / blobsSizes.length)) * nRows);\n\n\twhile (minVal < maxVal) {\n\t\tconst mid = (minVal + maxVal) / 2;\n\t\tif (canBlobsFitIntoMatrix(blobsSizes, nColumns, mid * nRows)) {\n\t\t\tmaxVal = mid;\n\t\t} else {\n\t\t\tminVal = mid + 1;\n\t\t}\n\t}\n\n\tconst symbolSize =\n\t\tMath.ceil(minVal / REQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType]) *\n\t\tREQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType];\n\n\tif (!canBlobsFitIntoMatrix(blobsSizes, nColumns, symbolSize * nRows)) {\n\t\tthrow new Error('Quilt oversize');\n\t}\n\n\tif (symbolSize > MAX_SYMBOL_SIZE_BY_ENCODING_TYPE[encodingType]) {\n\t\tthrow new Error(\n\t\t\t`Quilt oversize: the resulting symbol size ${symbolSize} is larger than the maximum symbol size ${MAX_SYMBOL_SIZE_BY_ENCODING_TYPE[encodingType]}; remove some blobs`,\n\t\t);\n\t}\n\n\treturn symbolSize;\n}\n\nfunction canBlobsFitIntoMatrix(\n\tblobsSizes: number[],\n\tnColumns: number,\n\tcolumnSize: number,\n): boolean {\n\treturn blobsSizes.reduce((acc, size) => acc + Math.ceil(size / columnSize), 0) <= nColumns;\n}\n\nexport function parseQuiltPatchId(id: string) {\n\treturn QuiltPatchId.parse(fromUrlSafeBase64(id));\n}\n"],
|
|
5
|
+
"mappings": "AAIA,SAAS,oBAAoB;AAC7B,SAAS,yBAAyB;AAE3B,MAAM,gCAAgC;AACtC,MAAM,6BAA6B;AACnC,MAAM,0BAA0B,6BAA6B;AAC7D,MAAM,+BAA+B,IAAI,IAAI;AAE7C,MAAM,oCAAoC;AAC1C,MAAM,yBAAyB;AAC/B,MAAM,oCAAoC,KAAM,IAAI,qCAAsC;AAC1F,MAAM,kCAAkC;AAExC,MAAM,gBAAgB,KAAK;AAElC,MAAM,sCAAsC;AAAA,EAC3C,KAAK;AAAA,EACL,UAAU;AACX;AAEA,MAAM,mCAAmC;AAAA,EACxC,KAAK,KAAK,KAAK;AAAA,EACf,UAAU,KAAK,KAAK;AACrB;AAqBO,SAAS,kBACf,YACA,UACA,OACA,4BACA,eAA6B,OACpB;AACT,MAAI,WAAW,SAAS,UAAU;AACjC,UAAM,IAAI,MAAM,6EAA6E;AAAA,EAC9F;AAEA,MAAI,WAAW,WAAW,GAAG;AAC5B,UAAM,IAAI,MAAM,mBAAmB;AAAA,EACpC;AAEA,MAAI,SAAS,KAAK;AAAA,IACjB,WAAW,OAAO,CAAC,KAAK,SAAS,MAAM,MAAM,CAAC,KAAK,WAAW;AAAA,IAC9D,WAAW,CAAC,KAAK,QAAQ;AAAA,IACzB,KAAK,KAAK,0BAA0B,KAAK;AAAA,EAC1C;AAEA,MAAI,SAAS,KAAK,KAAM,KAAK,IAAI,GAAG,UAAU,KAAK,WAAW,WAAW,UAAW,KAAK;AAEzF,SAAO,SAAS,QAAQ;AACvB,UAAM,OAAO,SAAS,UAAU;AAChC,QAAI,sBAAsB,YAAY,UAAU,MAAM,KAAK,GAAG;AAC7D,eAAS;AAAA,IACV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