This was originally an optimization PR for GetSigOpCount
- kept only the refactorings without any optimization.
The goal of the PR now is to document the actual behavior better (more descriptive names, parameters, tests, benchmarks, split out sub-functionality).
Context
Test coverage was thin for this code path that suddenly became popular for different reasons (https://github.com/bitcoin/bitcoin/pull/31624 and #32521 and #32533) highlighting the need for better code coverage.
The PR now splits out the common PUSHDATA
length/bounds reads and error checks and covers its corner cases with unit tests and benchmarks.
Testing
- added unit tests covering every standard script type (and a historical
OP_RETURN … OP_CHECKSIG
oddity); - added fuzz-style test that compares the refactored implementation against the exact legacy algorithm on random scripts - also ran it continuously for 25 hours without divergence;
- reindexed to height 897,000 comparing the outputs of the old and the new implementation without incidents.