Skip to content

Commit

Permalink
Move CTxDestination from script/script to script/standard
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Sep 16, 2014
1 parent ab3834b commit 0be990b
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 122 deletions.
1 change: 1 addition & 0 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "chainparams.h"
#include "key.h"
#include "script/script.h"
#include "script/standard.h"

#include <string>
#include <vector>
Expand Down
5 changes: 2 additions & 3 deletions src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,8 @@ static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
if (!addr.IsValid())
throw runtime_error("invalid TX output address");

// build standard output script via SetDestination()
CScript scriptPubKey;
scriptPubKey.SetDestination(addr.Get());
// build standard output script via GetScriptForDestination()
CScript scriptPubKey = GetScriptForDestination(addr.Get());

// construct TxOut, append to transaction output list
CTxOut txout(value, scriptPubKey);
Expand Down
4 changes: 3 additions & 1 deletion src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "init.h"
#include "main.h"
#include "protocol.h"
#include "script/script.h"
#include "script/standard.h"
#include "util.h"

#ifdef WIN32
Expand Down Expand Up @@ -222,7 +224,7 @@ QString formatBitcoinURI(const SendCoinsRecipient &info)
bool isDust(const QString& address, qint64 amount)
{
CTxDestination dest = CBitcoinAddress(address.toStdString()).Get();
CScript script; script.SetDestination(dest);
CScript script = GetScriptForDestination(dest);
CTxOut txOut(amount, script);
return txOut.IsDust(::minRelayTxFee);
}
Expand Down
4 changes: 2 additions & 2 deletions src/qt/paymentserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ void PaymentServer::fetchPaymentACK(CWallet* wallet, SendCoinsRecipient recipien
std::string strAccount = account.toStdString();
set<CTxDestination> refundAddresses = wallet->GetAccountAddresses(strAccount);
if (!refundAddresses.empty()) {
CScript s; s.SetDestination(*refundAddresses.begin());
CScript s = GetScriptForDestination(*refundAddresses.begin());
payments::Output* refund_to = payment.add_refund_to();
refund_to->set_script(&s[0], s.size());
}
Expand All @@ -620,7 +620,7 @@ void PaymentServer::fetchPaymentACK(CWallet* wallet, SendCoinsRecipient recipien
CKeyID keyID = newKey.GetID();
wallet->SetAddressBook(keyID, strAccount, "refund");

CScript s; s.SetDestination(keyID);
CScript s = GetScriptForDestination(keyID);
payments::Output* refund_to = payment.add_refund_to();
refund_to->set_script(&s[0], s.size());
}
Expand Down
3 changes: 1 addition & 2 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
setAddress.insert(rcp.address);
++nAddresses;

CScript scriptPubKey;
scriptPubKey.SetDestination(CBitcoinAddress(rcp.address.toStdString()).Get());
CScript scriptPubKey = GetScriptForDestination(CBitcoinAddress(rcp.address.toStdString()).Get());
vecSend.push_back(std::pair<CScript, int64_t>(scriptPubKey, rcp.amount));

total += rcp.amount;
Expand Down
4 changes: 3 additions & 1 deletion src/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "rpcserver.h"
#include "init.h"
#include "main.h"
#include "script/script.h"
#include "script/standard.h"
#include "sync.h"
#include "util.h"
#include "utiltime.h"
Expand Down Expand Up @@ -161,7 +163,7 @@ Value importaddress(const Array& params, bool fHelp)

CBitcoinAddress address(params[0].get_str());
if (address.IsValid()) {
script.SetDestination(address.Get());
script = GetScriptForDestination(address.Get());
} else if (IsHex(params[0].get_str())) {
std::vector<unsigned char> data(ParseHex(params[0].get_str()));
script = CScript(data.begin(), data.end());
Expand Down
3 changes: 1 addition & 2 deletions src/rpcmisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ CScript _createmultisig_redeemScript(const Array& params)
throw runtime_error(" Invalid public key: "+ks);
}
}
CScript result;
result.SetMultisig(nRequired, pubkeys);
CScript result = GetScriptForMultisig(nRequired, pubkeys);

if (result.size() > MAX_SCRIPT_ELEMENT_SIZE)
throw runtime_error(
Expand Down
3 changes: 1 addition & 2 deletions src/rpcrawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,7 @@ Value createrawtransaction(const Array& params, bool fHelp)
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
setAddress.insert(address);

CScript scriptPubKey;
scriptPubKey.SetDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(address.Get());
int64_t nAmount = AmountFromValue(s.value_);

CTxOut out(nAmount, scriptPubKey);
Expand Down
9 changes: 3 additions & 6 deletions src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
// Check if the current key has been used
if (account.vchPubKey.IsValid())
{
CScript scriptPubKey;
scriptPubKey.SetDestination(account.vchPubKey.GetID());
CScript scriptPubKey = GetScriptForDestination(account.vchPubKey.GetID());
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin();
it != pwalletMain->mapWallet.end() && account.vchPubKey.IsValid();
++it)
Expand Down Expand Up @@ -472,10 +471,9 @@ Value getreceivedbyaddress(const Array& params, bool fHelp)

// Bitcoin address
CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
CScript scriptPubKey;
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
scriptPubKey.SetDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(address.Get());
if (!IsMine(*pwalletMain,scriptPubKey))
return (double)0.0;

Expand Down Expand Up @@ -849,8 +847,7 @@ Value sendmany(const Array& params, bool fHelp)
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+s.name_);
setAddress.insert(address);

CScript scriptPubKey;
scriptPubKey.SetDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(address.Get());
int64_t nAmount = AmountFromValue(s.value_);
totalAmount += nAmount;

Expand Down
40 changes: 0 additions & 40 deletions src/script/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,43 +253,3 @@ bool CScript::HasCanonicalPushes() const
}
return true;
}

class CScriptVisitor : public boost::static_visitor<bool>
{
private:
CScript *script;
public:
CScriptVisitor(CScript *scriptin) { script = scriptin; }

bool operator()(const CNoDestination &dest) const {
script->clear();
return false;
}

bool operator()(const CKeyID &keyID) const {
script->clear();
*script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG;
return true;
}

bool operator()(const CScriptID &scriptID) const {
script->clear();
*script << OP_HASH160 << scriptID << OP_EQUAL;
return true;
}
};

void CScript::SetDestination(const CTxDestination& dest)
{
boost::apply_visitor(CScriptVisitor(this), dest);
}

void CScript::SetMultisig(int nRequired, const std::vector<CPubKey>& keys)
{
this->clear();

*this << EncodeOP_N(nRequired);
BOOST_FOREACH(const CPubKey& key, keys)
*this << key;
*this << EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
}
17 changes: 0 additions & 17 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,6 @@ inline std::string ValueString(const std::vector<unsigned char>& vch)
return HexStr(vch);
}

class CNoDestination {
public:
friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
};

/** A txout script template with a specific destination. It is either:
* * CNoDestination: no destination set
* * CKeyID: TX_PUBKEYHASH destination
* * CScriptID: TX_SCRIPTHASH destination
* A CTxDestination is the internal data type encoded in a CBitcoinAddress
*/
typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;

/** Serialized script, used inside transaction inputs and outputs */
class CScript : public std::vector<unsigned char>
{
Expand Down Expand Up @@ -604,9 +590,6 @@ class CScript : public std::vector<unsigned char>
return (size() > 0 && *begin() == OP_RETURN);
}

void SetDestination(const CTxDestination& address);
void SetMultisig(int nRequired, const std::vector<CPubKey>& keys);

std::string ToString() const
{
std::string str;
Expand Down
47 changes: 47 additions & 0 deletions src/script/standard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,50 @@ bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, vecto

return true;
}

namespace
{
class CScriptVisitor : public boost::static_visitor<bool>
{
private:
CScript *script;
public:
CScriptVisitor(CScript *scriptin) { script = scriptin; }

bool operator()(const CNoDestination &dest) const {
script->clear();
return false;
}

bool operator()(const CKeyID &keyID) const {
script->clear();
*script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG;
return true;
}

bool operator()(const CScriptID &scriptID) const {
script->clear();
*script << OP_HASH160 << scriptID << OP_EQUAL;
return true;
}
};
}

CScript GetScriptForDestination(const CTxDestination& dest)
{
CScript script;

boost::apply_visitor(CScriptVisitor(&script), dest);
return script;
}

CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys)
{
CScript script;

script << CScript::EncodeOP_N(nRequired);
BOOST_FOREACH(const CPubKey& key, keys)
script << key;
script << CScript::EncodeOP_N(keys.size()) << OP_CHECKMULTISIG;
return script;
}
17 changes: 17 additions & 0 deletions src/script/standard.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ enum txnouttype
TX_NULL_DATA,
};

class CNoDestination {
public:
friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
};

/** A txout script template with a specific destination. It is either:
* * CNoDestination: no destination set
* * CKeyID: TX_PUBKEYHASH destination
* * CScriptID: TX_SCRIPTHASH destination
* A CTxDestination is the internal data type encoded in a CBitcoinAddress
*/
typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;

const char* GetTxnOutputType(txnouttype t);

bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
Expand All @@ -53,4 +67,7 @@ bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);

CScript GetScriptForDestination(const CTxDestination& dest);
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);

#endif // H_BITCOIN_SCRIPT_STANDARD
6 changes: 3 additions & 3 deletions src/test/DoS_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
tx.vin[0].scriptSig << OP_1;
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
tx.vout[0].scriptPubKey.SetDestination(key.GetPubKey().GetID());
tx.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());

AddOrphanTx(tx, i);
}
Expand All @@ -189,7 +189,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
tx.vin[0].prevout.hash = txPrev.GetHash();
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
tx.vout[0].scriptPubKey.SetDestination(key.GetPubKey().GetID());
tx.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
SignSignature(keystore, txPrev, tx, 0);

AddOrphanTx(tx, i);
Expand All @@ -203,7 +203,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
CMutableTransaction tx;
tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT;
tx.vout[0].scriptPubKey.SetDestination(key.GetPubKey().GetID());
tx.vout[0].scriptPubKey = GetScriptForDestination(key.GetPubKey().GetID());
tx.vin.resize(500);
for (unsigned int j = 0; j < tx.vin.size(); j++)
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/miner_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
tx.vin[0].scriptSig = CScript() << OP_1;
tx.vout[0].nValue = 4900000000LL;
script = CScript() << OP_0;
tx.vout[0].scriptPubKey.SetDestination(script.GetID());
tx.vout[0].scriptPubKey = GetScriptForDestination(script.GetID());
hash = tx.GetHash();
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
tx.vin[0].prevout.hash = hash;
Expand Down
Loading

0 comments on commit 0be990b

Please sign in to comment.