add floats to reader and writer.

This commit is contained in:
Christopher Jeffrey 2016-05-16 15:17:51 -07:00
parent 052c18e091
commit 6e97a7d431
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 102 additions and 2 deletions

View File

@ -1825,7 +1825,7 @@ MempoolEntry.prototype.toRaw = function toRaw() {
var p = new BufferWriter();
bcoin.protocol.framer.tx(this.tx, p);
p.writeU32(this.height);
p.writeU64(this.priority);
p.writeDouble(this.priority);
p.writeVarint(this.chainValue);
p.writeU32(this.ts);
p.writeU32(this.count);
@ -1845,7 +1845,7 @@ MempoolEntry.fromRaw = function fromRaw(data) {
return new MempoolEntry({
tx: bcoin.tx.fromRaw(p),
height: p.readU32(),
priority: p.readU53(),
priority: p.readDouble(),
chainValue: p.readVarint(),
ts: p.readU32(),
count: p.readU32(),

View File

@ -545,6 +545,58 @@ BufferReader.prototype.verifyChecksum = function verifyChecksum() {
return checksum;
};
/**
* Read float le.
* @returns {Number}
*/
BufferReader.prototype.readFloat = function readFloat() {
var ret;
assert(this.offset + 4 <= this.data.length);
ret = this.data.readFloatLE(this.offset, true);
this.offset += 4;
return ret;
};
/**
* Read float be.
* @returns {Number}
*/
BufferReader.prototype.readFloatBE = function readFloatBE() {
var ret;
assert(this.offset + 4 <= this.data.length);
ret = this.data.readFloatBE(this.offset, true);
this.offset += 4;
return ret;
};
/**
* Read double float le.
* @returns {Number}
*/
BufferReader.prototype.readDouble = function readDouble() {
var ret;
assert(this.offset + 8 <= this.data.length);
ret = this.data.readDoubleLE(this.offset, true);
this.offset += 8;
return ret;
};
/**
* Read double float be.
* @returns {Number}
*/
BufferReader.prototype.readDoubleBE = function readDoubleBE() {
var ret;
assert(this.offset + 8 <= this.data.length);
ret = this.data.readDoubleBE(this.offset, true);
this.offset += 8;
return ret;
};
/*
* Expose
*/

View File

@ -62,6 +62,10 @@ BufferWriter.prototype.render = function render(keep) {
case '32be': off += utils.write32BE(data, item[1], off); break;
case '64': off += utils.write64(data, item[1], off); break;
case '64be': off += utils.write64BE(data, item[1], off); break;
case 'f': data.writeFloatLE(item[1], off, true); off += 4; break;
case 'fbe': data.writeFloatBE(item[1], off, true); off += 4; break;
case 'd': data.writeDoubleLE(item[1], off, true); off += 8; break;
case 'dbe': data.writeDoubleBE(item[1], off, true); off += 8; break;
case 'varint': off += utils.writeVarint(data, item[1], off); break;
case 'bytes': off += item[1].copy(data, off); break;
case 'str': off += data.write(item[1], off, item[2]); break;
@ -350,6 +354,50 @@ BufferWriter.prototype.fill = function fill(value, size) {
this.data.push(['bytes', buf]);
};
/**
* Write float le.
* @param {Number} value
*/
BufferWriter.prototype.writeFloat = function writeFloat(value) {
assert(typeof value === 'number');
this.written += 4;
this.data.push(['f', value]);
};
/**
* Write float be.
* @param {Number} value
*/
BufferWriter.prototype.writeFloatBE = function writeFloatBE(value) {
assert(typeof value === 'number');
this.written += 4;
this.data.push(['fbe', value]);
};
/**
* Write double le.
* @param {Number} value
*/
BufferWriter.prototype.writeDouble = function writeDouble(value) {
assert(typeof value === 'number');
this.written += 8;
this.data.push(['d', value]);
};
/**
* Write double be.
* @param {Number} value
*/
BufferWriter.prototype.writeDoubleBE = function writeDoubleBE(value) {
assert(typeof value === 'number');
this.written += 8;
this.data.push(['dbe', value]);
};
/*
* Expose
*/