Commit Graph

378 Commits

Author SHA1 Message Date
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
6dea351b42
txscript: Remove third GetPreciseSigOpCount param.
This removes the previously deprecated and unused third parameter of the
GetPreciseSigOpCount function.
2019-06-24 15:13:31 -05:00
Dave Collins
16b9f67d4f
txscript: Unexport HasP2SHScriptSigStakeOpCodes.
This unexports the previously deprecated HasP2SHScriptSigStakeOpCodes
function and updates all callers in the module accordingly.
2019-06-24 15:13:31 -05:00
Dave Collins
e0a318cd75
txscript: Remove DisableLog. 2019-06-24 15:13:30 -05:00
Dave Collins
ad0d98ce75
txscript; Use const for sighashall optimization.
This breaks the dependency on chaincfg.SigHashOptimization which is no
longer available in v2 of the chaincfg module.  The constant is set to
false to ensure the same semantics are kept and an additional comment
has been added regarding the status.
2019-06-24 15:13:30 -05:00
Dave Collins
b5dd4a030a
txscript: Prepare v1.1.0.
This updates the txscript dependencies and serves as a base for
txscript/v1.1.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/dcrec/secp256k1@v1.0.2
- github.com/decred/dcrd/dcrutil@v1.3.0
- golang.org/x/crypto@v0.0.0-20190611184440-5c40567a22f8

The full list of updated direct dependencies since the previous
txscripti/v1.0.2 release are as follows:

- github.com/decred/dcrd/chaincfg@v1.5.1
- github.com/decred/dcrd/dcrec@v1.0.0
- github.com/decred/dcrd/dcrec/edwards@v1.0.0
- github.com/decred/dcrd/dcrec/secp256k1@v1.0.2
- github.com/decred/dcrd/dcrutil@v1.3.0
- golang.org/x/crypto@v0.0.0-20190611184440-5c40567a22f8
2019-06-19 14:47:05 -05:00
Dave Collins
ce852eb852
blockchain: Do coinbase nulldata check locally.
This refactors the consensus code which extracts the null data from the
coinbase from txscript.ExtractCoinbaseNullData so that it is performed
directly in the validation code where it more properly belongs.

The only reason the extraction was previously done in txscript is
because it was not possible to parse scripts outside of it, but that is
no longer the case now that txscript offers an exported tokenizer for
that purpose.

The extraction code is ever so slightly more efficient now that it no
longer needs to be as generic since it now has direct knowledge of the
conditions that need to be handled.

Great care was taken to ensure the semantics are not changed while
refactoring the code and no additional tests are added in this commit
because all of the conditions and code paths are covered by the tests
recently added to the full block tests.

While here, also perform some related code cleanup in the function and
improve the error messages .

Since the txscript.ExtractCoinbaseNullData is no longer necessary, this
deprecates the function and releated error code and constant so they can
be removed in the next major version of txscript.

Finally, since this relies on the script tokenizer which is not yet in a
released version of the txscript module, bump the requirement to include
an as yet unreleased version of txscript to ensure the next time the
blockchain module is released, it will require a newer version of
txscript to be released first.
2019-06-19 13:26:33 -05:00
David Hill
893aa30dce multi: Use https links where available. 2019-06-18 14:20:06 -05:00
Dave Collins
c33be8c68c
multi: Update all modules for chaincfg v1.5.1.
This updates all of the modules that still rely on v1 of the chaincfg
module to use v1.5.1 which builds correctly with the major API bump
introduced in the edwards v1.0.0 module.

While here, it also updates all references to the v0 edwards and dcrec
modules to their tagged v1 counterparts, tidies all of the modules via
go mod tidy, and removes all unnecessary indirect entries from the mod
files to keep the modules using the versions the dependencies are tested
with.

The primary motivation for this change is that the chaincfg/v2 module
requires edwards/v1 which is not API compatible with edwards/v0.
Consequently, in order for each module to be incrementally updated to
use chaincfg/v2, all of its dependencies must also be able to build with
edwards/v1 which is the case as of chaincfg v1.5.1.
2019-06-17 15:54:31 -05:00
Dave Collins
e570fb8d75
txscript: Remove checks for impossible conditions.
This removes a couple of checks for impossible conditions found by the
staticcheck linter.  In the case of executeOpcode, bytes are always >= 0
and, similarly for SigCache.Add, uint are always >= 0.
2019-04-01 14:34:51 -05:00
Dave Collins
f71b0d246f
txscript: Cleanup opcode removal by data tests.
This converts the test definitions for TestRemoveOpcodeByData to make
use of mustParseShortForm which improves the readability and
construction of the tests as well as makes them more consistent with the
rest of the code base.

It also cleans up the recently added stake tx tests so that they don't
exceed col 80 and sets their names to indicate they are p2pkh and p2sh
forms.
2019-03-28 16:56:33 -05:00
Dave Collins
6adbaa62ab
txscript: Make op callbacks take opcode and data.
This converts the callback function defined on the internal opcode
struct to accept the opcode and data slice instead of a parsed opcode as
the final step towards removing the parsed opcode struct and associated
supporting code altogether.

It also updates all of the callbacks and tests accordingly and finally
removes the now unused parsedOpcode struct.
2019-03-26 14:55:46 -05:00
Dave Collins
5059be93f0
txscript: Make executeOpcode take opcode and data.
This converts the executeOpcode function defined on the engine to accept
an opcode and data slice instead of a parsed opcode as a step towards
removing the parsed opcode struct and associated supporting code altogether.

It also updates all callers accordingly.
2019-03-26 14:55:45 -05:00
Dave Collins
fcb1f3a7a1
txscript: Remove unused parseScriptTemplate func.
Also remove tests associated with the func accordingly.
2019-03-26 14:55:45 -05:00
Dave Collins
ceb58f4767
txscript: Remove unused parsedOpcode.bytes func. 2019-03-26 14:55:44 -05:00
Dave Collins
861032aa1e
txscript: Remove unused unparseScript func.
Also remove tests associated with unparsing opcodes accordingly.
2019-03-26 14:55:43 -05:00
Dave Collins
cef80862d6
txscript: Remove unused parseScript func. 2019-03-26 14:55:43 -05:00
Dave Collins
d72f6d3003
txscript: Rename calcSignatureHashRaw func.
This renames the calcSignatureHashRaw to calcSignatureHash now that the
old version has been removed.
2019-03-26 14:55:42 -05:00
Dave Collins
2e082a8004
txscript: Remove unused calcSignatureHash func. 2019-03-26 14:55:41 -05:00
Dave Collins
c598f59151
txscript: Rename removeOpcodeByDataRaw func.
This renames the removeOpcodeByDataRaw to removeOpcodeByData now that
the old version has been removed.
2019-03-26 14:55:41 -05:00
Dave Collins
bd040aea02
txscript: Remove unused removeOpcodeByData func. 2019-03-26 14:55:40 -05:00
Dave Collins
75c48ea8c7
txscript: Refactor engine to use raw scripts.
This refactors the script engine to store and step through raw scripts
by making using of the new zero-allocation script tokenizer as opposed
to the less efficient method of storing and stepping through parsed
opcodes.  It also improves several aspects while refactoring such as
optimizing the disassembly trace, showing all scripts in the trace in
the case of execution failure, and providing additional comments
describing the purpose of each field in the engine.

It should be noted that this is a step towards removing the parsed
opcode struct and associated supporting code altogether, however, in
order to ease the review process, this retains the struct and all
function signatures for opcode execution which make use of an individual
parsed opcode.  Those will be updated in future commits.

The following is an overview of the changes:

- Modify internal engine scripts slice to use raw scripts instead of
  parsed opcodes
- Introduce a tokenizer to the engine to track the current script
- Remove no longer needed script offset parameter from the engine since
  that is tracked by the tokenizer
- Add an opcode index counter for disassembly purposes to the engine
- Update check for valid program counter to only consider the script
  index
  - Update tests for bad program counter accordingly
- Rework the NewEngine function
  - Store the raw scripts
  - Setup the initial tokenizer
  - Explicitly check against version 0 instead of DefaultScriptVersion
    which would break consensus if changed
  - Check the scripts parse according to version 0 semantics to retain
    current consensus rules
  - Improve comments throughout
- Rework the Step function
  - Use the tokenizer and raw scripts
  - Create a parsed opcode on the fly for now to retain existing
    opcode execution function signatures
  - Improve comments throughout
- Update the Execute function
  - Explicitly check against version 0 instead of DefaultScriptVersion
    which would break consensus if changed
  - Improve the disassembly tracing in the case of error
- Update the CheckErrorCondition function
  - Modify clean stack error message to make sense in all cases
  - Improve the comments
- Update the DisasmPC and DisasmScript functions on the engine
  - Use the tokenizer
  - Optimize construction via the use of strings.Builder
- Modify the subScript function to return the raw script bytes since the
  parsed opcodes are no longer stored
- Update the various signature checking opcodes to use the raw opcode
  data removal and signature hash calculation functions since the
  subscript is now a raw script
  - opcodeCheckSig
  - opcodeCheckMultiSig
  - opcodeCheckSigAlt
2019-03-26 14:55:39 -05:00
Dave Collins
280c062930
txscript: Convert to use non-parsed opcode disasm.
This converts the engine's current program counter disasembly to make
use of the standalone disassembly function to remove the dependency on
the parsed opcode struct.

It also updates the tests accordingly.
2019-03-26 14:55:38 -05:00
Dave Collins
e915598b76
txscript: Make min push accept raw opcode and data.
This converts the checkMinimalDataPush function defined on a parsed
opcode to a standalone function which accepts an opcode and data slice
instead in order to make it more flexible for raw script analysis.

It also updates all callers accordingly.
2019-03-26 14:55:38 -05:00
Dave Collins
cfd3753756
txscript: Make isConditional accept raw opcode.
This converts the isConditional function defined on a parsed opcode to a
standalone function named isOpcodeConditional which accepts an opcode as
a byte instead in order to make it more flexible for raw script
analysis.

It also updates all callers accordingly.
2019-03-26 14:55:37 -05:00
Dave Collins
b62655222c
txscript: Make alwaysIllegal accept raw opcode.
This converts the alwaysIllegal function defined on a parsed opcode to a
standalone function named isOpcodeAlwaysIllegal which accepts an opcode
as a byte instead in order to make it more flexible for raw script
analysis.

It also updates all callers accordingly.
2019-03-26 14:55:36 -05:00
Dave Collins
d3518fc150
txscript: Make isDisabled accept raw opcode.
This converts the isDisabled function defined on a parsed opcode to a
standalone function which accepts an opcode as a byte instead in order
to make it more flexible for raw script analysis.

It also updates all callers accordingly.
2019-03-26 14:55:36 -05:00
Dave Collins
f93c1de13f
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.
2019-03-26 14:55:35 -05:00
Dave Collins
85c7a9ba8e
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.
2019-03-26 14:52:02 -05:00
Dave Collins
d8d561d4b6
txscript: Correct p2pkSignatureScriptAlt comment. 2019-03-26 14:52:01 -05:00
Dave Collins
706b3a1fcd
txscript: Use raw scripts in RawTxInSignatureAlt.
This converts RawTxInSignatureAlt to make use of the recently converted
CalcSignatureHash function that works with raw scripts in order to
remove the reliance on parsed opcodes as a step towards utlimately
removing them altogether and updates the comment 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.
2019-03-26 14:52:01 -05:00
Dave Collins
7cf42b0a70
txscript: Use raw scripts in RawTxInSignature.
This converts RawTxInSignature to make use of the recently converted
CalcSignatureHash function that works with raw scripts in order to
remove the reliance on parsed opcodes as a step towards utlimately
removing them altogether and updates the comment 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.
2019-03-26 14:52:00 -05:00
Dave Collins
251c9be0a1
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.
2019-03-26 14:51:59 -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
aaebc79459
txscript: Add ExtractPkScriptAltSigType benchmark. 2019-03-26 14:51:55 -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