Skip to content

store review records without publishing them #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/ChluIPFS.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions lib/ChluIPFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ var ChluIPFS = function () {
key: 'storeReviewRecord',
value: function () {
var _ref5 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5(reviewRecord) {
var previousVersionMultihash = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _regenerator2.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return this.reviewRecords.storeReviewRecord(reviewRecord, previousVersionMultihash);
return this.reviewRecords.storeReviewRecord(reviewRecord, options);

case 2:
return _context5.abrupt('return', _context5.sent);
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ var ChluIPFSAPI = function () {
key: 'storeReviewRecord',
value: function () {
var _ref6 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6(reviewRecord) {
var previousVersionMultihash = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return _regenerator2.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
_context6.next = 2;
return this.instance.storeReviewRecord(reviewRecord, previousVersionMultihash);
return this.instance.storeReviewRecord(reviewRecord, options);

case 2:
return _context6.abrupt('return', _context6.sent);
Expand Down
217 changes: 146 additions & 71 deletions lib/modules/reviewrecords.js

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions lib/utils/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ var createIPFS = function () {
};
}();

var storeBuffer = function () {
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(ipfs, buf) {
var dagNode;
return _regenerator2.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
if (Buffer.isBuffer(buf)) {
_context2.next = 2;
break;
}

throw new Error('Argument is not a buffer');

case 2:
_context2.next = 4;
return ipfs.object.put(buf);

case 4:
dagNode = _context2.sent;
return _context2.abrupt('return', multihashToString(dagNode.multihash));

case 6:
case 'end':
return _context2.stop();
}
}
}, _callee2, this);
}));

return function storeBuffer(_x2, _x3) {
return _ref2.apply(this, arguments);
};
}();

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var IPFS = require('ipfs');
Expand Down Expand Up @@ -117,6 +152,7 @@ module.exports = {
validateMultihash: validateMultihash,
multihashToString: multihashToString,
multihashToBuffer: multihashToBuffer,
storeBuffer: storeBuffer,
getDefaultRepoPath: getDefaultRepoPath,
getDefaultOrbitDBPath: getDefaultOrbitDBPath
};
15 changes: 8 additions & 7 deletions src/ChluIPFS.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const ipfsUtils = require('./utils/ipfs');
const IPFSUtils = require('./modules/ipfs');
const Pinning = require('./modules/pinning');
const Room = require('./modules/room');
const ReviewRecords = require('./modules/reviewrecords');
Expand All @@ -11,7 +11,7 @@ const defaultLogger = require('./utils/logger');

class ChluIPFS {
constructor(options = {}){
this.utils = ipfsUtils;
// Configuration
this.storage = storageUtils;
if (typeof options.enablePersistence === 'undefined') {
this.enablePersistence = true;
Expand All @@ -20,9 +20,9 @@ class ChluIPFS {
}
this.directory = options.directory || this.storage.getDefaultDirectory();
const additionalOptions = {
repo: this.utils.getDefaultRepoPath(this.directory)
repo: IPFSUtils.getDefaultRepoPath(this.directory)
};
this.orbitDbDirectory = options.orbitDbDirectory || this.utils.getDefaultOrbitDBPath(this.directory);
this.orbitDbDirectory = options.orbitDbDirectory || IPFSUtils.getDefaultOrbitDBPath(this.directory);
this.ipfsOptions = Object.assign(
{},
constants.defaultIPFSOptions,
Expand All @@ -36,6 +36,7 @@ class ChluIPFS {
this.events = new EventEmitter();
this.logger = options.logger || defaultLogger;
// Modules
this.ipfsUtils = new IPFSUtils(this);
this.orbitDb = new DB(this);
this.pinning = new Pinning(this);
this.room = new Room(this);
Expand All @@ -47,7 +48,7 @@ class ChluIPFS {
this.logger.debug('Starting ChluIPFS, directory: ' + this.directory);
if (!this.ipfs) {
this.logger.debug('Initializing IPFS');
this.ipfs = await this.utils.createIPFS(this.ipfsOptions);
this.ipfs = await IPFSUtils.createIPFS(this.ipfsOptions);
this.logger.debug('Initialized IPFS');
}

Expand Down Expand Up @@ -106,8 +107,8 @@ class ChluIPFS {
return await this.reviewRecords.readReviewRecord(multihash, notifyUpdate);
}

async storeReviewRecord(reviewRecord, previousVersionMultihash = null) {
return await this.reviewRecords.storeReviewRecord(reviewRecord, previousVersionMultihash);
async storeReviewRecord(reviewRecord, options = {}) {
return await this.reviewRecords.storeReviewRecord(reviewRecord, options);
}

async exportData() {
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ChluIPFSAPI {
return await this.instance.readReviewRecord(multihash, notifyUpdate);
}

async storeReviewRecord(reviewRecord, previousVersionMultihash = null){
return await this.instance.storeReviewRecord(reviewRecord, previousVersionMultihash);
async storeReviewRecord(reviewRecord, options = {}){
return await this.instance.storeReviewRecord(reviewRecord, options);
}

async exportData() {
Expand Down
34 changes: 34 additions & 0 deletions src/modules/ipfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const DAGNode = require('ipld-dag-pb').DAGNode;
const utils = require('../utils/ipfs');

class IPFS {
constructor(chluIpfs) {
this.chluIpfs = chluIpfs;
}

async get(multihash) {
const dagNode = await this.chluIpfs.ipfs.object.get(utils.multihashToBuffer(multihash));
return dagNode.data;
}

async createDAGNode(buf) {
if (!Buffer.isBuffer(buf)) {
throw new Error('Argument is not a buffer');
}
return await new Promise((fullfill, reject) => {
DAGNode.create(buf, [], (err, dagNode) => {
if (err) reject(err); else fullfill(dagNode);
});
});
}

async storeDAGNode(dagNode) {
const newDagNode = await this.chluIpfs.ipfs.object.put(dagNode);
if (newDagNode.toJSON().multihash !== dagNode.toJSON().multihash) {
throw new Error('Multihash mismatch');
}
return utils.getDAGNodeMultihash(newDagNode);
}
}

module.exports = Object.assign(IPFS, utils);
5 changes: 3 additions & 2 deletions src/modules/pinning.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const constants = require('../constants');
const IPFSUtils = require('../modules/ipfs');

class Pinning {

Expand All @@ -7,7 +8,7 @@ class Pinning {
}

async pin(multihash){
this.chluIpfs.utils.validateMultihash(multihash);
IPFSUtils.validateMultihash(multihash);
// TODO: check that the multihash evaluates to valid Chlu data
// broadcast start of pin process
await this.chluIpfs.room.broadcast({ type: constants.eventTypes.pinning, multihash });
Expand All @@ -17,7 +18,7 @@ class Pinning {
} else {
// TODO: Chlu service node need to be able to pin, so we should support using go-ipfs
this.chluIpfs.logger.warn('This node is running an IPFS client that does not implement pinning. Falling back to just retrieving the data non recursively. This will not be supported');
await this.chluIpfs.ipfs.object.get(multihash);
await this.chluIpfs.ipfsUtils.get(multihash);
}
// broadcast successful pin
await this.chluIpfs.room.broadcast({ type: constants.eventTypes.pinned, multihash });
Expand Down
46 changes: 32 additions & 14 deletions src/modules/reviewrecords.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const protons = require('protons');
const constants = require('../constants');
const protobuf = protons(require('../utils/protobuf'));
const IPFSUtils = require('./ipfs');

class ReviewRecords {

Expand Down Expand Up @@ -49,44 +50,61 @@ class ReviewRecords {
}

async getReviewRecord(multihash){
const dagNode = await this.chluIpfs.ipfs.object.get(this.chluIpfs.utils.multihashToBuffer(multihash));
const buffer = dagNode.data;
const reviewRecord = protobuf.ReviewRecord.decode(buffer);
return reviewRecord;
const buffer = await this.chluIpfs.ipfsUtils.get(multihash);
return protobuf.ReviewRecord.decode(buffer);
}

async readReviewRecord(multihash, notifyUpdate = null) {
this.chluIpfs.utils.validateMultihash(multihash);
IPFSUtils.validateMultihash(multihash);
const reviewRecord = await this.getReviewRecord(multihash);
// TODO validate
// TODO: validate
if (notifyUpdate) this.findLastReviewRecordUpdate(multihash, notifyUpdate);
return reviewRecord;
}

async storeReviewRecord(reviewRecord, previousVersionMultihash = null){
prepareReviewRecord(reviewRecord) {
// TODO: validate
if(this.chluIpfs.type === constants.types.customer) {
reviewRecord.orbitDb = this.chluIpfs.getOrbitDBAddress();
} else if (!reviewRecord.orbitDb) {
throw new Error('Can not set the orbitDb address since this is not a customer');
}
reviewRecord = this.setPointerToLastReviewRecord(reviewRecord);
const buffer = protobuf.ReviewRecord.encode(reviewRecord);
// TODO validate
// write thing to ipfs
const dagNode = await this.chluIpfs.ipfs.object.put(buffer);
const multihash = this.chluIpfs.utils.multihashToString(dagNode.multihash);
return protobuf.ReviewRecord.encode(reviewRecord);
}

async storeReviewRecord(reviewRecord, options = {}){
const defaultOptions = {
publish: true
};
const opt = Object.assign({}, defaultOptions, options);
const { previousVersionMultihash, publish } = opt;
const buffer = this.prepareReviewRecord(reviewRecord);
const dagNode = await this.chluIpfs.ipfsUtils.createDAGNode(buffer); // don't store to IPFS yet
const multihash = IPFSUtils.getDAGNodeMultihash(dagNode);
if (options.expectedMultihash) {
if (options.expectedMultihash !== multihash) {
throw new Error('Expected a different multihash');
}
}
if (publish) await this.publishReviewRecord(dagNode, previousVersionMultihash);
return multihash;
}

async publishReviewRecord(dagNode, previousVersionMultihash) {
// Broadcast request for pin, then wait for response
// TODO: handle a timeout and also rebroadcast periodically, otherwise new peers won't see the message
const multihash = await this.chluIpfs.ipfsUtils.storeDAGNode(dagNode); // store to IPFS
// Wait for it to be remotely pinned
let tasksToAwait = [this.waitForRemotePin(multihash)];
if (previousVersionMultihash) {
// This is a review update
tasksToAwait.push(this.setForwardPointerForReviewRecord(previousVersionMultihash, multihash));
}
await Promise.all(tasksToAwait);
// Store operation succeeded: set this as the last review record published
// Operation succeeded: set this as the last review record published
this.chluIpfs.lastReviewRecordMultihash = multihash;
await this.chluIpfs.persistence.persistData();
return multihash;
}

async waitForRemotePin(multihash) {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function multihashToBuffer(multihash) {
return multihashes.fromB58String(multihash);
}

function getDAGNodeMultihash(dagNode) {
return multihashToString(dagNode.multihash);
}

function getDefaultRepoPath(directory = storage.getDefaultDirectory()) {
// the versioning is required due to https://github.com/ipfs/js-ipfs/issues/1115
// in short, IPFS upgrades change the format of the repo
Expand All @@ -74,6 +78,7 @@ module.exports = {
validateMultihash,
multihashToString,
multihashToBuffer,
getDAGNodeMultihash,
getDefaultRepoPath,
getDefaultOrbitDBPath
};
Loading