forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutpoint-test.js
119 lines (96 loc) · 3.35 KB
/
outpoint-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* 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 = Buffer.from(out1RevHash.hash);
out1RevHash.hash[0] = 0;
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.strictEqual(out1.rhash(), util.revHex(out1.hash));
assert.strictEqual(out1.txid(), util.revHex(out1.hash));
});
it('should serialize to a key suitable for hash table', () => {
const expected = out1.toRaw();
const actual = out1.toKey();
assert.bufferEqual(expected, actual);
});
it('should inject properties from hash table key', () => {
const key = out1.toKey();
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.bufferEqual(fromTX.hash, tx.hash());
assert.strictEqual(fromTX.index, index);
});
});