Commit Graph

111 Commits

Author SHA1 Message Date
Dave Collins
2e2b9a34aa
txscript: Remove unused isStakeOutput function. 2020-02-13 15:45:09 -06:00
Dave Collins
5f8046a97c
txscript: Don't use GetScriptClass in consensus.
This modifies the consensus critical function which determines if the
redeem script of a p2sh or stake-tagged p2sh contains stake opcodes to
avoid calling the GetScriptClass function which is only intended
explicitly for working with standard script forms which only apply in
the context of the more restrictive standardness policy rules.

It also renames the function to better indicate its purpose is to
specifically check the redeem script as opposed to the entire signature
script.
2020-02-13 15:45:07 -06:00
Donald Adu-Poku
e19d435157 multi: replace GetScriptClass consensus calls.
This removes GetScriptClass consensus
calls in favour of using script type identification
functions defined in the blockchain package.
2020-02-07 20:35:39 -06:00
Dave Collins
85f0c09df2
secp256k1: Start v3 module dev cycle.
Upcoming changes constitute breaking public API changes to the secp256k1
module, therefore, this follows the process for introducing major API
breaks which consists of:

- Bump the major version in the go.mod of the affected module if not
  already done since the last release tag
- Add a replacement to the go.mod in the main module if not already done
  since the last release tag
- Update all imports in the repo to use the new major version as
  necessary
- Make necessary modifications to allow all other modules to use the new
  version in the same commit
- Repeat the process for any other modules the require a new major as a
  result of consuming the new major(s)
2020-01-29 13:45:59 -06:00
David Hill
6d39abb6be dcrutil: Start v3 module dev cycle.
Several upcoming changes constitute breaking the public API.
2019-10-24 19:13:26 -05:00
Dave Collins
cebab1ef64
multi: Use secp256k1/v2 module.
This updates the following modules to use the secp256k1/v2 module:

- blockchain
- chaincfg/v2
- dcrutil/v2
- hdkeychain/v2
- mempool/v3
- txscript/v2
- main

The hdkeychain/v3 and txscript/v2 modules both use types from secp256k1
in their public API.

Consequently, in order avoid forcing them to bump their major versions,
secp256k1/v1.0.3 was released with the types redefined in terms of the
secp256k1/v2 module so callers still using v1 of the module that are not
ready to upgrade to the v2 module yet can interoperate by updating to
the latest patch version.
2019-10-08 10:14:13 -05:00
David Hill
0781162661 multi: remove unused funcs and vars 2019-09-05 10:13:18 -05:00
Aaron Campbell
8be96a8729 multi: Correct typos.
Correct typos found by reading code, ispell, and creative grepping.
2019-08-22 10:20:03 -05:00
Aaron Campbell
03678bb754 multi: Correct typos.
Correct typos found by reading code and creative grepping.
2019-08-16 17:37:58 -05:00
Dave Collins
1e34495ddb
txscript: Use dcrutil/v2.
This udpates the txscript module to use v2 of the dcrutil module and v2
of the chaincfg module since dcrutil/v2 requires it.

While here, it also modifies the signing functions to accept the new
dcrutil.AddressParams instead of a pointer to a chaincfg.Params struct
in order to remove the tight coupling between txscript and chaincfg at
the API boundary.

It also updates the tests accordingly.
2019-06-24 15:13:39 -05:00
Dave Collins
d74040a8bf
txscript: Remove DefaultScriptVersion.
This removes the DefaultScriptVersion constant and updates the code and
tests to use version 0 scripts accordingly.

It is being removed because it is highly error prone since anything that
is working with scripts needs to understand the version it is dealing
with and having a constant that could change that version out from under
them could easily lead to buggy behavior.
2019-06-24 15:13:37 -05:00
Dave Collins
e5acc5651b
txscript: Remove GetMultisigMandN.
This removes the deprecated GetMultisigMandN function.
2019-06-24 15:13:35 -05:00
Dave Collins
641be388b4
txscript: Remove GetScriptHashFromP2SHScript.
This removes the deprecated GetScriptHashFromP2SHScript function.
2019-06-24 15:13:34 -05:00
Dave Collins
4f7a4c9858
txscript: Remove multisig redeem script err return.
This removes the previously deprecated error return from the
MultisigRedeemScriptFromScriptSig function and updates the tests
accordingly.
2019-06-24 15:13:34 -05:00
Dave Collins
b92aa63e07
txscript: Remove CalcScriptInfo.
This removes the deprecated CalcScriptInfo function along with the
associated ScriptInfo struct and tests.
2019-06-24 15:13:33 -05:00
Dave Collins
41f1139e3b
txscript: Unexport IsStakeOutput
This unexports the previously deprecated isStakeOutput function and
updates all callers in the module accordingly.
2019-06-24 15:13:32 -05:00
Dave Collins
68e1655bba
txscript: Remove IsMultisigScript err return.
This removes the previously deprecated error return from the
IsMultisigScript function and updates all callers in the module and
tests accordingly.
2019-06-24 15:13:32 -05:00
Dave Collins
a247a5207c
txscript: Remove unused isOneByteMaxDataPush func. 2019-03-26 14:51:59 -05:00
Dave Collins
9e3269ec2d
txscript: Remove unused isPubkeyHashAlt function. 2019-03-26 14:51:58 -05:00
Dave Collins
b19b6cbb40
txscript: Remove unused isPubkeyAlt function. 2019-03-26 14:51:57 -05:00
Dave Collins
007264ec21
txscript: Remove unused extractOneBytePush func. 2019-03-26 14:51:57 -05:00
Dave Collins
2fd1dcbbff
txscript: Optimize ExtractPkScriptAltSigType.
This converts the ExtractPkScriptAltSigType function to use the
optimized extraction functions recently introduced as part of the
typeOfScript conversion.

It is important to note that this new implementation intentionally has
the same semantic differences from the existing implementation as
discussed in the relevant commits that introduced the extraction
functions.

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

benchmark                    old ns/op    new ns/op    delta
---------------------------------------------------------------
BenchmarkExtractAltSigType   497          12.8         -97.42%

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

benchmark                    old bytes    new bytes    delta
---------------------------------------------------------------
BenchmarkExtractAltSigType   896          0            -100.00%
2019-03-26 14:51:56 -05:00
Dave Collins
d1944a942e
txscript: Optimize ExtractPkScriptAddrs nulldata.
This completes 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%
2019-03-26 14:51:55 -05:00
Dave Collins
c9bce2a0fb
txscript: Optimize ExtractPkScriptAddrs stakechange.
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 stake-change-tagged
pay-to-pubkey-hash and pay-to-script-hash scripts.
2019-03-26 14:51:54 -05:00
Dave Collins
a754af9145
txscript: Optimize ExtractPkScriptAddrs stakerev.
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 stake-revocation-tagged
pay-to-pubkey-hash and pay-to-script-hash scripts.
2019-03-26 14:51:53 -05:00
Dave Collins
8c763f198a
txscript: Optimize ExtractPkScriptAddrs stakegen.
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 stake-generation-tagged
pay-to-pubkey-hash and pay-to-script-hash scripts.
2019-03-26 14:51:53 -05:00
Dave Collins
57219fc17e
txscript: Optimize ExtractPkScriptAddrs stakesub.
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 stake-submission-tagged
pay-to-pubkey-hash and pay-to-script-hash scripts.
2019-03-26 14:51:52 -05:00
Dave Collins
6c1c2d1075
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.
2019-03-26 14:51:51 -05:00
Dave Collins
8280e50849
txscript: Optimize ExtractPkScriptAddrs altpubkey.
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-alt-pubkey
scripts.
2019-03-26 14:51:51 -05:00
Dave Collins
9e5744d4ed
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.
2019-03-26 14:51:50 -05:00
Dave Collins
bbca815a71
txscript: Optimize ExtractPkScriptAddrs altpubkeyhash.
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-alt-pubkey-hash
scripts.
2019-03-26 14:51:49 -05:00
Dave Collins
bdd98b15ba
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.
2019-03-26 14:51:49 -05:00
Dave Collins
49b3f9f61a
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.
2019-03-26 14:51:48 -05:00
Dave Collins
4774fda89a
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
------------------------------------------------------------------------------
BenchmarkExtractAtomicSwapDataPushes        1330         410          -69.17%
BenchmarkExtractAtomicSwapDataPushesLarge   136819       69.3         -99.95%

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

benchmark                                   old bytes    new bytes    delta
------------------------------------------------------------------------------
BenchmarkExtractAtomicSwapDataPushes        3168         96           -96.97%
BenchmarkExtractAtomicSwapDataPushesLarge   466944       0            -100.00%
2019-03-26 14:51:47 -05:00
Dave Collins
67d73853b2
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.
2019-03-26 14:51:45 -05:00
Dave Collins
9e7de33d6b
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   132400       1619         -98.78%

benchmark             old allocs   new allocs   delta
-------------------------------------------------------
BenchmarkPushedData   5            4            -20.00%

benchmark             old bytes    new bytes    delta
-------------------------------------------------------
BenchmarkPushedData   467320       368          -99.92%
2019-03-26 14:51:42 -05:00
Dave Collins
d82dfb76dd
txscript: Convert GetScriptHashFromP2SHScript.
This converts GetScriptHashFromP2SHScript to make use of the new script
tokenizer in order to remove the reliance on parsed opcodes as a step
towards utlimately removing them altogether.

It also deprecates the function since the current semantics are not
really ideal in that they simply return the data push just after the
first HASH160 opcode which is only valid in the case the script is
already known to be of the correct form and the task can be done more
efficiently via raw script analysis such as how it is done in the
recently added extractScriptHash function.

Finally, it modifies the comment to explicitly call out the script
version semantics as well as the aforemention precondition.

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.
2019-03-26 14:51:41 -05:00
Dave Collins
1466a2a72d
txscript: Optimize multi sig redeem script func.
This converts the MultisigRedeemScriptFromScriptSig function to make use
of the new finalOpcodeData function instead of the far less efficient
parseScript thereby significantly optimizing the function.

It also deprecates the error return since it really does not make sense
given the preconditions of the function.

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

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

benchmark                       old ns/op    new ns/op    delta
------------------------------------------------------------------
BenchmarkMultisigRedeemScript   153623       1830         -98.81%

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

benchmark                       old bytes    new bytes    delta
------------------------------------------------------------------
BenchmarkMultisigRedeemScript   466944       0            -100.00%
2019-03-26 14:51:40 -05:00
Dave Collins
c596826688
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%
2019-03-26 14:51:39 -05:00
Dave Collins
8f442764f4
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.
2019-03-26 14:51:36 -05:00
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
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
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
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
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
63e74185cf
txscript: Remove unused isNullData function. 2019-03-26 14:51:24 -05:00