txscript: Optimize ExtractWitnessProgramInfo

This commit is contained in:
Conner Fromknecht 2019-04-19 19:43:43 -07:00 committed by Olaoluwa Osuntokun
parent 4b03b59391
commit d410d7d7d4
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306
2 changed files with 4 additions and 11 deletions

View file

@ -131,23 +131,16 @@ func IsNullData(script []byte) bool {
// ExtractWitnessProgramInfo attempts to extract the witness program version, // ExtractWitnessProgramInfo attempts to extract the witness program version,
// as well as the witness program itself from the passed script. // as well as the witness program itself from the passed script.
func ExtractWitnessProgramInfo(script []byte) (int, []byte, error) { func ExtractWitnessProgramInfo(script []byte) (int, []byte, error) {
pops, err := parseScript(script)
if err != nil {
return 0, nil, err
}
// If at this point, the scripts doesn't resemble a witness program, // If at this point, the scripts doesn't resemble a witness program,
// then we'll exit early as there isn't a valid version or program to // then we'll exit early as there isn't a valid version or program to
// extract. // extract.
if !isWitnessProgram(pops) { version, program, valid := extractWitnessProgramInfo(script)
if !valid {
return 0, nil, fmt.Errorf("script is not a witness program, " + return 0, nil, fmt.Errorf("script is not a witness program, " +
"unable to extract version or witness program") "unable to extract version or witness program")
} }
witnessVersion := asSmallInt(pops[0].opcode.value) return version, program, nil
witnessProgram := pops[1].data
return witnessVersion, witnessProgram, nil
} }
// IsPushOnlyScript returns whether or not the passed script only pushes data // IsPushOnlyScript returns whether or not the passed script only pushes data

View file

@ -388,7 +388,7 @@ func isWitnessScriptHashScript(script []byte) bool {
} }
// extractWitnessProgramInfo returns the version and program if the passed // extractWitnessProgramInfo returns the version and program if the passed
// script constitutes a valid witness program. The alst return value indicates // script constitutes a valid witness program. The last return value indicates
// whether or not the script is a valid witness program. // whether or not the script is a valid witness program.
func extractWitnessProgramInfo(script []byte) (int, []byte, bool) { func extractWitnessProgramInfo(script []byte) (int, []byte, bool) {
// Skip parsing if we know the program is invalid based on size. // Skip parsing if we know the program is invalid based on size.