Skip to content

Commit

Permalink
docs: Update Examples with Blockstore
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Sep 5, 2019
1 parent 7572246 commit bdb2e80
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
17 changes: 10 additions & 7 deletions docs/examples/connect-to-peer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';

/*
* Usage:
* Run another Bitcoin node on local regtest network, for example
* $ ../../bin/bcoin --network=regtest
* Execute this script with the other node's address and port
* $ node connect-to-peer.js 127.0.0.1:48444
*/
if (process.argv.length !== 3) {
console.log(`
Usage:
Run another Bitcoin node on local regtest network, for example
$ ../../bin/bcoin --network=regtest
Execute this script with the other node's address and port
$ node connect-to-peer.js 127.0.0.1:48444
`);
process.exit(1);
}

const bcoin = require('../..');
const network = bcoin.Network.get('regtest');
Expand Down
13 changes: 10 additions & 3 deletions docs/examples/connect-to-the-p2p-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ const logger = new Logger({
});

// Create a blockchain and store it in memory.
const blocks = new bcoin.blockstore.LevelBlockStore({
memory: true
});
const chain = new bcoin.Chain({
memory: true,
network: 'main',
blocks: blocks,
logger: logger
});

Expand All @@ -31,6 +33,7 @@ const pool = new bcoin.Pool({

(async function() {
await logger.open();
await blocks.open();
await chain.open();

await pool.open();
Expand Down Expand Up @@ -64,8 +67,11 @@ const pool = new bcoin.Pool({

// Start up a testnet sync in-memory
// while we're at it (because we can).

const tblocks = new bcoin.blockstore.LevelBlockStore({
memory: true
});
const tchain = new bcoin.Chain({
blocks: tblocks,
memory: true,
network: 'testnet',
logger: logger
Expand All @@ -86,6 +92,7 @@ const tpool = new bcoin.Pool({
});

(async function() {
await tblocks.open();
await tchain.open();

await tpool.open();
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/create-a-blockchain-and-mempool.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ 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 blocks = new bcoin.blockstore.LevelBlockStore({ memory: true });
const chain = new bcoin.Chain({ blocks });
const mempool = new bcoin.Mempool({ chain: chain });
const miner = new bcoin.Miner({
chain: chain,
Expand All @@ -20,6 +21,7 @@ const miner = new bcoin.Miner({

(async () => {
// Open the chain
await blocks.open();
await chain.open();

// Open the miner (initialize the databases, etc).
Expand Down
8 changes: 6 additions & 2 deletions docs/examples/get-tx-from-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ const fs = require('bfile');
// Create chain for testnet, stored in memory by default.
// To store the chain on disk at the `prefix` location,
// set `memory: false`.
const blocks = new bcoin.blockstore.LevelBlockStore({
memory: true
});
const chain = new bcoin.Chain({
blocks,
network: 'testnet',
indexTX: true,
indexAddress: true,
db: 'leveldb',
prefix: '/tmp/bcoin-testnet-example',
memory: true
prefix: '/tmp/bcoin-testnet-example'
});

// Create a network pool of peers with a limit of 8 peers.
Expand All @@ -26,6 +29,7 @@ const pool = new bcoin.Pool({
if (!chain.options.memory)
await fs.mkdirp(chain.options.prefix);

await blocks.open();
await chain.open();

// Connect the blockchain to the network
Expand Down
4 changes: 3 additions & 1 deletion docs/examples/spv-sync-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const walletdb = new bcoin.wallet.WalletDB({ memory: true });

// Full node will provide tx data to SPV node
const full = {};
full.chain = new bcoin.Chain();
const blocks = new bcoin.blockstore.LevelBlockStore({ memory: true });
full.chain = new bcoin.Chain({ blocks });
full.pool = new bcoin.Pool({
chain: full.chain,
port: 44444,
Expand All @@ -29,6 +30,7 @@ full.pool = new bcoin.Pool({
(async () => {
await pool.open();
await walletdb.open();
await blocks.open();
await chain.open();
await pool.connect();

Expand Down

0 comments on commit bdb2e80

Please sign in to comment.