Skip to content

Commit 20670a5

Browse files
committed
fix readStorage signature changed
1 parent 581000a commit 20670a5

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

cli/src/lib/index/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export async function getOwnerFromWebsiteSC(
1313
address: string
1414
): Promise<string> {
1515
const ownerAddress = await sc.provider.readStorage(address, ['OWNER'], true)
16-
if (ownerAddress.length === 0) {
16+
if (ownerAddress.length === 0 || !ownerAddress[0]) {
1717
throw new Error(`Could not find owner for website ${address}`)
1818
}
1919

cli/src/lib/website/filesInit.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,12 @@ export async function filterUselessFileInits(
392392
)
393393

394394
for (let i = 0; i < batch.length; i++) {
395+
if (!results[i]) {
396+
throw new Error(`Could not retrieve totalChunk data entry for file ${batch[i].preStore.location}`);
397+
}
395398
if (
396-
results[i].length !== U32.SIZE_BYTE ||
397-
U32.fromBytes(results[i]) !== batch[i].preStore.totalChunk
399+
(results[i] as Uint8Array).length !== U32.SIZE_BYTE ||
400+
U32.fromBytes(results[i] as Uint8Array) !== batch[i].preStore.totalChunk
398401
) {
399402
fileInitsToKeep.push(batch[i].preStore)
400403
}

cli/src/lib/website/metadata.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@massalabs/massa-web3'
77
import { storageCostForEntry } from '../utils/storage'
88
import { Metadata } from './models/Metadata'
9-
import { globalMetadataKey } from './storageKeys'
9+
import { globalMetadataKey, GLOBAL_METADATA_TAG } from './storageKeys'
1010

1111
const SET_GLOBAL_METADATA_FUNCTION = 'setMetadataGlobal'
1212

@@ -35,9 +35,12 @@ export async function getGlobalMetadata(
3535

3636
return metadata.map((m, index) => {
3737
const metadataKeyBytes = metadataKeys[index].slice(
38-
globalMetadataKey(new Uint8Array()).length
38+
GLOBAL_METADATA_TAG.length
3939
)
40-
const key = String.fromCharCode(...new Uint8Array(metadataKeyBytes))
40+
const key = String.fromCharCode(...new Uint8Array(metadataKeyBytes));
41+
if (!m) {
42+
throw new Error(`Global metadata with key ${key} has empty value`);
43+
}
4144
const value = String.fromCharCode(...new Uint8Array(m))
4245

4346
return new Metadata(key, value)

cli/src/lib/website/read.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ export async function listFiles(
2222
)
2323
const fileLocations = await provider.readStorage(scAddress, allStorageKeys)
2424

25-
return fileLocations.map((location) =>
26-
String.fromCharCode(...new Uint8Array(location))
27-
)
25+
return fileLocations.map((location, i) => {
26+
if (!location) {
27+
throw new Error(`File location not found at index ${i}`);
28+
}
29+
return String.fromCharCode(...new Uint8Array(location));
30+
}
31+
);
2832
}
2933

3034
/**
@@ -44,7 +48,7 @@ export async function getFileTotalChunks(
4448
fileChunkCountKey(new Uint8Array(filePathHash)),
4549
])
4650

47-
if (fileTotalChunksResp.length !== 1) {
51+
if (fileTotalChunksResp.length !== 1 || !fileTotalChunksResp[0]) {
4852
throw new Error('Invalid response from getDatastoreEntries')
4953
}
5054

@@ -83,13 +87,15 @@ export async function getFileFromAddress(
8387
datastoreKeys.push(fileChunkKey(new Uint8Array(filePathHash), i))
8488
}
8589

86-
const rawChunks = await provider.readStorage(scAddress, datastoreKeys)
87-
88-
for (let i = 0; i < rawChunks.length; i++) {
89-
if (rawChunks[i].length === 0) {
90-
throw new Error(`file ${filePath} Chunk ${i} not found`)
91-
}
92-
}
90+
const rawChunks = (await provider.readStorage(scAddress, datastoreKeys))
91+
.map( // allow to return Uint8Array[] instead of (Uint8Array | null)[]
92+
(chunk, i) => {
93+
if (!chunk) {
94+
throw new Error(`file ${filePath} Chunk ${i} not found`);
95+
}
96+
return chunk;
97+
}
98+
);
9399

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

0 commit comments

Comments
 (0)