17 Commits

Author SHA1 Message Date
Râu Cao
77c2cea4a4 Pin reimbursement data 2022-11-01 16:36:58 +01:00
Râu Cao
3c02591061 Update README 2022-11-01 16:26:18 +01:00
Râu Cao
9d7760edef Update contracts dep to include signer fix 2022-11-01 15:20:42 +01:00
Râu Cao
4a3988137e Adapt code for new kredits release/contracts
Also process loading and pinning functions in batches, to prevent stack
size errors on RSK nodes.
2022-11-01 15:02:18 +01:00
Râu Cao
e12e798735 Update dependencies 2022-11-01 15:01:47 +01:00
Râu Cao
fee6b5c121 Move default peers to config file
Can later be used to persist peer config for a node, too.
2022-11-01 15:01:07 +01:00
Râu Cao
d8dda84fd6 Update bootstrap node 2022-09-30 15:19:01 +02:00
ba7c98b564 1.2.0 2019-08-18 12:40:27 +02:00
3f03368e25 Merge branch 'refactor/bootstrap_node' of kosmos/kredits-ipfs-pinner into master 2019-08-18 09:03:51 +00:00
a3d21ebb30 Refactor bootstrap node connect, add CLI argument
Removes the superfluous wrapper function (as the API already returns a
Promise). Also adds a command line argument so you can choose a
different bootstrap node.
2019-08-14 10:12:07 +02:00
b11dbb9fde 1.1.0 2019-08-13 11:10:09 +02:00
gregkare
bfb51d48bc Merge branch 'feature/connect_to_kosmos_ipfs_node' of kosmos/kredits-ipfs-pinner into master 2019-08-13 09:02:34 +00:00
f2f884a317 Connect to ipfs.kosmos.org node on start
This ensures that the local IPFS node is always connected to a known
Kosmos node before attempting to fetch documents. Vastly improves
performance.

closes #4
2019-08-13 10:07:25 +02:00
2a3c021737 1.0.3 2019-08-04 14:52:06 +02:00
c4c95d9b38 Move package to "kosmos" org 2019-08-04 14:51:38 +02:00
2d08cc929f Merge branch 'bugfix/broken_executable' of kosmos/kredits-ipfs-pinner into master 2019-08-04 12:51:03 +00:00
5d36ca05a5 Fix executable not working
Need to tell the system it's dealing with a node script. This fixes both
the installed `kredits-ipfs-pinner` command as well as npx (which is
also running that command).
2019-08-04 14:47:57 +02:00
6 changed files with 2932 additions and 1751 deletions

View File

@@ -1,6 +1,6 @@
# Kredits IPFS Pinner
This tool pins the IPFS hashes of a Kredits organisation on an IPFS node.
This tool pins the IPFS documents of a Kredits organization on any IPFS node.
## Usage
@@ -9,11 +9,11 @@ config flags, in case it is not running on localhost with default ports.)
### With NPX magic
npx @67p/kredits-ipfs-pinner
npx @kredits/ipfs-pinner
### Global install
npm install -g @67p/kredits-ipfs-pinner
npm install -g @kredits/ipfs-pinner
kredits-ipfs-pinner
### From repo

6
config/peers.json Normal file
View File

@@ -0,0 +1,6 @@
[
{
"ID": "QmRw21aC3TroRVdZhrE2Qh4W6PBA67kbE8p3fNfsVcfW8D",
"Addrs": ["/dns4/draco.kosmos.org/tcp/4001"]
}
]

View File

@@ -1,20 +1,30 @@
#!/usr/bin/env node
const fs = require('fs');
const debug = require('debug')('ipfs-pinner');
const Kredits = require('kredits-contracts');
const Kredits = require('@kredits/contracts');
const IpfsPinner = require('./lib/ipfs-pinner');
const defaultPeers = JSON.parse(fs.readFileSync('./config/peers.json'));
const argv = require('yargs')
.default({ network: 'rinkeby', apm: 'open.aragonpm.eth', host: 'localhost', port: '5001', protocol: 'http', monitor: true })
.default({
rpcUrl: 'https://rsk-testnet.kosmos.org',
host: 'localhost',
port: '5001',
protocol: 'http',
monitor: true,
bootstrapNode: `${defaultPeers[0].Addrs[0]}/ipfs/${defaultPeers[0].ID}`
})
.boolean('monitor')
.describe({
network: 'Ethereum network to connect to',
rpcUrl: 'Ethereum node RPC URL; alternative to --network',
daoAddress: 'Optional Kredits DAO address',
rpcUrl: 'Web3/EVM node RPC URL; alternative to --network',
host: 'IPFS API host',
port: 'IPFS API port',
protocol: 'IPFS API protocol',
monitor: 'Monitor contract events for new IPFS documents'
monitor: 'Monitor contract events for new IPFS documents',
bootstrapNode: 'IPFS node address to connect to before fetching documents'
})
.example('$0 --network rinkeby --host localhost', 'Pins all existing IPFS documents to the IPFS API running on localhost and monitors for new events.')
.example('$0 --host localhost', 'Pins all existing IPFS documents to the IPFS API running on localhost and monitors for new events')
.argv;
const ipfsConfig = {
@@ -28,13 +38,18 @@ console.log(`Using IPFS:`, ipfsConfig);
(async () => {
try {
const kredits = await Kredits.for(
{ network: argv.network, rpcUrl: argv.rpcUrl },
{ apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } }
{ rpcUrl: argv.rpcUrl },
{ ipfsConfig: ipfsConfig }
).init();
// check the connection to the IPFS client
// Check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute
await kredits.ipfs._ipfsAPI.id();
const ipfsApi = kredits.ipfs._ipfsAPI;
await ipfsApi.id();
debug(`Connecting to known IPFS node ${argv.bootstrapNode}`);
await ipfsApi.swarm.connect(argv.bootstrapNode);
const ipfsPinner = new IpfsPinner(kredits);
@@ -46,7 +61,8 @@ console.log(`Using IPFS:`, ipfsConfig);
console.log('Pinned a new document:', pin[0]["hash"]);
});
console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
// TODO Add new deployment/DAO/org ID or all contract proxy addresses
// console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
} catch(e) {
console.log('Failed to start');
console.log(e);

View File

@@ -1,5 +1,16 @@
const debug = require('debug')('ipfs-pinner');
async function promiseAllInBatches(task, items, batchSize) {
let position = 0;
let results = [];
while (position < items.length) {
const itemsForBatch = items.slice(position, position + batchSize);
results = [...results, ...await Promise.allSettled(itemsForBatch.map(item => task(item)))];
position += batchSize;
}
return results;
}
class IpfsPinner {
constructor (kredits, ipfsApi) {
this.kredits = kredits;
@@ -7,12 +18,12 @@ class IpfsPinner {
}
async pinAll () {
const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor);
const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution);
const proposalHashes = await this._pinAllFromContract(this.kredits.Proposal);
const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor);
const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution);
const reimbursementHashes = await this._pinAllFromContract(this.kredits.Reimbursement);
return contributorHashes.concat(contributionHashes)
.concat(proposalHashes);
.concat(reimbursementHashes);
}
monitor (callback) {
@@ -33,19 +44,25 @@ class IpfsPinner {
});
}
_pinAllFromContract (contract) {
async _pinAllFromContract (contract) {
debug(`Pinning data from ${contract.constructor.name}...`);
return contract.count.then(count => {
let promises = [...Array(count).keys()].map(i => {
let id = i + 1; // 0 => 1 - ids start with 1 and not with 0
debug(`Loading ${contract.constructor.name} #${id}`);
return contract.getData(id).then(data => {
debug(`Pinning ${contract.constructor.name} #${id}`);
return this.ipfsApi.pin(data);
});
const count = await contract.count;
debug('Item count:', count);
const ids = [...Array(count).keys()].map(i => i+1);
const cids = [];
async function loadAndPin (id) {
debug(`Loading ${contract.constructor.name} #${id}`);
return contract.getData(id).then(data => {
debug(`Pinning ${contract.constructor.name} #${id}`);
return this.ipfsApi.pin(data).then(cid => cids.push(cid));
});
return Promise.all(promises);
});
}
await promiseAllInBatches(loadAndPin.bind(this), ids, 100);
return cids;
}
}
module.exports = IpfsPinner;

4574
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "@67p/kredits-ipfs-pinner",
"version": "1.0.2",
"name": "@kosmos/kredits-ipfs-pinner",
"version": "1.2.0",
"description": "Pins IPFS data of a Kredits organization on an IPFS node",
"main": "index.js",
"scripts": {
@@ -20,9 +20,9 @@
],
"license": "MIT",
"dependencies": {
"debug": "^4.1.1",
"kredits-contracts": "^5.4.0",
"yargs": "^13.2.4"
"@kredits/contracts": "git+https://gitea.kosmos.org/kredits/contracts#6e0ec87",
"debug": "^4.3.4",
"yargs": "^17.6.0"
},
"repository": {
"type": "git",