Skip to content

Commit

Permalink
fix readStorage signature changed
Browse files Browse the repository at this point in the history
  • Loading branch information
fleandrei committed Feb 17, 2025
1 parent 581000a commit 20670a5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cli/src/lib/index/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getOwnerFromWebsiteSC(
address: string
): Promise<string> {
const ownerAddress = await sc.provider.readStorage(address, ['OWNER'], true)
if (ownerAddress.length === 0) {
if (ownerAddress.length === 0 || !ownerAddress[0]) {
throw new Error(`Could not find owner for website ${address}`)
}

Expand Down
7 changes: 5 additions & 2 deletions cli/src/lib/website/filesInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,12 @@ export async function filterUselessFileInits(
)

for (let i = 0; i < batch.length; i++) {
if (!results[i]) {
throw new Error(`Could not retrieve totalChunk data entry for file ${batch[i].preStore.location}`);
}
if (
results[i].length !== U32.SIZE_BYTE ||
U32.fromBytes(results[i]) !== batch[i].preStore.totalChunk
(results[i] as Uint8Array).length !== U32.SIZE_BYTE ||
U32.fromBytes(results[i] as Uint8Array) !== batch[i].preStore.totalChunk
) {
fileInitsToKeep.push(batch[i].preStore)
}
Expand Down
9 changes: 6 additions & 3 deletions cli/src/lib/website/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@massalabs/massa-web3'
import { storageCostForEntry } from '../utils/storage'
import { Metadata } from './models/Metadata'
import { globalMetadataKey } from './storageKeys'
import { globalMetadataKey, GLOBAL_METADATA_TAG } from './storageKeys'

const SET_GLOBAL_METADATA_FUNCTION = 'setMetadataGlobal'

Expand Down Expand Up @@ -35,9 +35,12 @@ export async function getGlobalMetadata(

return metadata.map((m, index) => {
const metadataKeyBytes = metadataKeys[index].slice(
globalMetadataKey(new Uint8Array()).length
GLOBAL_METADATA_TAG.length
)
const key = String.fromCharCode(...new Uint8Array(metadataKeyBytes))
const key = String.fromCharCode(...new Uint8Array(metadataKeyBytes));
if (!m) {
throw new Error(`Global metadata with key ${key} has empty value`);
}
const value = String.fromCharCode(...new Uint8Array(m))

return new Metadata(key, value)
Expand Down
28 changes: 17 additions & 11 deletions cli/src/lib/website/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ export async function listFiles(
)
const fileLocations = await provider.readStorage(scAddress, allStorageKeys)

return fileLocations.map((location) =>
String.fromCharCode(...new Uint8Array(location))
)
return fileLocations.map((location, i) => {
if (!location) {
throw new Error(`File location not found at index ${i}`);
}
return String.fromCharCode(...new Uint8Array(location));
}
);
}

/**
Expand All @@ -44,7 +48,7 @@ export async function getFileTotalChunks(
fileChunkCountKey(new Uint8Array(filePathHash)),
])

if (fileTotalChunksResp.length !== 1) {
if (fileTotalChunksResp.length !== 1 || !fileTotalChunksResp[0]) {
throw new Error('Invalid response from getDatastoreEntries')
}

Expand Down Expand Up @@ -83,13 +87,15 @@ export async function getFileFromAddress(
datastoreKeys.push(fileChunkKey(new Uint8Array(filePathHash), i))
}

const rawChunks = await provider.readStorage(scAddress, datastoreKeys)

for (let i = 0; i < rawChunks.length; i++) {
if (rawChunks[i].length === 0) {
throw new Error(`file ${filePath} Chunk ${i} not found`)
}
}
const rawChunks = (await provider.readStorage(scAddress, datastoreKeys))
.map( // allow to return Uint8Array[] instead of (Uint8Array | null)[]
(chunk, i) => {
if (!chunk) {
throw new Error(`file ${filePath} Chunk ${i} not found`);
}
return chunk;
}
);

const totalLength = rawChunks.reduce((acc, chunk) => acc + chunk.length, 0)
const concatenatedArray = new Uint8Array(totalLength)
Expand Down

0 comments on commit 20670a5

Please sign in to comment.