Skip to content

Commit

Permalink
fix read flow
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes-mysten committed Jan 18, 2025
1 parent 77605de commit e2e53ae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 52 deletions.
20 changes: 8 additions & 12 deletions packages/walrus/examples/read-blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@ const walrusClient = new WalrusClient({

export async function retrieveBlob(blobId: string) {
const systemState = await walrusClient.systemState();
// const blobBytes = await walrusClient.readBlob(blobId);
const blobBytes = await walrusClient.readBlob(blobId);

// // TODO: type wrappers for WASM methods ++ fix BCS blob_id to make comparison easier
const reconstructedBlobMetadata = computeMetadata(systemState.committee.n_shards, blobBytes);
if (reconstructedBlobMetadata.blob_id !== blobId) {
console.log('inconsistent blob -- try more slivers');
return null;
}

// const reconstructedBlobMetadata = computeMetadata(systemState.committee.n_shards, blobBytes);
// if (reconstructedBlobMetadata.blob_id !== blobId) {
// console.log('inconsistent blob -- try more slivers');
// return null;
// }

// console.log('blobBytes', blobBytes);

// return new Blob([new Uint8Array(blobBytes)]);
return new Blob([new Uint8Array(blobBytes)]);
}

(async function main() {
const blob = await retrieveBlob('cUTGpAG6MixSTbM8-KHvUoK_eGn4bXJP1a8U5cQq9yw');
const blob = await retrieveBlob('ssRKJFOCc-orFBKExBbNZs6z8y4IhTPqY-D6bSLYI98');

// Convert Uint8Array to string using TextDecoder
const textDecoder = new TextDecoder('utf-8'); // Specify encoding, e.g., "utf-8"
Expand Down
8 changes: 5 additions & 3 deletions packages/walrus/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ export class WalrusClient {
const slivers: (typeof SliverData.$inferType)[] = [];
const blobMetadata = await this.getBlobMetadata(blobId);

const sliverPromises = new Array(numShards).fill(null).map((_, sliverPairIndex) => {
console.log('blobMetadata', blobMetadata);

const sliverPromises = new Array(numShards).fill(null).map((_, sliverPairIndex) =>
taskPool
.runTask((signal) => {
return this.getSliver({ blobId, sliverPairIndex, signal }).then((response) => {
Expand All @@ -268,8 +270,8 @@ export class WalrusClient {
}

throw error;
});
});
}),
);

await Promise.allSettled(sliverPromises);

Expand Down
42 changes: 8 additions & 34 deletions packages/walrus/src/utils/object-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import type { BcsType } from '@mysten/bcs';
import { pureBcsSchemaFromTypeName } from '@mysten/sui/bcs';
import type { PureTypeName, ShapeFromPureTypeName } from '@mysten/sui/bcs';
import type { SuiClient, SuiObjectData, SuiObjectResponse } from '@mysten/sui/client';
import { deriveDynamicFieldID } from '@mysten/sui/utils';
import DataLoader from 'dataloader';

import { Field } from '../contracts/deps/0x0000000000000000000000000000000000000000000000000000000000000002/dynamic_field.js';

export class SuiObjectDataLoader extends DataLoader<string, SuiObjectData> {
#suiClient: SuiClient;
#dynamicFieldCache = new Map<string, Map<string, SuiObjectData>>();
Expand All @@ -17,6 +20,7 @@ export class SuiObjectDataLoader extends DataLoader<string, SuiObjectData> {
ids: ids as string[],
options: {
showBcs: true,
showContent: true,
},
});

Expand Down Expand Up @@ -98,38 +102,6 @@ export class SuiObjectDataLoader extends DataLoader<string, SuiObjectData> {
return response.data;
}

async #getDynamicFieldObject<T extends PureTypeName>(
parent: string,
name: {
type: PureTypeName;
value: ShapeFromPureTypeName<T>;
},
) {
const encodedName = pureBcsSchemaFromTypeName<T>(name.type as never)
.serialize(name.value)
.toBase64();

if (!this.#dynamicFieldCache.has(parent)) {
this.#dynamicFieldCache.set(parent, new Map());
}

const cache = this.#dynamicFieldCache.get(parent)!;
if (cache.has(encodedName)) {
return cache.get(encodedName)!;
}

const objectResponse = await this.#suiClient.getDynamicFieldObject({
parentId: parent,
name,
});

const data = this.#getObjectFromResponse(parent, objectResponse);

cache.set(encodedName, data);

return data;
}

async loadFieldObject<K extends PureTypeName, T>(
parent: string,
name: {
Expand All @@ -138,7 +110,9 @@ export class SuiObjectDataLoader extends DataLoader<string, SuiObjectData> {
},
type: BcsType<T, any>,
): Promise<T> {
const data = await this.#getDynamicFieldObject(parent, name);
return this.load(data.objectId, type);
const schema = pureBcsSchemaFromTypeName<K>(name.type as never);
const id = deriveDynamicFieldID(parent, 'u64', schema.serialize(name.value).toBytes());

return (await this.load(id, Field(schema, type))).value;
}
}
4 changes: 2 additions & 2 deletions packages/walrus/src/utils/task-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class TaskPool {
}

abortPendingTasks() {
this.#abortController.abort();
this.#queue = [];
// this.#abortController.abort();
// this.#queue = [];
}

async awaitAll() {
Expand Down
8 changes: 7 additions & 1 deletion packages/walrus/src/wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ export function decodePrimarySlivers(
slivers: (typeof SliverData.$inferInput)[],
): Uint8Array {
const encoder = new BlobEncoder(nShards);
const bytes = encoder.decode_primary(BigInt(size), slivers);
const bytes = encoder.decode_primary(
BigInt(size),
slivers.map((sliver) => ({
...sliver,
_sliver_type: undefined,
})),
);
return new Uint8Array(bytes);
}

Expand Down

0 comments on commit e2e53ae

Please sign in to comment.