Skip to content

Commit

Permalink
fix: Period.proof throwing incorrect error when tx is not in the peri…
Browse files Browse the repository at this point in the history
…od. (#163)

Reason: wrong check (blockPos is undefined, not negative)
  • Loading branch information
troggy authored Nov 26, 2019
1 parent 733d7e8 commit eec71e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions lib/period.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,17 @@ export default class Period {
}

proof(tx) {
let blockPos;
let periodPos = -1;
for (let i = 0; i < this.blockList.length; i++) {
const pos = this.blockList[i].txHashList.indexOf(tx.hash());
if (pos >= 0) {
if (periodPos >= 0) {
throw Error('tx doublespend');
}
blockPos = pos;
periodPos = i;
}
}
if (blockPos < 0) {
if (periodPos < 0) {
throw Error('tx not in this period');
}
// get block proof in period
Expand Down
20 changes: 20 additions & 0 deletions lib/period.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ describe('periods', () => {
done();
});

it('should throw if tx is not in the period', (done) => {
const value = 50000000;
const color = 1337;

const deposit1 = Tx.deposit(0, value, ADDR, color);
const block1 = new Block(1);
block1.addTx(deposit1);

const deposit2 = Tx.deposit(1, value, ADDR, color);
const block2 = new Block(2);
block2.addTx(deposit2);

const period = new Period(null, [block1]);
period.setValidatorData(slotId, ADDR);
expect(
() => period.proof(deposit2)
).to.throw('tx not in this period');
done();
});

it('should allow to get proof from period with CAS bitmap.', (done) => {
const height = 123;
const value = 50000000;
Expand Down

0 comments on commit eec71e1

Please sign in to comment.