Skip to content

Commit

Permalink
removing already uploaded cids when replicate
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Apr 10, 2024
1 parent 617cfd9 commit bc29103
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@functionland/react-native-fula",
"version": "1.54.17",
"version": "1.54.18",
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
47 changes: 47 additions & 0 deletions src/protocols/chain-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,50 @@ export const getAccountIdFromSeed = async (seed: string): Promise<string> => {
return Promise.reject(err);
}
};

/*
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
*/
export const manifestNewBatch = async (
api: ApiPromise | undefined,
poolId: number,
uploader: string,
cids: string[]
): Promise<string[]> => {
console.log('manifestAvailableBatch in react-native started');
let newCids: string[] = [];
try {
if (api === undefined) {
api = await init();
}
// Type guard to assure TypeScript that api is not undefined
if (!api?.query?.fula?.manifests) {
throw new Error('Failed to initialize api or api.query.fula');
}

for (const cid of cids) {
const manifestInfo = await api.query.fula
.manifests(poolId, cid)
.catch((err) => {
console.log(err);
});
if (!manifestInfo || manifestInfo == '' || manifestInfo == null) {
newCids.push(cid);
} else {
let formattedManifestInfo: BType.ManifestResponse = JSON.parse(
JSON.stringify(manifestInfo.toHuman())
);
if (
formattedManifestInfo?.usersData?.some(
(user) => user.uploader === uploader
)
) {
newCids.push(formattedManifestInfo.manifestMetadata.job.uri);
}
}
}
return Promise.resolve(newCids);
} catch (err) {
return Promise.reject(err);
}
};
74 changes: 41 additions & 33 deletions src/protocols/fula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
checkAccountBalance,
getAccountIdFromSeed,
batchUploadManifest,
manifestNewBatch,
} from './chain-api';
import { batchUploadManifest as batchUploadManifestBlox } from './blockchain';
import { ApiPromise } from '@polkadot/api';
Expand Down Expand Up @@ -355,10 +356,16 @@ export const replicateRecentCids = async (
seed: string,
poolId: number,
replicationNo: number = 4
): Promise<{ status: boolean; msg: string; cids: string[] }> => {
): Promise<{
status: boolean;
msg: string;
cids: string[];
allCids: string[];
}> => {
let status = true;
let msg = '';
let recentCids: string[] = [];
let newCids: string[] = [];
if (!api) {
api = await chainApiInit();
}
Expand All @@ -380,10 +387,11 @@ export const replicateRecentCids = async (
poolId,
replicationNo,
});
newCids = await manifestNewBatch(api, poolId, account, recentCids);
const res = await batchUploadManifest(
api,
seed,
recentCids,
newCids,
poolId,
replicationNo
);
Expand Down Expand Up @@ -429,7 +437,7 @@ export const replicateRecentCids = async (

// Return a value (true/false) depending on the outcome of the function
// For example:
return { status: status, msg: msg, cids: recentCids }; // or false, depending on your logic
return { status: status, msg: msg, cids: newCids, allCids: recentCids }; // or false, depending on your logic
};

/**
Expand All @@ -449,42 +457,42 @@ export const replicateRecentCidsBlox = async (
//const accountBal = await getAccountBalanceBlox();
const accountBal = '1';
console.log('account balance: ' + accountBal);
const recentCids = await listRecentCidsAsStringWithChildren();
console.log(recentCids);
if (recentCids) {
console.log({
api,
seed,
recentCids,
poolId,
replicationNo,
});
const res = await batchUploadManifestBlox(
api,
seed,
recentCids,
poolId,
replicationNo
);
console.log('batchUploadManifest res received');
console.log(res);
if (res) {
if (typeof res === 'object' && 'pool_id' in res) {
msg = res.storer;
} else {
status = false;
msg =
'Unexpected response from batchUploadManifestBlox: ' +
JSON.stringify(res);
}
const recentCids = await listRecentCidsAsStringWithChildren();
console.log(recentCids);
if (recentCids) {
console.log({
api,
seed,
recentCids,
poolId,
replicationNo,
});
const res = await batchUploadManifestBlox(
api,
seed,
recentCids,
poolId,
replicationNo
);
console.log('batchUploadManifest res received');
console.log(res);
if (res) {
if (typeof res === 'object' && 'pool_id' in res) {
msg = res.storer;
} else {
status = false;
msg = 'hash is not returned';
msg =
'Unexpected response from batchUploadManifestBlox: ' +
JSON.stringify(res);
}
} else {
status = false;
msg = 'No recent Cids found';
msg = 'hash is not returned';
}
} else {
status = false;
msg = 'No recent Cids found';
}
} catch (e: any) {
console.log('res failed');
console.log(e);
Expand Down
12 changes: 12 additions & 0 deletions src/types/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,15 @@ export interface BloxFreeSpaceResponse {
used: number;
used_percentage: number;
}

export interface UserData {
uploader: string;
storers: string[];
replicationFactor: number;
}

export interface ManifestResponse {
usersData: UserData[];
manifestMetadata: ManifestMetadata;
size_?: number | null; // The question mark indicates that this field is optional.
}

0 comments on commit bc29103

Please sign in to comment.