forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-a-blockchain-and-mempool.js
43 lines (34 loc) · 1.08 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
'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 chain = new bcoin.Chain({ network: 'regtest', memory: true });
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 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);
});