Merge pull request #1276 from greggzigler/feature/get-sigops-count
get-signature-count needed to measure txs with many inputs
This commit is contained in:
commit
6dd599187c
|
@ -838,4 +838,45 @@ Script.prototype.checkMinimalPush = function(i) {
|
|||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* Comes from bitcoind's script DecodOP_N function
|
||||
* @param {number} opcode
|
||||
* @returns {number} numeric value in range of 0 to 16
|
||||
*/
|
||||
Script.prototype._decodeOP_N = function(opcode) {
|
||||
if (opcode === Opcode.OP_0) {
|
||||
return 0;
|
||||
} else if (opcode >= Opcode.OP_1 && opcode <= Opcode.OP_16) {
|
||||
return opcode - (Opcode.OP_1 - 1);
|
||||
} else {
|
||||
throw new Error('Invalid opcode: ' + JSON.stringify(opcode));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Comes from bitcoind's script GetSigOpCount(boolean) function
|
||||
* @param {boolean} use current (true) or pre-version-0.6 (false) logic
|
||||
* @returns {number} number of signature operations required by this script
|
||||
*/
|
||||
Script.prototype.getSignatureOperationsCount = function(accurate) {
|
||||
accurate = (_.isUndefined(accurate) ? true : accurate);
|
||||
var self = this;
|
||||
var n = 0;
|
||||
var lastOpcode = Opcode.OP_INVALIDOPCODE;
|
||||
_.each(self.chunks, function getChunk(chunk) {
|
||||
var opcode = chunk.opcodenum;
|
||||
if (opcode == Opcode.OP_CHECKSIG || opcode == Opcode.OP_CHECKSIGVERIFY) {
|
||||
n++;
|
||||
} else if (opcode == Opcode.OP_CHECKMULTISIG || opcode == Opcode.OP_CHECKMULTISIGVERIFY) {
|
||||
if (accurate && lastOpcode >= Opcode.OP_1 && lastOpcode <= Opcode.OP_16) {
|
||||
n += self._decodeOP_N(lastOpcode);
|
||||
} else {
|
||||
n += 20;
|
||||
}
|
||||
}
|
||||
lastOpcode = opcode;
|
||||
});
|
||||
return n;
|
||||
};
|
||||
|
||||
module.exports = Script;
|
||||
|
|
|
@ -471,7 +471,7 @@ describe('Script', function() {
|
|||
});
|
||||
|
||||
describe('#buildMultisigOut', function() {
|
||||
var pubkey_hexs = [
|
||||
var pubKeyHexes = [
|
||||
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da',
|
||||
'03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9',
|
||||
'021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18',
|
||||
|
@ -479,7 +479,7 @@ describe('Script', function() {
|
|||
'036a98a36aa7665874b1ba9130bc6d318e52fd3bdb5969532d7fc09bf2476ff842',
|
||||
'033aafcbead78c08b0e0aacc1b0cdb40702a7c709b660bebd286e973242127e15b',
|
||||
];
|
||||
var sortkeys = pubkey_hexs.slice(0, 3).map(PublicKey);
|
||||
var sortkeys = pubKeyHexes.slice(0, 3).map(PublicKey);
|
||||
it('should create sorted script by default', function() {
|
||||
var s = Script.buildMultisigOut(sortkeys, 2);
|
||||
s.toString().should.equal('OP_2 33 0x021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18 33 0x022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da 33 0x03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9 OP_3 OP_CHECKMULTISIG');
|
||||
|
@ -501,7 +501,7 @@ describe('Script', function() {
|
|||
s.isMultisigOut().should.equal(true);
|
||||
});
|
||||
var test_mn = function(m, n) {
|
||||
var pubkeys = pubkey_hexs.slice(0, n).map(PublicKey);
|
||||
var pubkeys = pubKeyHexes.slice(0, n).map(PublicKey);
|
||||
var s = Script.buildMultisigOut(pubkeys, m);
|
||||
s.isMultisigOut().should.equal(true);
|
||||
};
|
||||
|
@ -775,5 +775,46 @@ describe('Script', function() {
|
|||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#getSignatureOperationsCount', function() {
|
||||
// comes from bitcoind src/test/sigopcount_tests
|
||||
// only test calls to function with boolean param, not signature ref param
|
||||
var pubKeyHexes = [
|
||||
'022df8750480ad5b26950b25c7ba79d3e37d75f640f8e5d9bcd5b150a0f85014da',
|
||||
'03e3818b65bcc73a7d64064106a859cc1a5a728c4345ff0b641209fba0d90de6e9',
|
||||
'021f2f6e1e50cb6a953935c3601284925decd3fd21bc445712576873fb8c6ebc18',
|
||||
];
|
||||
it('should return zero for empty scripts', function() {
|
||||
Script().getSignatureOperationsCount(false).should.equal(0);
|
||||
Script().getSignatureOperationsCount(true).should.equal(0);
|
||||
});
|
||||
it('should handle multi-sig multisig scripts from string', function() {
|
||||
var s1 = 'OP_1 01 FF OP_2 OP_CHECKMULTISIG';
|
||||
Script(s1).getSignatureOperationsCount(true).should.equal(2);
|
||||
s1 += ' OP_IF OP_CHECKSIG OP_ENDIF';
|
||||
Script(s1).getSignatureOperationsCount(true).should.equal(3);
|
||||
Script(s1).getSignatureOperationsCount(false).should.equal(21);
|
||||
});
|
||||
it('should handle multi-sig-out scripts from utility function', function() {
|
||||
var sortKeys = pubKeyHexes.slice(0, 3).map(PublicKey);
|
||||
var s2 = Script.buildMultisigOut(sortKeys, 1);
|
||||
Script(s2).getSignatureOperationsCount(true).should.equal(3);
|
||||
Script(s2).getSignatureOperationsCount(false).should.equal(20);
|
||||
});
|
||||
it('should handle P2SH-multisig-in scripts from utility', function() {
|
||||
// create a well-formed signature, does not need to match pubkeys
|
||||
var signature = bitcore.crypto.Signature.fromString('30060201FF0201FF');
|
||||
var signatures = [ signature.toBuffer() ];
|
||||
var p2sh = Script.buildP2SHMultisigIn(pubKeyHexes, 1, signatures, {});
|
||||
p2sh.getSignatureOperationsCount(true).should.equal(0);
|
||||
p2sh.getSignatureOperationsCount(false).should.equal(0);
|
||||
});
|
||||
it('should default the one and only argument to true', function() {
|
||||
var s1 = 'OP_1 01 FF OP_2 OP_CHECKMULTISIG';
|
||||
var trueCount = Script(s1).getSignatureOperationsCount(true);
|
||||
var falseCount = Script(s1).getSignatureOperationsCount(false);
|
||||
var defaultCount = Script(s1).getSignatureOperationsCount();
|
||||
trueCount.should.not.equal(falseCount);
|
||||
trueCount.should.equal(defaultCount);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue