dcrd/txscript
Dave Collins cb86bc073c
txscript: Introduce zero-alloc script tokenizer.
This implements an efficient and zero-allocation script tokenizer that
is exported to both provide a new capability to tokenize scripts to
external consumers of the API as well as to serve as a base for
refactoring the existing highly inefficient internal code.

It is important to note that this tokenizer is intended to be used in
consensus critical code in the future, so it must exactly follow the
existing semantics.

The current script parsing mechanism used throughout the txscript module
is to fully tokenize the scripts into an array of internal parsed
opcodes which are then examined and passed around in order to implement
virtually everything related to scripts.

While that approach does simplify the analysis of certain scripts and
thus provide some nice properties in that regard, it is both extremely
inefficient in many cases, and makes it impossible for external
consumers of the API to implement any form of custom script analysis
without manually implementing a bunch of error prone tokenizing code or,
alternatively, the script engine exposing internal structures.

For example, as shown by profiling the total memory allocations of an
initial sync, the existing script parsing code allocates a total of
around 295.12GB, which equates to around 50% of all allocations
performed.  The zero-alloc tokenizer this introduces will allow that to
be reduced to virtually zero.

The following is a before and after comparison of tokenizing a large
script with a high opcode count using the existing code versus the
tokenizer this introduces for both speed and memory allocations:

benchmark                old ns/op    new ns/op     delta
------------------------------------------------------------
BenchmarkScriptParsing   153099       961           -99.37%

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

benchmark                old bytes    new bytes     delta
------------------------------------------------------------
BenchmarkScriptParsing   466945       0             -100.00%

The following is an overview of the changes:

- Introduce new error code ErrUnsupportedScriptVersion
- Implement zero-allocation script tokenizer
- Add a full suite of tests to ensure the tokenizer works as intended
  and follows the required consensus semantics
- Add an example of using the new tokenizer to count the number of
  opcodes in a script
- Update README.md to include the new example
- Update script parsing benchmark to use the new tokenizer
2019-03-26 14:50:56 -05:00
..
data txscript: Add remove signature reference test. 2019-02-14 14:10:12 -06:00
bench_test.go txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00
consensus_test.go multi: Break coinbase dep on standardness rules. 2018-05-08 12:49:40 -05:00
consensus.go txscript: Significantly improve errors. 2018-07-01 15:04:59 -05:00
doc.go txscript: Significantly improve errors. 2018-07-01 15:04:59 -05:00
engine_test.go txscript: Remove DER signature verification flag. 2018-07-02 12:02:28 -05:00
engine.go txscript: Tighten standardness pubkey checks. 2019-03-11 19:33:27 -05:00
error_test.go txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00
error.go txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00
example_test.go txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00
go.mod multi: Add go 1.11 directive to all modules. 2019-03-18 02:02:35 -05:00
go.sum multi: Remove non-root module replacements. 2019-02-08 18:01:43 -06:00
log.go multi: deprecate DisableLog 2019-03-18 11:16:35 -05:00
opcode_test.go multi: cleanup linter warnings 2019-02-13 08:38:25 -05:00
opcode.go txscript: Introduce constant for max CSV bytes. 2019-03-12 10:26:04 -05:00
README.md txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00
reference_test.go txscript: add stake opcode tests. 2018-09-11 20:21:42 +00:00
script_test.go txscript: Add stake tx remove opcode tests. 2019-03-26 14:49:15 -05:00
script.go txscript: Deprecate HasP2SHScriptSigStakeOpCodes. 2019-03-26 14:50:52 -05:00
scriptbuilder_test.go txscript: Significantly improve errors. 2018-07-01 15:04:59 -05:00
scriptbuilder.go txscript: code cleanup 2019-02-08 09:18:53 -05:00
scriptnum_test.go txscript: Remove script num require minimal flag. 2018-07-02 12:14:15 -05:00
scriptnum.go txscript: Introduce constant for max CSV bytes. 2019-03-12 10:26:04 -05:00
sigcache_test.go multi: Remove unused secp256k1 sig parse parameter. 2018-07-04 12:27:34 -05:00
sigcache.go multi: release the mutex earlier 2018-02-13 13:55:02 -06:00
sighash_test.go txscript: Decouple and optimize sighash calc. 2018-05-01 08:51:41 -05:00
sighash.go txscript: code cleanup 2019-02-08 09:18:53 -05:00
sign_test.go txscript: code cleanup 2019-02-08 09:18:53 -05:00
sign.go txscript: code cleanup 2019-02-08 09:18:53 -05:00
stack_test.go txscript: Make PeekInt consistent with PopInt. 2018-07-02 12:14:37 -05:00
stack.go txscript: code cleanup 2019-02-08 09:18:53 -05:00
standard_test.go txscript: Tighten standardness pubkey checks. 2019-03-11 19:33:27 -05:00
standard.go txscript: Deprecate GetMultisigMandN. 2019-03-26 14:50:53 -05:00
tokenizer_test.go txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00
tokenizer.go txscript: Introduce zero-alloc script tokenizer. 2019-03-26 14:50:56 -05:00

txscript

Build Status ISC License GoDoc

Package txscript implements the Decred transaction script language. There is a comprehensive test suite.

This package has intentionally been designed so it can be used as a standalone package for any projects needing to use or validate Decred transaction scripts.

Decred Scripts

Decred provides a stack-based, FORTH-like language for the scripts in the Decred transactions. This language is not turing complete although it is still fairly powerful.

Installation and Updating

$ go get -u github.com/decred/dcrd/txscript

Examples

License

Package txscript is licensed under the copyfree ISC License.