commit
b312f0e9b8
@ -71,7 +71,7 @@ class Outpoint {
|
||||
|
||||
clone() {
|
||||
const outpoint = new this.constructor();
|
||||
outpoint.hash = this.value;
|
||||
outpoint.hash = this.hash;
|
||||
outpoint.index = this.index;
|
||||
return outpoint;
|
||||
}
|
||||
|
||||
118
test/outpoint-test.js
Normal file
118
test/outpoint-test.js
Normal file
@ -0,0 +1,118 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
'use strict';
|
||||
|
||||
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;
|
||||
|
||||
describe('Outpoint', () => {
|
||||
let raw1, tx1, out1;
|
||||
beforeEach(() => {
|
||||
tx1 = common.readTX('tx1').getRaw();
|
||||
raw1 = tx1.slice(5, 5+OUTPOINT_SIZE);
|
||||
out1 = Outpoint.fromRaw(raw1);
|
||||
});
|
||||
|
||||
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(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 = 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', () => {
|
||||
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 from a tx', () => {
|
||||
const tx = TX.fromRaw(tx1);
|
||||
const index = 0;
|
||||
const fromTX = Outpoint.fromTX(tx, index);
|
||||
|
||||
assert.equal(fromTX.hash, tx.hash('hex'));
|
||||
assert.equal(fromTX.index, index);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user