test: refactor script and tx test parsing.

This commit is contained in:
Christopher Jeffrey 2017-08-11 00:06:33 -07:00
parent 6c96a8e6ba
commit e7bc53c995
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 44 additions and 36 deletions

View File

@ -557,9 +557,17 @@ exports.formatCode = function formatCode(code) {
const out = []; const out = [];
for (const op of code) { for (const op of code) {
// Bad push
if (op.value === -1) {
out.push('OP_INVALIDOPCODE');
break;
}
if (op.data) { if (op.data) {
const symbol = exports.opcodesByVal[op.value];
// Direct push // Direct push
if (!exports.opcodesByVal[op.value]) { if (!symbol) {
let size = op.value.toString(16); let size = op.value.toString(16);
if (size.length < 2) if (size.length < 2)
size = '0' + size; size = '0' + size;
@ -568,8 +576,6 @@ exports.formatCode = function formatCode(code) {
} }
// Pushdatas // Pushdatas
const symbol = exports.opcodesByVal[op.value];
let size = op.data.length.toString(16); let size = op.data.length.toString(16);
while (size.length % 2 !== 0) while (size.length % 2 !== 0)
@ -581,20 +587,14 @@ exports.formatCode = function formatCode(code) {
} }
// Opcodes // Opcodes
if (exports.opcodesByVal[op.value]) { let symbol = exports.opcodesByVal[op.value];
const symbol = exports.opcodesByVal[op.value]; if (symbol) {
out.push(symbol); out.push(symbol);
continue; continue;
} }
// Bad push
if (op.value === -1) {
out.push('OP_INVALIDOPCODE');
break;
}
// Unknown opcodes // Unknown opcodes
let symbol = op.value.toString(16); symbol = op.value.toString(16);
if (symbol.length < 2) if (symbol.length < 2)
symbol = '0' + symbol; symbol = '0' + symbol;

View File

@ -2692,7 +2692,7 @@ Script.prototype.fromString = function fromString(code) {
if (value == null) { if (value == null) {
if (item[0] === '\'') { if (item[0] === '\'') {
assert(item[item.length - 1] === '\'', 'Unknown opcode.'); assert(item[item.length - 1] === '\'', 'Invalid string.');
const str = item.slice(1, -1); const str = item.slice(1, -1);
const op = Opcode.fromString(str); const op = Opcode.fromString(str);
bw.writeBytes(op.toRaw()); bw.writeBytes(op.toRaw());
@ -2708,10 +2708,10 @@ Script.prototype.fromString = function fromString(code) {
assert(item.indexOf('0x') === 0, 'Unknown opcode.'); assert(item.indexOf('0x') === 0, 'Unknown opcode.');
const str = item.substring(2); const hex = item.substring(2);
const data = Buffer.from(str, 'hex'); const data = Buffer.from(hex, 'hex');
assert(data.length === str.length / 2, 'Unknown opcode.'); assert(data.length === hex.length / 2, 'Invalid hex string.');
bw.writeBytes(data); bw.writeBytes(data);

View File

@ -26,11 +26,11 @@ function isSuccess(stack) {
function parseScriptTest(data) { function parseScriptTest(data) {
const witArr = Array.isArray(data[0]) ? data.shift() : []; const witArr = Array.isArray(data[0]) ? data.shift() : [];
const inpHex = data[0] ? data[0].trim() : data[0] || ''; const inpHex = data[0];
const outHex = data[1] ? data[1].trim() : data[1] || ''; const outHex = data[1];
const names = data[2] ? data[2].trim().split(/,\s*/) : []; const names = data[2] || 'NONE';
const expected = data[3] || ''; const expected = data[3];
let comments = Array.isArray(data[4]) ? data[4].join('. ') : data[4] || ''; let comments = data[4];
if (!comments) if (!comments)
comments = outHex.slice(0, 60); comments = outHex.slice(0, 60);
@ -46,10 +46,13 @@ function parseScriptTest(data) {
const output = Script.fromString(outHex); const output = Script.fromString(outHex);
let flags = 0; let flags = 0;
for (const name of names) { for (const name of names.split(',')) {
const flag = `VERIFY_${name}`; const flag = Script.flags[`VERIFY_${name}`];
assert(Script.flags[flag] != null, 'Unknown flag.');
flags |= Script.flags[flag]; if (flag == null)
throw new Error(`Unknown flag: ${name}.`);
flags |= flag;
} }
return { return {

View File

@ -9,7 +9,6 @@ const encoding = require('../lib/utils/encoding');
const random = require('../lib/crypto/random'); const random = require('../lib/crypto/random');
const consensus = require('../lib/protocol/consensus'); const consensus = require('../lib/protocol/consensus');
const TX = require('../lib/primitives/tx'); const TX = require('../lib/primitives/tx');
const Coin = require('../lib/primitives/coin');
const Output = require('../lib/primitives/output'); const Output = require('../lib/primitives/output');
const Outpoint = require('../lib/primitives/outpoint'); const Outpoint = require('../lib/primitives/outpoint');
const Script = require('../lib/script/script'); const Script = require('../lib/script/script');
@ -36,24 +35,30 @@ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
const MAX_SAFE_ADDITION = 0xfffffffffffff; const MAX_SAFE_ADDITION = 0xfffffffffffff;
function clearCache(tx, noCache) { function clearCache(tx, noCache) {
if (!noCache) { if (noCache) {
const copy = tx.clone(); tx.refresh();
assert.strictEqual(tx.txid(), copy.txid());
assert.strictEqual(tx.wtxid(), copy.wtxid());
return; return;
} }
tx.refresh();
const copy = tx.clone();
assert.strictEqual(tx.txid(), copy.txid());
assert.strictEqual(tx.wtxid(), copy.wtxid());
} }
function parseTXTest(data) { function parseTXTest(data) {
const [coins, hex, names] = data; const coins = data[0];
const hex = data[1];
const names = data[2] || 'NONE';
let flags = 0; let flags = 0;
for (const name of (names || '').trim().split(/,\s*/)) { for (const name of names.split(',')) {
const flag = `VERIFY_${name}`; const flag = Script.flags[`VERIFY_${name}`];
assert(Script.flags[flag] != null, 'Unknown flag.');
flags |= Script.flags[flag]; if (flag == null)
throw new Error(`Unknown flag: ${name}.`);
flags |= flag;
} }
const view = new CoinView(); const view = new CoinView();