From dc56cb8d45ef2f770f0d6de5723662f9986b2efc Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 7 Mar 2014 15:41:27 -0300 Subject: [PATCH] fixed Script parse tests for all scripts (valid and invalid) --- Script.js | 5 +++-- ScriptInterpreter.js | 2 ++ test/test.Script.js | 2 +- util/util.js | 19 ++++++++++++++----- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Script.js b/Script.js index 01e5da6..ffc4ace 100644 --- a/Script.js +++ b/Script.js @@ -270,13 +270,14 @@ function spec(b) { var split = s.split(' '); for (var i = 0; i < split.length; i++) { var word = split[i]; + if (word === '') continue; if (word.length > 2 && word.substring(0, 2) === '0x') { // raw hex value //console.log('hex value'); chunks.push(new Buffer(word.substring(2, word.length), 'hex')); } else { var opcode = Opcode.map['OP_' + word]; - if (opcode) { + if (typeof opcode !== 'undefined') { // op code in string form //console.log('opcode'); chunks.push(opcode); @@ -297,7 +298,7 @@ function spec(b) { } chunks.push(new Buffer(hex,'hex')); } else { - throw new Error('Could not parse word from script: ' +word); + throw new Error('Could not parse word "' +word+'" from script "'+s+'"'); } } } diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 6e2628f..809d7f7 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -393,6 +393,8 @@ function spec(b) { // (x1 x2 - bool) var v1 = this.stackTop(2); var v2 = this.stackTop(1); + console.log(v1); + console.log(v2); var value = buffertools.compare(v1, v2) === 0; // OP_NOTEQUAL is disabled because it would be too easy to say diff --git a/test/test.Script.js b/test/test.Script.js index 00a2187..48a4a7d 100644 --- a/test/test.Script.js +++ b/test/test.Script.js @@ -84,7 +84,7 @@ describe('Script', function() { }); }); - test_data.dataScriptValid.forEach(function(datum) { + test_data.dataScriptAll.forEach(function(datum) { if (datum.length < 2) throw new Error('Invalid test data'); var human = datum[0] + ' ' + datum[1]; it('should parse script from human readable ' + human, function() { diff --git a/util/util.js b/util/util.js index c66bca4..8a01292 100644 --- a/util/util.js +++ b/util/util.js @@ -111,18 +111,27 @@ var intTo64Bits = function(integer) { lo: (integer & 0xFFFFFFFF) >>> 0 }; }; -var fitsIn32Bits = function(integer) { +var fitsInNBits = function(integer, n) { // TODO: make this efficient!!! - return integer.toString(2).replace('-','').length < 32; + return integer.toString(2).replace('-','').length < n; } exports.intToBuffer = function(integer) { - if (fitsIn32Bits(integer)) { - var data = new Buffer(4); + var data = null; + if (fitsInNBits(integer, 8)) { + data = new Buffer(1); + data.writeInt8(integer, 0); + return data; + } else if (fitsInNBits(integer, 16)) { + data = new Buffer(2); + data.writeInt16LE(integer, 0); + return data; + } else if (fitsInNBits(integer, 32)) { + data = new Buffer(4); data.writeInt32LE(integer, 0); return data; } else { var x = intTo64Bits(integer); - var data = new Buffer(8); + data = new Buffer(8); data.writeInt32LE(x.hi, 0); // high part contains sign information (signed) data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer return data;