forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-a-blockchain-and-mempool.js
53 lines (44 loc) · 1.19 KB
/
create-a-blockchain-and-mempool.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
'use strict';
const bcoin = require('../..');
// Default network (so we can avoid passing
// the `network` option into every object below.)
bcoin.set('regtest');
// Start up a blockchain, mempool, and miner using in-memory
// databases (stored in a red-black tree instead of on-disk).
const blocks = bcoin.blockstore.create({
memory: true
});
const chain = new bcoin.Chain({
network: 'regtest',
memory: true,
blocks: blocks
});
const mempool = new bcoin.Mempool({
chain: chain
});
const miner = new bcoin.Miner({
chain: chain,
mempool: mempool,
// Make sure miner won't block the main thread.
useWorkers: true
});
(async () => {
// Open the chain
await blocks.open();
await chain.open();
// Open the miner (initialize the databases, etc).
// Miner will implicitly call `open` on mempool.
await miner.open();
// Create a Cpu miner job
const job = await miner.createJob();
// run miner
const block = await job.mineAsync();
// Add the block to the chain
console.log('Adding %s to the blockchain.', block.rhash());
console.log(block);
await chain.add(block);
console.log('Added block!');
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});