Commit graph

3449 commits

Author SHA1 Message Date
Conner Fromknecht
a4720f30e5
txscript: Optimize removeOpcodeRaw 2021-11-16 18:48:47 -08:00
Dave Collins
30874ff76b
txscript: Implement efficient opcode data removal.
This introduces a new function named removeOpcodeByDataRaw which accepts
the raw scripts and data to remove versus requiring the parsed opcodes
to both significantly optimize it as well as make it more flexible for
working with raw scripts.

There are several places in the rest of the code that currently only
have access to the parsed opcodes, so this only introduces the function
for use in the future and deprecates the existing one.

Note that, in practice, the script will never actually contain the data
that is intended to be removed since the function is only used during
signature verification to remove the signature itself which would
require some incredibly non-standard code to create.

Thus, as an optimization, it avoids allocating a new script unless there
is actually a match that needs to be removed.

Finally, it updates the tests to use the new function.
2021-11-16 18:48:45 -08:00
Dave Collins
f3354beb12
txscript: Use raw scripts in SignTxOutput.
This converts SignTxOutput and supporting funcs, namely sign,
mergeScripts and mergeMultiSig, to make use of the new tokenizer as well
as some recently added funcs that deal with raw scripts in order to
remove the reliance on parsed opcodes as a step towards utlimately
removing them altogether and updates the comments to explicitly call out
the script version semantics.

It is worth noting that this has the side effect of optimizing the
function as well, however, since this change is not focused on the
optimization aspects, no benchmarks are provided.
2021-11-16 18:48:42 -08:00
Conner Fromknecht
e00fec1557
txscript: Use optimized calcWitnessSignatureHashRaw w/o parsing 2021-11-16 18:48:40 -08:00
Conner Fromknecht
ed9e17a043
txscript: Remove unused isWitnessPubKeyHash 2021-11-16 18:48:37 -08:00
Conner Fromknecht
dd609d6e36
txscript: Introduce calcWitnessSignatureHashRaw 2021-11-16 18:48:35 -08:00
Dave Collins
7ad3a10442
txscript: mergeMultiSig function def order cleanup.
This moves the function definition for mergeMultiSig so it is more
consistent with the preferred order used through the codebase.  In
particular, the functions are defined before they're first used and
generally as close as possible to the first use when they're defined in
the same file.
2021-11-16 18:48:32 -08:00
Conner Fromknecht
d410d7d7d4
txscript: Optimize ExtractWitnessProgramInfo 2021-11-16 18:48:30 -08:00
Conner Fromknecht
4b03b59391
txscript: Use internal analysis methods for GetWitnessSigOpCount 2021-11-16 18:48:27 -08:00
Conner Fromknecht
8b706344a1
txscript: Return witness version and program in one pass 2021-11-16 18:48:25 -08:00
Conner Fromknecht
6fb1c82fe5
txscript: Optimize IsWitnessProgram 2021-11-16 18:48:22 -08:00
Conner Fromknecht
1034a66b35
txscript: Optimize ExtractPkScriptAddr assume non-standard if no success
This completes the process of converting the ExtractPkScriptAddr
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this cleans up the final remaining case for non-standard
transactions. The method now returns NonStandardTy direclty if no other
branch was taken.

The following is a before and after comparison of attempting to extract
pkscript addrs from a very large, non-standard script.

benchmark                                old ns/op     new ns/op     delta
BenchmarkExtractPkScriptAddrsLarge-8     60713         17.0          -99.97%
BenchmarkExtractPkScriptAddrs-8          289           17.0          -94.12%

benchmark                                old allocs     new allocs     delta
BenchmarkExtractPkScriptAddrsLarge-8     1              0              -100.00%
BenchmarkExtractPkScriptAddrs-8          1              0              -100.00%

benchmark                                old bytes     new bytes     delta
BenchmarkExtractPkScriptAddrsLarge-8     311299        0             -100.00%
BenchmarkExtractPkScriptAddrs-8          768           0             -100.00%
2021-11-16 18:48:20 -08:00
Conner Fromknecht
a83152214c
txscript: Optimize ExtractPkScriptAddrs witness script hash
This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the extract of witness-pay-to-script-hash
scripts.
2021-11-16 18:48:17 -08:00
Conner Fromknecht
ae7fffbe52
txscript: Optimize ExtractPkScriptAddrs witness pubkey hash
This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the extraction for witness-pubkey-hash
scripts.
2021-11-16 18:48:15 -08:00
Dave Collins
507a4dcc00
txscript: Optimize ExtractPkScriptAddrs nulldata.
This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the detection for nulldata scripts, removes
the slow path fallback code since it is the final case, and modifies the
comment to call out the script version semantics.

The following is a before and after comparison of analyzing both a
typical standard script and a very large non-standard script:

benchmark                            old ns/op    new ns/op    delta
-----------------------------------------------------------------------
BenchmarkExtractPkScriptAddrsLarge   132400       44.4         -99.97%
BenchmarkExtractPkScriptAddrs        1265         231          -81.74%

benchmark                            old allocs   new allocs   delta
-----------------------------------------------------------------------
BenchmarkExtractPkScriptAddrsLarge   1            0            -100.00%
BenchmarkExtractPkScriptAddrs        5            2            -60.00%

benchmark                            old bytes    new bytes    delta
-----------------------------------------------------------------------
BenchmarkExtractPkScriptAddrsLarge   466944       0            -100.00%
BenchmarkExtractPkScriptAddrs        1600         48           -97.00%
2021-11-16 18:48:12 -08:00
Dave Collins
0bc18254d4
txscript: Optimize ExtractPkScriptAddrs multisig.
This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the detection for multisig scripts.

Also, since the remaining slow path cases are all recursive calls,
the parsed opcodes are no longer used, so parsing is removed.
2021-11-16 18:48:10 -08:00
Dave Collins
0e810b4ef4
txscript: Optimize ExtractPkScriptAddrs pubkey.
This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the detection for pay-to-pubkey scripts.
2021-11-16 18:48:07 -08:00
Dave Collins
16bd6633b6
txscript: Optimize ExtractPkScriptAddrs pubkeyhash.
This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the detection for pay-to-pubkey-hash
scripts.
2021-11-16 18:48:05 -08:00
Dave Collins
055be988c0
txscript: Optimize ExtractPkScriptAddrs scripthash.
This begins the process of converting the ExtractPkScriptAddrs function
to use the optimized extraction functions recently introduced as part of
the typeOfScript conversion.

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

In particular, this converts the detection for pay-to-script-hash
scripts.
2021-11-16 18:48:02 -08:00
Dave Collins
33ee3e2f53
txscript: Add ExtractPkScriptAddrs benchmarks. 2021-11-16 18:47:59 -08:00
Dave Collins
367a75a3f4
txscript: Optimize ExtractAtomicSwapDataPushes.
This converts the ExtractAtomicSwapDataPushes function to make use of
the new tokenizer instead of the far less efficient parseScript thereby
significantly optimizing the function.

The new implementation is designed such that it should be fairly easy to
move the function into the atomic swap tools where it more naturally
belongs now that the tokenizer makes it possible to analyze scripts
outside of the txscript module.  Consequently, this also deprecates the
function.

The following is a before and after comparison of attempting to extract
from both a typical atomic swap script and a very large non-atomic swap
script:

benchmark                                       old ns/op     new ns/op     delta
BenchmarkExtractAtomicSwapDataPushesLarge-8     61332         44.4          -99.93%
BenchmarkExtractAtomicSwapDataPushes-8          990           260           -73.74%

benchmark                                       old allocs     new allocs     delta
BenchmarkExtractAtomicSwapDataPushesLarge-8     1              0              -100.00%
BenchmarkExtractAtomicSwapDataPushes-8          2              1              -50.00%

benchmark                                       old bytes     new bytes     delta
BenchmarkExtractAtomicSwapDataPushesLarge-8     311299        0             -100.00%
BenchmarkExtractAtomicSwapDataPushes-8          3168          96            -96.97%
2021-11-16 18:47:57 -08:00
Conner Fromknecht
6ec9b73a53
txscript/scriptnum: add maxscriptnum and maxcltvlength 2021-11-16 18:47:54 -08:00
Dave Collins
81b80328bd
txscript: Add ExtractAtomicSwapDataPushes benches. 2021-11-16 18:47:52 -08:00
Dave Collins
da9fdabbd5
txscript: Make canonicalPush accept raw opcode.
This renames the canonicalPush function to isCanonicalPush and converts
it to accept an opcode as a byte and the associate data as a byte slice
instead of the internal parse opcode data struct in order to make it
more flexible for raw script analysis.

It also updates all callers and tests accordingly.
2021-11-16 18:47:49 -08:00
Dave Collins
0a4f228dd1
txscript: Optimize PushedData.
This converts the PushedData function to make use of the new tokenizer
instead of the far less efficient parseScript thereby significantly
optimizing the function.

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

The following is a before and after comparison of extracting the data
from a very large script:

benchmark                 old ns/op     new ns/op     delta
BenchmarkPushedData-8     64837         1790          -97.24%

benchmark                 old allocs     new allocs     delta
BenchmarkPushedData-8     7              6              -14.29%

benchmark                 old bytes     new bytes     delta
BenchmarkPushedData-8     312816        1520          -99.51%
2021-11-16 18:47:47 -08:00
Dave Collins
c4f6302189
txscript: Add benchmark for PushedData. 2021-11-16 18:47:44 -08:00
Dave Collins
7791f92f6f
txscript: Optimize CalcMultiSigStats.
This converts the CalcMultiSigStats function to make use of the new
extractMultisigScriptDetails function instead of the far less efficient
parseScript thereby significantly optimizing the function.

The tests are also updated accordingly.

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

benchmark                    old ns/op    new ns/op    delta
---------------------------------------------------------------
BenchmarkCalcMultiSigStats   972          79.5         -91.82%

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

benchmark                    old bytes    new bytes    delta
---------------------------------------------------------------
BenchmarkCalcMultiSigStats   2304         0            -100.00%
2021-11-16 18:47:42 -08:00
Dave Collins
8c54905959
txscript: Remove unused getSigOpCount function. 2021-11-16 18:47:39 -08:00
Dave Collins
6c212fd7ee
txscript: Remove unused isPushOnly function. 2021-11-16 18:47:37 -08:00
Dave Collins
705d24cab4
txscript: Convert CalcScriptInfo.
This converts CalcScriptInfo and dependent expectedInputs to make use of
the new script tokenizer as well as several of the other recently added
raw script analysis functions in order to remove the reliance on parsed
opcodes as a step towards utlimately removing them altogether.

It is worth noting that this has the side effect of significantly
optimizing the function as well, however, since it is deprecated, no
benchmarks are provided.
2021-11-16 18:47:34 -08:00
Conner Fromknecht
43846b1edf
txscript: Remove unused isWitnessScriptHash 2021-11-16 18:47:31 -08:00
Conner Fromknecht
1a60e11da7
txscript: Optimize typeOfScript for witness-script-hash
This concludes 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 witness script hash scripts
to use raw script analysis and the new tokenizer.

With all of the limbs now useing optimized variants, the following is a
before an after comparison of calling GetScriptClass on a large script:

benchmark                     old ns/op     new ns/op     delta
BenchmarkGetScriptClass-8     61515         15.3          -99.98%

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

benchmark                     old bytes     new bytes     delta
BenchmarkGetScriptClass-8     311299        0             -100.00%
2021-11-16 18:47:29 -08:00
Conner Fromknecht
847a262d78
txscript: Optimize typeOfScript witness-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 witness pubkey hash scripts
to use 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
BenchmarkIsWitnessPubKeyHash-8     61688         62839         +1.87%

benchmark                          old allocs     new allocs     delta
BenchmarkIsWitnessPubKeyHash-8     1              1              +0.00%

benchmark                          old bytes     new bytes     delta
BenchmarkIsWitnessPubKeyHash-8     311299        311299        +0.00%
2021-11-16 18:47:27 -08:00
Dave Collins
78046b3815
txscript: Remove unused isNullData function. 2021-11-16 18:47:24 -08:00
Conner Fromknecht
d80863da92
txscript: Optimize typeOfScript for null data scripts
This continues the process of converting the typeOfScript function to
use a combination of raw script analysize and the tokenizer instead of
parsed opcode, with the intent of significanty optimizing the function.

In particular, it converts the detection of null data scripts to use raw
script analysis.
2021-11-16 18:47:21 -08:00
Dave Collins
d02f97e04a
txscript: Remove unused isPubkeyHash function. 2021-11-16 18:47:19 -08:00
Dave Collins
13f646243d
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.
2021-11-16 18:47:16 -08:00
Dave Collins
1133ea0bb3
txscript: Remove unused isPubkey function. 2021-11-16 18:47:14 -08:00
Conner Fromknecht
8b64adc234
txscript: Optimze 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 face less efficient parsed opcodes, with the intent of
significantly optimizing the function.

In particular, it converts the detection of pay-to-pubkey scripts to use
raw script analysis.
2021-11-16 18:47:11 -08:00
Dave Collins
6e86f0d09b
txscript: Remove unused isMultiSig function. 2021-11-16 18:47:09 -08:00
Dave Collins
71bf51e82c
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.
2021-11-16 18:47:06 -08:00
Dave Collins
671b5fefef
txscript: Remove unused isScriptHash function. 2021-11-16 18:47:04 -08:00
Dave Collins
3b86e0a0e2
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.
2021-11-16 18:47:01 -08:00
Dave Collins
9b06388f76
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.
2021-11-16 18:46:58 -08:00
Dave Collins
69d560c03c
txscript: Add benchmark for GetScriptClass. 2021-11-16 18:46:56 -08:00
Conner Fromknecht
77863f5649
txscript: Optimize GetWitnessSigOpCount
This converts the GetWitnessSigOpCount function to use a combination of
raw script analysis and the new tokenizer instead of the far less
efficeint parseScript, thereby significantly optimizing the funciton.

In particular, it use the recently added countSigOpsv0 in precise mode
to avoid calling paseScript.
2021-11-16 18:46:53 -08:00
Conner Fromknecht
26d63c61dc
txscript: add GetWitnessSigOpCountBenchmarks 2021-11-16 18:46:51 -08:00
Dave Collins
a4a21c0b08
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-8     130223        742           -99.43%

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

benchmark                           old bytes     new bytes     delta
BenchmarkGetPreciseSigOpCount-8     623367        0             -100.00%
2021-11-16 18:46:48 -08:00
Dave Collins
cc802d1407
txscript: Add benchmark for GetPreciseSigOpCount. 2021-11-16 18:46:46 -08:00
Dave Collins
4d5c0b2529
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-8     61051         677           -98.89%

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

benchmark                    old bytes     new bytes     delta
BenchmarkGetSigOpCount-8     311299        0             -100.00%
2021-11-16 18:46:43 -08:00