From d7c1210515bc52f0d4d774a1a76da19fea805a54 Mon Sep 17 00:00:00 2001 From: Node Date: Wed, 9 Aug 2017 14:15:24 +0400 Subject: [PATCH] Update examples --- ...ample-Creating-a-Blockchain-and-Mempool.md | 35 -------- docs/Example-Fullnode-Object.md | 72 ----------------- docs/Example-SPV-Sync.md | 44 ----------- .../connect-to-the-p2p-network.js} | 59 ++++++++------ .../create-a-blockchain-and-mempool.js | 39 +++++++++ docs/Examples/fullnode-and-wallet.js | 79 +++++++++++++++++++ docs/Examples/spv-sync-wallet.js | 51 ++++++++++++ 7 files changed, 203 insertions(+), 176 deletions(-) delete mode 100644 docs/Example-Creating-a-Blockchain-and-Mempool.md delete mode 100644 docs/Example-Fullnode-Object.md delete mode 100644 docs/Example-SPV-Sync.md rename docs/{Example-Connecting-to-the-P2P-Network.md => Examples/connect-to-the-p2p-network.js} (51%) create mode 100644 docs/Examples/create-a-blockchain-and-mempool.js create mode 100644 docs/Examples/fullnode-and-wallet.js create mode 100644 docs/Examples/spv-sync-wallet.js diff --git a/docs/Example-Creating-a-Blockchain-and-Mempool.md b/docs/Example-Creating-a-Blockchain-and-Mempool.md deleted file mode 100644 index 0941d931..00000000 --- a/docs/Example-Creating-a-Blockchain-and-Mempool.md +++ /dev/null @@ -1,35 +0,0 @@ -``` js -var bcoin = require('bcoin'); - -bcoin.set({ - // Default network (so we can avoid passing - // the `network` option into every object below. - network: 'regtest', - // Enable the global worker pool - // for mining and transaction verification. - useWorkers: true -}); - -// Start up a blockchain, mempool, and miner using in-memory -// databases (stored in a red-black tree instead of on-disk). -var chain = new bcoin.chain({ db: 'memory' }); -var mempool = new bcoin.mempool({ chain: chain }); -var miner = new bcoin.miner({ chain: chain, mempool: mempool }); - -// Open the miner (initialize the databases, etc). -// Miner will implicitly call `open` on chain and mempool. -miner.open().then(function() { - // Create a Cpu miner job - return miner.createJob(); -}).then(function(job) { - // Mine the block on the worker pool (use mine() for the master process) - return job.mineAsync(); -}).then(function(block) { - // Add the block to the chain - console.log('Adding %s to the blockchain.', block.rhash); - console.log(block); - return chain.add(block); -}).then(function() { - console.log('Added block!'); -}); -``` \ No newline at end of file diff --git a/docs/Example-Fullnode-Object.md b/docs/Example-Fullnode-Object.md deleted file mode 100644 index 92ddf471..00000000 --- a/docs/Example-Fullnode-Object.md +++ /dev/null @@ -1,72 +0,0 @@ -``` js -var bcoin = require('bcoin').set('main'); - -var node = bcoin.fullnode({ - checkpoints: true, - // Primary wallet passphrase - passsphrase: 'node', - logLevel: 'info' -}); - -// We get a lot of errors sometimes, -// usually from peers hanging up on us. -// Just ignore them for now. -node.on('error', function(err) { - ; -}); - -// Start the node -node.open().then(function() { - // Create a new wallet (or get an existing one with the same ID) - var options = { - id: 'mywallet', - passphrase: 'foo', - witness: false, - type: 'pubkeyhash' - }; - - return node.walletdb.create(options); -}).then(function(wallet) { - console.log('Created wallet with address: %s', wallet.getAddress('base58')); - - node.connect().then(function() { - // Start syncing the blockchain - node.startSync(); - }); - - // Wait for balance and send it to a new address. - wallet.once('balance', function(balance) { - // Create a transaction, fill - // it with coins, and sign it. - var options = { - subtractFee: true, - outputs: [{ - address: newReceiving, - value: balance.total - }] - }; - wallet.createTX(options).then(function(tx) { - // Need to pass our passphrase back in to sign! - return wallet.sign(tx, 'foo'); - }).then(function(tx) { - console.log('sending tx:'); - console.log(tx); - return node.sendTX(tx); - }).then(function() { - console.log('tx sent!'); - }); - }); -}); - -node.chain.on('block', function(block) { - ; -}); - -node.mempool.on('tx', function(tx) { - ; -}); - -node.chain.on('full', function() { - node.mempool.getHistory().then(console.log); -}); -``` \ No newline at end of file diff --git a/docs/Example-SPV-Sync.md b/docs/Example-SPV-Sync.md deleted file mode 100644 index 1a08fc25..00000000 --- a/docs/Example-SPV-Sync.md +++ /dev/null @@ -1,44 +0,0 @@ -``` js -var bcoin = require('bcoin').set('testnet'); - -// SPV chains only store the chain headers. -var chain = new bcoin.chain({ - db: 'leveldb', - location: process.env.HOME + '/spvchain', - spv: true -}); - -var pool = new bcoin.pool({ - chain: chain, - spv: true, - maxPeers: 8 -}); - -var walletdb = new bcoin.walletdb({ db: 'memory' }); - -pool.open().then(function() { - return walletdb.open(); -}).then(function() { - return walletdb.create(); -}).then(function(wallet) { - console.log('Created wallet with address %s', wallet.getAddress('base58')); - - // Add our address to the spv filter. - pool.watchAddress(wallet.getAddress()); - - // Connect, start retrieving and relaying txs - pool.connect().then(function() { - // Start the blockchain sync. - pool.startSync(); - - pool.on('tx', function(tx) { - walletdb.addTX(tx); - }); - - wallet.on('balance', function(balance) { - console.log('Balance updated.'); - console.log(bcoin.amount.btc(balance.unconfirmed)); - }); - }); -}); -``` \ No newline at end of file diff --git a/docs/Example-Connecting-to-the-P2P-Network.md b/docs/Examples/connect-to-the-p2p-network.js similarity index 51% rename from docs/Example-Connecting-to-the-P2P-Network.md rename to docs/Examples/connect-to-the-p2p-network.js index c25453d9..e9b852ac 100644 --- a/docs/Example-Connecting-to-the-P2P-Network.md +++ b/docs/Examples/connect-to-the-p2p-network.js @@ -1,15 +1,26 @@ -``` js -var bcoin = require('bcoin').set('main'); +'use strict'; +const bcoin = require('../..').set('main'); +const Chain = bcoin.chain; +const Mempool = bcoin.mempool; +const Pool = bcoin.pool; // Create a blockchain and store it in leveldb. // `db` also accepts `rocksdb` and `lmdb`. -var prefix = process.env.HOME + '/my-bcoin-environment'; -var chain = new bcoin.chain({ db: 'leveldb', location: prefix + '/chain' }); +const prefix = process.env.HOME + '/my-bcoin-environment'; +const chain = new Chain({ + db: 'leveldb', + location: prefix + '/chain', + network: 'main' +}); -var mempool = new bcoin.mempool({ chain: chain }); +const mempool = new Mempool({ chain: chain }); // Create a network pool of peers with a limit of 8 peers. -var pool = new bcoin.pool({ chain: chain, mempool: mempool, maxPeers: 8 }); +const pool = new Pool({ + chain: chain, + mempool: mempool, + maxPeers: 8 +}); // Open the pool (implicitly opens mempool and chain). (async function() { @@ -22,44 +33,44 @@ var pool = new bcoin.pool({ chain: chain, mempool: mempool, maxPeers: 8 }); pool.startSync(); // Watch the action - chain.on('block', function(block) { + chain.on('block', (block) => { console.log('Connected block to blockchain:'); console.log(block); }); - mempool.on('tx', function(tx) { + mempool.on('tx', (tx) => { console.log('Added tx to mempool:'); console.log(tx); }); - pool.on('tx', function(tx) { + pool.on('tx', (tx) => { console.log('Saw transaction:'); console.log(tx.rhash); }); })(); -// Start up a segnet4 sync in-memory +// Start up a testnet sync in-memory // while we're at it (because we can). -var tchain = new bcoin.chain({ - network: 'segnet4', +const tchain = new Chain({ + network: 'testnet', db: 'memory' }); -var tmempool = new bcoin.mempool({ - network: 'segnet4', +const tmempool = new Mempool({ + network: 'testnet', chain: tchain }); -var tpool = new bcoin.pool({ - network: 'segnet4', +const tpool = new Pool({ + network: 'testnet', chain: tchain, mempool: tmempool, size: 8 }); (async function() { - await pool.open(); + await tpool.open(); // Connect, start retrieving and relaying txs await tpool.connect(); @@ -67,20 +78,18 @@ var tpool = new bcoin.pool({ // Start the blockchain sync. tpool.startSync(); - tchain.on('block', function(block) { - console.log('Added segnet4 block:'); + tchain.on('block', (block) => { + console.log('Added testnet block:'); console.log(block); }); - tmempool.on('tx', function(tx) { - console.log('Added segnet4 tx to mempool:'); + tmempool.on('tx', (tx) => { + console.log('Added testnet tx to mempool:'); console.log(tx); }); - tpool.on('tx', function(tx) { - console.log('Saw segnet4 transaction:'); + tpool.on('tx', (tx) => { + console.log('Saw testnet transaction:'); console.log(tx); }); })(); - -``` \ No newline at end of file diff --git a/docs/Examples/create-a-blockchain-and-mempool.js b/docs/Examples/create-a-blockchain-and-mempool.js new file mode 100644 index 00000000..13d3f88c --- /dev/null +++ b/docs/Examples/create-a-blockchain-and-mempool.js @@ -0,0 +1,39 @@ +'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!'); +})(); diff --git a/docs/Examples/fullnode-and-wallet.js b/docs/Examples/fullnode-and-wallet.js new file mode 100644 index 00000000..15283214 --- /dev/null +++ b/docs/Examples/fullnode-and-wallet.js @@ -0,0 +1,79 @@ +'use strict'; +const bcoin = require('../..').set('main'); +const WalletDB = bcoin.walletdb; + +const node = bcoin.fullnode({ + checkpoints: true, + // Primary wallet passphrase + passsphrase: 'node', + logLevel: 'info' +}); + +// We get a lot of errors sometimes, +// usually from peers hanging up on us. +// Just ignore them for now. +node.on('error', (err) => { + ; +}); + +// New Address we'll be sending to. +const newReceiving = 'AddressHere'; + +// Start the node +(async () => { + await node.open(); + + const options = { + id: 'mywallet', + passphrase: 'foo', + witness: false, + type: 'pubkeyhash' + }; + + console.log(node); + const walletdb = new WalletDB(); + + await walletdb.open(); + const wallet = await walletdb.create(options); + + console.log('Created wallet with address: %s', wallet.getAddress('base58')); + + await node.connect(); + + // Start syncing the blockchain + node.startSync(); + + // Wait for balance and send it to a new address. + wallet.once('balance', async (balance) => { + // Create a transaction, fill + // it with coins, and sign it. + const options = { + subtractFee: true, + outputs: [{ + address: newReceiving, + value: balance.total + }] + }; + + const tx = await wallet.createTX(options); + const stx = await wallet.sign(tx, 'foo'); + + console.log('sending tx:'); + console.log(stx); + + await node.sendTX(stx); + console.log('tx sent!'); + }); + + node.chain.on('block', (block) => { + ; + }); + + node.mempool.on('tx', (tx) => { + ; + }); + + node.chain.on('full', () => { + node.mempool.getHistory().then(console.log); + }); +})(); diff --git a/docs/Examples/spv-sync-wallet.js b/docs/Examples/spv-sync-wallet.js new file mode 100644 index 00000000..bd622aa0 --- /dev/null +++ b/docs/Examples/spv-sync-wallet.js @@ -0,0 +1,51 @@ +'use strict'; + +const bcoin = require('../..').set('testnet'); +const Chain = bcoin.chain; +const Pool = bcoin.pool; +const WalletDB = bcoin.walletdb; + +// SPV chains only store the chain headers. +const chain = Chain({ + db: 'leveldb', + location: process.env.HOME + '/spvchain', + spv: true +}); + +const pool = new Pool({ + chain: chain, + spv: true, + maxPeers: 8 +}); + +const walletdb = new WalletDB({ db: 'memory' }); + +(async () => { + await pool.open(); + await walletdb.open(); + + const wallet = await walletdb.create(); + + console.log('Created wallet with address %s', wallet.getAddress('base58')); + + // Add our address to the spv filter. + pool.watchAddress(wallet.getAddress()); + + // Connect, start retrieving and relaying txs + await pool.connect(); + + // Start the blockchain sync. + pool.startSync(); + + pool.on('tx', async (tx) => { + console.log('received TX'); + + await walletdb.addTX(tx); + console.log('Transaction added to walletDB'); + }); + + wallet.on('balance', (balance) => { + console.log('Balance updated.'); + console.log(bcoin.amount.btc(balance.unconfirmed)); + }); +})();