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); + }); });