11import {
22 Args ,
33 Operation ,
4- Provider ,
54 SmartContract ,
65 strToBytes ,
76 U32 ,
@@ -20,7 +19,6 @@ import { FileInit } from './models/FileInit'
2019import { Metadata } from './models/Metadata'
2120
2221import {
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- }
0 commit comments