-
Notifications
You must be signed in to change notification settings - Fork 809
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Ignore everything in this dir except this file | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-env mocha */ | ||
/* eslint prefer-arrow-callback: "off" */ | ||
|
||
'use strict'; | ||
|
||
const assert = require('../util/assert'); | ||
const NodeFactory = require('../util/nodefactory'); | ||
|
||
const nodeFactory = new NodeFactory(); | ||
|
||
describe('Sync bcoin and Core through a reorg', function () { | ||
this.timeout(3 * 60 * 60 * 1000); | ||
|
||
it('should keep bcoin in sync with core', async () => { | ||
const core = nodeFactory.createCore(); | ||
const bcoin = await nodeFactory.createBcoin(); | ||
|
||
// Core generates 100 blocks | ||
await new Promise(r => setTimeout(r, 2000)); | ||
const blocks = await core.rpc( | ||
'generatetoaddress', | ||
[100, 'mfWxJ45yp2SFn7UciZyNpvDKrzbhyfKrY8'] | ||
); | ||
|
||
// bcoin connects to Core and syncs | ||
await new Promise(r => setTimeout(r, 5000)); | ||
await bcoin.rpc( | ||
'addnode', | ||
[`127.0.0.1:${core.ports.port}`, 'add'] | ||
); | ||
|
||
// Core prompts a reorg by invalidating an old block | ||
// then building a new chain on top of its parent. | ||
// Mine new blocks to different address. | ||
await new Promise(r => setTimeout(r, 5000)); | ||
await core.rpc( | ||
'invalidateblock', | ||
[blocks[blocks.length - 5]] | ||
); | ||
await core.rpc( | ||
'generatetoaddress', | ||
[10, 'mrkZVNDhZufJfCSw4nbXAgSUPqroNRPYto'] | ||
); | ||
|
||
// Output | ||
await new Promise(r => setTimeout(r, 10000)); | ||
const coreinfo = await core.rpc( | ||
'getblockchaininfo', | ||
[] | ||
); | ||
const bcoininfo = await bcoin.rpc( | ||
'getblockchaininfo', | ||
[] | ||
); | ||
|
||
console.log('Core: ', coreinfo); | ||
console.log('bcoin: ', bcoininfo); | ||
|
||
assert.strictEqual(coreinfo.blocks, bcoininfo.blocks); | ||
assert.strictEqual(coreinfo.headers, bcoininfo.headers); | ||
assert.strictEqual(coreinfo.bestblockhash, bcoininfo.bestblockhash); | ||
|
||
// Close | ||
await new Promise(r => setTimeout(r, 5000)); | ||
await core.rpc( | ||
'stop', | ||
[] | ||
); | ||
await bcoin.rpc( | ||
'stop', | ||
[] | ||
); | ||
await new Promise(r => setTimeout(r, 5000)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
'use strict'; | ||
|
||
const FullNode = require('../../lib/node/fullnode'); | ||
const format = require('blgr/lib/format'); | ||
|
||
const path = require('path'); | ||
const bcurl = require('bcurl'); | ||
const cp = require('child_process'); | ||
|
||
class NodeFactory { | ||
constructor() { | ||
this.count = 0; | ||
} | ||
|
||
createDir(index) { | ||
const dataDir = path.join( | ||
__dirname, | ||
`../bitcoind-sync/data/datadir_${index}` | ||
); | ||
|
||
cp.spawnSync('rm', ['-rf', dataDir]); | ||
cp.spawnSync('mkdir', [dataDir]); | ||
|
||
return dataDir; | ||
} | ||
|
||
getPorts(index) { | ||
return { | ||
port: 10000 + index, | ||
rpcport: 20000 + index | ||
}; | ||
} | ||
|
||
initNode() { | ||
this.count += 1; | ||
const index = this.count; | ||
const dataDir = this.createDir(index); | ||
const ports = this.getPorts(index); | ||
const client = bcurl.client({ | ||
password: 'x', | ||
port: ports.rpcport | ||
}); | ||
|
||
const rpc = function (cmd, args) { | ||
return client.execute('', cmd, args); | ||
}; | ||
|
||
return { | ||
index, | ||
dataDir, | ||
ports, | ||
rpc | ||
}; | ||
} | ||
|
||
async createBcoin() { | ||
const {index, dataDir, ports, rpc} = this.initNode(); | ||
|
||
const node = new FullNode({ | ||
network: 'regtest', | ||
workers: true, | ||
logLevel: 'spam', | ||
listen: true, | ||
prefix: `${dataDir}`, | ||
memory: false, | ||
port: ports.port, | ||
httpPort: ports.rpcport, | ||
maxOutbound: 1 | ||
}); | ||
|
||
const printStdout = this.printStdout; | ||
node.logger.logger.writeConsole = function(level, module, args) { | ||
printStdout(index, '[' + module + '] ' + format(args, false)); | ||
}; | ||
|
||
await node.ensure(); | ||
await node.open(); | ||
await node.connect(); | ||
node.startSync(); | ||
|
||
return {index, dataDir, ports, rpc, node}; | ||
} | ||
|
||
createCore() { | ||
const {index, dataDir, ports, rpc} = this.initNode(); | ||
|
||
this.spawnSyncPrint( | ||
index, | ||
'bitcoind', | ||
[ | ||
`-datadir=${dataDir}`, | ||
'-regtest', | ||
'-rpcpassword=x', | ||
`-rpcport=${ports.rpcport}`, | ||
`-port=${ports.port}`, | ||
'-debug=net' | ||
], | ||
{stdio: 'pipe'} | ||
); | ||
|
||
return {index, dataDir, ports, rpc}; | ||
} | ||
|
||
spawnSyncPrint(id, cmd, arg, opt) { | ||
const proc = cp.spawn(cmd, arg, opt); | ||
|
||
proc.stdout.on('data', (data) => { | ||
this.printStdout(id, data); | ||
}); | ||
|
||
proc.stderr.on('data', (data) => { | ||
this.printStdout(id, data); | ||
}); | ||
|
||
proc.on('close', (code) => { | ||
return(code); | ||
}); | ||
|
||
proc.on('error', (data) => { | ||
this.printStdout(id, data); | ||
}); | ||
} | ||
|
||
printStdout(index, data) { | ||
const header = `${index}: `; | ||
let str = data.toString(); | ||
str = str.replace(/\n/g, `\n${header}`); | ||
str = header + str; | ||
console.log(`\x1b[${31 + index}m%s\x1b[0m`, str); | ||
} | ||
} | ||
|
||
module.exports = NodeFactory; |