-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathwallet-events-test.js
201 lines (159 loc) · 5.5 KB
/
wallet-events-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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
const assert = require('bsert');
const Address = require('../lib/primitives/address');
const FullNode = require('../lib/node/fullnode');
const Wallet = require('../lib/wallet/wallet');
const WalletKey = require('../lib/wallet/walletkey');
const ChainEntry = require('../lib/blockchain/chainentry');
const MemWallet = require('./util/memwallet');
const {forEvent} = require('./util/common');
describe('WalletDB Events', function () {
const node = new FullNode({
memory: true,
network: 'regtest',
noDNS: true,
plugins: [require('../lib/wallet/plugin')]
});
const { wdb } = node.require('walletdb');
let wallet;
async function mineBlocks(n, addr) {
addr = addr ? addr : new Address().toString('regtest');
for (let i = 0; i < n; i++) {
const block = await node.miner.mineBlock(null, addr);
await node.chain.add(block);
}
}
before(async () => {
await node.open();
wallet = await wdb.get('primary');
});
after(async () => {
await node.close();
});
it('should emit `open`/`close` and `connect`/`disconnect` events', async () => {
const closeEvent = forEvent(wdb, 'close');
const disconnectEvent = forEvent(wdb, 'disconnect');
await node.close();
await disconnectEvent;
await closeEvent;
const openEvent = forEvent(wdb, 'open');
const connectEvent = forEvent(wdb, 'connect');
const syncDone = forEvent(wdb, 'sync done');
await node.open();
await openEvent;
await connectEvent;
await syncDone;
wallet = await wdb.get('primary');
});
it('should emit `tx` events', async () => {
const waiter = forEvent(wdb, 'tx');
const walletReceive = await wallet.receiveAddress();
await mineBlocks(1, walletReceive);
const events = await waiter;
const [w, tx] = events[0].values;
assert(w);
assert(w instanceof Wallet);
assert(w.wid === 0);
assert(tx);
assert(tx.outputs[0].address.equals(walletReceive));
assert(tx.outputs[0].value === 2000 * 1e6);
});
it('should emit `address` events', async () => {
const waiter = forEvent(wdb, 'address');
const walletReceive = await wallet.receiveAddress();
await mineBlocks(1, walletReceive);
const events = await waiter;
const [w, walletKey] = events[0].values;
assert(w);
assert(w instanceof Wallet);
assert(w.wid === 0);
assert(walletKey);
assert(Array.isArray(walletKey) && walletKey.length > 0);
assert(walletKey[0] instanceof WalletKey);
});
describe('should emit `block connect` events', () => {
it('with a block that includes a wallet tx', async () => {
const waiter = forEvent(wdb, 'block connect');
const walletReceive = await wallet.receiveAddress();
await wallet.send({
outputs: [
{ address: walletReceive, value: 42 * 1e6 }
]
});
await mineBlocks(1);
const events = await waiter;
const [entry, txs] = events[0].values;
assert(entry);
assert(entry instanceof ChainEntry);
assert(txs);
assert(Array.isArray(txs) && txs.length === 1);
const output = txs[0].outputs.find(
output => output.address.equals(walletReceive) && output.value === 42 * 1e6
);
assert(output);
});
it('with a block that does not include a wallet tx', async () => {
// Create a transaction not related to any wallet in wdb
let otherTx;
{
// New wallet
const otherWallet = new MemWallet({ network: 'regtest' });
node.chain.on('connect', (entry, block) => {
otherWallet.addBlock(entry, block.txs);
});
// Fund wallet
const otherReceive = await otherWallet.getReceive();
await mineBlocks(2, otherReceive);
// Create tx
const otherMtx = await otherWallet.create({
outputs: [
{ address: await otherWallet.getReceive(), value: 42 * 1e6 }
]
});
await otherWallet.sign(otherMtx);
otherTx = otherMtx.toTX();
}
const waiter = forEvent(wdb, 'block connect');
await node.sendTX(otherTx);
await mineBlocks(1);
const events = await waiter;
const [entry, txs] = events[0].values;
assert(entry);
assert(entry instanceof ChainEntry);
// txs is empty as none belong to any wallet
assert(txs);
assert(Array.isArray(txs) && txs.length === 0);
});
});
describe('should emit `block disconnect` events', () => {
it('with a block that includes a wallet tx', async () => {
// Mine a block
const walletReceive = await wallet.receiveAddress();
await wallet.send({
outputs: [
{ address: walletReceive, value: 42 * 1e6 }
]
});
await mineBlocks(1);
// Disconnect it
const waiter = forEvent(wdb, 'block disconnect');
const entryToDisconnect = node.chain.tip;
await node.chain.disconnect(entryToDisconnect);
const events = await waiter;
const entry = events[0].values[0];
assert(entry);
assert(entry instanceof ChainEntry);
assert(entry.hash = entryToDisconnect.hash);
});
it('with a block that does not include a wallet tx', async () => {
const waiter = forEvent(wdb, 'block disconnect');
const entryToDisconnect = node.chain.tip;
await node.chain.disconnect(entryToDisconnect);
const events = await waiter;
const entry = events[0].values[0];
assert(entry);
assert(entry instanceof ChainEntry);
assert(entry.hash = entryToDisconnect.hash);
});
});
});