itns-sidechain/docs/Examples/peers-plugin.js

60 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-03-24 16:26:34 -07:00
'use strict';
2017-08-09 16:38:20 +04:00
const bcoin = require('../..');
const FullNode = bcoin.fullnode;
2017-03-24 16:26:34 -07:00
function MyPlugin(node) {
this.node = node;
}
MyPlugin.id = 'my-plugin';
MyPlugin.init = function init(node) {
return new MyPlugin(node);
};
MyPlugin.prototype.open = function open() {
console.log('Opened my plugin.');
return Promise.resolve();
};
MyPlugin.prototype.close = function close() {
console.log('Closed my plugin.');
return Promise.resolve();
};
MyPlugin.prototype.sayPeers = function sayPeers() {
console.log('Number of peers: %d', this.node.pool.peers.size());
};
2017-06-29 20:54:07 -07:00
const node = new FullNode({
2017-03-24 16:26:34 -07:00
network: 'testnet',
2017-07-17 16:36:01 -07:00
db: 'memory',
workers: true
2017-03-24 16:26:34 -07:00
});
node.use(MyPlugin);
2017-07-17 16:36:01 -07:00
(async () => {
2017-07-27 09:27:00 -07:00
const plugin = node.require('my-plugin');
2017-03-24 16:26:34 -07:00
await node.open();
await node.connect();
plugin.sayPeers();
2017-06-29 20:54:07 -07:00
node.on('connect', (entry, block) => {
2017-03-24 16:26:34 -07:00
console.log('%s (%d) added to chain.', entry.rhash(), entry.height);
});
2017-06-29 20:54:07 -07:00
node.on('tx', (tx) => {
2017-03-24 16:26:34 -07:00
console.log('%s added to mempool.', tx.txid());
});
2017-07-17 16:36:01 -07:00
node.startSync();
2017-08-09 16:38:20 +04:00
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});