Compare commits

...

32 Commits

Author SHA1 Message Date
Râu Cao bcb46c2b2b
2.3.0 2024-03-14 16:03:23 +01:00
Râu Cao 7e7416a8fd
Update @kredits/contracts 2024-03-14 16:02:57 +01:00
Râu Cao b8fa61a434
2.2.0 2023-06-18 12:36:59 +02:00
Râu Cao 1aa2fac3cc Merge pull request 'Pin reimbursements' (#16) from feature/14-reimbursements into master
Reviewed-on: #16
2023-06-18 10:35:34 +00:00
Râu Cao b7d1600c59 Merge pull request 'Add release drafter action' (#17) from dev/release_drafter into master
Reviewed-on: #17
2023-04-26 14:04:17 +00:00
Râu Cao ed50e84662
Add release drafter action 2023-04-26 16:03:42 +02:00
Râu Cao c7fe50ffa9 Merge pull request 'Document CLI options in README' (#15) from docs/readme_cli_options into master
Reviewed-on: #15
2023-04-26 13:59:43 +00:00
Râu Cao 83b3045afd
Pin reimbursements
Release Drafter / Update release notes draft (pull_request) Successful in 3s Details
2023-04-26 15:58:19 +02:00
Râu Cao 3c7bcf038e
Upgrade @kredits/contracts 2023-04-26 15:58:07 +02:00
Râu Cao ca0b870315
Document CLI options in README 2023-04-26 15:17:11 +02:00
Râu Cao 0a21c57251
2.1.0 2022-11-02 20:53:25 +01:00
Râu Cao 1401adbf0c Merge pull request 'Prevent bootstrap node connects on the same IPFS node' (#12) from feature/prevent_self_dials into master
Reviewed-on: #12
2022-11-02 19:52:32 +00:00
Râu Cao 2401d8dff2
Prevent bootstrap node connects on the same IPFS node 2022-11-02 20:50:54 +01:00
Râu Cao 45c0aed4a5
2.0.1 2022-11-02 19:09:31 +01:00
Râu Cao f25fcaa1b2
Add npm badge to README 2022-11-02 19:09:22 +01:00
Râu Cao 7c8f0814b9
2.0.0 2022-11-02 18:58:55 +01:00
Râu Cao b566a432a9
Publish as @kredits/ipfs-pinner 2022-11-02 18:58:43 +01:00
Greg b7a7214f08 Merge pull request 'Upgrade for new kredits contracts and RSK' (#10) from chore/upgrade_kredits into master
Reviewed-on: #10
2022-11-02 17:42:38 +00:00
Greg 6829a13a57 Merge pull request 'Add CLI option to display progress bars' (#11) from feature/progress_bars into chore/upgrade_kredits
Reviewed-on: #11
2022-11-02 17:39:20 +00:00
Râu Cao 328905814d
Use new @kredits/contracts release 2022-11-02 18:27:59 +01:00
Râu Cao 03ee17f00e
Fix progress bar rendering when finished 2022-11-02 13:34:51 +01:00
Râu Cao 9dffb15f45
Improve initialization 2022-11-02 13:27:55 +01:00
Râu Cao c21512fa0b
Add error handling and retries for loading/pinning 2022-11-02 12:58:07 +01:00
Râu Cao 4888d1be78
Don't automatically watch for events 2022-11-02 12:57:53 +01:00
Râu Cao 0ac7228bfe
Add CLI option for progress bars
Also refactors some of the other output and options.
2022-11-01 22:10:57 +01:00
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
8 changed files with 3079 additions and 1776 deletions

View File

@ -0,0 +1,14 @@
name-template: 'v$RESOLVED_VERSION'
tag-template: 'v$RESOLVED_VERSION'
version-resolver:
major:
labels:
- release/major
minor:
labels:
- release/minor
- feature
patch:
labels:
- release/patch
default: patch

View File

@ -0,0 +1,11 @@
name: Release Drafter
on:
pull_request:
types: [closed]
jobs:
release_drafter_job:
name: Update release notes draft
runs-on: ubuntu-latest
steps:
- name: Release Drafter
uses: https://github.com/raucao/gitea-release-drafter@dev

View File

@ -1,6 +1,8 @@
[![npm](https://img.shields.io/npm/v/@kredits/ipfs-pinner.svg)](https://www.npmjs.com/package/@kredits/ipfs-pinner)
# 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 +11,11 @@ config flags, in case it is not running on localhost with default ports.)
### With NPX magic
npx @kosmos/kredits-ipfs-pinner
npx @kredits/ipfs-pinner
### Global install
npm install -g @kosmos/kredits-ipfs-pinner
npm install -g @kredits/ipfs-pinner
kredits-ipfs-pinner
### From repo
@ -25,3 +27,21 @@ Clone the Git repository, then use npm scripts from its root directory.
Or with debug output:
npm run debug
### CLI options
Display progress bars:
kredits-ipfs-pinner --progress
Or with npm:
npm start -- --progress
Keep running and watch for new data/documents:
kredits-ipfs-pinner --watch
Multiple options at once:
kredits-ipfs-pinner --progress --watch

6
config/peers.json Normal file
View File

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

View File

@ -1,31 +1,33 @@
#!/usr/bin/env node
const Kredits = require('kredits-contracts');
const IpfsPinner = require('./lib/ipfs-pinner');
const fs = require('fs');
const debug = require('debug')('ipfs-pinner');
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',
rpcUrl: 'https://rsk-testnet.kosmos.org',
host: 'localhost',
port: '5001',
protocol: 'http',
monitor: true,
bootstrapNode: '/ip4/46.4.18.160/tcp/4001/ipfs/QmZ4Lpzhz8bafbTYvEMMCmrbrMM4JfyHDR23WbCSAd9bo7'
watch: false,
progress: false,
bootstrapNode: `${defaultPeers[0].Addrs[0]}/ipfs/${defaultPeers[0].ID}`
})
.boolean('monitor')
.boolean('watch')
.boolean('progress')
.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',
watch: 'Monitor contract events for new IPFS documents',
progress: 'Show progress bars',
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 = {
@ -34,38 +36,55 @@ const ipfsConfig = {
protocol: argv.protocol
};
console.log(`Using IPFS:`, ipfsConfig);
debug(`IPFS node:`, ipfsConfig);
(async () => {
try {
const kredits = await Kredits.for(
{ network: argv.network, rpcUrl: argv.rpcUrl },
{ apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } }
).init();
const kredits = await Kredits.for(
{ rpcUrl: argv.rpcUrl },
{ ipfsConfig: ipfsConfig }
).init().catch(e => {
console.log('Failed to initialize Kredits:');
console.log(e.message);
process.exit(1);
});
// check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute
const ipfsApi = kredits.ipfs._ipfsAPI;
// TODO redesign IPFS wrapper API and do not use an internal attribute
const ipfsApi = kredits.ipfs._ipfsAPI;
let connectBootstrapNode = true;
await ipfsApi.id();
await ipfsApi.id().then(res => {
if (res.id === argv.bootstrapNode.split('/')[argv.bootstrapNode.split('/').length-1]) {
connectBootstrapNode = false;
}
}).catch(e => {
console.log('Failed to initialize IPFS:');
console.log(e.message);
process.exit(1);
});
if (connectBootstrapNode) {
debug(`Connecting to known IPFS node ${argv.bootstrapNode}`);
await ipfsApi.swarm.connect(argv.bootstrapNode);
}
const ipfsPinner = new IpfsPinner(kredits);
const ipfsPinner = new IpfsPinner(kredits, {
progress: argv.progress
});
ipfsPinner.pinAll().then(pins => {
console.log(`Pinned ${pins.length} existing documents`);
});
await ipfsPinner.pinAll().then(cids => {
console.log(`\nSuccessfully pinned ${cids.length} documents`)
});
ipfsPinner.monitor(pin => {
if (argv.watch) {
console.log('\nWatching contract events for new documents...');
ipfsPinner.watch(pin => {
console.log('Pinned a new document:', pin[0]["hash"]);
});
console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
} catch(e) {
console.log('Failed to start');
console.log(e);
process.exit(1);
} else {
process.exit(0);
}
// TODO Add new deployment/DAO/org ID or all contract proxy addresses
// console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
})();

View File

@ -1,21 +1,61 @@
const debug = require('debug')('ipfs-pinner');
const cliProgress = require('cli-progress');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
class IpfsPinner {
constructor (kredits, ipfsApi) {
constructor (kredits, options={}) {
this.kredits = kredits;
this.ipfsApi = ipfsApi || this.kredits.ipfs;
this.ipfsApi = this.kredits.ipfs;
this.progressBars = !!options.progress && !process.env.DEBUG;
if (this.progressBars) {
this.multibar = new cliProgress.MultiBar({
stopOnComplete: true,
clearOnComplete: false,
hideCursor: false,
etaBuffer: 30,
format: '{entity} [{bar}] {percentage}% | ETA: {eta_formatted} | {value}/{total}'
}, cliProgress.Presets.shades_grey);
}
}
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);
console.log('Pinning IPFS documents for all known items...\n')
const cids = [];
const promises = [];
const contracts = [
this.kredits.Contributor,
this.kredits.Contribution,
this.kredits.Reimbursement
]
return contributorHashes.concat(contributionHashes)
.concat(proposalHashes);
for (const contract of contracts) {
debug(`Pinning data from ${contract.constructor.name}...`);
const itemCount = await contract.count;
debug(`${contract.constructor.name} item count:`, itemCount);
let bar;
if (this.progressBars) {
bar = this.multibar.create(itemCount, 0);
bar.update(0, {entity: `${contract.constructor.name}s`.padEnd(14)});
}
promises.push(this._pinAllFromContract(contract, itemCount, bar)
.then(res => { cids.push(...res); }));
}
await Promise.all(promises);
// Avoid console output race condition with progress bars finishing update
if (this.progressBars) await sleep(1000);
return cids;
}
monitor (callback) {
watch (callback) {
this.kredits.Contribution.on('ContributionAdded', (id) => {
this.kredits.Contribution.getData(id)
.then(data => { return this.ipfsApi.pin(data); })
@ -31,21 +71,50 @@ class IpfsPinner {
.then(data => { return this.ipfsApi.pin(data); })
.then(callback);
});
}
_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);
});
});
return Promise.all(promises);
this.kredits.Reimbursement.on('ReimbursementAdded', (id) => {
this.kredits.Reimbursement.getData(id)
.then(data => { return this.ipfsApi.pin(data); })
.then(callback);
});
}
async _pinAllFromContract (contract, itemCount, progressBar) {
const ipfsApi = this.ipfsApi;
const progressBars = this.progressBars;
const ids = [...Array(itemCount).keys()].map(i => i+1);
const cids = [];
const batchSize = 20;
let position = 0;
async function loadAndPin(id) {
let cid;
try {
const data = await contract.getData(id);
debug(`Loaded ${contract.constructor.name} #${id}`);
cid = await ipfsApi.pin(data);
debug(`Pinned ${contract.constructor.name} #${id} at ${cid}`);
} catch(e) {
debug(`Error while trying to load and pin ${contract.constructor.name} #${id}:`)
debug(e);
debug(`\nTrying again...`);
loadAndPin(id);
} finally {
cids.push(cid);
if (progressBars) { progressBar.increment(); }
}
}
while (position < itemCount) {
const batchIds = ids.slice(position, position + batchSize);
await Promise.all(batchIds.map(async id => loadAndPin(id)));
position += batchSize;
}
return cids;
}
}
module.exports = IpfsPinner;

4589
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@kosmos/kredits-ipfs-pinner",
"version": "1.2.0",
"name": "@kredits/ipfs-pinner",
"version": "2.3.0",
"description": "Pins IPFS data of a Kredits organization on an IPFS node",
"main": "index.js",
"scripts": {
@ -20,9 +20,10 @@
],
"license": "MIT",
"dependencies": {
"debug": "^4.1.1",
"kredits-contracts": "^5.4.0",
"yargs": "^13.2.4"
"@kredits/contracts": "^7.4.0",
"cli-progress": "^3.11.2",
"debug": "^4.3.4",
"yargs": "^17.6.0"
},
"repository": {
"type": "git",