Skip to content
This repository has been archived by the owner on Oct 30, 2018. It is now read-only.

Commit

Permalink
add static method for creating an AuditStream object from the results…
Browse files Browse the repository at this point in the history
… of another
  • Loading branch information
Gordon Hall committed May 6, 2016
1 parent 2247173 commit 5e62803
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
23 changes: 23 additions & 0 deletions lib/auditstream.js
Expand Up @@ -140,4 +140,27 @@ AuditStream.prototype._createResponseInput = function(challenge) {
return crypto.createHash('sha256').update(challenge);
};

/**
* Returns a new instance from the predefined challenges and tree
* @param {Array} challenges - The precomputed challenges
* @param {Array} tree - The bottom leaves of the existing merkle tree
* @returns {AuditStream}
*/
AuditStream.fromRecords = function(challenges, tree) {
assert(Array.isArray(challenges), 'Invalid challenges supplied');
assert(Array.isArray(tree), 'Invalid tree supplied');
assert(
tree.length === utils.getNextPowerOfTwo(challenges.length),
'Challenges and tree do not match'
);

var auditor = new AuditStream(challenges.length);

auditor._challenges = challenges;
auditor._tree = new MerkleTree(tree, utils.rmd160sha256);
auditor._finished = true;

return auditor;
};

module.exports = AuditStream;
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "storj",
"version": "0.6.10",
"version": "0.6.11",
"description": "implementation of the storj protocol for node.js and the browser",
"main": "index.js",
"directories": {
Expand Down
34 changes: 34 additions & 0 deletions test/auditstream.unit.js
Expand Up @@ -82,6 +82,40 @@ describe('AuditStream', function() {

});

describe('AuditStream#fromRecords', function() {

it('should return the same result when created from record', function(done) {
var audit1 = new AuditStream(12);
audit1.on('finish', function() {
var tree1 = audit1.getPublicRecord();
var challenges1 = audit1.getPrivateRecord().challenges;
var audit2 = AuditStream.fromRecords(challenges1, tree1);
var tree2 = audit2.getPublicRecord();
var challenges2 = audit2.getPrivateRecord().challenges;
challenges2.forEach(function(c, i) {
expect(c).to.equal(challenges1[i]);
});
tree2.forEach(function(l, i) {
expect(l).to.equal(tree1[i]);
});
expect(
audit1.getPrivateRecord().depth
).to.equal(
audit2.getPrivateRecord().depth
);
expect(
audit1.getPrivateRecord().root
).to.equal(
audit2.getPrivateRecord().root
);
done();
});
audit1.write(SHARD);
audit1.end();
});

});

describe('Audit+AuditStream/Compatibility', function() {

it('should return the same structures given the same input', function(done) {
Expand Down

0 comments on commit 5e62803

Please sign in to comment.