Skip to content

Commit

Permalink
Corrected the manifest upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan6sha committed Nov 21, 2023
1 parent b275de2 commit 3ca633b
Show file tree
Hide file tree
Showing 5 changed files with 1,392 additions and 1,357 deletions.
2 changes: 1 addition & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ const App = () => {
if (api && initComplete) {
console.log('replicate');
fula
.replicateRecentCids(api, seed, 3)
.replicateRecentCids(api, seed, 1, 6)
.then((res: any) => {
console.log('res received');
console.log(res);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
"@polkadot/keyring": "^10.2.6",
"@polkadot/typegen": "^10.10.1",
"@polkadot/util": "^10.2.6",
"@polkadot/util-crypto": "^10.2.6"
"@polkadot/util-crypto": "^10.2.6",
"text-encoding": "^0.7.0"
}
}
23 changes: 18 additions & 5 deletions src/protocols/chain-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
const { cryptoWaitReady } = require('@polkadot/util-crypto');
import type * as BType from '../types/blockchain';
import { TextEncoder } from 'text-encoding';

const types = {
FulaPoolPool: EventTypes.FulaPoolPool,
Expand Down Expand Up @@ -31,13 +32,17 @@ function addDoubleSlashToSeed(seed: string): string {
/*
createManifest: This function batch uploads manifests
*/
function serialize(obj: any): string {
return JSON.stringify(obj);
}

function createManifest(
cids: string[],
poolId: number,
replicationFactor: number = 4
): {
manifest: any[];
cids: string[];
manifest: string[]; // or string[]
cids: string[]; // or string[]
poolId: number[];
replicationFactor: number[];
} {
Expand All @@ -49,19 +54,27 @@ function createManifest(
},
}));

// Create arrays filled with `poolId` and `replicationFactor`, one element for each `cid`
// Serialize manifest_metadata to Uint8Array or string
const serializedManifest = manifest_metadata.map((item) => serialize(item)); // Implement `serialize` accordingly

// Serialize cids to Uint8Array or string
const serializedCids = cids.map((cid) => serialize(cid)); // Implement `serialize` accordingly

// Create arrays for `poolId` and `replicationFactor`
const poolIds = new Array(cids.length).fill(poolId);
const replicationFactors = new Array(cids.length).fill(replicationFactor);

const batchUploadManifest = {
manifest: manifest_metadata,
cids: cids,
manifest: serializedManifest,
cids: serializedCids,
poolId: poolIds,
replicationFactor: replicationFactors,
};

return batchUploadManifest;
}


export const batchUploadManifest = async (
api: ApiPromise | undefined,
seed: string,
Expand Down
18 changes: 17 additions & 1 deletion src/protocols/fula.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ export const isReady = (filesystemCheck: boolean = true): Promise<boolean> => {
export const replicateRecentCids = async (
api: ApiPromise,
seed: string,
poolId: number,
replicationNo: number = 4
): Promise<{ status: boolean; msg: string }> => {
let status = true;
Expand All @@ -351,22 +352,34 @@ export const replicateRecentCids = async (
console.log('uploading manifests');
try {
let account = await getAccountIdFromSeed(seed);
console.log('account: ' + account);
const accountBal = await checkAccountBalance(api, account);
console.log('account balance: ' + accountBal);
if (accountBal !== '0') {
const recentCids = await listRecentCidsAsString();
console.log(recentCids);
if (recentCids) {
console.log({
api,
seed,
recentCids,
poolId,
replicationNo,
});
const res = await batchUploadManifest(
api,
seed,
recentCids,
poolId,
replicationNo
);
console.log('res received');
console.log('batchUploadManifest res received');
console.log(res);
if (res && res.hash) {
const signedBlock = await api.rpc.chain.getBlock(res.hash);
if (signedBlock?.block?.extrinsics?.length) {
await clearCidsFromRecent(recentCids);
msg = res.hash;
} else {
status = false;
msg = 'block data is not found';
Expand All @@ -375,6 +388,9 @@ export const replicateRecentCids = async (
status = false;
msg = 'hash is not returned';
}
} else {
status = false;
msg = 'No recent Cids found';
}
} else {
status = false;
Expand Down
Loading

0 comments on commit 3ca633b

Please sign in to comment.