Browse Source

replace .buffer with .read

patch-2
Ryan X. Charles 11 years ago
parent
commit
0212e4bd4c
  1. 4
      lib/blockheader.js
  2. 12
      lib/bufferreader.js
  3. 8
      lib/script.js
  4. 4
      lib/txin.js
  5. 2
      lib/txout.js
  6. 14
      test/bufferreader.js

4
lib/blockheader.js

@ -35,8 +35,8 @@ Blockheader.prototype.fromBuffer = function(buf) {
Blockheader.prototype.fromBufferReader = function(br) {
this.version = br.readUInt32LE();
this.prevblockidbuf = br.buffer(32);
this.merklerootbuf = br.buffer(32);
this.prevblockidbuf = br.read(32);
this.merklerootbuf = br.read(32);
this.time = br.readUInt32LE();
this.bits = br.readUInt32LE();
this.nonce = br.readUInt32LE();

12
lib/bufferreader.js

@ -22,10 +22,6 @@ BufferReader.prototype.eof = function() {
return this.pos >= this.buf.length;
};
BufferReader.prototype.buffer = function(len) {
return this.read(len);
};
BufferReader.prototype.read = function(len) {
if (!len)
var len = this.buf.length;
@ -102,13 +98,13 @@ BufferReader.prototype.readVarintBuf = function() {
var first = this.buf.readUInt8(this.pos);
switch (first) {
case 0xFD:
return this.buffer(1 + 2);
return this.read(1 + 2);
case 0xFE:
return this.buffer(1 + 4);
return this.read(1 + 4);
case 0xFF:
return this.buffer(1 + 8);
return this.read(1 + 8);
default:
return this.buffer(1);
return this.read(1);
}
};

8
lib/script.js

@ -37,13 +37,13 @@ Script.prototype.fromBuffer = function(buf) {
if (opcodenum > 0 && opcodenum < Opcode.map.OP_PUSHDATA1) {
len = opcodenum;
this.chunks.push({
buf: br.buffer(len),
buf: br.read(len),
len: len,
opcodenum: opcodenum
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA1) {
len = br.readUInt8();
var buf = br.buffer(len);
var buf = br.read(len);
this.chunks.push({
buf: buf,
len: len,
@ -51,7 +51,7 @@ Script.prototype.fromBuffer = function(buf) {
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA2) {
len = br.readUInt16LE();
buf = br.buffer(len);
buf = br.read(len);
this.chunks.push({
buf: buf,
len: len,
@ -59,7 +59,7 @@ Script.prototype.fromBuffer = function(buf) {
});
} else if (opcodenum === Opcode.map.OP_PUSHDATA4) {
len = br.readUInt32LE();
buf = br.buffer(len);
buf = br.read(len);
this.chunks.push({
buf: buf,
len: len,

4
lib/txin.js

@ -32,10 +32,10 @@ Txin.prototype.fromBuffer = function(buf) {
};
Txin.prototype.fromBufferReader = function(br) {
this.txidbuf = br.buffer(32);
this.txidbuf = br.read(32);
this.txoutnum = br.readUInt32LE();
this.varint = Varint(br.readVarintBuf());
this.script = Script().fromBuffer(br.buffer(this.varint.toNumber()));
this.script = Script().fromBuffer(br.read(this.varint.toNumber()));
this.seqnum = br.readUInt32LE();
return this;
};

2
lib/txout.js

@ -33,7 +33,7 @@ Txout.prototype.fromBuffer = function(buf) {
Txout.prototype.fromBufferReader = function(br) {
this.valuebn = br.readUInt64LEBN();
this.varint = Varint(br.readVarintNum());
this.script = Script().fromBuffer(br.buffer(this.varint.toNumber()));
this.script = Script().fromBuffer(br.read(this.varint.toNumber()));
return this;
};

14
test/bufferreader.js

@ -36,20 +36,6 @@ describe('BufferReader', function() {
});
describe('#buffer', function() {
it('should return a buffer of this length', function() {
var buf = new Buffer(10);
buf.fill(0);
var br = new BufferReader(buf);
var buf2 = br.buffer(2);
buf2.length.should.equal(2);
br.eof().should.equal(false);
br.pos.should.equal(2);
});
});
describe('read', function() {
it('should return the same buffer', function() {

Loading…
Cancel
Save