Browse Source

Add cleanwallettransactions RPC

pull/4/head
blackjok3r 6 years ago
parent
commit
9452d0d77d
  1. 2
      src/rpc/server.cpp
  2. 2
      src/rpc/server.h
  3. 101
      src/wallet/rpcwallet.cpp

2
src/rpc/server.cpp

@ -508,7 +508,7 @@ static const CRPCCommand vRPCCommands[] =
{ "wallet", "getaccountaddress", &getaccountaddress, true },
{ "wallet", "getaccount", &getaccount, true },
{ "wallet", "getaddressesbyaccount", &getaddressesbyaccount, true },
{ "wallet", "cleanwallettransactions", &cleanwallettransactions, false },
{ "wallet", "getbalance", &getbalance, false },
{ "wallet", "getbalance64", &getbalance64, false },
{ "wallet", "getnewaddress", &getnewaddress, true },

2
src/rpc/server.h

@ -317,7 +317,7 @@ extern UniValue signmessage(const UniValue& params, bool fHelp);
extern UniValue verifymessage(const UniValue& params, bool fHelp);
extern UniValue getreceivedbyaddress(const UniValue& params, bool fHelp);
extern UniValue getreceivedbyaccount(const UniValue& params, bool fHelp);
extern UniValue cleanwallettransactions(const UniValue& params, bool fHelp);
extern UniValue getbalance(const UniValue& params, bool fHelp);
extern UniValue getbalance64(const UniValue& params, bool fHelp);
extern UniValue getunconfirmedbalance(const UniValue& params, bool fHelp);

101
src/wallet/rpcwallet.cpp

@ -1016,7 +1016,108 @@ CAmount GetAccountBalance(const string& strAccount, int nMinDepth, const isminef
return GetAccountBalance(walletdb, strAccount, nMinDepth, filter);
}
UniValue cleanwallettransactions(const UniValue& params, bool fHelp)
{
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() > 1 )
throw runtime_error(
"cleanwallettransactions \"txid\"\n"
"\nRemove all txs that are spent. You can clear all txs bar one, by specifiying a txid.\n"
"\nPlease backup your wallet.dat before running this command.\n"
"\nArguments:\n"
"1. \"txid\" (string, optional) The transaction id to keep.\n"
"\nResult:\n"
"{\n"
" \"total_transactons\" : n, (numeric) Transactions in wallet of " + strprintf("%s",komodo_chainname()) + "\n"
" \"remaining_transactons\" : n, (numeric) Transactions in wallet after clean.\n"
" \"removed_transactons\" : n, (numeric) The number of transactions removed.\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("cleanwallettransactions", "")
+ HelpExampleCli("cleanwallettransactions","\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
+ HelpExampleRpc("cleanwallettransactions", "")
+ HelpExampleRpc("cleanwallettransactions","\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
);
LOCK2(cs_main, pwalletMain->cs_wallet);
UniValue ret(UniValue::VOBJ);
uint256 exception; int32_t txs = pwalletMain->mapWallet.size();
std::vector<uint256> TxToRemove;
if (params.size() == 1)
{
exception.SetHex(params[0].get_str());
uint256 tmp_hash; CTransaction tmp_tx;
if (GetTransaction(exception,tmp_tx,tmp_hash,false))
{
if ( !pwalletMain->IsMine(tmp_tx) )
{
throw runtime_error("\nThe transaction is not yours!\n");
}
else
{
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
{
const CWalletTx& wtx = (*it).second;
if ( wtx.GetHash() != exception )
{
TxToRemove.push_back(wtx.GetHash());
}
}
}
}
else
{
throw runtime_error("\nThe transaction could not be found!\n");
}
}
else
{
// get all locked utxos to relock them later.
vector<COutPoint> vLockedUTXO;
pwalletMain->ListLockedCoins(vLockedUTXO);
// unlock all coins so that the following call containes all utxos.
pwalletMain->UnlockAllCoins();
// listunspent call... this gets us all the txids that are unspent, we search this list for the oldest tx,
vector<COutput> vecOutputs;
assert(pwalletMain != NULL);
pwalletMain->AvailableCoins(vecOutputs, false, NULL, true);
int32_t oldestTxDepth = 0;
BOOST_FOREACH(const COutput& out, vecOutputs)
{
if ( out.nDepth > oldestTxDepth )
oldestTxDepth = out.nDepth;
}
oldestTxDepth = oldestTxDepth + 1; // add extra block just for safety.
// lock all the previouly locked coins.
BOOST_FOREACH(COutPoint &outpt, vLockedUTXO) {
pwalletMain->LockCoin(outpt);
}
// then add all txs in the wallet before this block to the list to remove.
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
{
const CWalletTx& wtx = (*it).second;
if (wtx.GetDepthInMainChain() > oldestTxDepth)
TxToRemove.push_back(wtx.GetHash());
}
}
// erase txs
BOOST_FOREACH (uint256& hash, TxToRemove)
{
pwalletMain->EraseFromWallet(hash);
LogPrintf("Erased %s from wallet.\n",hash.ToString().c_str());
}
// build return JSON for stats.
int remaining = pwalletMain->mapWallet.size();
ret.push_back(Pair("total_transactons", (int)txs));
ret.push_back(Pair("remaining_transactons", (int)remaining));
ret.push_back(Pair("removed_transactions", (int)(txs-remaining)));
return (ret);
}
UniValue getbalance(const UniValue& params, bool fHelp)
{

Loading…
Cancel
Save