-
Notifications
You must be signed in to change notification settings - Fork 51
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
Adding Prune command #69
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,71 @@ module.exports = { | |
return JSON.stringify(json) | ||
}, | ||
|
||
/* | ||
* Delete all the dead branches | ||
*/ | ||
pruneTree (timestamp) { | ||
let prun = timestamp.attestations.length === 0 | ||
let changed = false | ||
|
||
for (const element of timestamp.ops) { | ||
var result = this.pruneTree(element[1]) | ||
var stampPrunable = result.prunable | ||
var stampChanged = result.change | ||
changed = changed || stampChanged || stampPrunable | ||
if (stampPrunable) { | ||
timestamp.ops.delete(element[0]) | ||
} else { | ||
prun = false | ||
} | ||
} | ||
return { prunable: prun, change: changed } | ||
}, | ||
|
||
/* | ||
* Delete all the leaves which didn't match the height passed | ||
*/ | ||
discard_attestation (timestamp, heightToKeep) { | ||
timestamp.getAttestations().forEach(opStamp => { | ||
if (!(opStamp instanceof Notary.BitcoinBlockHeaderAttestation && opStamp.height === heightToKeep)) { | ||
timestamp.attestations.splice(timestamp.attestations.indexOf(opStamp), 1) | ||
} | ||
}) | ||
var iter = timestamp.ops.values() | ||
let res = iter.next() | ||
while (!res.done) { | ||
this.discard_attestation(res.value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add the heightToKeep param |
||
res = iter.next() | ||
} | ||
}, | ||
/** | ||
* Prune a timestamp dropping the redundant data included in the ots receipt, storing only the linear proof from the file hash to the best attestation. | ||
* @exports OpenTimestamps/prune | ||
* @param {DetachedTimestampFile[]} detaches - The array of detached file to stamp; input/output parameter. | ||
* @param {Object} options - The option arguments. Not used yet | ||
*/ | ||
prune (detaches, options = {}) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to update the prune prototype in order to take one DetachedTimestampFile at time, instead an array of file (following the PR's code you are just handle 1 file at time). |
||
let prunable = false | ||
let maxHead = 0 | ||
|
||
if (detaches.timestamp.getAttestations().length <= 1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. count only BitcoinBlockHeaderAttestation, because usually there is at least one PendingAttestation that shouldn't be counted |
||
return new Promise((resolve, reject) => { reject(new Error('Just one attestation founded')) }) | ||
} | ||
detaches.timestamp.getAttestations().forEach(subStamp => { | ||
if (subStamp instanceof Notary.BitcoinBlockHeaderAttestation) { | ||
prunable = true | ||
maxHead = (maxHead < subStamp.height) ? subStamp.height : maxHead | ||
} | ||
}) | ||
if (prunable) { | ||
this.discard_attestation(detaches.timestamp, maxHead) | ||
this.pruneTree(detaches.timestamp) | ||
return new Promise((resolve, reject) => { resolve(detaches) }) | ||
} else { | ||
return new Promise((resolve, reject) => { reject(new Error('There are no Bitcoin Attestation in the file')) }) | ||
} | ||
}, | ||
|
||
/** | ||
* Create timestamp with the aid of a remote calendar for one or multiple files. | ||
* @exports OpenTimestamps/stamp | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
const test = require('tape') | ||
const Calendar = require('../src/calendar.js') | ||
const Utils = require('../src/utils.js') | ||
const { URL } = require('url') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it is auto merging stuff that add URL here and in the src/calendar.js file |
||
|
||
test('Calendar.privateSubmit()', assert => { | ||
const digest = Array.from(Utils.randBytes(32)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getAttestations() returns a Set of every attestations walking the timestamp tree until the leaf. I think in this case, you need to check only the current attestations as:
timestamp.attestations