Skip to content

Commit

Permalink
Move getNamePrevout close to name_firstupdate.
Browse files Browse the repository at this point in the history
getNamePrevout is a helper method that is only used by name_firstupdate,
so it makes sense to move it close to that place.  The "general front"
of rpcnames.cpp should be used for things that are used by multiple
methods, like the DestinationAddressHelper and more things in the
future.

This is a pure code move, together with also moving
MAX_NAME_PREVOUT_TRIALS into the function.
  • Loading branch information
domob1812 committed Jun 11, 2018
1 parent 998f076 commit 0151bbd
Showing 1 changed file with 49 additions and 44 deletions.
93 changes: 49 additions & 44 deletions src/wallet/rpcnames.cpp
Expand Up @@ -23,51 +23,10 @@

#include <memory>

/* ************************************************************************** */
namespace
{

// Maximum number of outputs that are checked for the NAME_NEW prevout.
constexpr unsigned MAX_NAME_PREVOUT_TRIALS = 1000;

/**
* Helper routine to fetch the name output of a previous transaction. This
* is required for name_firstupdate.
* @param txid Previous transaction ID.
* @param txOut Set to the corresponding output.
* @param txIn Set to the CTxIn to include in the new tx.
* @return True if the output could be found.
*/
bool
getNamePrevout (const uint256& txid, CTxOut& txOut, CTxIn& txIn)
{
AssertLockHeld (cs_main);

// Unfortunately, with the change of the txdb to be based on outputs rather
// than full transactions, we can no longer just look up the txid and iterate
// over all outputs. Since this is only necessary for a corner case, we just
// keep trying with indices until we find the output (up to a maximum number
// of trials).

for (unsigned i = 0; i < MAX_NAME_PREVOUT_TRIALS; ++i)
{
const COutPoint outp(txid, i);

Coin coin;
if (!pcoinsTip->GetCoin (outp, coin))
continue;

if (!coin.out.IsNull ()
&& CNameScript::isNameScript (coin.out.scriptPubKey))
{
txOut = coin.out;
txIn = CTxIn (outp);
return true;
}
}

return false;
}

/**
* A simple helper class that handles determination of the address to which
* name outputs should be sent. It handles the CReserveKey reservation
Expand Down Expand Up @@ -160,8 +119,7 @@ std::string getNameOpOptionsHelp ()
" }\n";
}

} // namespace

} // anonymous namespace
/* ************************************************************************** */

UniValue
Expand Down Expand Up @@ -345,6 +303,53 @@ name_new (const JSONRPCRequest& request)

/* ************************************************************************** */

namespace
{

/**
* Helper routine to fetch the name output of a previous transaction. This
* is required for name_firstupdate.
* @param txid Previous transaction ID.
* @param txOut Set to the corresponding output.
* @param txIn Set to the CTxIn to include in the new tx.
* @return True if the output could be found.
*/
bool
getNamePrevout (const uint256& txid, CTxOut& txOut, CTxIn& txIn)
{
AssertLockHeld (cs_main);

// Maximum number of outputs that are checked for the NAME_NEW prevout.
constexpr unsigned MAX_NAME_PREVOUT_TRIALS = 1000;

// Unfortunately, with the change of the txdb to be based on outputs rather
// than full transactions, we can no longer just look up the txid and iterate
// over all outputs. Since this is only necessary for a corner case, we just
// keep trying with indices until we find the output (up to a maximum number
// of trials).

for (unsigned i = 0; i < MAX_NAME_PREVOUT_TRIALS; ++i)
{
const COutPoint outp(txid, i);

Coin coin;
if (!pcoinsTip->GetCoin (outp, coin))
continue;

if (!coin.out.IsNull ()
&& CNameScript::isNameScript (coin.out.scriptPubKey))
{
txOut = coin.out;
txIn = CTxIn (outp);
return true;
}
}

return false;
}

} // anonymous namespace

UniValue
name_firstupdate (const JSONRPCRequest& request)
{
Expand Down

0 comments on commit 0151bbd

Please sign in to comment.