forked from bcoin-org/bcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers-plugin.js
58 lines (42 loc) · 1.06 KB
/
peers-plugin.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
const bcoin = require('../..');
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());
};
const node = new bcoin.FullNode({
memory: true,
network: 'testnet',
workers: true
});
node.use(MyPlugin);
(async () => {
const plugin = node.require('my-plugin');
await node.open();
await node.connect();
plugin.sayPeers();
node.on('connect', (entry, block) => {
console.log('%s (%d) added to chain.', entry.rhash(), entry.height);
});
node.on('tx', (tx) => {
console.log('%s added to mempool.', tx.txid());
});
node.startSync();
})().catch((err) => {
console.error(err.stack);
process.exit(1);
});