Skip to content

Commit 50cf16b

Browse files
committed
fix file content diff check
1 parent 47468bd commit 50cf16b

File tree

6 files changed

+16
-92
lines changed

6 files changed

+16
-92
lines changed

cli/src/lib/website/filesInit.ts

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
Args,
33
Operation,
4-
Provider,
54
SmartContract,
65
strToBytes,
76
U32,
@@ -20,7 +19,6 @@ import { FileInit } from './models/FileInit'
2019
import { Metadata } from './models/Metadata'
2120

2221
import {
23-
FILE_TAG,
2422
fileChunkCountKey,
2523
fileLocationKey,
2624
globalMetadataKey,
@@ -291,63 +289,3 @@ class Batch {
291289
)
292290
}
293291
}
294-
295-
/**
296-
* Filter out pre-stores that are already stored on the blockchain
297-
* @param provider - the web3 provider
298-
* @param scAddress - the smart contract address
299-
* @param fileInits - the pre-stores to filter
300-
* @returns the pre-stores that are not stored on the blockchain
301-
*/
302-
export async function filterUselessFileInits(
303-
provider: Provider,
304-
scAddress: string,
305-
fileInits: FileInit[]
306-
): Promise<FileInit[]> {
307-
const fileInitsWithKey = fileInits.map((preStore) => {
308-
return {
309-
preStore: preStore,
310-
totalChunkKey: fileChunkCountKey(preStore.hashLocation),
311-
}
312-
})
313-
314-
const batches: {
315-
preStore: FileInit
316-
totalChunkKey: Uint8Array
317-
}[][] = []
318-
319-
for (let i = 0; i < fileInitsWithKey.length; i += 100) {
320-
batches.push(fileInitsWithKey.slice(i, i + 100))
321-
}
322-
323-
const fileInitsToKeep: FileInit[] = []
324-
325-
const keys = await provider.getStorageKeys(scAddress, FILE_TAG)
326-
for (const batch of batches) {
327-
// Remove missing keys from the batch and add them to the list of files to keep
328-
for (let i = batch.length - 1; i >= 0; i--) {
329-
if (!keys.includes(batch[i].totalChunkKey)) {
330-
fileInitsToKeep.push(batch[i].preStore)
331-
batch.splice(i, 1)
332-
}
333-
}
334-
335-
const results = await provider.readStorage(
336-
scAddress,
337-
batch.map((key) => key.totalChunkKey)
338-
)
339-
340-
for (let i = 0; i < batch.length; i++) {
341-
const chunkData = results[i]
342-
if (
343-
!chunkData ||
344-
chunkData.length !== U32.SIZE_BYTE ||
345-
U32.fromBytes(chunkData) !== batch[i].preStore.totalChunk
346-
) {
347-
fileInitsToKeep.push(batch[i].preStore)
348-
}
349-
}
350-
}
351-
352-
return fileInitsToKeep
353-
}

cli/src/lib/website/read.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,17 @@ export async function getFileFromAddress(
8888
filePath
8989
)
9090

91-
const datastoreKeys = []
91+
const chunksKeys = []
9292
for (let i = 0n; i < fileTotalChunks; i++) {
93-
datastoreKeys.push(fileChunkKey(filePathHash, i))
93+
chunksKeys.push(fileChunkKey(filePathHash, i))
9494
}
9595

96-
const rawChunks = (await provider.readStorage(scAddress, datastoreKeys)).map(
97-
// allow to return Uint8Array[] instead of (Uint8Array | null)[]
98-
(chunk, i) => {
99-
if (!chunk || chunk.length === 0) {
100-
throw new Error(`file ${filePath} Chunk ${i} not found`)
101-
}
102-
return chunk
103-
}
104-
)
105-
106-
const totalLength = rawChunks.reduce((acc, chunk) => acc + chunk.length, 0)
107-
const concatenatedArray = new Uint8Array(totalLength)
96+
const fileChunks = await provider.readStorage(scAddress, chunksKeys)
10897

109-
let offset = 0
110-
for (const chunk of rawChunks) {
111-
concatenatedArray.set(chunk, offset)
112-
offset += chunk.length
113-
}
114-
115-
return concatenatedArray
98+
return fileChunks
99+
.filter((chunk) => !!chunk)
100+
.reduce(
101+
(acc, chunk) => Uint8Array.from([...acc, ...chunk]),
102+
new Uint8Array()
103+
)
116104
}

cli/src/lib/website/storageKeys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export function fileChunkKey(
4141
): Uint8Array {
4242
return new Uint8Array([
4343
...fileKey(hashLocation),
44-
...CHUNK_NB_TAG,
44+
...CHUNK_TAG,
4545
...U32.toBytes(index),
4646
])
4747
}

cli/src/tasks/prepareChunk.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { FileInit } from '../lib/website/models/FileInit'
1818

1919
import { FileDelete } from '../lib/website/models/FileDelete'
2020
import { UploadCtx } from './tasks'
21-
import { filterUselessFileInits } from '../lib/website/filesInit'
2221

2322
/**
2423
* Create a task to prepare batches from the website file
@@ -34,6 +33,7 @@ export function prepareBatchesTask(): ListrTask {
3433
ctx.chunkSize,
3534
ctx.sc
3635
)
36+
ctx.fileInits = fileInits
3737

3838
// check that the provided website directory has correct format
3939
if (!ctx.skipIndexHtmlCheck && !localFiles.includes('index.html')) {
@@ -59,9 +59,6 @@ export function prepareBatchesTask(): ListrTask {
5959
}
6060

6161
ctx.batches = batcher(chunks, ctx.chunkSize)
62-
ctx.fileInits = ctx.sc
63-
? await filterUselessFileInits(ctx.provider, ctx.sc.address, fileInits)
64-
: fileInits
6562

6663
if (ctx.batches.length < 16) {
6764
for (const batch of ctx.batches) {
@@ -72,14 +69,16 @@ export function prepareBatchesTask(): ListrTask {
7269
}
7370
}
7471

75-
task.output = `Total of ${fileInits.length} files, ${ctx.fileInits.length} require update`
72+
task.output = `Total of ${localFiles.length} files, ${fileInits.length} require update`
7673
task.output = `${ctx.filesToDelete.length} files will be deleted from the smart contract`
7774
if (ctx.filesToDelete.length < 16) {
7875
for (const file of ctx.filesToDelete) {
7976
task.output = `- ${file.location}`
8077
}
8178
}
82-
task.output = `Total of ${chunks.length} chunks divided into ${ctx.batches.length} batches`
79+
if (ctx.batches.length) {
80+
task.output = `Total of ${chunks.length} chunks divided into ${ctx.batches.length} batches`
81+
}
8382
},
8483
rendererOptions: {
8584
outputBar: Infinity,

cli/src/tasks/prepareUpload.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function prepareUploadTask(): ListrTask {
2626
ctx.metadatas.length === 0 &&
2727
ctx.metadatasToDelete.length === 0
2828
) {
29-
task.skip('All files are ready for upload, and no metadata changes')
29+
task.skip('No changes in files and metadata to upload')
3030
return
3131
}
3232

cli/tests/fileInit.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { describe, it, expect } from '@jest/globals'
21
import { createBatches, batchSize } from '../src/lib/website/filesInit'
32
import { FileInit } from '../src/lib/website/models/FileInit'
43
import { FileDelete } from '../src/lib/website/models/FileDelete'

0 commit comments

Comments
 (0)