changes in response to feedback

This commit is contained in:
Patrick Nagurny 2015-07-17 10:53:02 -04:00
parent 162088f8f4
commit 655ac37ba0
6 changed files with 4 additions and 126 deletions

View File

@ -22,7 +22,7 @@ describe('Basic Functionality', function() {
before(function(done) {
this.timeout(30000);
bitcoind = require('../')({
bitcoind = require('../').daemon({
directory: process.env.BITCOINDJS_DIR || '~/.bitcoin',
});

View File

@ -22,38 +22,8 @@ function Block(obj) {
util.inherits(Block, BaseBlock);
Block.prototype.validate = function(chain, callback) {
var self = this;
// Get previous block
chain.db.getBlock(self.prevHash, function(err, prevBlock) {
if (err) {
return callback(err);
}
// Validate POW
// First make sure the current block hash is less than the target derived from this block's bits
if (!self.validProofOfWork(chain)) {
return callback(new Error('Invalid proof of work (hash is greater than target)'));
}
// Second make sure that this block's bits is correct based off of the last block
chain.getNextWorkRequired(prevBlock, function(err, bits) {
if (err) {
return callback(err);
}
if (bits !== self.bits) {
return callback(new Error(
'Invalid proof of work, expected block bits "' + self.bits + '" to equal "' + bits + '"'
));
}
// Validate block data
chain.db.validateBlockData(self, callback);
});
});
// bitcoind does all validation
setImmediate(callback);
};
/**

View File

@ -263,7 +263,6 @@ DB.prototype.getAPIMethods = function() {
return [
['getTransaction', this, this.getTransaction, 2],
['getBalance', this, this.getBalance, 2],
['sendFunds', this, this.sendFunds, 2],
['getOutputs', this, this.getOutputs, 2],
['getUnspentOutputs', this, this.getUnspentOutputs, 2],
['isSpent', this, this.isSpent, 2]

View File

@ -23,36 +23,6 @@ describe('Bitcoin Block', function() {
});
});
describe('#validate', function() {
it('rejects with invalid proof of work because hash is greater', function(done) {
var block = new Block(chainData[1]);
block.validProofOfWork = sinon.stub().returns(false);
block.validate({
db: {
getBlock: sinon.stub().callsArgWith(1, null, {})
}
}, function(err) {
should.exist(err);
err.message.should.match(/Invalid proof of work/);
done();
});
});
it('rejects with unexpected bits for chain', function(done) {
var block = new Block(chainData[1]);
block.validProofOfWork = sinon.stub().returns(true);
block.validate({
db: {
getBlock: sinon.stub().callsArgWith(1, null, {})
},
getNextWorkRequired: sinon.stub().callsArgWith(1, null, 0x1d00ffff)
}, function(err) {
should.exist(err);
err.message.should.match(/Invalid proof of work, expected block bits/);
done();
});
});
});
describe('#validProofOfWork', function() {
it('returns false if block hash is greater than target from bits', function() {
var block = new Block(chainData[1]);

View File

@ -1,61 +0,0 @@
{
"xprivkey": "tprv8ZgxMBicQKsPeHnrBXa8NfcpBQExTXq9gz8ijFPx6EJonZ5xiMt4kqWYRPJb5AmvNkTxV5qMTzkCUUfC4s3MQhpBcLg9AtpCAg5mqMeRqVr",
"utxos": {
"mv7aNQh6soUFysgh1Ax82BKBTyf1V4qWha": [
{
"txId" : "a0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9",
"outputIndex" : 0,
"address" : "mv7aNQh6soUFysgh1Ax82BKBTyf1V4qWha",
"script" : "76a914a01e048440b4cc651df6bdc41ec47781788b4b6d88ac",
"satoshis" : 70000
}
],
"mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L": [
{
"txId" : "b0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9",
"outputIndex" : 1,
"address" : "mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L",
"script" : "76a914089acaba6af8b2b4fb4bed3b747ab1e4e60b496588ac",
"satoshis" : 30000
}
]
},
"utxosAmount": {
"mv7aNQh6soUFysgh1Ax82BKBTyf1V4qWha": [
{
"txId" : "a0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9",
"outputIndex" : 0,
"address" : "mv7aNQh6soUFysgh1Ax82BKBTyf1V4qWha",
"script" : "76a914089acaba6af8b2b4fb4bed3b747ab1e4e60b496588ac",
"amount" : 0.0007
}
],
"mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L": [
{
"txId" : "b0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9",
"outputIndex" : 1,
"address" : "mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L",
"script" : "76a914089acaba6af8b2b4fb4bed3b747ab1e4e60b496588ac",
"amount" : 0.0003
}
]
},
"utxosNaN": {
"mv7aNQh6soUFysgh1Ax82BKBTyf1V4qWha": [
{
"txId" : "a0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9",
"outputIndex" : 0,
"address" : "mv7aNQh6soUFysgh1Ax82BKBTyf1V4qWha",
"script" : "76a914089acaba6af8b2b4fb4bed3b747ab1e4e60b496588ac"
}
],
"mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L": [
{
"txId" : "b0a08e397203df68392ee95b3f08b0b3b3e2401410a38d46ae0874f74846f2e9",
"outputIndex" : 1,
"address" : "mzso6uXxfDCq4L6xAffUD9BPWo6bdFBZ2L",
"script" : "76a914089acaba6af8b2b4fb4bed3b747ab1e4e60b496588ac"
}
]
}
}

View File

@ -385,7 +385,7 @@ describe('Bitcoin DB', function() {
it('should return the correct methods', function() {
var db = new DB({path: 'path', store: memdown});
var methods = db.getAPIMethods();
methods.length.should.equal(6);
methods.length.should.equal(5);
});
});