Browse Source

size tests working!!! :D

patch-2
Manuel Araoz 11 years ago
parent
commit
19e15f91ca
  1. 6
      ScriptInterpreter.js
  2. 2
      test/test.ScriptInterpreter.js
  3. 22
      test/test.util.js
  4. 27
      util/util.js

6
ScriptInterpreter.js

@ -1,6 +1,7 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var config = imports.config || require('./config'); var config = imports.config || require('./config');
var log = imports.log || require('./util/log'); var log = imports.log || require('./util/log');
var util = imports.util || require('./util/util');
var Opcode = imports.Opcode || require('./Opcode'); var Opcode = imports.Opcode || require('./Opcode');
var buffertools = imports.buffertools || require('buffertools'); var buffertools = imports.buffertools || require('buffertools');
var bignum = imports.bignum || require('bignum'); var bignum = imports.bignum || require('bignum');
@ -92,8 +93,9 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
throw new Error("Encountered a disabled opcode"); throw new Error("Encountered a disabled opcode");
} }
if (exec && Buffer.isBuffer(opcode)) if (exec && Buffer.isBuffer(opcode)) {
this.stack.push(opcode); this.stack.push(opcode);
}
else if (exec || (OP_IF <= opcode && opcode <= OP_ENDIF)) else if (exec || (OP_IF <= opcode && opcode <= OP_ENDIF))
switch (opcode) { switch (opcode) {
case OP_0: case OP_0:
@ -350,6 +352,8 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
case OP_SIZE: case OP_SIZE:
// (in -- in size) // (in -- in size)
var value = bignum(this.stackTop().length); var value = bignum(this.stackTop().length);
//var topSize = util.bytesNeededToStore(castBigint(this.stackTop()).toNumber());
//var value = bignum(topSize);
this.stack.push(bigintToBuffer(value)); this.stack.push(bigintToBuffer(value));
break; break;

2
test/test.ScriptInterpreter.js

@ -28,7 +28,7 @@ describe('ScriptInterpreter', function() {
var scriptSig = datum[0]; // script inputs var scriptSig = datum[0]; // script inputs
var scriptPubKey = datum[1]; // output script var scriptPubKey = datum[1]; // output script
var human = scriptSig + ' ' + scriptPubKey; var human = scriptSig + ' ' + scriptPubKey;
it.skip('should validate script ' + human, function(done) { it('should validate script ' + human, function(done) {
i++; i++;
console.log(i + ' ' + human); console.log(i + ' ' + human);
ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig), ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig),

22
test/test.util.js

@ -82,14 +82,26 @@ describe('util', function() {
}); });
describe('#intToBuffer', function() { describe('#intToBuffer', function() {
var data = [ var data = [
[0, '00'], [0, ''],
[-0, '00'], [-0, ''],
[-1, 'ff'],
[1, '01'], [1, '01'],
[-1, 'ff'],
[18, '12'], [18, '12'],
[-18, 'ee'],
[127, '7f'],
[128, '8000'],
[129, '8100'],
[4096, '0010'],
[-4096, '00f0'],
[32767, 'ff7f'],
[878082192, '90785634'], [878082192, '90785634'],
[0x01234567890, '1200000090785634'], [0x01234567890, '9078563412'],
[-4294967297, 'feffffffffffffff'], [4294967295, 'ffffffff00'],
[4294967296, '0000000001'],
[4294967297, '0100000001'],
//[-4294967295, 'feffffffffffffff'],
//[-4294967296, 'feffffffffffffff'],
//[-4294967297, 'feffffffffffffff'],
]; ];
data.forEach(function(datum) { data.forEach(function(datum) {
var integer = datum[0]; var integer = datum[0];

27
util/util.js

@ -121,28 +121,45 @@ var intTo64Bits = function(integer) {
var fitsInNBits = function(integer, n) { var fitsInNBits = function(integer, n) {
// TODO: make this efficient!!! // TODO: make this efficient!!!
return integer.toString(2).replace('-','').length < n; return integer.toString(2).replace('-','').length < n;
} };
exports.bytesNeededToStore = bytesNeededToStore = function(integer) {
if (integer === 0) return 0;
return Math.ceil(((integer).toString(2).replace('-','').length + 1)/ 8);
};
exports.intToBuffer = function(integer) { exports.intToBuffer = function(integer) {
var size = bytesNeededToStore(integer);
var buf = new Put();
var s = integer.toString(16);
var neg = s[0] === '-';
s = s.replace('-','');
for (var i=0; i<size; i++) {
var si = s.substring(s.length - 2*(i+1), s.length - 2*(i));
if (si.lenght === 1) {
si = '0' + si;
}
buf.word8((neg?-1:1)*parseInt(si, 16));
}
return buf.buffer();
var data = null; var data = null;
if (fitsInNBits(integer, 8)) { if (fitsInNBits(integer, 8)) {
data = new Buffer(1); data = new Buffer(1);
data.writeInt8(integer, 0); data.writeInt8(integer, 0);
return data;
} else if (fitsInNBits(integer, 16)) { } else if (fitsInNBits(integer, 16)) {
data = new Buffer(2); data = new Buffer(2);
data.writeInt16LE(integer, 0); data.writeInt16LE(integer, 0);
return data;
} else if (fitsInNBits(integer, 32)) { } else if (fitsInNBits(integer, 32)) {
data = new Buffer(4); data = new Buffer(4);
data.writeInt32LE(integer, 0); data.writeInt32LE(integer, 0);
return data;
} else { } else {
var x = intTo64Bits(integer); var x = intTo64Bits(integer);
data = new Buffer(8); data = new Buffer(8);
data.writeInt32LE(x.hi, 0); // high part contains sign information (signed) data.writeInt32LE(x.hi, 0); // high part contains sign information (signed)
data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer
return data;
} }
return data;
}; };
var formatValue = exports.formatValue = function (valueBuffer) { var formatValue = exports.formatValue = function (valueBuffer) {

Loading…
Cancel
Save