-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathairdrop-test.js
354 lines (270 loc) · 8.73 KB
/
airdrop-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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict';
const fs = require('fs');
const {resolve} = require('path');
const assert = require('bsert');
const Chain = require('../lib/blockchain/chain');
const BlockStore = require('../lib/blockstore/level');
const WorkerPool = require('../lib/workers/workerpool');
const Miner = require('../lib/mining/miner');
const MemWallet = require('./util/memwallet');
const Network = require('../lib/protocol/network');
const AirdropProof = require('../lib/primitives/airdropproof');
const network = Network.get('regtest');
const workers = new WorkerPool({
enabled: false,
size: 2
});
const AIRDROP_PROOF_FILE = resolve(__dirname, 'data', 'airdrop-proof.base64');
const FAUCET_PROOF_FILE = resolve(__dirname, 'data', 'faucet-proof.base64');
const read = file => Buffer.from(fs.readFileSync(file, 'binary'), 'base64');
// Sent to:
// {
// pub: '02a8959cc6491aed3fb96b3b684400311f2779fb092b026a4b170b35c175d48cec',
// hash: '95cb6129c6b98179866094b2717bfbe27d9c1921',
// addr: 'hs1qjh9kz2wxhxqhnpnqjje8z7lmuf7ecxfp6kxlly'
// }
// Doxing myself (watch some wiseguy publish this on mainnet):
const rawProof = read(AIRDROP_PROOF_FILE);
const rawFaucetProof = read(FAUCET_PROOF_FILE); // hs1qmjpjjgpz7dmg37paq9uksx4yjp675690dafg3q
function createNode() {
const blocks = new BlockStore({
memory: true,
network
});
const chain = new Chain({
memory: true,
blocks,
network,
workers
});
const miner = new Miner({
chain,
workers
});
return {
chain,
blocks,
miner,
cpu: miner.cpu,
wallet: () => {
const wallet = new MemWallet({ network });
chain.on('connect', (entry, block) => {
wallet.addBlock(entry, block.txs);
});
chain.on('disconnect', (entry, block) => {
wallet.removeBlock(entry, block.txs);
});
return wallet;
}
};
}
describe('Airdrop', function() {
this.timeout(15000);
const node = createNode();
const orig = createNode();
const comp = createNode();
const {chain, miner, cpu, blocks} = node;
const wallet = node.wallet();
let snapshot = null;
it('should open chain, blocks and miner', async () => {
await blocks.open();
await chain.open();
await miner.open();
});
it('should add addrs to miner', async () => {
miner.addresses.length = 0;
miner.addAddress(wallet.getReceive());
});
it('should mine 20 blocks', async () => {
for (let i = 0; i < 20; i++) {
const block = await cpu.mineBlock();
assert(block);
assert(await chain.add(block));
}
});
it('should fail to mine airdrop proof', async () => {
const proof = AirdropProof.decode(rawProof);
const key = proof.getKey();
assert(key);
// Flipping one bit should break everything.
key.C1[Math.random() * key.C1.length | 0] ^= 1;
proof.key = key.encode();
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
await assert.rejects(chain.add(block),
{ reason: 'mandatory-script-verify-flag-failed' });
});
it('should mine airdrop proof', async () => {
const proof = AirdropProof.decode(rawProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
assert(block.txs.length === 1);
const [cb] = block.txs;
assert(cb.inputs.length === 2);
assert(cb.outputs.length === 2);
const [, input] = cb.inputs;
const [, output] = cb.outputs;
assert(input);
assert(input.prevout.isNull());
assert(input.witness.length === 1);
assert.strictEqual(output.value, 4246894314);
assert.strictEqual(output.address.toString(),
'hs1qlpj3rwvtz83fvk6z0nm2rw57f3cwdczmc2j6a2');
assert(await chain.add(block));
});
it('should prevent double spend with bitfield', async () => {
const proof = AirdropProof.decode(rawProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
await assert.rejects(chain.add(block),
{ reason: 'bad-txns-bits-missingorspent' });
});
it('should mine 10 blocks', async () => {
for (let i = 0; i < 10; i++) {
const block = await cpu.mineBlock();
assert(block);
assert(await chain.add(block));
}
snapshot = chain.db.state.value;
});
it('should open other nodes', async () => {
await orig.blocks.open();
await orig.chain.open();
await orig.miner.open();
await comp.blocks.open();
await comp.chain.open();
await comp.miner.open();
});
it('should clone the chain', async () => {
for (let i = 1; i <= chain.height; i++) {
const block = await chain.getBlock(i);
assert(block);
assert(await orig.chain.add(block));
}
});
it('should mine a competing chain', async () => {
while (comp.chain.tip.chainwork.lte(chain.tip.chainwork)) {
const block = await comp.cpu.mineBlock();
assert(block);
assert(await comp.chain.add(block));
}
});
it('should reorg the airdrop', async () => {
let reorgd = false;
chain.once('reorganize', () => reorgd = true);
for (let i = 1; i <= comp.chain.height; i++) {
assert(!reorgd);
const block = await comp.chain.getBlock(i);
assert(block);
assert(await chain.add(block));
}
assert(reorgd);
});
it('should mine airdrop+faucet proof', async () => {
const proof = AirdropProof.decode(rawProof);
const fproof = AirdropProof.decode(rawFaucetProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.addAirdrop(fproof);
job.refresh();
const block = await job.mineAsync();
assert(block.txs.length === 1);
const [cb] = block.txs;
assert(cb.inputs.length === 3);
assert(cb.outputs.length === 3);
{
const input = cb.inputs[1];
const output = cb.outputs[1];
assert(input);
assert(input.prevout.isNull());
assert(input.witness.length === 1);
assert.strictEqual(output.value, 4246894314);
}
{
const input = cb.inputs[2];
const output = cb.outputs[2];
assert(input);
assert(input.prevout.isNull());
assert(input.witness.length === 1);
assert.strictEqual(output.value, 8493988628 - 100e6);
}
assert(await chain.add(block));
});
it('should reorg back to the correct state', async () => {
let reorgd = false;
chain.once('reorganize', () => reorgd = true);
while (!reorgd) {
const block = await orig.cpu.mineBlock();
assert(block);
assert(await orig.chain.add(block));
assert(await chain.add(block));
}
assert.strictEqual(chain.db.state.value, snapshot + 6000e6);
});
it('should prevent double spend with bitfield', async () => {
const proof = AirdropProof.decode(rawProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
await assert.rejects(chain.add(block),
{ reason: 'bad-txns-bits-missingorspent' });
});
it('should mine faucet proof', async () => {
const proof = AirdropProof.decode(rawFaucetProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
const [tx] = block.txs;
assert(await chain.add(block));
assert.strictEqual(tx.outputs.length, 2);
assert.strictEqual(tx.outputs[0].value, 2100e6);
assert.strictEqual(tx.outputs[1].value, 8393988628);
assert.strictEqual(tx.outputs[1].address.toString(),
'hs1qmjpjjgpz7dmg37paq9uksx4yjp675690dafg3q');
});
it('should prevent double spend with bitfield', async () => {
const proof = AirdropProof.decode(rawFaucetProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
await assert.rejects(chain.add(block),
{ reason: 'bad-txns-bits-missingorspent' });
});
it('should close and open', async () => {
await blocks.close();
await chain.close();
await blocks.open();
await chain.open();
});
it('should prevent double spend with bitfield', async () => {
const proof = AirdropProof.decode(rawFaucetProof);
const job = await cpu.createJob();
job.addAirdrop(proof);
job.refresh();
const block = await job.mineAsync();
await assert.rejects(chain.add(block),
{ reason: 'bad-txns-bits-missingorspent' });
});
it('should close other nodes', async () => {
await orig.miner.close();
await orig.chain.close();
await orig.blocks.close();
await comp.miner.close();
await comp.chain.close();
await comp.blocks.close();
});
it('should cleanup', async () => {
await miner.close();
await chain.close();
await blocks.close();
});
});