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