Skip to content

Commit

Permalink
txscript: Add benchmark for IsPayToPubKeyHash
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Feb 5, 2021
1 parent 8ceea24 commit dff834b
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 @@ -144,3 +144,18 @@ func BenchmarkIsPubKeyScript(b *testing.B) {
_ = IsPayToPubKey(script)
}
}

// BenchmarkIsPubKeyHashScript benchmarks how long it takes to analyze a very
// large script to determine if it is a standard pay-to-pubkey-hash script.
func BenchmarkIsPubKeyHashScript(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++ {
_ = IsPayToPubKeyHash(script)
}
}
10 changes: 10 additions & 0 deletions txscript/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ func IsPayToPubKey(script []byte) bool {
return isPubKeyScript(script)
}

// IsPayToPubKeyHash returns true if the script is in the standard
// pay-to-pubkey-hash (P2PKH) format, false otherwise.
func IsPayToPubKeyHash(script []byte) bool {
pops, err := parseScript(script)
if err != nil {
return false
}
return isPubkeyHash(pops)
}

// IsPayToScriptHash returns true if the script is in the standard
// pay-to-script-hash (P2SH) format, false otherwise.
func IsPayToScriptHash(script []byte) bool {
Expand Down

0 comments on commit dff834b

Please sign in to comment.