docs: Move examples to docs

This commit is contained in:
Node 2017-08-09 16:38:20 +04:00
parent 1d8cfe85e0
commit 1d9d52e0d3
No known key found for this signature in database
GPG Key ID: 8E1B4DC29040BD90
14 changed files with 140 additions and 53 deletions

View File

@ -1,12 +1,13 @@
'use strict';
const encoding = require('bcoin/lib/utils/encoding');
const co = require('bcoin/lib/utils/co');
const Outpoint = require('bcoin/lib/primitives/outpoint');
const MTX = require('bcoin/lib/primitives/mtx');
const HTTP = require('bcoin/lib/http');
const FullNode = require('bcoin/lib/node/fullnode');
const plugin = require('bcoin/lib/wallet/plugin');
const bcoin = require('../..');
const encoding = bcoin.encoding;
const co = bcoin.co;
const Outpoint = bcoin.outpoint;
const MTX = bcoin.mtx;
const HTTP = bcoin.http;
const FullNode = bcoin.fullnode;
const plugin = bcoin.wallet.plugin;
const node = new FullNode({
network: 'regtest',
@ -113,4 +114,7 @@ async function callNodeApi() {
console.log(tx);
await callNodeApi();
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -1,10 +1,11 @@
'use strict';
// Usage: $ node ./examples/peer.js [ip]:[port]
// Usage: $ node ./docs/Examples/connect-to-peer.js [ip]:[port]
const Peer = require('bcoin/lib/net/peer');
const NetAddress = require('bcoin/lib/primitives/netaddress');
const Network = require('bcoin/lib/protocol/network');
const bcoin = require('../..');
const Peer = bcoin.peer;
const NetAddress = bcoin.netaddress;
const Network = bcoin.network;
const network = Network.get('testnet');
const peer = Peer.fromOptions({
@ -17,6 +18,8 @@ const peer = Peer.fromOptions({
const addr = NetAddress.fromHostname(process.argv[2], 'testnet');
console.log(`Connecting to ${addr.hostname}`);
peer.connect(addr);
peer.tryOpen();

View File

@ -92,4 +92,7 @@ const tpool = new Pool({
console.log('Saw testnet transaction:');
console.log(tx);
});
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -36,4 +36,7 @@ const miner = new Miner({
console.log(block);
await chain.add(block);
console.log('Added block!');
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -2,7 +2,7 @@
/* eslint new-cap: "off" */
const bcoin = require('bcoin');
const bcoin = require('../..');
const assert = require('assert');
(async () => {

View File

@ -1,6 +1,6 @@
'use strict';
const bcoin = require('../..').set('main');
const WalletDB = bcoin.walletdb;
const walletPlugin = bcoin.wallet.plugin;
const node = bcoin.fullnode({
checkpoints: true,
@ -9,6 +9,8 @@ const node = bcoin.fullnode({
logLevel: 'info'
});
node.use(walletPlugin);
// We get a lot of errors sometimes,
// usually from peers hanging up on us.
// Just ignore them for now.
@ -30,8 +32,7 @@ const newReceiving = 'AddressHere';
type: 'pubkeyhash'
};
console.log(node);
const walletdb = new WalletDB();
const walletdb = node.require('walletdb');
await walletdb.open();
const wallet = await walletdb.create(options);
@ -76,4 +77,7 @@ const newReceiving = 'AddressHere';
node.chain.on('full', () => {
node.mempool.getHistory().then(console.log);
});
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -1,6 +1,7 @@
'use strict';
const FullNode = require('bcoin/lib/node/fullnode');
const bcoin = require('../..');
const FullNode = bcoin.fullnode;
const node = new FullNode({
network: 'testnet',
@ -21,4 +22,7 @@ const node = new FullNode({
});
node.startSync();
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -0,0 +1,48 @@
'use strict';
const path = require('path');
const bcoin = require('../..');
const Chain = bcoin.chain;
const Logger = bcoin.logger;
const util = bcoin.util;
const HOME = process.env.HOME;
// Setup logger to see what's Bcoin doing.
const logger = new Logger({
level: 'debug'
});
// Create chain for testnet, specify chain directory
const chain = new Chain({
logger: logger,
network: 'testnet',
db: 'leveldb',
prefix: path.join(HOME, '.bcoin/testnet'),
indexTX: true,
indexAddress: true
});
(async () => {
await logger.open();
await chain.open();
console.log('Current height:', chain.height);
const entry = await chain.getEntry(50000);
console.log('Block at 50k:', entry);
// eslint-disable-next-line max-len
const txhash = '7f5990b008a2d0fc006d13b15e25d05ff30fadab656d49a5c6afea0e0d0b458c';
const txmeta = await chain.db.getMeta(util.revHex(txhash));
console.log(`Tx with hash ${txhash}:`, txmeta);
// eslint-disable-next-line max-len
const bhash = '00000000077eacdd2c803a742195ba430a6d9545e43128ba55ec3c80beea6c0c';
const block = await chain.db.getBlock(util.revHex(bhash));
console.log(`Block with hash ${bhash}:`, block);
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -1,9 +1,10 @@
'use strict';
const KeyRing = require('bcoin/lib/primitives/keyring');
const WorkerPool = require('bcoin/lib/workers/workerpool');
const Chain = require('bcoin/lib/blockchain/chain');
const Miner = require('bcoin/lib/mining/miner');
const bcoin = require('../..');
const KeyRing = bcoin.keyring;
const WorkerPool = bcoin.workerpool;
const Chain = bcoin.chain;
const Miner = bcoin.miner;
const key = KeyRing.generate('regtest');
@ -31,7 +32,7 @@ const miner = new Miner({
console.log('Block template:');
console.log(tmpl);
const job = await miner.cpu.createJob();
const job = await miner.createJob();
const block = await job.mineAsync();
console.log('Mined block:');
@ -42,4 +43,7 @@ const miner = new Miner({
console.log('New tip:');
console.log(chain.tip);
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -1,6 +1,7 @@
'use strict';
const FullNode = require('bcoin/lib/node/fullnode');
const bcoin = require('../..');
const FullNode = bcoin.fullnode;
function MyPlugin(node) {
this.node = node;
@ -52,4 +53,7 @@ node.use(MyPlugin);
});
node.startSync();
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -1,10 +1,12 @@
'use strict';
const bcoin = require('../..').set('testnet');
const bcoin = require('../..');
const Chain = bcoin.chain;
const Pool = bcoin.pool;
const WalletDB = bcoin.walletdb;
bcoin.set('testnet');
// SPV chains only store the chain headers.
const chain = Chain({
db: 'leveldb',
@ -48,4 +50,7 @@ const walletdb = new WalletDB({ db: 'memory' });
console.log('Balance updated.');
console.log(bcoin.amount.btc(balance.unconfirmed));
});
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -1,9 +1,10 @@
'use strict';
const random = require('bcoin/lib/crypto/random');
const WalletDB = require('bcoin/lib/wallet/walletdb');
const MTX = require('bcoin/lib/primitives/mtx');
const Outpoint = require('bcoin/lib/primitives/outpoint');
const bcoin = require('../..');
const random = bcoin.crypto.random;
const WalletDB = bcoin.walletdb;
const MTX = bcoin.mtx;
const Outpoint = bcoin.outpoint;
function dummy() {
const hash = random.randomBytes(32).toString('hex');
@ -42,4 +43,7 @@ const walletdb = new WalletDB({
console.log('Added transaction');
console.log(wtx);
})();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});

View File

@ -13,10 +13,18 @@ Welcome to the bcoin docs!
- [REST and RPC API][rest-rpc]
## Code Examples
- [Simple Fullnode][example-simple-fullnode]
- [Connect to Peer][example-connect-peer]
- [Connecting to the P2P Network][example-p2p]
- [Creating a Blockchain and Mempool][example-blockchain]
- [Fullnode Object][example-fullnode]
- [Wallet with Dummy TX][example-wallet-dummy]
- [Fullnode Object][example-fullnode-wallet]
- [SPV Sync][example-spv]
- [Plugin Example][example-peers-plugin]
- [Client API Usage][example-client-api]
- [Miner with WorkerPool][example-miner-configs]
- [Create and Sign TX][example-tx-create-sign]
- [Get Transaction from Chain][example-tx-chain]
## Advanced
- [Working with transactions][work-transactions]
@ -38,5 +46,13 @@ Welcome to the bcoin docs!
[example-p2p]: Examples/connect-to-the-p2p-network.js
[example-blockchain]: Examples/create-a-blockchain-and-mempool.js
[example-fullnode]: Examples/fullnode-and-wallet.js
[example-fullnode-wallet]: Examples/fullnode-and-wallet.js
[example-spv]: Examples/spv-sync-wallet.js
[example-wallet-dummy]: Examples/wallet.js
[example-peers-plugin]: Examples/peers-plugin.js
[example-client-api]: Examples/client-api.js
[example-miner-configs]: Examples/miner-configs.js
[example-connect-peer]: Examples/connect-to-peer.js
[example-simple-fullnode]: Examples/fullnode.js
[example-tx-create-sign]: Examples/create-sign-tx.js
[example-tx-from-chain]: Examples/get-tx-from-chain.js

View File

@ -1,15 +0,0 @@
'use strict';
const Chain = require('bcoin/lib/blockchain/chain');
const chain = new Chain({
network: 'testnet'
});
(async () => {
await chain.open();
const entry = await chain.getEntry(0);
console.log(entry);
})();