Skip to content

Commit

Permalink
Adds feature to make multiple deals when startBackup() is called
Browse files Browse the repository at this point in the history
  • Loading branch information
ottpeter committed Apr 3, 2023
1 parent 9b074f2 commit 938613c
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 274 deletions.
19 changes: 15 additions & 4 deletions backend/routes/backup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const router = express.Router();
const { fillArrayWithPinnedCIDs, copyToMFS, createCAR, addBackCAR, calculateCommP, listActiveBackups, startBackup } = require('../utils/backupUtils');
const { fillArrayWithPinnedCIDs, copyToMFS, createCAR, addBackCAR, calculateCommP, listActiveBackups, startBackup, clearInProgressBackups } = require('../utils/backupUtils');

// This will start the backup process. Probably we will change it to POST instead of GET, and it would be good if we could give in some parameters, like PeerID
router.get('/start', async (req, res) => {
Expand All @@ -10,16 +10,15 @@ router.get('/start', async (req, res) => {
await copyToMFS(ipfs, arrayOfCIDs, folderName);
const { payloadCID, payloadSize } = await createCAR(ipfs, CID, folderName, globSource);
await calculateCommP(folderName, payloadCID);
//await addBackCAR(ipfs, CID, folderName, globSource)

//await addToFilecoin(); // we chained this to calculateCommP, because couldn't solve it other way
//await checkDealStatus(); // we chained this to addToFilecoin
});

// This will backup a single folder, that it is pointed to
router.get('/folder', async (req, res) => {
const { ipfs, CID, globSource, folderName } = await startBackup(req.query.name, res);
const { payloadCID, payloadSize } = await createCAR(ipfs, CID, folderName);
await calculateCommP(folderName, payloadCID, CID);
//await addBackCAR(ipfs, CID, folderName, globSource);
});

// Delete backup folders
Expand Down Expand Up @@ -53,4 +52,16 @@ router.get('/show-inprogress', async (req, res) => {
res.json(listActiveBackups(req.query.name));
});

router.get('/clear-inprogress', (req, res) => {
try {
clearInProgressBackups();
res.json({message: "InProgressBackups were cleared."});
} catch (error) {
res.json({
message: "There was an error while trying to clear the InProgressBackups object.",
error: error
});
}
})

module.exports = router;
78 changes: 31 additions & 47 deletions backend/utils/backupUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async function calculateCommP(folderName, payloadCID, CID) {
console.log("CommP: ", commPCid)
console.log("Payload size: ", payloadSize);
console.log("Piece Size: ", paddedPieceSize);
addToFilecoin(CID, folderName);
addToFilecoin(folderName);
} catch (error) {
console.error("There was an error while trying to calculate commP!", error);
inProgressBackups[folderName].commPCalculationError = error;
Expand All @@ -180,54 +180,35 @@ async function calculateCommP(folderName, payloadCID, CID) {
return {commPCid, paddedPieceSize}
}

async function addToFilecoin(not_used, folderName) {
async function addToFilecoin(folderName) {
// Convert piece CID string to hex bytes
const cid = inProgressBackups[folderName].commP;
const cidHexRaw = new CID(cid).toString('base16').substring(1);
const cidHex = "0x" + cidHexRaw;
const contractAddr = process.env.DEAL_CONTRACT;

const verified = false;
const skipIpniAnnounce = false;
const removeUnsealedCopy = false;

const extraParamsV1 = [
"http://45.91.171.156:3000/fetch?fileName=" + folderName + ".car",
inProgressBackups[folderName].payloadSize,
skipIpniAnnounce,
removeUnsealedCopy,
]

const startEpoch = 215000;

const DealRequestStruct = [
cidHex,
inProgressBackups[folderName].pieceSize,
verified,
inProgressBackups[folderName].payloadCID,
startEpoch, // arbitrary number, will need to fetch this later
(startEpoch+600000), // end
0, // storage price per epoch
0, // provider collateral
0, // client collateral
1, // extra params version
extraParamsV1,
];

const BackupRequestStruct = {
pieceCID: cidHex,
pieceSize: inProgressBackups[folderName].pieceSize,
label: inProgressBackups[folderName].payloadCID,
dealDuration: 600000,
maxPricePerEpoch: 0, // Max price per epoch
originalLocation: "http://45.91.171.156:3000/fetch?fileName=" + folderName + ".car",
carSize: inProgressBackups[folderName].payloadSize,
}

const networkId = network.defaultNetwork;
console.log("Making deal proposal on network", networkId)

const wallet = new ethers.Wallet(network.networks[networkId].accounts[0], ethers.provider); // Create a new wallet instance
const DealClient = await ethers.getContractFactory("DealClient", wallet); // Contract Factory
const dealClient = await DealClient.attach(contractAddr); // Contract instance

transaction = await dealClient.makeDealProposal(DealRequestStruct) // Transaction
transaction = await dealClient.startBackup(BackupRequestStruct) // Transaction
transactionReceipt = await transaction.wait()

const event = transactionReceipt.events[0].topics[1]; // Listen for DealProposalCreate event
//console.log("transactionReceipt: ", transactionReceipt);
//console.log("Events: ", transactionReceipt.events);
//console.log("Topics: ", transactionReceipt.events[0].topics);

inProgressBackups[folderName].dealRequestMade = true;
console.log("Complete! Event Emitted. ProposalId is:", event);

Expand All @@ -245,35 +226,31 @@ async function checkDealStatus(folderName) {
const dealClient = await DealClient.attach(contractAddr); // Contract instance
const max_try = 50;
let try_count = 0;
let dealID = 0;
let deals = [];

do {
console.log("Attempt ", try_count);
const result = await dealClient.getDealId(commPasBytes); // Send transaction
dealID = result.toNumber();
console.log("Deal ID: ", dealID);
if (dealID !== 0) {
deals = await dealClient.getDeals(commPasBytes); // Send transaction
//dealID = result.toNumber();
console.log("Deals array: ", deals);
if (deals.length > 0) {
inProgressBackups[folderName].dealPublished = true;
break;
}
try_count++;
await delay(1000*60*2);
} while (try_count < max_try && dealID === 0);
} while (try_count < max_try && deals.length === 0);

if (try_count === max_try && dealID === 0) {
if (try_count === max_try && deals.length === 0) {
inProgressBackups[folderName].dealIdError = `Tried to get the DealID ${try_count} times without success. Most likely there was an error with making the deal.`;
console.error(`Tried to get the DealID ${try_count} times without success. Most likely there was an error with making the deal.`);
return;
}

console.log(`Backup finished successfully.`);
/*const refreshTransaction = dealClient.refreshValues(dealID);
console.log("Refresh transaction made. Hash: ", refreshTransaction.hash);
console.log("Deals: ", deals);
delete inProgressBackups[folderName];

const isDealActivated = await dealClient.getDealVerificationStatus(dealID);
console.log("Is Deal Activated? ", isDealActivated);
const dealActive = await dealClient.getDealActivationStatus(dealID);
console.log("Deal Active: ", dealActive);*/
} catch (error) {
inProgressBackups[folderName].dealIdError = error;
console.error("There was an error while trying to get DealID", error);
Expand All @@ -292,8 +269,15 @@ function listActiveBackups(name) {
}
}

function clearInProgressBackups() {
//inProgressBackups = Object.assign({}, {});
for (let key in inProgressBackups) {
delete inProgressBackups[key];
}
}

function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}

module.exports = { startBackup, fillArrayWithPinnedCIDs, copyToMFS, createCAR, addBackCAR, calculateCommP, addToFilecoin, listActiveBackups }
module.exports = { startBackup, fillArrayWithPinnedCIDs, copyToMFS, createCAR, addBackCAR, calculateCommP, addToFilecoin, listActiveBackups, clearInProgressBackups }
Empty file added commP
Empty file.
Loading

0 comments on commit 938613c

Please sign in to comment.