txscript: Optimize ExtractPkScriptAddrs multisig.

This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the detection for multisig scripts.

Also, since the remaining slow path cases are all recursive calls,
the parsed opcodes are no longer used, so parsing is removed.
This commit is contained in:
Dave Collins 2019-03-13 01:12:33 -05:00 committed by Olaoluwa Osuntokun
parent 0e810b4ef4
commit 0bc18254d4
No known key found for this signature in database
GPG key ID: 3BBD59E99B280306

View file

@ -868,11 +868,27 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
return PubKeyTy, addrs, 1, nil return PubKeyTy, addrs, 1, nil
} }
// Check for multi-signature script.
const scriptVersion = 0
details := extractMultisigScriptDetails(scriptVersion, pkScript, true)
if details.valid {
// Convert the public keys while skipping any that are invalid.
addrs := make([]btcutil.Address, 0, len(details.pubKeys))
for _, pubkey := range details.pubKeys {
addr, err := btcutil.NewAddressPubKey(pubkey, chainParams)
if err == nil {
addrs = append(addrs, addr)
}
}
return MultiSigTy, addrs, details.requiredSigs, nil
}
// Fall back to slow path. Ultimately these are intended to be replaced by // Fall back to slow path. Ultimately these are intended to be replaced by
// faster variants based on the unparsed raw scripts. // faster variants based on the unparsed raw scripts.
var addrs []btcutil.Address var addrs []btcutil.Address
var requiredSigs int var requiredSigs int
var err error
// No valid addresses or required signatures if the script doesn't // No valid addresses or required signatures if the script doesn't
// parse. // parse.
@ -881,7 +897,6 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
return NonStandardTy, nil, 0, err return NonStandardTy, nil, 0, err
} }
const scriptVersion = 0
scriptClass := typeOfScript(scriptVersion, pkScript) scriptClass := typeOfScript(scriptVersion, pkScript)
switch scriptClass { switch scriptClass {
@ -909,25 +924,6 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
addrs = append(addrs, addr) addrs = append(addrs, addr)
} }
case MultiSigTy:
// A multi-signature script is of the form:
// <numsigs> <pubkey> <pubkey> <pubkey>... <numpubkeys> OP_CHECKMULTISIG
// Therefore the number of required signatures is the 1st item
// on the stack and the number of public keys is the 2nd to last
// item on the stack.
requiredSigs = asSmallInt(pops[0].opcode.value)
numPubKeys := asSmallInt(pops[len(pops)-2].opcode.value)
// Extract the public keys while skipping any that are invalid.
addrs = make([]btcutil.Address, 0, numPubKeys)
for i := 0; i < numPubKeys; i++ {
addr, err := btcutil.NewAddressPubKey(pops[i+1].data,
chainParams)
if err == nil {
addrs = append(addrs, addr)
}
}
case NullDataTy: case NullDataTy:
// Null data transactions have no addresses or required // Null data transactions have no addresses or required
// signatures. // signatures.