Refactor parsing test data into function, add iteration over invalid transaction tests
This commit is contained in:
parent
5af02e937a
commit
4ad36b4fb8
|
@ -15,6 +15,44 @@ var util = bitcore.util;
|
||||||
var buffertools = require('buffertools');
|
var buffertools = require('buffertools');
|
||||||
var testdata = testdata || require('./testdata');
|
var testdata = testdata || require('./testdata');
|
||||||
|
|
||||||
|
// Read tests from test/data/tx_valid.json and tx_invalid.json
|
||||||
|
// Format is an array of arrays
|
||||||
|
// Inner arrays are either [ "comment" ]
|
||||||
|
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
|
||||||
|
// ... where all scripts are stringified scripts.
|
||||||
|
// Returns an object with the Transaction object, and an array of input objects
|
||||||
|
function parse_test_transaction(entry) {
|
||||||
|
// Ignore comments
|
||||||
|
if (entry.length !== 3) return;
|
||||||
|
|
||||||
|
var inputs = [];
|
||||||
|
entry[0].forEach(function(vin) {
|
||||||
|
var hash = vin[0];
|
||||||
|
var index = vin[1];
|
||||||
|
var scriptPubKey = Script.fromHumanReadable(vin[2]);
|
||||||
|
|
||||||
|
inputs.push({
|
||||||
|
'prev_tx_hash': hash,
|
||||||
|
'index': index,
|
||||||
|
'scriptPubKey': scriptPubKey
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(scriptPubKey.toHumanReadable());
|
||||||
|
console.log('********************************');
|
||||||
|
});
|
||||||
|
|
||||||
|
var raw = new Buffer(entry[1], 'hex');
|
||||||
|
var tx = new TransactionModule();
|
||||||
|
tx.parse(raw);
|
||||||
|
|
||||||
|
// Sanity check transaction has been parsed correctly
|
||||||
|
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw));
|
||||||
|
return {
|
||||||
|
'transaction': tx,
|
||||||
|
'inputs': inputs
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
describe('Transaction', function() {
|
describe('Transaction', function() {
|
||||||
it('should initialze the main object', function() {
|
it('should initialze the main object', function() {
|
||||||
should.exist(TransactionModule);
|
should.exist(TransactionModule);
|
||||||
|
@ -261,60 +299,49 @@ describe('Transaction', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// Read tests from test/data/tx_valid.json
|
// Verify that known valid transactions are intepretted correctly
|
||||||
// Format is an array of arrays
|
|
||||||
// Inner arrays are either [ "comment" ]
|
|
||||||
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
|
|
||||||
// ... where all scripts are stringified scripts.
|
|
||||||
testdata.dataTxValid.forEach(function(datum) {
|
testdata.dataTxValid.forEach(function(datum) {
|
||||||
if (datum.length === 3) {
|
var testTx = parse_test_transaction(datum);
|
||||||
it('valid tx=' + datum[1], function() {
|
if (!testTx) return;
|
||||||
var inputs = datum[0];
|
var transactionString = buffertools.toHex(
|
||||||
var inputScriptPubKeys = [];
|
testTx.transaction.serialize());
|
||||||
inputs.forEach(function(vin) {
|
|
||||||
var hash = vin[0];
|
|
||||||
var index = vin[1];
|
|
||||||
debugger;
|
|
||||||
var scriptPubKey = Script.fromHumanReadable(vin[2]);
|
|
||||||
inputScriptPubKeys.push(scriptPubKey);
|
|
||||||
console.log(scriptPubKey.toHumanReadable());
|
|
||||||
console.log('********************************');
|
|
||||||
done();
|
|
||||||
|
|
||||||
});
|
it('valid tx=' + transactionString, function() {
|
||||||
var raw = new Buffer(datum[1], 'hex');
|
// Verify that all inputs are valid
|
||||||
var tx = new Transaction();
|
testTx.inputs.forEach(function(input) {
|
||||||
tx.parse(raw);
|
testTx.transaction.verifyInput(input.index, input.scriptPubKey,
|
||||||
|
function(err, results) {
|
||||||
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw));
|
|
||||||
|
|
||||||
var n = 0;
|
|
||||||
inputScriptPubKeys.forEach(function(scriptPubKey) {
|
|
||||||
var err = undefined;
|
|
||||||
var results = undefined;
|
|
||||||
var inputVerified = false;
|
|
||||||
|
|
||||||
tx.verifyInput(n, scriptPubKey, function(e, r) {
|
|
||||||
// Exceptions raised inside this function will be handled
|
// Exceptions raised inside this function will be handled
|
||||||
// ...by this function, so don't do it.
|
// ...by this function, so ignore if that is the case
|
||||||
err = e;
|
if (err && err.constructor.name === "AssertionError") return;
|
||||||
results = r;
|
|
||||||
inputVerified = true;
|
should.not.exist(err);
|
||||||
|
should.exist(results);
|
||||||
|
results.should.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO(mattfaus): Add a Promise or something that makes this code
|
|
||||||
// execute only after the verifyInput() callback has finished
|
|
||||||
while (!inputVerified) { }
|
|
||||||
|
|
||||||
should.not.exist(err);
|
|
||||||
should.exist(results);
|
|
||||||
results.should.equal(true);
|
|
||||||
|
|
||||||
n += 1;
|
|
||||||
});
|
|
||||||
|
|
||||||
// TODO(mattfaus): Other verifications?
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Verify that known invalid transactions are interpretted correctly
|
||||||
|
test_data.dataTxInvalid.forEach(function(datum) {
|
||||||
|
var testTx = parse_test_transaction(datum);
|
||||||
|
if (!testTx) return;
|
||||||
|
var transactionString = buffertools.toHex(
|
||||||
|
testTx.transaction.serialize());
|
||||||
|
|
||||||
|
it('valid tx=' + transactionString, function() {
|
||||||
|
// Verify that all inputs are invalid
|
||||||
|
testTx.inputs.forEach(function(input) {
|
||||||
|
testTx.transaction.verifyInput(input.index, input.scriptPubKey,
|
||||||
|
function(err, results) {
|
||||||
|
// Exceptions raised inside this function will be handled
|
||||||
|
// ...by this function, so ignore if that is the case
|
||||||
|
if (err && err.constructor.name === "AssertionError") return;
|
||||||
|
|
||||||
|
should.exist(err);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue