aboutsummaryrefslogtreecommitdiff
path: root/src/script/standard.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2018-08-25 17:32:39 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2018-08-25 17:41:00 +0200
commit4cef8e05938e55d2d1bb3c36642aa294fb63b6e2 (patch)
tree23ce5015967c22d121fe47127ac72ebe92c62cef /src/script/standard.cpp
parent776fa60c4bebb809dfadd9859cc2789769d492b9 (diff)
parent984d72ec659361d8c1a6f3c6864e839a807817a7 (diff)
downloadbitcoin-4cef8e05938e55d2d1bb3c36642aa294fb63b6e2.tar.xz
Merge #13429: Return the script type from Solver
984d72ec659361d8c1a6f3c6864e839a807817a7 Return the script type from Solver (Ben Woosley) Pull request description: Because false is synonymous with TX_NONSTANDARD, this conveys the same information and makes the handling explicitly based on script type, simplifying each call site. Prior to this change it was common for the return value to be ignored, or for the return value and TX_NONSTANDARD to be redundantly handled. Tree-SHA512: 31864f856b8cb75f4b782d12678070e8b1cfe9665c6f57cfb25e7ac8bcea8a22f9a78d7c8cf0101c841f2a612400666fb91798bffe88de856e98b873703b0965
Diffstat (limited to 'src/script/standard.cpp')
-rw-r--r--src/script/standard.cpp57
1 files changed, 21 insertions, 36 deletions
diff --git a/src/script/standard.cpp b/src/script/standard.cpp
index bfbf9f13db..08ba1b1e0f 100644
--- a/src/script/standard.cpp
+++ b/src/script/standard.cpp
@@ -87,7 +87,7 @@ static bool MatchMultisig(const CScript& script, unsigned int& required, std::ve
return (it + 1 == script.end());
}
-bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet)
+txnouttype Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
{
vSolutionsRet.clear();
@@ -95,33 +95,28 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
// it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
if (scriptPubKey.IsPayToScriptHash())
{
- typeRet = TX_SCRIPTHASH;
std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
vSolutionsRet.push_back(hashBytes);
- return true;
+ return TX_SCRIPTHASH;
}
int witnessversion;
std::vector<unsigned char> witnessprogram;
if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
- typeRet = TX_WITNESS_V0_KEYHASH;
vSolutionsRet.push_back(witnessprogram);
- return true;
+ return TX_WITNESS_V0_KEYHASH;
}
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
- typeRet = TX_WITNESS_V0_SCRIPTHASH;
vSolutionsRet.push_back(witnessprogram);
- return true;
+ return TX_WITNESS_V0_SCRIPTHASH;
}
if (witnessversion != 0) {
- typeRet = TX_WITNESS_UNKNOWN;
vSolutionsRet.push_back(std::vector<unsigned char>{(unsigned char)witnessversion});
vSolutionsRet.push_back(std::move(witnessprogram));
- return true;
+ return TX_WITNESS_UNKNOWN;
}
- typeRet = TX_NONSTANDARD;
- return false;
+ return TX_NONSTANDARD;
}
// Provably prunable, data-carrying output
@@ -130,47 +125,39 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
// byte passes the IsPushOnly() test we don't care what exactly is in the
// script.
if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
- typeRet = TX_NULL_DATA;
- return true;
+ return TX_NULL_DATA;
}
std::vector<unsigned char> data;
if (MatchPayToPubkey(scriptPubKey, data)) {
- typeRet = TX_PUBKEY;
vSolutionsRet.push_back(std::move(data));
- return true;
+ return TX_PUBKEY;
}
if (MatchPayToPubkeyHash(scriptPubKey, data)) {
- typeRet = TX_PUBKEYHASH;
vSolutionsRet.push_back(std::move(data));
- return true;
+ return TX_PUBKEYHASH;
}
unsigned int required;
std::vector<std::vector<unsigned char>> keys;
if (MatchMultisig(scriptPubKey, required, keys)) {
- typeRet = TX_MULTISIG;
vSolutionsRet.push_back({static_cast<unsigned char>(required)}); // safe as required is in range 1..16
vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
vSolutionsRet.push_back({static_cast<unsigned char>(keys.size())}); // safe as size is in range 1..16
- return true;
+ return TX_MULTISIG;
}
vSolutionsRet.clear();
- typeRet = TX_NONSTANDARD;
- return false;
+ return TX_NONSTANDARD;
}
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
{
std::vector<valtype> vSolutions;
- txnouttype whichType;
- if (!Solver(scriptPubKey, whichType, vSolutions))
- return false;
+ txnouttype whichType = Solver(scriptPubKey, vSolutions);
- if (whichType == TX_PUBKEY)
- {
+ if (whichType == TX_PUBKEY) {
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid())
return false;
@@ -212,11 +199,11 @@ bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet)
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet)
{
addressRet.clear();
- typeRet = TX_NONSTANDARD;
std::vector<valtype> vSolutions;
- if (!Solver(scriptPubKey, typeRet, vSolutions))
+ typeRet = Solver(scriptPubKey, vSolutions);
+ if (typeRet == TX_NONSTANDARD) {
return false;
- if (typeRet == TX_NULL_DATA){
+ } else if (typeRet == TX_NULL_DATA) {
// This is data, not addresses
return false;
}
@@ -324,14 +311,12 @@ CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
CScript GetScriptForWitness(const CScript& redeemscript)
{
- txnouttype typ;
std::vector<std::vector<unsigned char> > vSolutions;
- if (Solver(redeemscript, typ, vSolutions)) {
- if (typ == TX_PUBKEY) {
- return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0].begin(), vSolutions[0].end())));
- } else if (typ == TX_PUBKEYHASH) {
- return GetScriptForDestination(WitnessV0KeyHash(vSolutions[0]));
- }
+ txnouttype typ = Solver(redeemscript, vSolutions);
+ if (typ == TX_PUBKEY) {
+ return GetScriptForDestination(WitnessV0KeyHash(Hash160(vSolutions[0].begin(), vSolutions[0].end())));
+ } else if (typ == TX_PUBKEYHASH) {
+ return GetScriptForDestination(WitnessV0KeyHash(vSolutions[0]));
}
return GetScriptForDestination(WitnessV0ScriptHash(redeemscript));
}