From 68b3ddf48225e20078aa097e5eb59a12ed61d231 Mon Sep 17 00:00:00 2001 From: Buck Date: Fri, 3 Nov 2017 14:51:37 -0700 Subject: [PATCH 1/4] fixed outpoint bug and added first test --- lib/primitives/outpoint.js | 2 +- test/outpoint-test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/outpoint-test.js diff --git a/lib/primitives/outpoint.js b/lib/primitives/outpoint.js index 10292dd5..379dc5a6 100644 --- a/lib/primitives/outpoint.js +++ b/lib/primitives/outpoint.js @@ -69,7 +69,7 @@ Outpoint.fromOptions = function fromOptions(options) { Outpoint.prototype.clone = function clone() { const outpoint = new Outpoint(); - outpoint.hash = this.value; + outpoint.hash = this.hash; outpoint.index = this.index; return outpoint; }; diff --git a/test/outpoint-test.js b/test/outpoint-test.js new file mode 100644 index 00000000..f55566f7 --- /dev/null +++ b/test/outpoint-test.js @@ -0,0 +1,26 @@ +/* eslint-env mocha */ + +'use strict'; + +const Outpoint = require('../lib/primitives/outpoint'); +const assert = require('./util/assert'); +const common = require('./util/common'); + +const OUTPOINT_SIZE = 36; +const tx1 = common.readTX('tx1').getRaw(); +const rawOutpoint1 = tx1.slice(5, 5+OUTPOINT_SIZE); + +const tx2 = common.readTX('tx2').getRaw(); +const rawOutpoint2 = tx2.slice(5, 5+OUTPOINT_SIZE); + +describe('Outpoint', () => { + it('should clone the outpoint correctly', () => { + const raw = rawOutpoint1.slice(); + const outpointObject = Outpoint.fromRaw(raw); + const clone = outpointObject.clone(); + const equals = outpointObject.equals(clone); + + assert.strictEqual(outpointObject !== clone, true); + assert.strictEqual(equals, true); + }); +}); From 48d41ed55d922edf2a33a7179a3c250eeefa4ee6 Mon Sep 17 00:00:00 2001 From: Buck Date: Fri, 3 Nov 2017 18:42:33 -0700 Subject: [PATCH 2/4] tests for outpoint --- test/outpoint-test.js | 115 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 12 deletions(-) diff --git a/test/outpoint-test.js b/test/outpoint-test.js index f55566f7..86bdeb3d 100644 --- a/test/outpoint-test.js +++ b/test/outpoint-test.js @@ -5,22 +5,113 @@ const Outpoint = require('../lib/primitives/outpoint'); const assert = require('./util/assert'); const common = require('./util/common'); - +const util = require('../lib/utils/util'); +const TX = require('../lib/primitives/tx'); const OUTPOINT_SIZE = 36; -const tx1 = common.readTX('tx1').getRaw(); -const rawOutpoint1 = tx1.slice(5, 5+OUTPOINT_SIZE); - -const tx2 = common.readTX('tx2').getRaw(); -const rawOutpoint2 = tx2.slice(5, 5+OUTPOINT_SIZE); +const raw3 = common.readTX('tx2').getRaw(); +const raw4 = common.readTX('tx1').getRaw(); +console.log(TX.fromRaw(raw3).inputs); describe('Outpoint', () => { - it('should clone the outpoint correctly', () => { - const raw = rawOutpoint1.slice(); - const outpointObject = Outpoint.fromRaw(raw); - const clone = outpointObject.clone(); - const equals = outpointObject.equals(clone); + let raw1, raw2, tx1, tx2, out1, out2; + beforeEach(() => { + tx1 = common.readTX('tx1').getRaw(); + tx2 = common.readTX('tx3').getRaw(); + raw1 = tx1.slice(5, 5+OUTPOINT_SIZE); + raw2 = tx2.slice(5, 5+OUTPOINT_SIZE); + out1 = Outpoint.fromRaw(raw1); + out2 = Outpoint.fromRaw(raw2); + }); - assert.strictEqual(outpointObject !== clone, true); + it('should clone the outpoint correctly', () => { + const out1 = Outpoint.fromRaw(raw1); + const clone = out1.clone(); + const equals = out1.equals(clone); + + assert.strictEqual(out1 !== clone, true); assert.strictEqual(equals, true); }); + + it('should create outpoint from options object', () => { + const options = {}; + options.hash = out1.hash; + options.index = out1.index; + + const newOut = Outpoint.fromOptions(options); + assert(newOut.equals(out1), true); + }); + + it('should check hash and index are equal', () => { + const out1Clone = Outpoint.fromOptions(Object.assign(out1, {})); + + assert(out1.equals(out2) === false, true); + assert(out1Clone.hash === out1.hash, out1Clone.equals(out1)); + assert(out1Clone.index === out1.index, out1Clone.equals(out1)); + }); + + it('should compare the indexes between outpoints', () => { + const index1 = out1.index; + const index2 = out2.index; + assert.equal(out1.compare(out2), index1 - index2); + }); + + it('should detect if the outpoint is null', () => { + const rawHash = '00000000000000000000000000000000000000000000' + + '00000000000000000000'; + const rawIndex = 'ffffffff'; + const nullOut = Outpoint.fromRaw(Buffer.from(rawHash + rawIndex, 'hex')); + assert(nullOut.isNull(), true); + }); + + it('should retrieve little endian hash', () => { + assert.equal(out1.rhash(), util.revHex(out1.hash)); + assert.equal(out1.txid(), util.revHex(out1.hash)); + }); + + it('should serialize to a key suitable for hash table', () => { + const expected = out1.hash + out1.index; + const actual = out1.toKey(); + assert.equal(expected, actual); + }); + + it('should inject properties from hash table key', () => { + const key = out1.hash + out1.index; + const fromKey = Outpoint.fromKey(key); + assert(out1.equals(fromKey), true); + }); + + it('should return a size of 36', () => { + assert(out1.getSize() === OUTPOINT_SIZE, true); + }); + + it('should create an outpoint from JSON', () => { + const json = { + hash: out1.txid(), + index: out1.index + }; + const fromJSON = Outpoint.fromJSON(json); + + assert.deepEqual(out1, fromJSON); + }); + + it('should return an object with reversed hash', () => { + const hash = out1.hash; + const index = out1.index; + + const expected = { + hash: util.revHex(hash), + index + }; + assert.deepEqual(expected, out1.toJSON()); + }); + + it('should instantiate an outpoint fron a tx', () => { + const inputs = TX.fromRaw(tx1).inputs; + const tx = new TX({ inputs }); + const index = 0; + const fromTX = Outpoint.fromTX(tx, index); + + assert.equal(fromTX.hash, tx.hash('hex')); + assert.equal(fromTX.index, index); + }); }); From c51cc88ef61cd43f5765afbf8769a92745ad0851 Mon Sep 17 00:00:00 2001 From: Buck Date: Sun, 5 Nov 2017 14:46:02 -0800 Subject: [PATCH 3/4] PR cleanup --- test/outpoint-test.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/test/outpoint-test.js b/test/outpoint-test.js index 86bdeb3d..5d9daa46 100644 --- a/test/outpoint-test.js +++ b/test/outpoint-test.js @@ -9,9 +9,6 @@ const util = require('../lib/utils/util'); const TX = require('../lib/primitives/tx'); const OUTPOINT_SIZE = 36; -const raw3 = common.readTX('tx2').getRaw(); -const raw4 = common.readTX('tx1').getRaw(); -console.log(TX.fromRaw(raw3).inputs); describe('Outpoint', () => { let raw1, raw2, tx1, tx2, out1, out2; beforeEach(() => { @@ -44,15 +41,23 @@ describe('Outpoint', () => { it('should check hash and index are equal', () => { const out1Clone = Outpoint.fromOptions(Object.assign(out1, {})); - assert(out1.equals(out2) === false, true); - assert(out1Clone.hash === out1.hash, out1Clone.equals(out1)); - assert(out1Clone.index === out1.index, out1Clone.equals(out1)); + assert(out1Clone.hash === out1.hash); + assert(out1Clone.index === out1.index); }); it('should compare the indexes between outpoints', () => { + const out1RevHash = out1.clone(); + out1RevHash.hash = out1RevHash.rhash(); + + const out1AdjIndex = out1.clone(); + out1AdjIndex.index += 1; + const index1 = out1.index; - const index2 = out2.index; - assert.equal(out1.compare(out2), index1 - index2); + const index2 = out1AdjIndex.index; + // assert that it compares txid first + assert(out1.compare(out1RevHash) !== 0, 'txid wasn\'t compared correctly'); + assert(out1.compare(out1) === 0); + assert(out1.compare(out1AdjIndex) === index1 - index2); }); it('should detect if the outpoint is null', () => { @@ -105,9 +110,8 @@ describe('Outpoint', () => { assert.deepEqual(expected, out1.toJSON()); }); - it('should instantiate an outpoint fron a tx', () => { - const inputs = TX.fromRaw(tx1).inputs; - const tx = new TX({ inputs }); + it('should instantiate an outpoint from a tx', () => { + const tx = TX.fromRaw(tx1); const index = 0; const fromTX = Outpoint.fromTX(tx, index); From dd69f18cc349122165e47387a56ff5b320603901 Mon Sep 17 00:00:00 2001 From: Buck Date: Sun, 5 Nov 2017 14:48:37 -0800 Subject: [PATCH 4/4] fixed lint errors --- test/outpoint-test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/outpoint-test.js b/test/outpoint-test.js index 5d9daa46..46c71575 100644 --- a/test/outpoint-test.js +++ b/test/outpoint-test.js @@ -10,14 +10,11 @@ const TX = require('../lib/primitives/tx'); const OUTPOINT_SIZE = 36; describe('Outpoint', () => { - let raw1, raw2, tx1, tx2, out1, out2; + let raw1, tx1, out1; beforeEach(() => { tx1 = common.readTX('tx1').getRaw(); - tx2 = common.readTX('tx3').getRaw(); raw1 = tx1.slice(5, 5+OUTPOINT_SIZE); - raw2 = tx2.slice(5, 5+OUTPOINT_SIZE); out1 = Outpoint.fromRaw(raw1); - out2 = Outpoint.fromRaw(raw2); }); it('should clone the outpoint correctly', () => {