Skip to content

Commit bc29103

Browse files
committed
removing already uploaded cids when replicate
1 parent 617cfd9 commit bc29103

File tree

4 files changed

+101
-34
lines changed

4 files changed

+101
-34
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "1.54.17",
3+
"version": "1.54.18",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/protocols/chain-api.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,50 @@ export const getAccountIdFromSeed = async (seed: string): Promise<string> => {
306306
return Promise.reject(err);
307307
}
308308
};
309+
310+
/*
311+
listPools: This function takes start index and length and returns a promise of an object that contains a list of pools. Each pool in the list contains the poolID, owner, poolName, parent, and participants of the pool
312+
*/
313+
export const manifestNewBatch = async (
314+
api: ApiPromise | undefined,
315+
poolId: number,
316+
uploader: string,
317+
cids: string[]
318+
): Promise<string[]> => {
319+
console.log('manifestAvailableBatch in react-native started');
320+
let newCids: string[] = [];
321+
try {
322+
if (api === undefined) {
323+
api = await init();
324+
}
325+
// Type guard to assure TypeScript that api is not undefined
326+
if (!api?.query?.fula?.manifests) {
327+
throw new Error('Failed to initialize api or api.query.fula');
328+
}
329+
330+
for (const cid of cids) {
331+
const manifestInfo = await api.query.fula
332+
.manifests(poolId, cid)
333+
.catch((err) => {
334+
console.log(err);
335+
});
336+
if (!manifestInfo || manifestInfo == '' || manifestInfo == null) {
337+
newCids.push(cid);
338+
} else {
339+
let formattedManifestInfo: BType.ManifestResponse = JSON.parse(
340+
JSON.stringify(manifestInfo.toHuman())
341+
);
342+
if (
343+
formattedManifestInfo?.usersData?.some(
344+
(user) => user.uploader === uploader
345+
)
346+
) {
347+
newCids.push(formattedManifestInfo.manifestMetadata.job.uri);
348+
}
349+
}
350+
}
351+
return Promise.resolve(newCids);
352+
} catch (err) {
353+
return Promise.reject(err);
354+
}
355+
};

src/protocols/fula.ts

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
checkAccountBalance,
55
getAccountIdFromSeed,
66
batchUploadManifest,
7+
manifestNewBatch,
78
} from './chain-api';
89
import { batchUploadManifest as batchUploadManifestBlox } from './blockchain';
910
import { ApiPromise } from '@polkadot/api';
@@ -355,10 +356,16 @@ export const replicateRecentCids = async (
355356
seed: string,
356357
poolId: number,
357358
replicationNo: number = 4
358-
): Promise<{ status: boolean; msg: string; cids: string[] }> => {
359+
): Promise<{
360+
status: boolean;
361+
msg: string;
362+
cids: string[];
363+
allCids: string[];
364+
}> => {
359365
let status = true;
360366
let msg = '';
361367
let recentCids: string[] = [];
368+
let newCids: string[] = [];
362369
if (!api) {
363370
api = await chainApiInit();
364371
}
@@ -380,10 +387,11 @@ export const replicateRecentCids = async (
380387
poolId,
381388
replicationNo,
382389
});
390+
newCids = await manifestNewBatch(api, poolId, account, recentCids);
383391
const res = await batchUploadManifest(
384392
api,
385393
seed,
386-
recentCids,
394+
newCids,
387395
poolId,
388396
replicationNo
389397
);
@@ -429,7 +437,7 @@ export const replicateRecentCids = async (
429437

430438
// Return a value (true/false) depending on the outcome of the function
431439
// For example:
432-
return { status: status, msg: msg, cids: recentCids }; // or false, depending on your logic
440+
return { status: status, msg: msg, cids: newCids, allCids: recentCids }; // or false, depending on your logic
433441
};
434442

435443
/**
@@ -449,42 +457,42 @@ export const replicateRecentCidsBlox = async (
449457
//const accountBal = await getAccountBalanceBlox();
450458
const accountBal = '1';
451459
console.log('account balance: ' + accountBal);
452-
const recentCids = await listRecentCidsAsStringWithChildren();
453-
console.log(recentCids);
454-
if (recentCids) {
455-
console.log({
456-
api,
457-
seed,
458-
recentCids,
459-
poolId,
460-
replicationNo,
461-
});
462-
const res = await batchUploadManifestBlox(
463-
api,
464-
seed,
465-
recentCids,
466-
poolId,
467-
replicationNo
468-
);
469-
console.log('batchUploadManifest res received');
470-
console.log(res);
471-
if (res) {
472-
if (typeof res === 'object' && 'pool_id' in res) {
473-
msg = res.storer;
474-
} else {
475-
status = false;
476-
msg =
477-
'Unexpected response from batchUploadManifestBlox: ' +
478-
JSON.stringify(res);
479-
}
460+
const recentCids = await listRecentCidsAsStringWithChildren();
461+
console.log(recentCids);
462+
if (recentCids) {
463+
console.log({
464+
api,
465+
seed,
466+
recentCids,
467+
poolId,
468+
replicationNo,
469+
});
470+
const res = await batchUploadManifestBlox(
471+
api,
472+
seed,
473+
recentCids,
474+
poolId,
475+
replicationNo
476+
);
477+
console.log('batchUploadManifest res received');
478+
console.log(res);
479+
if (res) {
480+
if (typeof res === 'object' && 'pool_id' in res) {
481+
msg = res.storer;
480482
} else {
481483
status = false;
482-
msg = 'hash is not returned';
484+
msg =
485+
'Unexpected response from batchUploadManifestBlox: ' +
486+
JSON.stringify(res);
483487
}
484488
} else {
485489
status = false;
486-
msg = 'No recent Cids found';
490+
msg = 'hash is not returned';
487491
}
492+
} else {
493+
status = false;
494+
msg = 'No recent Cids found';
495+
}
488496
} catch (e: any) {
489497
console.log('res failed');
490498
console.log(e);

src/types/blockchain.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,15 @@ export interface BloxFreeSpaceResponse {
112112
used: number;
113113
used_percentage: number;
114114
}
115+
116+
export interface UserData {
117+
uploader: string;
118+
storers: string[];
119+
replicationFactor: number;
120+
}
121+
122+
export interface ManifestResponse {
123+
usersData: UserData[];
124+
manifestMetadata: ManifestMetadata;
125+
size_?: number | null; // The question mark indicates that this field is optional.
126+
}

0 commit comments

Comments
 (0)