refactor. framer sizes.
This commit is contained in:
parent
498944781e
commit
359cef93a0
@ -100,7 +100,6 @@ bcoin.coinview = require('./bcoin/coinview');
|
||||
bcoin.tx = require('./bcoin/tx');
|
||||
bcoin.mtx = require('./bcoin/mtx');
|
||||
bcoin.ldb = require('./bcoin/ldb');
|
||||
bcoin.txpool = require('./bcoin/tx-pool');
|
||||
bcoin.txdb = require('./bcoin/txdb');
|
||||
bcoin.abstractblock = require('./bcoin/abstractblock');
|
||||
bcoin.compactblock = require('./bcoin/compactblock');
|
||||
|
||||
@ -108,16 +108,11 @@ MTX.prototype.getRaw = function getRaw() {
|
||||
};
|
||||
|
||||
MTX.prototype.getSize = function getSize() {
|
||||
return this.getRaw().length;
|
||||
return bcoin.protocol.framer.tx.witnessSize(this);
|
||||
};
|
||||
|
||||
MTX.prototype.getVirtualSize = function getVirtualSize() {
|
||||
var raw = this.getRaw();
|
||||
var size = raw.length;
|
||||
var witnessSize = raw._witnessSize;
|
||||
var base = size - witnessSize;
|
||||
|
||||
return (base * 4 + witnessSize + 3) / 4 | 0;
|
||||
return bcoin.protocol.framer.tx.virtualSize(this);
|
||||
};
|
||||
|
||||
MTX.prototype.addInput = function addInput(options, index) {
|
||||
@ -135,15 +130,12 @@ MTX.prototype.addInput = function addInput(options, index) {
|
||||
|
||||
assert(options.prevout);
|
||||
|
||||
// i = this._inputIndex(options.prevout.hash, options.prevout.index);
|
||||
// assert(i === -1);
|
||||
|
||||
input = bcoin.input(options, this);
|
||||
|
||||
if (options.script instanceof bcoin.script)
|
||||
if (options.script instanceof Script)
|
||||
input.script = options.script.clone();
|
||||
|
||||
if (options.witness instanceof bcoin.script.witness)
|
||||
if (options.witness instanceof Witness)
|
||||
input.witness = options.witness.clone();
|
||||
|
||||
this.inputs.push(input);
|
||||
@ -666,18 +658,17 @@ MTX.prototype.scriptOutput = function scriptOutput(index, options) {
|
||||
output = this.outputs[index];
|
||||
assert(output);
|
||||
|
||||
if (options.script instanceof bcoin.script)
|
||||
if (options.script instanceof Script)
|
||||
output.script = options.script.clone();
|
||||
else if (options.script)
|
||||
output.script = bcoin.script(options.script);
|
||||
output.script = Script(options.script);
|
||||
else
|
||||
output.script = Script.createOutputScript(options);
|
||||
};
|
||||
|
||||
MTX.prototype.maxSize = function maxSize(maxM, maxN) {
|
||||
var copy = this.clone();
|
||||
var i, j, input, total, size, prev, m, n;
|
||||
var witness;
|
||||
var i, j, input, total, size, prev, m, n, witness;
|
||||
|
||||
// Create copy with 0-script inputs
|
||||
for (i = 0; i < copy.inputs.length; i++) {
|
||||
@ -685,7 +676,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) {
|
||||
copy.inputs[i].witness = new Witness([]);
|
||||
}
|
||||
|
||||
total = copy.render().length;
|
||||
total = bcoin.protocol.framer.tx.size(copy);
|
||||
|
||||
// Add size for signatures and public keys
|
||||
for (i = 0; i < copy.inputs.length; i++) {
|
||||
@ -706,7 +697,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) {
|
||||
// the isMultisig clause.
|
||||
// OP_PUSHDATA2 [redeem]
|
||||
prev = this.inputs[i].script.getRedeem();
|
||||
size += utils.sizeVarint(prev.getSize()) + prev.getSize();
|
||||
size += 3 + prev.getSize();
|
||||
}
|
||||
|
||||
if (prev.isWitnessProgram()) {
|
||||
@ -717,7 +708,7 @@ MTX.prototype.maxSize = function maxSize(maxM, maxN) {
|
||||
size *= 4;
|
||||
if (this.inputs[i].witness.items.length && prev.isWitnessScripthash()) {
|
||||
prev = this.inputs[i].witness.getRedeem();
|
||||
size += utils.sizeVarint(prev.getSize()) + prev.getSize();
|
||||
size += 3 + prev.getSize();
|
||||
} else if (prev.isWitnessPubkeyhash()) {
|
||||
prev = Script.createPubkeyhash(prev.code[1]);
|
||||
}
|
||||
|
||||
@ -695,50 +695,56 @@ Framer.alert = function alert(data, writer) {
|
||||
return p;
|
||||
};
|
||||
|
||||
// Witness size
|
||||
Framer.blockSizes = function blockSizes(block) {
|
||||
var sizes = Framer.witnessBlock(block, new BufferWriter());
|
||||
// Total size and size of witness
|
||||
Framer.block._sizes = function blockSizes(block) {
|
||||
var writer = new BufferWriter();
|
||||
Framer.witnessBlock(block, writer);
|
||||
return {
|
||||
size: sizes._size,
|
||||
witnessSize: sizes._witnessSize
|
||||
size: writer.written,
|
||||
witnessSize: writer._witnessSize
|
||||
};
|
||||
};
|
||||
|
||||
Framer.txSizes = function txSizes(tx) {
|
||||
var sizes = Framer.renderTX(tx, true, new BufferWriter());
|
||||
Framer.tx._sizes = function txSizes(tx) {
|
||||
var writer = new BufferWriter();
|
||||
Framer.renderTX(tx, true, writer);
|
||||
return {
|
||||
size: sizes._size,
|
||||
witnessSize: sizes._witnessSize
|
||||
size: writer.written,
|
||||
witnessSize: writer._witnessSize
|
||||
};
|
||||
};
|
||||
|
||||
// Size with witness (if present)
|
||||
Framer.blockRealSize = function blockRealSize(block) {
|
||||
return Framer.blockSizes(block).size;
|
||||
Framer.block.witnessSize = function blockWitnessSize(block) {
|
||||
return Framer.block._sizes(block).size;
|
||||
};
|
||||
|
||||
Framer.txRealSize = function txRealSize(tx) {
|
||||
return Framer.txSizes(tx).size;
|
||||
Framer.tx.witnessSize = function txWitnessSize(tx) {
|
||||
return Framer.tx._sizes(tx).size;
|
||||
};
|
||||
|
||||
// Size without witness
|
||||
Framer.blockSize = function blockSize(block) {
|
||||
return Framer.block(block, new BufferWriter())._size;
|
||||
Framer.block.size = function blockSize(block) {
|
||||
var writer = new BufferWriter();
|
||||
Framer.block(block, writer);
|
||||
return writer.written;
|
||||
};
|
||||
|
||||
Framer.txSize = function txSize(tx) {
|
||||
return Framer.renderTX(tx, false, new BufferWriter())._size;
|
||||
Framer.tx.size = function txSize(tx) {
|
||||
var writer = new BufferWriter()
|
||||
Framer.renderTX(tx, false, writer);
|
||||
return writer.written;
|
||||
};
|
||||
|
||||
// Virtual size
|
||||
Framer.blockVsize = function blockVsize(block) {
|
||||
var sizes = this.blockSizes(block);
|
||||
Framer.block.virtualSize = function blockVirtualSize(block) {
|
||||
var sizes = Framer.block._sizes(block);
|
||||
var base = sizes.size - sizes.witnessSize;
|
||||
return (base * 4 + sizes.witnessSize + 3) / 4 | 0;
|
||||
};
|
||||
|
||||
Framer.txVsize = function txVsize(tx) {
|
||||
var sizes = this.txSizes(tx);
|
||||
Framer.tx.virtualSize = function txVirtualSize(tx) {
|
||||
var sizes = Framer.tx._sizes(tx);
|
||||
var base = sizes.size - sizes.witnessSize;
|
||||
return (base * 4 + sizes.witnessSize + 3) / 4 | 0;
|
||||
};
|
||||
|
||||
@ -1280,7 +1280,7 @@ Script.checkPush = function checkPush(value, flags) {
|
||||
};
|
||||
|
||||
Script.isCode = function isCode(buf) {
|
||||
var i, b;
|
||||
var i, op, code;
|
||||
|
||||
if (!buf)
|
||||
return false;
|
||||
@ -1288,13 +1288,13 @@ Script.isCode = function isCode(buf) {
|
||||
if (!Buffer.isBuffer(buf))
|
||||
return false;
|
||||
|
||||
buf = Script.decode(buf);
|
||||
code = Script.decode(buf);
|
||||
|
||||
for (i = 0; i < buf.length; i++) {
|
||||
b = buf[i];
|
||||
if (Buffer.isBuffer(b))
|
||||
for (i = 0; i < code.length; i++) {
|
||||
op = code[i];
|
||||
if (Buffer.isBuffer(op))
|
||||
continue;
|
||||
if (constants.opcodesByVal[b] == null)
|
||||
if (constants.opcodesByVal[op] == null)
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -1321,16 +1321,24 @@ Script.createPubkeyhash = function createPubkeyhash(hash) {
|
||||
};
|
||||
|
||||
Script.createMultisig = function createMultisig(keys, m, n) {
|
||||
if (keys.length !== n)
|
||||
throw new Error(n + ' keys are required to generate multisig script');
|
||||
var code = [];
|
||||
var i;
|
||||
|
||||
assert(keys.length === n, '`n` keys are required for multisig.');
|
||||
assert(m >= 1 && m <= n);
|
||||
assert(n >= 1 && n <= 15);
|
||||
|
||||
return new Script([m + 0x50].concat(
|
||||
utils.sortKeys(keys),
|
||||
[n + 0x50, opcodes.OP_CHECKMULTISIG]
|
||||
));
|
||||
keys = utils.sortKeys(keys);
|
||||
|
||||
code.push(m + 0x50);
|
||||
|
||||
for (i = 0; i < keys.length; i++)
|
||||
code.push(keys[i]);
|
||||
|
||||
code.push(n + 0x50);
|
||||
code.push(opcodes.OP_CHECKMULTISIG);
|
||||
|
||||
return new Script(code);
|
||||
};
|
||||
|
||||
Script.createScripthash = function createScripthash(hash) {
|
||||
@ -1348,6 +1356,13 @@ Script.createNulldata = function createNulldata(flags) {
|
||||
]);
|
||||
};
|
||||
|
||||
Script.createWitnessProgram = function createWitnessProgram(version, data) {
|
||||
assert(typeof version === 'number' && version >= 0 && version <= 16);
|
||||
assert(Buffer.isBuffer(data));
|
||||
assert(data.length === 20 || data.length === 32);
|
||||
return new Script([version === 0 ? 0 : version + 0x50, data]);
|
||||
};
|
||||
|
||||
Script.prototype.getRedeem = function getRedeem() {
|
||||
if (!this.redeem)
|
||||
this.redeem = Script.getRedeem(this.code);
|
||||
@ -1703,13 +1718,6 @@ Script.prototype.isWitnessScripthash = function isWitnessScripthash() {
|
||||
return this.code[0] === opcodes.OP_0 && this.code[1].length === 32;
|
||||
};
|
||||
|
||||
Script.createWitnessProgram = function createWitnessProgram(version, data) {
|
||||
assert(typeof version === 'number' && version >= 0 && version <= 16);
|
||||
assert(Buffer.isBuffer(data));
|
||||
assert(data.length === 20 || data.length === 32);
|
||||
return new Script([version === 0 ? 0 : version + 0x50, data]);
|
||||
};
|
||||
|
||||
Script.prototype.getInputType = function getInputType(prev) {
|
||||
return Script.getInputType(this.code, prev);
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user