forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoins-test.js
102 lines (77 loc) · 2.7 KB
/
coins-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
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const bio = require('bufio');
const assert = require('./util/assert');
const Output = require('../lib/primitives/output');
const Input = require('../lib/primitives/input');
const Outpoint = require('../lib/primitives/outpoint');
const CoinView = require('../lib/coins/coinview');
const CoinEntry = require('../lib/coins/coinentry');
const common = require('./util/common');
const tx1 = common.readTX('tx1');
function reserialize(coin) {
const raw = coin.toRaw();
const entry = CoinEntry.fromRaw(raw);
entry.raw = null;
return CoinEntry.fromRaw(entry.toRaw());
}
function deepCoinsEqual(a, b) {
assert.strictEqual(a.version, b.version);
assert.strictEqual(a.height, b.height);
assert.strictEqual(a.coinbase, b.coinbase);
assert.bufferEqual(a.raw, b.raw);
}
describe('Coins', function() {
it('should instantiate coinview from tx', () => {
const [tx] = tx1.getTX();
const hash = tx.hash();
const view = new CoinView();
const prevout = new Outpoint(hash, 0);
const input = Input.fromOutpoint(prevout);
view.addTX(tx, 1);
const coins = view.get(hash);
assert.strictEqual(coins.outputs.size, tx.outputs.length);
const entry = coins.get(0);
assert(entry);
assert.strictEqual(entry.version, 1);
assert.strictEqual(entry.height, 1);
assert.strictEqual(entry.coinbase, false);
assert.strictEqual(entry.raw, null);
assert.instanceOf(entry.output, Output);
assert.strictEqual(entry.spent, false);
const output = view.getOutputFor(input);
assert(output);
deepCoinsEqual(entry, reserialize(entry));
});
it('should spend an output', () => {
const [tx] = tx1.getTX();
const hash = tx.hash();
const view = new CoinView();
view.addTX(tx, 1);
const coins = view.get(hash);
assert(coins);
const length = coins.outputs.size;
view.spendEntry(new Outpoint(hash, 0));
assert.strictEqual(view.get(hash), coins);
const entry = coins.get(0);
assert(entry);
assert(entry.spent);
deepCoinsEqual(entry, reserialize(entry));
assert.strictEqual(coins.outputs.size, length);
assert.strictEqual(view.undo.items.length, 1);
});
it('should handle coin view', () => {
const [tx, view] = tx1.getTX();
const size = view.getSize(tx);
const bw = bio.write(size);
const raw = view.toWriter(bw, tx).render();
const br = bio.read(raw);
const res = CoinView.fromReader(br, tx);
const prev = tx.inputs[0].prevout;
const coins = res.get(prev.hash);
assert.strictEqual(coins.outputs.size, 1);
assert.strictEqual(coins.get(0), null);
deepCoinsEqual(coins.get(1), reserialize(coins.get(1)));
});
});