Commit Graph

307 Commits

Author SHA1 Message Date
Dave Collins
0e021a9564
txscript: Optimize ContainsStakeOpCodes.
This converts the ContainsStakeOpCodes function to make use of the new
tokenizer instead of the far less efficient parseScript thereby
significantly optimizing the function.

The following is a before and after comparison of analyzing a large
script:

benchmark                       old ns/op    new ns/op    delta
------------------------------------------------------------------
BenchmarkContainsStakeOpCodes   134599       968          -99.28%

benchmark                       old allocs   new allocs   delta
------------------------------------------------------------------
BenchmarkContainsStakeOpCodes   1            0            -100.00%

benchmark                       old bytes    new bytes    delta
------------------------------------------------------------------
BenchmarkContainsStakeOpCodes   466944       0            -100.00%
2019-03-26 14:51:34 -05:00
Dave Collins
bfab5dbb93
txscript: Add benchmark for ContainsStakeOpCodes. 2019-03-26 14:51:33 -05:00
Dave Collins
28765fa2f1
txscript: Remove unused isSStxChange function. 2019-03-26 14:51:33 -05:00
Dave Collins
89d4941164
txscript: Optimize typeOfScript stakechange detect.
This completes the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of stake change scripts to use
raw script analysis by introducing a new function named
isStakeChangeScript which makes use of the recently added
extractStakePubKeyHash and extractStakeScriptHash functions and removes
the script parsing fallback from the typeOfScript function since this is
the final case.

The following is a before and after comparison of analyzing a large
script for both the stake change script change and the overall
GetScriptClass function which relies on the now fully converted
typeOfScript function:

benchmark                      old ns/op    new ns/op    delta
-----------------------------------------------------------------
BenchmarkIsStakeChangeScript   133810       4.39         -100.00%
BenchmarkGetScriptClass        145001       62.9         -99.96%

benchmark                      old allocs   new allocs   delta
-----------------------------------------------------------------
BenchmarkIsStakeChangeScript   1            0            -100.00%
BenchmarkGetScriptClass        1            0            -100.00%

benchmark                      old bytes    new bytes    delta
-----------------------------------------------------------------
BenchmarkIsStakeChangeScript   466944       0            -100.00%
BenchmarkGetScriptClass        466944       0            -100.00%
2019-03-26 14:51:32 -05:00
Dave Collins
123a733665
txscript: Add bench for stake change scripts. 2019-03-26 14:51:31 -05:00
Dave Collins
61872f9fb5
txscript: Remove unused isStakeRevocation function. 2019-03-26 14:51:31 -05:00
Dave Collins
8dfae89220
txscript: Optimize typeOfScript stakerev detection.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of stake revocation scripts to
use raw script analysis.

In order to accomplish this, it introduces a new function named
isStakeGenScript which makes of the recently added
extractStakePubKeyHash and extractStakeScriptHash functions.

The following is a before and after comparison of analyzing a large
script:

benchmark                          old ns/op    new ns/op    delta
---------------------------------------------------------------------
BenchmarkIsStakeRevocationScript   117699       4.58         -100.00%

benchmark                          old allocs   new allocs   delta
---------------------------------------------------------------------
BenchmarkIsStakeRevocationScript   1            0            -100.00%

benchmark                          old bytes    new bytes    delta
---------------------------------------------------------------------
BenchmarkIsStakeRevocationScript   466944       0            -100.00%
2019-03-26 14:51:30 -05:00
Dave Collins
9b3f4c924e
txscript: Add bench for stake revocation scripts. 2019-03-26 14:51:29 -05:00
Dave Collins
8468b0de2f
txscript: Remove unused isStakeGen function. 2019-03-26 14:51:29 -05:00
Dave Collins
5a24f508e3
txscript: Optimize typeOfScript stakegen detection.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of stake generation scripts to
use raw script analysis.

In order to accomplish this, it introduces a new function named
isStakeGenScript which makes of the recently added
extractStakePubKeyHash and extractStakeScriptHash functions.

The following is a before and after comparison of analyzing a large
script:

benchmark                          old ns/op    new ns/op    delta
---------------------------------------------------------------------
BenchmarkIsStakeGenerationScript   121043       4.26         -100.00%

benchmark                          old allocs   new allocs   delta
---------------------------------------------------------------------
BenchmarkIsStakeGenerationScript   1            0            -100.00%

benchmark                          old bytes    new bytes    delta
---------------------------------------------------------------------
BenchmarkIsStakeGenerationScript   466944       0            -100.00%
2019-03-26 14:51:28 -05:00
Dave Collins
c07f9cbc1b
txscript: Add bench for stake generation scripts. 2019-03-26 14:51:27 -05:00
Dave Collins
974ae66529
txscript: Remove unused isStakeSubmission function. 2019-03-26 14:51:26 -05:00
Dave Collins
5455dbce3c
txscript: Optimize typeOfScript stakesub detection.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of stake submission scripts to
use raw script analysis.

In order to accomplish this, it introduces three new functions.  The first
one is named extractStakePubKeyHash and works with the raw script bytes
to simultaneously determine if the script is a stake-tagged
pay-to-pubkey-hash script tagged with a specified stake opcode, and in
the case it is, extract and return the hash.  The second new function,
named extractStakeScriptHash, is similar except it detect a stake-tagged
pay-to-script-hash script tagged with a specified stake opcode.
Finally, the third function is named isStakeSubmissionScript and is
defined in terms of the former two functions.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

The following is a before and after comparison of analyzing a large
script:

benchmark                          old ns/op    new ns/op    delta
---------------------------------------------------------------------
BenchmarkIsStakeSubmissionScript   140308       4.20         -100.00%

benchmark                          old allocs   new allocs   delta
---------------------------------------------------------------------
BenchmarkIsStakeSubmissionScript   1            0            -100.00%

benchmark                          old bytes    new bytes    delta
---------------------------------------------------------------------
BenchmarkIsStakeSubmissionScript   466944       0            -100.00%
2019-03-26 14:51:25 -05:00
Dave Collins
6de8d6901d
txscript: Add bench for stake submission scripts. 2019-03-26 14:51:24 -05:00
Dave Collins
63e74185cf
txscript: Remove unused isNullData function. 2019-03-26 14:51:24 -05:00
Dave Collins
f589dd8fdf
txscript: Optimize typeOfScript nulldata detection.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of nulldata scripts to use both raw
script analysis and the new tokenizer.

The following is a before and after comparison of analyzing a large
script:

benchmark                   old ns/op    new ns/op    delta
--------------------------------------------------------------
BenchmarkIsNullDataScript   120800       3.81         -100.00%

benchmark                   old allocs   new allocs   delta
--------------------------------------------------------------
BenchmarkIsNullDataScript   1            0            -100.00%

benchmark                   old bytes    new bytes    delta
--------------------------------------------------------------
BenchmarkIsNullDataScript   466944       0            -100.00%
2019-03-26 14:51:23 -05:00
Dave Collins
15416b09dc
txscript: Add bench for null scripts. 2019-03-26 14:51:23 -05:00
Dave Collins
e7a172c441
txscript: Optimize typeOfScript pay-to-alt-pk-hash.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of pay-to-alt-pubkey-hash
scripts to use raw script analysis.

In order to accomplish this, it introduces two new functions.  The first
one is named extractPubKeyHashAltDetails and works with the raw script
bytes to simultaneously determine if the script is a
pay-to-alt-pubkey-hash script, and in the case it is, extract and return
the hash and signature type.  The second new function is named
isPubKeyHashAltScript and is defined in terms of the former.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

It is important to note that this new implementation intentionally has a
semantic difference from the existing implementation in that it will now
only pass when one of two signature types currently supported by
consensus are specified whereas previously it would allow any single
byte data push.

The following is a before and after comparison of analyzing a large
script:

benchmark                        old ns/op    new ns/op    delta
-------------------------------------------------------------------
BenchmarkIsAltPubKeyHashScript   107100       2.63         -100.00%

benchmark                        old allocs   new allocs   delta
-------------------------------------------------------------------
BenchmarkIsAltPubKeyHashScript   1            0            -100.00%

benchmark                        old bytes    new bytes    delta
-------------------------------------------------------------------
BenchmarkIsAltPubKeyHashScript   466944       0            -100.00%
2019-03-26 14:51:22 -05:00
Dave Collins
95175cb873
txscript: Add bench for pay-to-alt-pubkey-hash scripts. 2019-03-26 14:51:21 -05:00
Dave Collins
ffe80c736a
txscript: Remove unused isPubkeyHash function. 2019-03-26 14:51:21 -05:00
Dave Collins
406f851716
txscript: Optimize typeOfScript pay-to-pubkey-hash.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of pay-to-pubkey-hash scripts
to use raw script analysis.

In order to accomplish this, it introduces two new functions.  The first
one is named extractPubKeyHash and works with the raw script bytes
to simultaneously determine if the script is a pay-to-pubkey-hash script,
and in the case it is, extract and return the hash.  The second new
function is named isPubKeyHashScript and is defined in terms of the
former.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

The following is a before and after comparison of analyzing a large
script:

benchmark                     old ns/op    new ns/op    delta
----------------------------------------------------------------
BenchmarkIsPubKeyHashScript   165903       0.64         -100.00%

benchmark                     old allocs   new allocs   delta
----------------------------------------------------------------
BenchmarkIsPubKeyHashScript   1            0            -100.00%

benchmark                     old bytes    new bytes    delta
----------------------------------------------------------------
BenchmarkIsPubKeyHashScript   466945       0            -100.00%
2019-03-26 14:51:20 -05:00
Dave Collins
d07d626882
txscript: Add bench for pay-to-pubkey-hash scripts. 2019-03-26 14:51:19 -05:00
Dave Collins
26026475be
txscript: Optimize typeOfScript pay-to-alt-pubkey.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of pay-to-alt-pubkey scripts to
use raw script analysis.

In order to accomplish this, it introduces two new functions.  The first
one is named extractPubKeyAltDetails and works with the raw script bytes
to simultaneously determine if the script is a pay-to-alt-pubkey script,
and in the case it is, extract and return the relevant details.  The
second new function is named isPubKeyAltScript and is defined in terms
of the former.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

It is important to note that this new implementation intentionally
tightens the following semantics as compared to the existing
implementation:

- The signature type must now be one of the two supported types versus
  allowing any single byte data push
- The public key must now be of the correct length for the given
  signature type versus allowing any size up to 512 bytes
- The public key for schnorr secp256k1 pubkeys must now be a compressed
  public key and adhere to the strict encoding requirements for them

The following is a before and after comparison of analyzing a large
script:

benchmark                    old ns/op    new ns/op    delta
---------------------------------------------------------------
BenchmarkIsAltPubKeyScript   143449       2.99         -100.00%

benchmark                    old allocs   new allocs   delta
---------------------------------------------------------------
BenchmarkIsAltPubKeyScript   1            0            -100.00%

benchmark                    old bytes    new bytes    delta
---------------------------------------------------------------
BenchmarkIsAltPubKeyScript   466944       0            -100.00%
2019-03-26 14:51:19 -05:00
Dave Collins
e64e21c29f
txscript: Add bench for pay-to-alt-pubkey scripts. 2019-03-26 14:51:18 -05:00
Dave Collins
b273b8343c
txscript: Remove unused isPubkey function. 2019-03-26 14:51:17 -05:00
Dave Collins
1b067cb785
txscript: Optimize typeOfScript pay-to-pubkey.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, it converts the detection of pay-to-pubkey scripts to use
raw script analysis.

In order to accomplish this, it introduces four new functions:
extractCompressedPubKey, extractUncompressedPubKey, extractPubKey, and
isPubKeyScript.  The extractPubKey function makes use of
extractCompressedPubKey and extractUncompressedPubKey to combine their
functionality as a convenience and isPubKeyScript is defined in terms of
extractPubKey.

The extractCompressedPubKey works with the raw script bytes to
simultaneously determine if the script is a pay-to-compressed-pubkey
script, and in the case it is, extract and return the raw compressed
pubkey bytes.

Similarly, the extractUncompressedPubKey works in the same way except it
determines if the script is a pay-to-uncompressed-pubkey script and
returns the raw uncompressed pubkey bytes in the case it is.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

The following is a before and after comparison of analyzing a large
script:

benchmark                 old ns/op    new ns/op    delta
------------------------------------------------------------
BenchmarkIsPubKeyScript   124749       4.01         -100.00%

benchmark                 old allocs   new allocs   delta
------------------------------------------------------------
BenchmarkIsPubKeyScript   1            0            -100.00%

benchmark                 old bytes    new bytes    delta
------------------------------------------------------------
BenchmarkIsPubKeyScript   466944       0            -100.00%
2019-03-26 14:51:17 -05:00
Dave Collins
c4a15275ce
txscript: Add benchmark for pay-to-pubkey scripts. 2019-03-26 14:51:16 -05:00
Dave Collins
07168b8623
txscript: Remove unused isMultiSig function. 2019-03-26 14:51:15 -05:00
Dave Collins
900af9b029
txscript: Optimize typeOfScript multisig.
This continues the process of converting the typeOfScript function to
use a combination of raw script analysis and the new tokenizer instead
of the far less efficient parsed opcodes.

In particular, for this commit, since the ability to detect multisig
scripts via the new tokenizer is now available, the function is simply
updated to make use of it.
2019-03-26 14:51:15 -05:00
Dave Collins
efd7e2f572
txscript: Remove unused isScriptHash function. 2019-03-26 14:51:14 -05:00
Dave Collins
bdd51a0ddc
txscript: Optimize typeOfScript pay-to-script-hash.
This begins the process of converting the typeOfScript function to use a
combination of raw script analysis and the new tokenizer instead of the
far less efficient parsed opcodes with the intent of significantly
optimizing the function.

In order to ease the review process, each script type will be converted
in a separate commit and the typeOfScript function will be updated such
that the script is only parsed as a fallback for the cases that are not
already converted to more efficient raw script variants.

In particular, for this commit, since the ability to detect
pay-to-script-hash via raw script analysis is now available, the
function is simply updated to make use of it.
2019-03-26 14:51:14 -05:00
Dave Collins
4f27dfc507
txscript: Make typeOfScript accept raw script.
This converts the typeOfScript function to accept a script version and
raw script instead of an array of internal parsed opcodes in order to
make it more flexible for raw script analysis.

Also, this adds a comment to CalcScriptInfo to call out the specific
version semantics and deprecates the function since nothing currently
uses it, and the relevant information can now be obtained by callers
more directly through the use of the new script tokenizer.

All other callers are updated accordingly.
2019-03-26 14:51:13 -05:00
Dave Collins
4dc1ffbe6b
txscript: Add benchmark for GetScriptClass. 2019-03-26 14:51:12 -05:00
Dave Collins
a1b24adf27
txscript: Optimize GetPreciseSigOpCount.
This converts the GetPreciseSigOpCount function to use a combination of
raw script analysis and the new tokenizer instead of the far less
efficient parseScript thereby significantly optimizing the function.

In particular it uses the recently converted isScriptHashScript,
IsPushOnlyScript, and countSigOpsV0 functions along with the recently
added finalOpcodeData functions.

It also modifies the comment to explicitly call out the script version
semantics.

The following is a before and after comparison of analyzing a large
script:

benchmark                       old ns/op    new ns/op    delta
------------------------------------------------------------------
BenchmarkGetPreciseSigOpCount   287939       1077         -99.63%

benchmark                       old allocs   new allocs   delta
------------------------------------------------------------------
BenchmarkGetPreciseSigOpCount   3            0            -100.00%

benchmark                       old bytes    new bytes    delta
------------------------------------------------------------------
BenchmarkGetPreciseSigOpCount   934657       0            -100.00%
2019-03-26 14:51:12 -05:00
Dave Collins
75e71d849e
txscript: Add benchmark for GetPreciseSigOpCount. 2019-03-26 14:51:11 -05:00
Dave Collins
281f794408
txscript: Check p2sh push before parsing scripts.
This moves the check for non push-only pay-to-script-hash signature
scripts before the script parsing logic when creating a new engine
instance to avoid the extra overhead in the error case.
2019-03-26 14:51:10 -05:00
Dave Collins
af67951b9a
txscript: Optimize new engine push only script.
This modifies the check for whether or not a pay-to-script-hash
signature script is a push only script to make use of the new and more
efficient raw script function.

Also, since the script will have already been checked further above when
the ScriptVerifySigPushOnly flags is set, avoid checking it again in
that case.
2019-03-26 14:51:10 -05:00
Dave Collins
9e8bfd2493
txscript: Optimize IsPushOnlyScript.
This converts the IsPushOnlyScript function to make use of the new
tokenizer instead of the far less efficient parseScript thereby
significantly optimizing the function.

It also deprecates the isPushOnly function that requires opcodes in
favor of the new function and modifies the comment on IsPushOnlyScript
to explicitly call out the script version semantics.

The following is a before and after comparison of analyzing a large
script:

benchmark                    old ns/op    new ns/op    delta
---------------------------------------------------------------
BenchmarkIsPayToScriptHash   139961       0.66         -100.00%

benchmark                    old allocs   new allocs   delta
---------------------------------------------------------------
BenchmarkIsPayToScriptHash   1            0            -100.00%

benchmark                    old bytes    new bytes    delta
---------------------------------------------------------------
BenchmarkIsPayToScriptHash   466944       0            -100.00%
2019-03-26 14:51:09 -05:00
Dave Collins
93b039d5ac
txscript: Add benchmark for IsPushOnlyScript. 2019-03-26 14:51:08 -05:00
Dave Collins
a598838fb7
txscript: Optimize isAnyKindOfScriptHash.
This converts the isAnyKindOfScriptHash function to analyze the raw
script instead of requiring far less efficient parsed opcodes thereby
significantly optimizing the function.

Since the function relies on isStakeScriptHash to identify a stake
tagged pay-to-script-hash, and is the only consumer of it, this also
converts that function to analyze the raw script and renames it to
isStakeScriptHashScript for more consistent naming.

Finally, the tests are updated accordingly.

The following is a before and after comparison of analyzing a large
script:

benchmark                        old ns/op    new ns/op    delta
-------------------------------------------------------------------
BenchmarkIsAnyKindOfScriptHash   101249       3.83         -100.00%

benchmark                        old allocs   new allocs   delta
-------------------------------------------------------------------
BenchmarkIsAnyKindOfScriptHash   1            0            -100.00%

benchmark                        old bytes    new bytes    delta
-------------------------------------------------------------------
BenchmarkIsAnyKindOfScriptHash   466944       0            -100.00%
2019-03-26 14:51:08 -05:00
Dave Collins
bc56df1046
txscript: Add benchmark for isAnyKindOfScriptHash. 2019-03-26 14:51:07 -05:00
Dave Collins
51f76392b4
txscript: Add tests for stake-tagged script hash.
This adds tests to ensure the isAnyKindOfScriptHash function properly
identifies the four stake-tagged pay-to-script-hash possibilities in
addition to ensuring they are not misidentified as standard
pay-to-script-hash scripts.
2019-03-26 14:51:06 -05:00
Dave Collins
ffa6fb9e9d
txscript: Optimize GetSigOpCount.
This converts the GetSigOpCount function to make use of the new
tokenizer instead of the far less efficient parseScript thereby
significantly optimizing the function.

A new function named countSigOpsV0 which accepts the raw script is
introduced to perform the bulk of the work so it can be reused for
precise signature operation counting as well in a later commit.  It
retains the same semantics in terms of counting the number of signature
operations either up to the first parse error or the end of the script
in the case it parses successfully as required by consensus.

Finally, this also deprecates the getSigOpCount function that requires
opcodes in favor of the new function and modifies the comment on
GetSigOpCount to explicitly call out the script version semantics.

The following is a before and after comparison of analyzing a large
script:

benchmark                old ns/op    new ns/op    delta
-----------------------------------------------------------
BenchmarkGetSigOpCount   163896       1048         -99.36%

benchmark                old allocs   new allocs   delta
-----------------------------------------------------------
BenchmarkGetSigOpCount   1            0            -100.00%

benchmark                old bytes    new bytes    delta
-----------------------------------------------------------
BenchmarkGetSigOpCount   466945       0            -100.00%
2019-03-26 14:51:06 -05:00
Dave Collins
2d70450b7b
txscript: Add benchmark for GetSigOpCount. 2019-03-26 14:51:05 -05:00
Dave Collins
462eea3b82
txscript: Optimize IsMultisigSigScript.
This converts the IsMultisigSigScript function to analyze the raw script
and make use of the new tokenizer instead of the far less efficient
parseScript thereby significantly optimizing the function.

In order to accomplish this, it first rejects scripts that can't
possibly fit the bill due to the final byte of what would be the redeem
script not being the appropriate opcode or the overall script not having
enough bytes.  Then, it uses a new function that is introduced named
finalOpcodeData that uses the tokenizer to return any data associated
with the final opcode in the signature script (which will be nil for
non-push opcodes or if the script fails to parse) and analyzes it as if
it were a redeem script when it is non nil.

It is also worth noting that this new implementation intentionally has
the same semantic difference from the existing implementation as the
updated IsMultisigScript function in regards to allowing zero pubkeys
whereas previously it incorrectly required at least one pubkey.

Finally, the comment is modified to explicitly call out the script
version semantics.

The following is a before and after comparison of analyzing a large
script that is not a multisig script and both a 1-of-2 multisig public
key script (which should be false) and a signature script comprised of a
pay-to-script-hash 1-of-2 multisig redeem script (which should be true):

benchmark                           old ns/op    new ns/op     delta
-----------------------------------------------------------------------
BenchmarkIsMultisigSigScriptLarge   158149       4             -100.00%
BenchmarkIsMultisigSigScript        3445         202           -94.14%

benchmark                           old allocs   new allocs    delta
-----------------------------------------------------------------------
BenchmarkIsMultisigSigScriptLarge   9            0             -100.00%
BenchmarkIsMultisigSigScript        3            0             -100.00%

benchmark                           old bytes    new bytes     delta
-----------------------------------------------------------------------
BenchmarkIsMultisigSigScriptLarge   533189       0             -100.00%
BenchmarkIsMultisigSigScript        9472         0             -100.00%
2019-03-26 14:51:04 -05:00
Dave Collins
d7492c38ac
txscript: Add benchmarks for IsMutlsigSigScript. 2019-03-26 14:51:04 -05:00
Dave Collins
7b8259b4ed
txscript: Optimize IsMultisigScript.
This converts the IsMultisigScript function to make use of the new
tokenizer instead of the far less efficient parseScript thereby
significantly optimizing the function.

In order to accomplish this, it introduces two new functions.  The first
one is named extractMultisigScriptDetails and works with the raw script
bytes to simultaneously determine if the script is a multisignature
script, and in the case it is, extract and return the relevant details.
The second new function is named isMultisigScript and is defined in
terms of the former.

The extract function accepts the script version, raw script bytes, and a
flag to determine whether or not the public keys should also be
extracted.  The flag is provided because extracting pubkeys results in
an allocation that the caller might wish to avoid.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

It is important to note that this new implementation intentionally has a
semantic difference from the existing implementation in that it will now
correctly identify a multisig script with zero pubkeys whereas
previously it incorrectly required at least one pubkey.  This change is
acceptable because the function only deals with standardness rather than
consensus rules.

Finally, this also deprecates the isMultiSig function that requires
opcodes in favor of the new functions and deprecates the error return on
the export IsMultisigScript function since it really does not make sense
given the purpose of the function.

The following is a before and after comparison of analyzing both a large
script that is not a multisig script and a 1-of-2 multisig public key
script:

benchmark                        old ns/op    new ns/op    delta
-------------------------------------------------------------------
BenchmarkIsMultisigScriptLarge   121599       8.63         -99.99%
BenchmarkIsMultisigScript        797          72.8         -90.87%

benchmark                        old allocs   new allocs   delta
-------------------------------------------------------------------
BenchmarkIsMultisigScriptLarge   1            0            -100.00%
BenchmarkIsMultisigScript        1            0            -100.00%

benchmark                        old bytes    new bytes    delta
-------------------------------------------------------------------
BenchmarkIsMultisigScriptLarge   466944       0            -100.00%
BenchmarkIsMultisigScript        2304         0            -100.00%
2019-03-26 14:51:03 -05:00
Dave Collins
356492bc42
txscript: Add benchmarks for IsMutlsigScript. 2019-03-26 14:51:03 -05:00
Dave Collins
9f2f038842
txscript: Optimize IsPayToScriptHash.
This converts the IsPayToScriptHash function to analyze the raw script
instead of using the far less efficient parseScript thereby
significantly optimizing the function.

In order to accomplish this, it introduces two new functions.  The first
one is named extractScriptHash and works with the raw script bytes to
simultaneously determine if the script is a p2sh script, and in the case
it is, extract and return the hash.  The second new function is named
isScriptHashScript and is defined in terms of the former.

The extract function approach was chosen because it is common for
callers to want to only extract relevant details from a script if the
script is of the specific type.  Extracting those details requires
performing the exact same checks to ensure the script is of the correct
type, so it is more efficient to combine the two into one and define the
type determination in terms of the result so long as the extraction does
not require allocations.

Finally, this also deprecates the isScriptHash function that requires
opcodes in favor of the new functions and modifies the comment on
IsPayToScriptHash to explicitly call out the script version semantics.

The following is a before and after comparison of analyzing a large
script that is not a p2sh script:

benchmark                    old ns/op    new ns/op    delta
---------------------------------------------------------------
BenchmarkIsPayToScriptHash   139961       0.66         -100.00%

benchmark                    old allocs   new allocs   delta
---------------------------------------------------------------
BenchmarkIsPayToScriptHash   1            0            -100.00%

benchmark                    old bytes    new bytes    delta
---------------------------------------------------------------
BenchmarkIsPayToScriptHash   466944       0            -100.00%
2019-03-26 14:51:02 -05:00
Dave Collins
c705a0e31b
txscript: Add benchmark for IsPayToScriptHash. 2019-03-26 14:51:01 -05:00