Skip to content

Commit

Permalink
txscript: Add benchmark for IsNullData
Browse files Browse the repository at this point in the history
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent 5b69223 commit 0ccd702
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
15 changes: 15 additions & 0 deletions txscript/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,18 @@ func BenchmarkIsWitnessScriptHash(b *testing.B) {
_ = IsPayToWitnessScriptHash(script)
}
}

// BenchmarkIsNullDataScript benchmarks how long it takes to analyze a very
// large script to determine if it is a standard nulldata script.
func BenchmarkIsNullDataScript(b *testing.B) {
script, err := genComplexScript()
if err != nil {
b.Fatalf("failed to create benchmark script: %v", err)
}

b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = IsNullData(script)
}
}
10 changes: 10 additions & 0 deletions txscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ func isWitnessProgram(pops []parsedOpcode) bool {
(len(pops[1].data) >= 2 && len(pops[1].data) <= 40)
}

// IsNullData returns true if the passed script is a null data script, false
// otherwise.
func IsNullData(script []byte) bool {
pops, err := parseScript(script)
if err != nil {
return false
}
return isNullData(pops)
}

// ExtractWitnessProgramInfo attempts to extract the witness program version,
// as well as the witness program itself from the passed script.
func ExtractWitnessProgramInfo(script []byte) (int, []byte, error) {
Expand Down

0 comments on commit 0ccd702

Please sign in to comment.