-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathwallet-importname-test.js
295 lines (238 loc) · 9.25 KB
/
wallet-importname-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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'use strict';
const assert = require('bsert');
const Network = require('../lib/protocol/network');
const FullNode = require('../lib/node/fullnode');
const Address = require('../lib/primitives/address');
const rules = require('../lib/covenants/rules');
const WalletClient = require('../lib/client/wallet');
const {forValue} = require('./util/common');
const network = Network.get('regtest');
const ports = {
p2p: 14331,
node: 14332,
wallet: 14333
};
const node = new FullNode({
memory: true,
network: 'regtest',
plugins: [require('../lib/wallet/plugin')],
env: {
'HSD_WALLET_HTTP_PORT': ports.wallet.toString()
}
});
const wclient = new WalletClient({
port: ports.wallet
});
const {wdb} = node.require('walletdb');
const GNAME_SIZE = 10;
const name = rules.grindName(GNAME_SIZE, 1, network);
const nameHash = rules.hashName(name);
const wrongName = rules.grindName(GNAME_SIZE, 1, network);
const wrongNameHash = rules.hashName(wrongName);
let alice, bob, aliceReceive, bobReceive;
let charlie;
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);
}
}
describe('Wallet Import Name', function() {
before(async () => {
await node.open();
await wclient.open();
alice = await wdb.create();
bob = await wdb.create();
charlie = await wdb.create({id: 'charlie'});
aliceReceive = await alice.receiveAddress();
bobReceive = await bob.receiveAddress();
});
after(async () => {
await wclient.close();
await node.close();
});
it('should fund both wallets', async () => {
await mineBlocks(2, aliceReceive);
await mineBlocks(2, bobReceive);
// Wallet rescan is an effective way to ensure that
// wallet and chain are synced before proceeding.
await wdb.rescan(0);
const aliceBal = await alice.getBalance();
const bobBal = await bob.getBalance();
assert(aliceBal.confirmed === 2000 * 2 * 1e6);
assert(bobBal.confirmed === 2000 * 2 * 1e6);
});
it('should open name from Alice\'s wallet', async () => {
// Alice an Bob are not tracking this name
const ns1 = await alice.getNameStateByName(name);
assert(ns1 === null);
const ns2 = await bob.getNameStateByName(name);
assert(ns2 === null);
await alice.sendOpen(name);
await alice.sendOpen(wrongName);
await mineBlocks(network.names.treeInterval);
await wdb.rescan(0);
// Alice is now tracking, Bob is not
const ns3 = await alice.getNameStateByName(name);
assert(ns3);
const ns4 = await bob.getNameStateByName(name);
assert(ns4 === null);
});
it('should not re-import an existing name', async () => {
await assert.rejects(
alice.importName(name),
{message: 'Name already exists.'}
);
});
it('should bid on names from Alice\'s wallet', async () => {
// Sanity check: bids are allowed starting in the NEXT block
await assert.rejects(
alice.sendBid(name, 100001, 200001),
{message: `Name has not reached the bidding phase yet: ${name}.`}
);
await mineBlocks(1);
await wdb.rescan(0);
// Send 1 bid, confirm
await alice.sendBid(name, 100001, 200001);
await mineBlocks(1);
// Send 2 bids in one block, confirm
await alice.sendBid(name, 100002, 200002);
await alice.sendBid(name, 100003, 200003);
await mineBlocks(1);
// Send 3 bids in one block, confirm
await alice.sendBid(name, 100004, 200004);
await alice.sendBid(name, 100005, 200005);
await alice.sendBid(name, 100006, 200006);
await mineBlocks(1);
// One block with some bids for another name
await alice.sendBid(wrongName, 100007, 200007);
await alice.sendBid(wrongName, 100008, 200008);
await mineBlocks(1);
// Still in the bidding phase with one block left
const ns = await node.chain.db.getNameStateByName(name);
assert(ns.isBidding(node.chain.height + 1, network));
await wdb.rescan(0);
// Alice is tracking auction, Bob is not
const aliceBids = await alice.getBidsByName(name);
assert.strictEqual(aliceBids.length, 6);
for (const bid of aliceBids)
assert(bid.own);
const bobBids = await bob.getBidsByName(name);
assert.strictEqual(bobBids.length, 0);
});
it('should import name into Bob\'s wallet', async () => {
await bob.importName(name);
// Bob's wallet still has no name data for this name
const ns1 = await bob.getNameStateByName(name);
assert(ns1 === null);
// The WalletDB knows Alice & Bob are watching this name
const map1 = await wdb.getNameMap(nameHash);
assert(map1.wids.has(alice.wid));
assert(map1.wids.has(bob.wid));
// Only Alice is watching the other name
const map2 = await wdb.getNameMap(wrongNameHash);
assert(map1.wids.has(alice.wid));
assert(!map2.wids.has(bob.wid));
});
it('should not track bids for imported name before OPEN', async () => {
// Rescan covers bidding phase but does not include OPEN transaction
await wdb.rescan(6);
// Alice is tracking auction, Bob is not
const aliceBids = await alice.getBidsByName(name);
assert.strictEqual(aliceBids.length, 6);
for (const bid of aliceBids)
assert(bid.own);
const bobBids = await bob.getBidsByName(name);
assert.strictEqual(bobBids.length, 0);
});
it('should start tracking bids after seeing OPEN', async () => {
await wdb.rescan(0);
// After a sufficient rescan, Bob now has all auction data
const ns2 = await bob.getNameStateByName(name);
assert(ns2);
// ...even though he hasn't placed a bid yet
const bobBids = await bob.getBidsByName(name);
assert.strictEqual(bobBids.length, 6);
for (const bid of bobBids)
assert(!bid.own);
// Sanity check: only the "right" name was imported
const ns3 = await bob.getNameStateByName(wrongName);
assert(ns3 === null);
const ns4 = await alice.getNameStateByName(wrongName);
assert(ns4);
});
describe('rpc importname', function() {
it('should not have name data in Charlie\'s wallet', async () => {
const ns1 = await charlie.getNameStateByName(name);
assert(ns1 === null);
});
it('should import name with rescan', async () => {
await wclient.execute('selectwallet', ['charlie']);
await wclient.execute('importname', [name, 0]);
const ns1 = await charlie.getNameStateByName(name);
assert(ns1);
const charlieBids = await bob.getBidsByName(name);
assert.strictEqual(charlieBids.length, 6);
for (const bid of charlieBids)
assert(!bid.own);
});
it('should not re-import name', async () => {
await wclient.execute('selectwallet', ['charlie']);
await assert.rejects(
wclient.execute('importname', [name, 0]),
{message: 'Name already exists.'}
);
});
});
describe('import multiple / overlapping names', function() {
const name1 = rules.grindName(GNAME_SIZE, 1, network);
const name2 = rules.grindName(GNAME_SIZE, 1, network);
const name3 = rules.grindName(GNAME_SIZE, 1, network);
let startHeight;
it('should open and bid from Alice\'s wallet', async () => {
await alice.sendOpen(name1);
await alice.sendOpen(name2);
await alice.sendOpen(name3);
startHeight = node.chain.tip.height;
await mineBlocks(network.names.treeInterval + 1);
await forValue(wdb, 'height', node.chain.height);
await alice.sendBid(name1, 10000, 10000);
await alice.sendBid(name2, 20000, 20000);
await alice.sendBid(name3, 30000, 30000);
await mineBlocks(1);
});
it('should import names into Bob\'s wallet', async () => {
assert.strictEqual(await bob.getNameStateByName(name1), null);
assert.strictEqual(await bob.getNameStateByName(name2), null);
assert.strictEqual(await bob.getNameStateByName(name3), null);
assert.deepStrictEqual(await bob.getBidsByName(name1), []);
assert.deepStrictEqual(await bob.getBidsByName(name2), []);
assert.deepStrictEqual(await bob.getBidsByName(name3), []);
await bob.importName(name1);
await wdb.rescan(startHeight);
assert.ok(await bob.getNameStateByName(name1));
assert.strictEqual(await bob.getNameStateByName(name2), null);
assert.strictEqual(await bob.getNameStateByName(name3), null);
assert.ok(await bob.getBidsByName(name1));
assert.deepStrictEqual(await bob.getBidsByName(name2), []);
assert.deepStrictEqual(await bob.getBidsByName(name3), []);
await bob.importName(name2);
await wdb.rescan(startHeight);
assert.ok(await bob.getNameStateByName(name1));
assert.ok(await bob.getNameStateByName(name2));
assert.strictEqual(await bob.getNameStateByName(name3), null);
assert.ok(await bob.getBidsByName(name1));
assert.ok(await bob.getBidsByName(name2));
assert.deepStrictEqual(await bob.getBidsByName(name3), []);
await bob.importName(name3);
await wdb.rescan(startHeight);
assert.ok(await bob.getNameStateByName(name1));
assert.ok(await bob.getNameStateByName(name2));
assert.ok(await bob.getNameStateByName(name3));
assert.ok(await bob.getBidsByName(name1));
assert.ok(await bob.getBidsByName(name2));
assert.ok(await bob.getBidsByName(name3));
});
});
});