forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-a-blockchain-and-mempool.js
42 lines (35 loc) · 1.09 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
'use strict';
const bcoin = require('../..');
const Chain = bcoin.chain;
const Mempool = bcoin.mempool;
const Miner = bcoin.miner;
// 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 Chain({ db: 'memory' });
const mempool = new Mempool({ chain: chain });
const miner = new Miner({
chain: chain,
mempool: mempool,
// Make sure miner won't block the main thread.
useWorkers: true
});
(async () => {
// Open the miner (initialize the databases, etc).
// Miner will implicitly call `open` on chain and 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);
});