forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wallet-rescan-test.js
162 lines (127 loc) · 3.89 KB
/
wallet-rescan-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
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const assert = require('./util/assert');
const rimraf = require('./util/rimraf');
const sleep = require('./util/sleep');
const KeyRing = require('../lib/primitives/keyring');
const {
initFullNode,
initNodeClient,
initWalletClient,
initWallet,
generateInitialBlocks,
generateBlocks,
sendCoinbase
} = require('./util/regtest');
const testPrefix = '/tmp/bcoin-fullnode';
const genesisTime = 1534965859;
const ports = {
full: {
p2p: 49331,
node: 49332,
wallet: 49333
}
};
describe('Wallet Rescan', function() {
this.timeout(30000);
// SPV isn't tested here as rescanning will
// not work in that case, furthermore it will also
// not work for pruned nodes. There may need to be
// additional error messages on related APIs that
// assume use of rescan.
let node, wallet = null;
let nclient, wclient = null;
let coinbase, coinbaseKey = null;
let fulladdr = null;
let key1 = null;
before(async () => {
await rimraf(testPrefix);
node = await initFullNode({
ports,
prefix: testPrefix,
logLevel: 'none'
});
nclient = await initNodeClient({ports: ports.full});
wclient = await initWalletClient({ports: ports.full});
wallet = await initWallet(wclient);
coinbaseKey = KeyRing.generate();
await wclient.execute('selectwallet', ['test']);
fulladdr = await wclient.execute('getnewaddress', ['blue']);
// Use coinbase outside of any wallet so
// that we can send funds to various addresses
// without affecting the wallets.
coinbase = coinbaseKey.getAddress('base58', 'regtest').toString();
await generateInitialBlocks({
nclient,
coinbase,
genesisTime,
blocks: 200
});
// TODO remove this
await sleep(1000);
key1 = KeyRing.generate();
let height = 0;
// Send funds to the existing wallets
await sendCoinbase({
nclient,
height: ++height,
coinbaseKey,
address: fulladdr
});
await generateBlocks(1, nclient, coinbase);
// Send funds to the addresses to be imported
let importTotal = 0;
for (++height; height < 14; height++) {
const address = key1.getAddress('base58', 'regtest').toString();
await sendCoinbase({
nclient,
height,
coinbaseKey,
address
});
importTotal++;
}
assert.strictEqual(importTotal, 12);
await generateBlocks(1, nclient, coinbase);
// Send more funds to the wallets after the
// addresses to be imported
await sendCoinbase({
nclient,
height: ++height,
coinbaseKey,
address: fulladdr
});
await generateBlocks(1, nclient, coinbase);
// TODO remove this
await sleep(1000);
const history = await wclient.execute('listhistory',
[null, 100, true]);
assert.strictEqual(history.length, 2);
// Import keys into wallets and rescan
const key1Priv = key1.getPrivateKey('base58', 'regtest');
await wclient.execute('importprivkey',
[key1Priv, null, true]);
// TODO remove this
await sleep(1000);
});
after(async () => {
await wallet.close();
await wclient.close();
await nclient.close();
await node.close();
});
it('will include txs of imported addresses', async () => {
const history = await wclient.execute('listhistory',
[null, 100, true]);
assert.strictEqual(history.length, 14);
});
it('will include txs in correct order', async() => {
const history = await wclient.execute('listhistory',
[null, 100, true]);
assert.strictEqual(history[0].account, 'blue');
for (let i = 1; i < 13; i++)
assert.strictEqual(history[i].account, 'default');
assert.strictEqual(history[13].account, 'blue');
});
});