c997417978
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-8 63464 677 -98.93% benchmark old allocs new allocs delta BenchmarkScriptParsing-8 1 0 -100.00% benchmark old bytes new bytes delta BenchmarkScriptParsing-8 311299 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
132 lines
4.3 KiB
Go
132 lines
4.3 KiB
Go
// Copyright (c) 2017 The btcsuite developers
|
|
// Copyright (c) 2015-2019 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txscript
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
|
|
func TestErrorCodeStringer(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
in ErrorCode
|
|
want string
|
|
}{
|
|
{ErrInternal, "ErrInternal"},
|
|
{ErrInvalidFlags, "ErrInvalidFlags"},
|
|
{ErrInvalidIndex, "ErrInvalidIndex"},
|
|
{ErrUnsupportedAddress, "ErrUnsupportedAddress"},
|
|
{ErrTooManyRequiredSigs, "ErrTooManyRequiredSigs"},
|
|
{ErrTooMuchNullData, "ErrTooMuchNullData"},
|
|
{ErrUnsupportedScriptVersion, "ErrUnsupportedScriptVersion"},
|
|
{ErrNotMultisigScript, "ErrNotMultisigScript"},
|
|
{ErrEarlyReturn, "ErrEarlyReturn"},
|
|
{ErrEmptyStack, "ErrEmptyStack"},
|
|
{ErrEvalFalse, "ErrEvalFalse"},
|
|
{ErrScriptUnfinished, "ErrScriptUnfinished"},
|
|
{ErrInvalidProgramCounter, "ErrInvalidProgramCounter"},
|
|
{ErrScriptTooBig, "ErrScriptTooBig"},
|
|
{ErrElementTooBig, "ErrElementTooBig"},
|
|
{ErrTooManyOperations, "ErrTooManyOperations"},
|
|
{ErrStackOverflow, "ErrStackOverflow"},
|
|
{ErrInvalidPubKeyCount, "ErrInvalidPubKeyCount"},
|
|
{ErrInvalidSignatureCount, "ErrInvalidSignatureCount"},
|
|
{ErrNumberTooBig, "ErrNumberTooBig"},
|
|
{ErrVerify, "ErrVerify"},
|
|
{ErrEqualVerify, "ErrEqualVerify"},
|
|
{ErrNumEqualVerify, "ErrNumEqualVerify"},
|
|
{ErrCheckSigVerify, "ErrCheckSigVerify"},
|
|
{ErrCheckMultiSigVerify, "ErrCheckMultiSigVerify"},
|
|
{ErrDisabledOpcode, "ErrDisabledOpcode"},
|
|
{ErrReservedOpcode, "ErrReservedOpcode"},
|
|
{ErrMalformedPush, "ErrMalformedPush"},
|
|
{ErrInvalidStackOperation, "ErrInvalidStackOperation"},
|
|
{ErrUnbalancedConditional, "ErrUnbalancedConditional"},
|
|
{ErrMinimalData, "ErrMinimalData"},
|
|
{ErrInvalidSigHashType, "ErrInvalidSigHashType"},
|
|
{ErrSigTooShort, "ErrSigTooShort"},
|
|
{ErrSigTooLong, "ErrSigTooLong"},
|
|
{ErrSigInvalidSeqID, "ErrSigInvalidSeqID"},
|
|
{ErrSigInvalidDataLen, "ErrSigInvalidDataLen"},
|
|
{ErrSigMissingSTypeID, "ErrSigMissingSTypeID"},
|
|
{ErrSigMissingSLen, "ErrSigMissingSLen"},
|
|
{ErrSigInvalidSLen, "ErrSigInvalidSLen"},
|
|
{ErrSigInvalidRIntID, "ErrSigInvalidRIntID"},
|
|
{ErrSigZeroRLen, "ErrSigZeroRLen"},
|
|
{ErrSigNegativeR, "ErrSigNegativeR"},
|
|
{ErrSigTooMuchRPadding, "ErrSigTooMuchRPadding"},
|
|
{ErrSigInvalidSIntID, "ErrSigInvalidSIntID"},
|
|
{ErrSigZeroSLen, "ErrSigZeroSLen"},
|
|
{ErrSigNegativeS, "ErrSigNegativeS"},
|
|
{ErrSigTooMuchSPadding, "ErrSigTooMuchSPadding"},
|
|
{ErrSigHighS, "ErrSigHighS"},
|
|
{ErrNotPushOnly, "ErrNotPushOnly"},
|
|
{ErrSigNullDummy, "ErrSigNullDummy"},
|
|
{ErrPubKeyType, "ErrPubKeyType"},
|
|
{ErrCleanStack, "ErrCleanStack"},
|
|
{ErrNullFail, "ErrNullFail"},
|
|
{ErrDiscourageUpgradableNOPs, "ErrDiscourageUpgradableNOPs"},
|
|
{ErrNegativeLockTime, "ErrNegativeLockTime"},
|
|
{ErrUnsatisfiedLockTime, "ErrUnsatisfiedLockTime"},
|
|
{ErrWitnessProgramEmpty, "ErrWitnessProgramEmpty"},
|
|
{ErrWitnessProgramMismatch, "ErrWitnessProgramMismatch"},
|
|
{ErrWitnessProgramWrongLength, "ErrWitnessProgramWrongLength"},
|
|
{ErrWitnessMalleated, "ErrWitnessMalleated"},
|
|
{ErrWitnessMalleatedP2SH, "ErrWitnessMalleatedP2SH"},
|
|
{ErrWitnessUnexpected, "ErrWitnessUnexpected"},
|
|
{ErrMinimalIf, "ErrMinimalIf"},
|
|
{ErrWitnessPubKeyType, "ErrWitnessPubKeyType"},
|
|
{ErrDiscourageUpgradableWitnessProgram, "ErrDiscourageUpgradableWitnessProgram"},
|
|
{0xffff, "Unknown ErrorCode (65535)"},
|
|
}
|
|
|
|
// Detect additional error codes that don't have the stringer added.
|
|
if len(tests)-1 != int(numErrorCodes) {
|
|
t.Errorf("It appears an error code was added without adding an " +
|
|
"associated stringer test")
|
|
}
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
for i, test := range tests {
|
|
result := test.in.String()
|
|
if result != test.want {
|
|
t.Errorf("String #%d\n got: %s want: %s", i, result,
|
|
test.want)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestError tests the error output for the Error type.
|
|
func TestError(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
in Error
|
|
want string
|
|
}{
|
|
{
|
|
Error{Description: "some error"},
|
|
"some error",
|
|
},
|
|
{
|
|
Error{Description: "human-readable error"},
|
|
"human-readable error",
|
|
},
|
|
}
|
|
|
|
t.Logf("Running %d tests", len(tests))
|
|
for i, test := range tests {
|
|
result := test.in.Error()
|
|
if result != test.want {
|
|
t.Errorf("Error #%d\n got: %s want: %s", i, result,
|
|
test.want)
|
|
continue
|
|
}
|
|
}
|
|
}
|