tests. block reward.

This commit is contained in:
Christopher Jeffrey 2016-05-15 21:47:39 -07:00
parent 39db4e065b
commit 52541fe123
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 89 additions and 87 deletions

View File

@ -475,14 +475,13 @@ Block.prototype.getClaimed = function getClaimed() {
Block.reward = function _reward(height, network) {
var halvings, reward;
assert(height !== -1, 'Height is negative.');
assert(height >= 0, 'Bad height for reward.');
network = bcoin.network.get(network);
halvings = height / network.halvingInterval | 0;
if (height < 0)
return 0;
// BIP 42
// https://github.com/bitcoin/bips/blob/master/bip-0042.mediawiki
if (halvings >= 64)
return 0;
@ -500,15 +499,7 @@ Block.reward = function _reward(height, network) {
if (halvings >= 33)
return 0;
reward = 2500000000 >>> (halvings - 1);
// We could do this to keep this more (or
// less?) accurate, but this is infinitely
// slower.
// reward = 5000000000;
// reward = Math.floor(reward / Math.pow(2, halvings));
return reward;
return 2500000000 >>> (halvings - 1);
};
/**

View File

@ -1,11 +1,13 @@
var bn = require('bn.js');
var bcoin = require('../').set('main');
var utils = bcoin.utils;
var constants = bcoin.protocol.constants;
var network = bcoin.protocol.network;
var assert = require('assert');
describe('Block', function() {
var parser = bcoin.protocol.parser;
var block = bcoin.merkleblock({
type: 'block',
version: 2,
prevBlock: 'd1831d4411bdfda89d9d8c842b541beafd1437fc560dbe5c0000000000000000',
merkleRoot: '28bec1d35af480ba3884553d72694f6ba6c163a5c081d7e6edaec15f373f19af',
@ -13,7 +15,7 @@ describe('Block', function() {
bits: 419465580,
nonce: 1186968784,
totalTX: 461,
hashes:[
hashes: [
'7d22e53bce1bbb3294d1a396c5acc45bdcc8f192cb492f0d9f55421fd4c62de1',
'9d6d585fdaf3737b9a54aaee1dd003f498328d699b7dfb42dd2b44b6ebde2333',
'8b61da3053d6f382f2145bdd856bc5dcf052c3a11c1784d3d51b2cbe0f6d0923',
@ -23,9 +25,10 @@ describe('Block', function() {
'c7c152869db09a5ae2291fa03142912d9d7aba75be7d491a8ac4230ee9a920cb',
'5adbf04583354515a225f2c418de7c5cdac4cef211820c79717cd2c50412153f',
'1f5e46b9da3a8b1241f4a1501741d3453bafddf6135b600b926e3f4056c6d564',
'33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ],
flags: new Buffer([ 245, 90, 0 ])
}, 'merkleblock');
'33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562'
],
flags: new Buffer([245, 90, 0])
});
var raw = block.toRaw('hex');
it('should parse partial merkle tree', function() {
@ -40,19 +43,19 @@ describe('Block', function() {
});
it('should decode/encode with parser/framer', function() {
var b = bcoin.merkleblock(parser.parseMerkleBlock(new Buffer(raw, 'hex')));
var b = bcoin.merkleblock.fromRaw(raw, 'hex');
assert.equal(b.render().toString('hex'), raw);
});
it('should be verifiable', function() {
var b = bcoin.merkleblock(parser.parseMerkleBlock(new Buffer(raw, 'hex')));
var b = bcoin.merkleblock.fromRaw(raw, 'hex');
assert(b.verify());
});
it('should be jsonified and unjsonified and still verify', function() {
var json = block.toRaw();
var b = bcoin.merkleblock.fromRaw(json);
assert.deepEqual(b.render(), json);
var raw = block.toRaw();
var b = bcoin.merkleblock.fromRaw(raw);
assert.deepEqual(b.render(), raw);
assert(b.verify());
});
@ -63,6 +66,7 @@ describe('Block', function() {
for (;;) {
reward = bcoin.block.reward(height);
assert(reward <= constants.COIN * 50);
total += reward;
if (reward === 0)
break;

View File

@ -10,19 +10,17 @@ var bn = require('bn.js');
describe('Script', function() {
it('should encode/decode script', function() {
var src = '20' +
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f' +
'20' +
'101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f' +
'ac';
var src = '20'
+ '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
+ '20'
+ '101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f'
+ 'ac';
var decoded = bcoin.script.decode(new Buffer(src, 'hex'));
assert.equal(decoded.length, 3);
assert.equal(
decoded[0].toString('hex'),
assert.equal(decoded[0].toString('hex'),
'000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f');
assert.equal(
decoded[1].toString('hex'),
assert.equal(decoded[1].toString('hex'),
'101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f');
assert.equal(decoded[2], opcodes.OP_CHECKSIG);
@ -31,25 +29,26 @@ describe('Script', function() {
});
it('should encode/decode numbers', function() {
var script = [ 0, 0x51, 0x52, 0x60 ];
var script = [0, 0x51, 0x52, 0x60];
var encoded = bcoin.script.encode(script);
var decoded = bcoin.script.decode(encoded);
assert.deepEqual(decoded, script);
});
it('should recognize a P2SH output', function () {
var hex = 'a91419a7d869032368fd1f1e26e5e73a4ad0e474960e87'
it('should recognize a P2SH output', function() {
var hex = 'a91419a7d869032368fd1f1e26e5e73a4ad0e474960e87';
var decoded = bcoin.script.fromRaw(hex, 'hex');
assert(decoded.isScripthash())
assert(decoded.isScripthash());
});
it('should recognize a Null Data output', function () {
var hex = '6a28590c080112220a1b353930632e6f7267282a5f5e294f7665726c6179404f7261636c65103b1a010c'
it('should recognize a Null Data output', function() {
var hex = '6a28590c080112220a1b353930632e6f7267282a5f'
+ '5e294f7665726c6179404f7261636c65103b1a010c';
var decoded = bcoin.script.fromRaw(hex, 'hex');
assert(decoded.isNulldata())
assert(decoded.isNulldata());
});
it('should handle if statements correctly', function () {
it('should handle if statements correctly', function() {
var inputScript = new Script([opcodes.OP_1, opcodes.OP_2]);
var prevOutScript = new Script([
opcodes.OP_2,
@ -140,7 +139,7 @@ describe('Script', function() {
return true;
}
it('should handle bad size pushes correctly.', function () {
it('should handle bad size pushes correctly.', function() {
var err;
var stack = new bcoin.stack();
var s = bcoin.script.fromString(
@ -214,44 +213,52 @@ describe('Script', function() {
assert(err.code === 'BAD_OPCODE');
});
it('should handle CScriptNums correctly', function () {
var s = bcoin.script.fromSymbolic([
new Buffer([0xff, 0xff, 0xff, 0x7f]), 'OP_NEGATE', 'OP_DUP', 'OP_ADD'
it('should handle CScriptNums correctly', function() {
var s = new bcoin.script([
new Buffer([0xff, 0xff, 0xff, 0x7f]),
opcodes.OP_NEGATE,
opcodes.OP_DUP,
opcodes.OP_ADD
]);
var s2 = bcoin.script.fromSymbolic([
var s2 = new bcoin.script([
new Buffer([0xfe, 0xff, 0xff, 0xff, 0x80]),
'OP_EQUAL'
opcodes.OP_EQUAL
]);
var stack = new bcoin.stack();
assert(s.execute(stack));
assert(success(s2.execute(stack), stack));
});
it('should handle CScriptNums correctly', function () {
var s = bcoin.script.fromSymbolic([
'OP_11', 'OP_10', 'OP_1', 'OP_ADD'
it('should handle CScriptNums correctly', function() {
var s = new bcoin.script([
opcodes.OP_11,
opcodes.OP_10,
opcodes.OP_1,
opcodes.OP_ADD
]);
var s2 = bcoin.script.fromSymbolic([
'OP_NUMNOTEQUAL',
'OP_NOT'
var s2 = new bcoin.script([
opcodes.OP_NUMNOTEQUAL,
opcodes.OP_NOT
]);
var stack = new bcoin.stack();
assert(s.execute(stack));
assert(success(s2.execute(stack), stack));
});
it('should handle OP_ROLL correctly', function () {
var s = bcoin.script.fromSymbolic([
new Buffer([0x16]), new Buffer([0x15]), new Buffer([0x14])
it('should handle OP_ROLL correctly', function() {
var s = new bcoin.script([
new Buffer([0x16]),
new Buffer([0x15]),
new Buffer([0x14])
]);
var s2 = bcoin.script.fromSymbolic([
'OP_0',
'OP_ROLL',
var s2 = new bcoin.script([
opcodes.OP_0,
opcodes.OP_ROLL,
new Buffer([0x14]),
'OP_EQUALVERIFY',
'OP_DEPTH',
'OP_2',
'OP_EQUAL'
opcodes.OP_EQUALVERIFY,
opcodes.OP_DEPTH,
opcodes.OP_2,
opcodes.OP_EQUAL
]);
var stack = new bcoin.stack();
assert(s.execute(stack));
@ -287,7 +294,7 @@ describe('Script', function() {
[false, true].forEach(function(nocache) {
var suffix = nocache ? ' without cache' : ' with cache';
it('should handle script test' + suffix + ': ' + comments, function () {
it('should handle script test' + suffix + ': ' + comments, function() {
var coin = bcoin.tx({
version: 1,
inputs: [{

View File

@ -214,19 +214,19 @@ describe('TX', function() {
if (valid) {
if (comments.indexOf('Coinbase') === 0) {
it('should handle valid coinbase' + suffix + ': ' + comments, function () {
it('should handle valid coinbase' + suffix + ': ' + comments, function() {
clearCache(tx, nocache);
assert.ok(tx.isSane());
});
return;
}
it('should handle valid tx test' + suffix + ': ' + comments, function () {
it('should handle valid tx test' + suffix + ': ' + comments, function() {
clearCache(tx, nocache);
assert.ok(tx.verify(null, true, flags));
});
} else {
if (comments === 'Duplicate inputs') {
it('should handle duplicate input test' + suffix + ': ' + comments, function () {
it('should handle duplicate input test' + suffix + ': ' + comments, function() {
clearCache(tx, nocache);
assert.ok(tx.verify(null, true, flags));
assert.ok(!tx.isSane());
@ -234,7 +234,7 @@ describe('TX', function() {
return;
}
if (comments === 'Negative output') {
it('should handle invalid tx (negative)' + suffix + ': ' + comments, function () {
it('should handle invalid tx (negative)' + suffix + ': ' + comments, function() {
clearCache(tx, nocache);
assert.ok(tx.verify(null, true, flags));
assert.ok(!tx.isSane());
@ -242,13 +242,13 @@ describe('TX', function() {
return;
}
if (comments.indexOf('Coinbase') === 0) {
it('should handle invalid coinbase' + suffix + ': ' + comments, function () {
it('should handle invalid coinbase' + suffix + ': ' + comments, function() {
clearCache(tx, nocache);
assert.ok(!tx.isSane());
});
return;
}
it('should handle invalid tx test' + suffix + ': ' + comments, function () {
it('should handle invalid tx test' + suffix + ': ' + comments, function() {
clearCache(tx, nocache);
assert.ok(!tx.verify(null, true, flags));
});
@ -273,7 +273,7 @@ describe('TX', function() {
hexType = hexType.toString(16);
if (hexType.length % 2 !== 0)
hexType = '0' + hexType;
it('should get signature hash of ' + data[4] + ' (' + hexType + ')' + suffix, function () {
it('should get signature hash of ' + data[4] + ' (' + hexType + ')' + suffix, function() {
var subscript = script.getSubscript(0).removeSeparators();
var hash = tx.signatureHash(index, subscript, type, 0).toString('hex');
assert.equal(hash, expected);
@ -303,7 +303,7 @@ describe('TX', function() {
};
}
it('should fail on >51 bit coin values', function () {
it('should fail on >51 bit coin values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY + 1)],
@ -317,7 +317,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should handle 51 bit coin values', function () {
it('should handle 51 bit coin values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY)],
@ -331,7 +331,7 @@ describe('TX', function() {
assert.ok(tx.checkInputs(0));
});
it('should fail on >51 bit output values', function () {
it('should fail on >51 bit output values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY)],
@ -345,7 +345,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should handle 51 bit output values', function () {
it('should handle 51 bit output values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY)],
@ -359,7 +359,7 @@ describe('TX', function() {
assert.ok(tx.checkInputs(0));
});
it('should fail on >51 bit fees', function () {
it('should fail on >51 bit fees', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY + 1)],
@ -373,7 +373,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >51 bit values from multiple', function () {
it('should fail on >51 bit values from multiple', function() {
var tx = bcoin.tx({
version: 1,
inputs: [
@ -391,7 +391,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >51 bit output values from multiple', function () {
it('should fail on >51 bit output values from multiple', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY)],
@ -415,7 +415,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >51 bit fees from multiple', function () {
it('should fail on >51 bit fees from multiple', function() {
var tx = bcoin.tx({
version: 1,
inputs: [
@ -433,7 +433,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >51 bit fees from multiple txs', function () {
it('should fail on >51 bit fees from multiple txs', function() {
var data = utils.merge({}, bcoin.network.get().genesis, { height: 0 });
var block = new bcoin.block(data);
for (var i = 0; i < 3; i++) {
@ -453,7 +453,7 @@ describe('TX', function() {
assert.equal(block.getReward(), -1);
});
it('should fail to parse >53 bit values', function () {
it('should fail to parse >53 bit values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [
@ -479,7 +479,7 @@ describe('TX', function() {
});
});
it('should fail on 53 bit coin values', function () {
it('should fail on 53 bit coin values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(utils.MAX_SAFE_INTEGER)],
@ -493,7 +493,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on 53 bit output values', function () {
it('should fail on 53 bit output values', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY)],
@ -507,7 +507,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on 53 bit fees', function () {
it('should fail on 53 bit fees', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(utils.MAX_SAFE_INTEGER)],
@ -522,7 +522,7 @@ describe('TX', function() {
});
[utils.MAX_SAFE_ADDITION, utils.MAX_SAFE_INTEGER].forEach(function(MAX) {
it('should fail on >53 bit values from multiple', function () {
it('should fail on >53 bit values from multiple', function() {
var tx = bcoin.tx({
version: 1,
inputs: [
@ -540,7 +540,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >53 bit output values from multiple', function () {
it('should fail on >53 bit output values from multiple', function() {
var tx = bcoin.tx({
version: 1,
inputs: [createInput(constants.MAX_MONEY)],
@ -564,7 +564,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >53 bit fees from multiple', function () {
it('should fail on >53 bit fees from multiple', function() {
var tx = bcoin.tx({
version: 1,
inputs: [
@ -582,7 +582,7 @@ describe('TX', function() {
assert.ok(!tx.checkInputs(0));
});
it('should fail on >53 bit fees from multiple txs', function () {
it('should fail on >53 bit fees from multiple txs', function() {
var data = utils.merge({}, bcoin.network.get().genesis, { height: 0 });
var block = new bcoin.block(data);
for (var i = 0; i < 3; i++) {