tests with and without caching.
This commit is contained in:
parent
5bf0824000
commit
80e6126f0c
@ -30,11 +30,9 @@ describe('Script', function() {
|
|||||||
assert.equal(dst.toString('hex'), src);
|
assert.equal(dst.toString('hex'), src);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (0)
|
|
||||||
it('should encode/decode numbers', function() {
|
it('should encode/decode numbers', function() {
|
||||||
var script = [ 0, 1, 2, 16 ];
|
var script = [ 0, 0x51, 0x52, 0x60 ];
|
||||||
var encoded = bcoin.script.encode(script);
|
var encoded = bcoin.script.encode(script);
|
||||||
assert.deepEqual(encoded, [ 0, 0x51, 0x52, 0x60 ]);
|
|
||||||
var decoded = bcoin.script.decode(encoded);
|
var decoded = bcoin.script.decode(encoded);
|
||||||
assert.deepEqual(decoded, script);
|
assert.deepEqual(decoded, script);
|
||||||
});
|
});
|
||||||
@ -218,48 +216,55 @@ describe('Script', function() {
|
|||||||
}
|
}
|
||||||
flags = flag;
|
flags = flag;
|
||||||
|
|
||||||
it('should handle script test: ' + comments, function () {
|
[false, true].forEach(function(nocache) {
|
||||||
var coin = bcoin.tx({
|
var suffix = nocache ? ' without cache' : ' with cache';
|
||||||
version: 1,
|
it('should handle script test' + suffix + ': ' + comments, function () {
|
||||||
inputs: [{
|
var coin = bcoin.tx({
|
||||||
prevout: {
|
version: 1,
|
||||||
hash: constants.NULL_HASH,
|
inputs: [{
|
||||||
index: 0xffffffff
|
prevout: {
|
||||||
},
|
hash: constants.NULL_HASH,
|
||||||
coin: null,
|
index: 0xffffffff
|
||||||
script: [bcoin.script.array(0), bcoin.script.array(0)],
|
},
|
||||||
witness: new bcoin.script.witness(),
|
coin: null,
|
||||||
sequence: 0xffffffff
|
script: [bcoin.script.array(0), bcoin.script.array(0)],
|
||||||
}],
|
witness: new bcoin.script.witness(),
|
||||||
outputs: [{
|
sequence: 0xffffffff
|
||||||
script: output,
|
}],
|
||||||
value: new bn(0)
|
outputs: [{
|
||||||
}],
|
script: output,
|
||||||
locktime: 0
|
value: new bn(0)
|
||||||
|
}],
|
||||||
|
locktime: 0
|
||||||
|
});
|
||||||
|
var tx = bcoin.tx({
|
||||||
|
version: 1,
|
||||||
|
inputs: [{
|
||||||
|
prevout: {
|
||||||
|
hash: coin.hash('hex'),
|
||||||
|
index: 0
|
||||||
|
},
|
||||||
|
coin: bcoin.coin(coin, 0),
|
||||||
|
script: input,
|
||||||
|
witness: witness,
|
||||||
|
sequence: 0xffffffff
|
||||||
|
}],
|
||||||
|
outputs: [{
|
||||||
|
script: new bcoin.script(),
|
||||||
|
value: new bn(0)
|
||||||
|
}],
|
||||||
|
locktime: 0
|
||||||
|
});
|
||||||
|
if (nocache) {
|
||||||
|
delete input.raw;
|
||||||
|
delete output.raw;
|
||||||
|
}
|
||||||
|
var res = Script.verify(input, witness, output, tx, 0, flags);
|
||||||
|
if (expected === 'OK')
|
||||||
|
assert.ok(res);
|
||||||
|
else
|
||||||
|
assert.ok(!res);
|
||||||
});
|
});
|
||||||
var tx = bcoin.tx({
|
|
||||||
version: 1,
|
|
||||||
inputs: [{
|
|
||||||
prevout: {
|
|
||||||
hash: coin.hash('hex'),
|
|
||||||
index: 0
|
|
||||||
},
|
|
||||||
coin: bcoin.coin(coin, 0),
|
|
||||||
script: input,
|
|
||||||
witness: witness,
|
|
||||||
sequence: 0xffffffff
|
|
||||||
}],
|
|
||||||
outputs: [{
|
|
||||||
script: new bcoin.script(),
|
|
||||||
value: new bn(0)
|
|
||||||
}],
|
|
||||||
locktime: 0
|
|
||||||
});
|
|
||||||
var res = Script.verify(input, witness, output, tx, 0, flags);
|
|
||||||
if (expected === 'OK')
|
|
||||||
assert.ok(res);
|
|
||||||
else
|
|
||||||
assert.ok(!res);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
272
test/tx-test.js
272
test/tx-test.js
@ -22,6 +22,32 @@ function parseTX(file) {
|
|||||||
return tx;
|
return tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function clearCache(tx, nocache) {
|
||||||
|
var i, input, output;
|
||||||
|
|
||||||
|
if (!nocache)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (tx instanceof bcoin.script) {
|
||||||
|
delete tx.raw;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
delete tx.raw;
|
||||||
|
|
||||||
|
for (i = 0; i < tx.inputs.length; i++) {
|
||||||
|
input = tx.inputs[i];
|
||||||
|
delete input.script.raw;
|
||||||
|
if (input.coin)
|
||||||
|
delete input.coin.script.raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < tx.outputs.length; i++) {
|
||||||
|
output = tx.outputs[i];
|
||||||
|
delete output.script.raw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe('TX', function() {
|
describe('TX', function() {
|
||||||
var parser = bcoin.protocol.parser;
|
var parser = bcoin.protocol.parser;
|
||||||
var raw = '010000000125393c67cd4f581456dd0805fa8e9db3abdf90dbe1d4b53e28' +
|
var raw = '010000000125393c67cd4f581456dd0805fa8e9db3abdf90dbe1d4b53e28' +
|
||||||
@ -61,143 +87,157 @@ describe('TX', function() {
|
|||||||
'0f7c00000000001976a91495ad422bb5911c2c9fe6ce4f82a13c85f03d9b' +
|
'0f7c00000000001976a91495ad422bb5911c2c9fe6ce4f82a13c85f03d9b' +
|
||||||
'2e88ac00000000';
|
'2e88ac00000000';
|
||||||
|
|
||||||
it('should decode/encode with parser/framer', function() {
|
[false, true].forEach(function(nocache) {
|
||||||
var tx = bcoin.tx(parser.parseTX(new Buffer(raw, 'hex')));
|
var suffix = nocache ? ' without cache' : ' with cache';
|
||||||
assert.equal(tx.render().toString('hex'), raw);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be verifiable', function() {
|
it('should decode/encode with parser/framer' + suffix, function() {
|
||||||
var tx = bcoin.tx(parser.parseTX(new Buffer(raw, 'hex')));
|
var tx = bcoin.tx(parser.parseTX(new Buffer(raw, 'hex')));
|
||||||
var p = bcoin.tx(parser.parseTX(new Buffer(inp, 'hex')));
|
clearCache(tx, nocache);
|
||||||
tx.fillCoins(p);
|
assert.equal(tx.render().toString('hex'), raw);
|
||||||
|
});
|
||||||
|
|
||||||
assert(tx.verify());
|
it('should be verifiable' + suffix, function() {
|
||||||
});
|
var tx = bcoin.tx(parser.parseTX(new Buffer(raw, 'hex')));
|
||||||
|
var p = bcoin.tx(parser.parseTX(new Buffer(inp, 'hex')));
|
||||||
|
tx.fillCoins(p);
|
||||||
|
|
||||||
it('should verify non-minimal output', function() {
|
clearCache(tx, nocache);
|
||||||
assert(tx1.verify(null, true, constants.flags.VERIFY_P2SH));
|
clearCache(p, nocache);
|
||||||
});
|
|
||||||
|
|
||||||
it('should verify tx.version == 0', function() {
|
assert(tx.verify());
|
||||||
assert(tx2.verify(null, true, constants.flags.VERIFY_P2SH));
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('should verify sighash_single bug w/ findanddelete', function() {
|
it('should verify non-minimal output' + suffix, function() {
|
||||||
assert(tx3.verify(null, true, constants.flags.VERIFY_P2SH));
|
clearCache(tx1, nocache);
|
||||||
});
|
assert(tx1.verify(null, true, constants.flags.VERIFY_P2SH));
|
||||||
|
});
|
||||||
|
|
||||||
function parseTest(data) {
|
it('should verify tx.version == 0' + suffix, function() {
|
||||||
var coins = data[0];
|
clearCache(tx2, nocache);
|
||||||
var tx = bcoin.tx.fromRaw(data[1], 'hex');
|
assert(tx2.verify(null, true, constants.flags.VERIFY_P2SH));
|
||||||
var flags = data[2] ? data[2].trim().split(/,\s*/) : [];
|
});
|
||||||
var flag = 0;
|
|
||||||
|
|
||||||
for (var i = 0; i < flags.length; i++)
|
it('should verify sighash_single bug w/ findanddelete' + suffix, function() {
|
||||||
flag |= constants.flags['VERIFY_' + flags[i]];
|
clearCache(tx3, nocache);
|
||||||
|
assert(tx3.verify(null, true, constants.flags.VERIFY_P2SH));
|
||||||
|
});
|
||||||
|
|
||||||
flags = flag;
|
function parseTest(data) {
|
||||||
|
var coins = data[0];
|
||||||
|
var tx = bcoin.tx.fromRaw(data[1], 'hex');
|
||||||
|
var flags = data[2] ? data[2].trim().split(/,\s*/) : [];
|
||||||
|
var flag = 0;
|
||||||
|
|
||||||
coins.forEach(function(data) {
|
for (var i = 0; i < flags.length; i++)
|
||||||
var hash = data[0];
|
flag |= constants.flags['VERIFY_' + flags[i]];
|
||||||
var index = data[1];
|
|
||||||
var script = bcoin.script.fromTestString(data[2]);
|
flags = flag;
|
||||||
var value = data[3];
|
|
||||||
var coin = new bcoin.coin({
|
coins.forEach(function(data) {
|
||||||
version: 1,
|
var hash = data[0];
|
||||||
height: -1,
|
var index = data[1];
|
||||||
coinbase: false,
|
var script = bcoin.script.fromTestString(data[2]);
|
||||||
hash: utils.revHex(hash),
|
var value = data[3];
|
||||||
index: index,
|
var coin = new bcoin.coin({
|
||||||
script: script,
|
version: 1,
|
||||||
value: value != null ? new bn(value) : new bn(0)
|
height: -1,
|
||||||
|
coinbase: false,
|
||||||
|
hash: utils.revHex(hash),
|
||||||
|
index: index,
|
||||||
|
script: script,
|
||||||
|
value: value != null ? new bn(value) : new bn(0)
|
||||||
|
});
|
||||||
|
tx.fillCoins(coin);
|
||||||
});
|
});
|
||||||
tx.fillCoins(coin);
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
tx: tx,
|
tx: tx,
|
||||||
flags: flags,
|
flags: flags,
|
||||||
comments: tx.hasCoins()
|
comments: tx.hasCoins()
|
||||||
? utils._inspect(tx.inputs[0].coin.script, false).slice(0, -1)
|
? utils._inspect(tx.inputs[0].coin.script, false).slice(0, -1)
|
||||||
: 'coinbase',
|
: 'coinbase',
|
||||||
data: data
|
data: data
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[[valid, true], [invalid, false]].forEach(function(test) {
|
[[valid, true], [invalid, false]].forEach(function(test) {
|
||||||
// ["[[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"],
|
// ["[[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"],
|
||||||
var arr = test[0];
|
var arr = test[0];
|
||||||
var valid = test[1];
|
var valid = test[1];
|
||||||
var comment = '';
|
var comment = '';
|
||||||
arr.forEach(function(json, i) {
|
arr.forEach(function(json, i) {
|
||||||
if (json.length === 1) {
|
if (json.length === 1) {
|
||||||
comment += ' ' + json[0];
|
comment += ' ' + json[0];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = parseTest(json);
|
var data = parseTest(json);
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
|
comment = '';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var tx = data.tx;
|
||||||
|
clearCache(tx, nocache);
|
||||||
|
var flags = data.flags;
|
||||||
|
var comments = comment.trim();
|
||||||
|
if (!comments)
|
||||||
|
comments = data.comments;
|
||||||
comment = '';
|
comment = '';
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var tx = data.tx;
|
if (valid) {
|
||||||
var flags = data.flags;
|
if (comments.indexOf('Coinbase') === 0) {
|
||||||
var comments = comment.trim();
|
it('should handle valid coinbase' + suffix + ': ' + comments, function () {
|
||||||
if (!comments)
|
assert.ok(tx.isSane());
|
||||||
comments = data.comments;
|
});
|
||||||
comment = '';
|
return;
|
||||||
|
}
|
||||||
if (valid) {
|
it('should handle valid tx test' + suffix + ': ' + comments, function () {
|
||||||
if (comments.indexOf('Coinbase') === 0) {
|
assert.ok(tx.verify(null, true, flags));
|
||||||
it('should handle valid coinbase: ' + comments, function () {
|
|
||||||
assert.ok(tx.isSane());
|
|
||||||
});
|
});
|
||||||
return;
|
} else {
|
||||||
}
|
if (comments === 'Duplicate inputs') {
|
||||||
it('should handle valid tx test: ' + comments, function () {
|
it('should handle duplicate input test' + suffix + ': ' + comments, function () {
|
||||||
assert.ok(tx.verify(null, true, flags));
|
assert.ok(!tx.isSane());
|
||||||
});
|
});
|
||||||
} else {
|
return;
|
||||||
if (comments === 'Duplicate inputs') {
|
}
|
||||||
it('should handle duplicate input test: ' + comments, function () {
|
if (comments.indexOf('Coinbase') === 0) {
|
||||||
assert.ok(!tx.isSane());
|
it('should handle invalid coinbase' + suffix + ': ' + comments, function () {
|
||||||
|
assert.ok(!tx.isSane());
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
it('should handle invalid tx test' + suffix + ': ' + comments, function () {
|
||||||
|
assert.ok(!tx.verify(null, true, flags));
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (comments.indexOf('Coinbase') === 0) {
|
});
|
||||||
it('should handle invalid coinbase: ' + comments, function () {
|
|
||||||
assert.ok(!tx.isSane());
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
it('should handle invalid tx test: ' + comments, function () {
|
|
||||||
assert.ok(!tx.verify(null, true, flags));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
sighash.forEach(function(data) {
|
sighash.forEach(function(data) {
|
||||||
// ["raw_transaction, script, input_index, hashType, signature_hash (result)"],
|
// ["raw_transaction, script, input_index, hashType, signature_hash (result)"],
|
||||||
if (data.length === 1)
|
if (data.length === 1)
|
||||||
return;
|
return;
|
||||||
var tx = bcoin.tx.fromRaw(data[0], 'hex');
|
var tx = bcoin.tx.fromRaw(data[0], 'hex');
|
||||||
var script = new bcoin.script(new Buffer(data[1], 'hex'));
|
clearCache(tx, nocache);
|
||||||
var index = data[2];
|
var script = new bcoin.script(new Buffer(data[1], 'hex'));
|
||||||
var type = data[3];
|
clearCache(script, nocache);
|
||||||
var expected = utils.revHex(data[4]);
|
var index = data[2];
|
||||||
var hexType = type & 3;
|
var type = data[3];
|
||||||
if (type & 0x80)
|
var expected = utils.revHex(data[4]);
|
||||||
hexType |= 0x80;
|
var hexType = type & 3;
|
||||||
hexType = hexType.toString(16);
|
if (type & 0x80)
|
||||||
if (hexType.length % 2 !== 0)
|
hexType |= 0x80;
|
||||||
hexType = '0' + hexType;
|
hexType = hexType.toString(16);
|
||||||
it('should get signature hash of ' + data[4] + ' (' + hexType + ')', function () {
|
if (hexType.length % 2 !== 0)
|
||||||
var subscript = script.getSubscript();
|
hexType = '0' + hexType;
|
||||||
var hash = tx.signatureHash(index, subscript, type, 0).toString('hex');
|
it('should get signature hash of ' + data[4] + ' (' + hexType + ')' + suffix, function () {
|
||||||
assert.equal(hash, expected);
|
var subscript = script.getSubscript();
|
||||||
|
var hash = tx.signatureHash(index, subscript, type, 0).toString('hex');
|
||||||
|
assert.equal(hash, expected);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user