Refactor reorg regtest.

This commit is contained in:
Braydon Fuller 2015-08-24 09:56:38 -04:00
parent e707b297d1
commit d03d452981

View File

@ -3,10 +3,12 @@
// These tests require bitcoind.js Bitcoin Core bindings to be compiled with // These tests require bitcoind.js Bitcoin Core bindings to be compiled with
// the environment variable BITCOINDJS_ENV=test. This enables the use of regtest // the environment variable BITCOINDJS_ENV=test. This enables the use of regtest
// functionality by including the wallet in the build. // functionality by including the wallet in the build.
// To run the tests: $ mocha -R spec integration/regtest.js // To run the tests: $ mocha -R spec integration/regtest-node.js
var chainlib = require('chainlib'); var chainlib = require('chainlib');
var async = require('async');
var log = chainlib.log; var log = chainlib.log;
log.debug = function() {};
if (process.env.BITCORENODE_ENV !== 'test') { if (process.env.BITCORENODE_ENV !== 'test') {
log.info('Please set the environment variable BITCORENODE_ENV=test and make sure bindings are compiled for testing'); log.info('Please set the environment variable BITCORENODE_ENV=test and make sure bindings are compiled for testing');
@ -77,12 +79,14 @@ describe('Node Functionality', function() {
pass: 'local321' pass: 'local321'
}); });
node.on('synced', function() { var syncedHandler = function() {
//todo: refactor to remove the event listener
if (node.chain.tip.__height === 150) { if (node.chain.tip.__height === 150) {
node.removeListener('synced', syncedHandler);
done(); done();
} }
}); };
node.on('synced', syncedHandler);
client.generate(150, function(err, response) { client.generate(150, function(err, response) {
if (err) { if (err) {
@ -102,41 +106,65 @@ describe('Node Functionality', function() {
}); });
}); });
describe('bitcoin core daemon reorgs', function() { it('will handle a reorganization', function(done) {
before(function(done) { var count;
client.getBlockCount(function(err, response) { var blockHash;
if (err) {
throw err; async.series([
} function(next) {
var count = response.result; client.getBlockCount(function(err, response) {
if (err) {
return next(err);
}
count = response.result;
next();
});
},
function(next) {
client.getBlockHash(count, function(err, response) { client.getBlockHash(count, function(err, response) {
if (err) { if (err) {
throw err; return next(err);
} }
var blockHash = response.result; blockHash = response.result;
client.invalidateBlock(blockHash, function(err, response) { next();
if (err) {
throw err;
}
client.getBlockCount(function(err, response) {
if (err) {
throw err;
}
response.result.should.equal(count - 1);
done();
});
});
}); });
}); },
}); function(next) {
client.invalidateBlock(blockHash, next);
},
function(next) {
client.getBlockCount(function(err, response) {
if (err) {
return next(err);
}
response.result.should.equal(count - 1);
next();
});
}
], function(err) {
if (err) {
throw err;
}
var blocksRemoved = 0;
var blocksAdded = 0;
it('will handle a reorganization', function(done) { var removeBlock = function() {
blocksRemoved++;
};
node.db.bitcoind.on('tip', function(height) { node.chain.on('removeblock', removeBlock);
height.should.equal(151);
done(); var addBlock = function() {
}); blocksAdded++;
if (blocksAdded === 2 && blocksRemoved === 1) {
node.chain.removeListener('addblock', addBlock);
node.chain.removeListener('removeblock', removeBlock);
done();
}
};
node.chain.on('addblock', addBlock);
// We need to add a transaction to the mempool so that the next block will // We need to add a transaction to the mempool so that the next block will
// have a different hash as the hash has been invalidated. // have a different hash as the hash has been invalidated.
@ -150,8 +178,7 @@ describe('Node Functionality', function() {
} }
}); });
}); });
}); });
});
});
}); });