coins: refactor compression.
This commit is contained in:
parent
d57dac13e0
commit
8b0ec7b179
@ -7,12 +7,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
|
const Coin = require('../primitives/coin');
|
||||||
const Output = require('../primitives/output');
|
const Output = require('../primitives/output');
|
||||||
const BufferReader = require('../utils/reader');
|
const BufferReader = require('../utils/reader');
|
||||||
const StaticWriter = require('../utils/staticwriter');
|
const StaticWriter = require('../utils/staticwriter');
|
||||||
const encoding = require('../utils/encoding');
|
const encoding = require('../utils/encoding');
|
||||||
const Coin = require('../primitives/coin');
|
const compress = require('./compress');
|
||||||
const {compress, decompress} = require('../coins/compress');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an unspent output.
|
* Represents an unspent output.
|
||||||
@ -188,7 +188,7 @@ CoinEntry.prototype.toWriter = function toWriter(bw) {
|
|||||||
|
|
||||||
bw.writeVarint(this.version);
|
bw.writeVarint(this.version);
|
||||||
bw.writeU32(field);
|
bw.writeU32(field);
|
||||||
compress.output(this.output, bw);
|
compress.pack(this.output, bw);
|
||||||
|
|
||||||
return bw;
|
return bw;
|
||||||
};
|
};
|
||||||
@ -236,7 +236,7 @@ CoinEntry.prototype.fromReader = function fromReader(br) {
|
|||||||
this.coinbase = (flags & 1) !== 0;
|
this.coinbase = (flags & 1) !== 0;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
|
|
||||||
decompress.output(this.output, br);
|
compress.unpack(this.output, br);
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -186,63 +186,6 @@ function sizeOutput(output) {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Compress an output.
|
|
||||||
* @param {Coin} coin
|
|
||||||
* @param {BufferWriter} bw
|
|
||||||
*/
|
|
||||||
|
|
||||||
function compressCoin(coin, bw) {
|
|
||||||
bw.writeVarint(coin.value);
|
|
||||||
compressScript(coin.script, bw);
|
|
||||||
return bw;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Decompress a script from buffer reader.
|
|
||||||
* @param {Coin} coin
|
|
||||||
* @param {BufferReader} br
|
|
||||||
*/
|
|
||||||
|
|
||||||
function decompressCoin(coin, br) {
|
|
||||||
coin.value = br.readVarint();
|
|
||||||
decompressScript(coin.script, br);
|
|
||||||
return coin;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Skip past a compressed output.
|
|
||||||
* @param {BufferWriter} bw
|
|
||||||
* @returns {Number}
|
|
||||||
*/
|
|
||||||
|
|
||||||
function skipOutput(br) {
|
|
||||||
let start = br.offset;
|
|
||||||
|
|
||||||
// Skip past the value.
|
|
||||||
br.skipVarint();
|
|
||||||
|
|
||||||
// Skip past the compressed scripts.
|
|
||||||
switch (br.readU8()) {
|
|
||||||
case 0:
|
|
||||||
case 1:
|
|
||||||
br.seek(20);
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
case 3:
|
|
||||||
case 4:
|
|
||||||
case 5:
|
|
||||||
br.seek(32);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
br.offset -= 1;
|
|
||||||
br.seek(br.readVarint() - COMPRESS_TYPES);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return br.offset - start;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compress value using an exponent. Takes advantage of
|
* Compress value using an exponent. Takes advantage of
|
||||||
* the fact that many bitcoin values are divisible by 10.
|
* the fact that many bitcoin values are divisible by 10.
|
||||||
@ -400,20 +343,6 @@ function decompressKey(key) {
|
|||||||
* Expose
|
* Expose
|
||||||
*/
|
*/
|
||||||
|
|
||||||
exports.compress = {
|
exports.pack = compressOutput;
|
||||||
output: compressOutput,
|
exports.unpack = decompressOutput;
|
||||||
coin: compressCoin,
|
exports.size = sizeOutput;
|
||||||
size: sizeOutput,
|
|
||||||
script: compressScript,
|
|
||||||
value: compressValue,
|
|
||||||
key: compressKey
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.decompress = {
|
|
||||||
output: decompressOutput,
|
|
||||||
coin: decompressCoin,
|
|
||||||
skip: skipOutput,
|
|
||||||
script: decompressScript,
|
|
||||||
value: decompressValue,
|
|
||||||
key: decompressKey
|
|
||||||
};
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user