Skip to content

Commit ffb32ac

Browse files
committed
Merge pull request #1583
2a72d45 JSON-RPC method: prioritisetransaction <txid> <priority delta> <priority tx fee> (Luke Dashjr)
2 parents f40d193 + 2a72d45 commit ffb32ac

File tree

8 files changed

+74
-3
lines changed

8 files changed

+74
-3
lines changed

src/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,16 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state)
789789

790790
int64_t GetMinFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree, enum GetMinFee_mode mode)
791791
{
792+
{
793+
LOCK(mempool.cs);
794+
uint256 hash = tx.GetHash();
795+
double dPriorityDelta = 0;
796+
int64_t nFeeDelta = 0;
797+
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
798+
if (dPriorityDelta > 0 || nFeeDelta > 0)
799+
return 0;
800+
}
801+
792802
// Base fee is either minTxFee or minRelayTxFee
793803
CFeeRate baseFeeRate = (mode == GMF_RELAY) ? tx.minRelayTxFee : tx.minTxFee;
794804

src/miner.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Distributed under the MIT/X11 software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6+
#include <inttypes.h>
7+
68
#include "miner.h"
79

810
#include "core.h"
@@ -186,6 +188,9 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
186188
unsigned int nTxSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION);
187189
dPriority = tx.ComputePriority(dPriority, nTxSize);
188190

191+
uint256 hash = tx.GetHash();
192+
mempool.ApplyDeltas(hash, dPriority, nTotalIn);
193+
189194
CFeeRate feeRate(nTotalIn-tx.GetValueOut(), nTxSize);
190195

191196
if (porphan)
@@ -227,10 +232,14 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
227232
continue;
228233

229234
// Skip free transactions if we're past the minimum block size:
230-
if (fSortedByFee && (feeRate < CTransaction::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
235+
const uint256& hash = tx.GetHash();
236+
double dPriorityDelta = 0;
237+
int64_t nFeeDelta = 0;
238+
mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
239+
if (fSortedByFee && (dPriorityDelta <= 0) && (nFeeDelta <= 0) && (feeRate < CTransaction::minRelayTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
231240
continue;
232241

233-
// Prioritize by fee once past the priority size or we run out of high-priority
242+
// Prioritise by fee once past the priority size or we run out of high-priority
234243
// transactions:
235244
if (!fSortedByFee &&
236245
((nBlockSize + nTxSize >= nBlockPrioritySize) || !AllowFree(dPriority)))
@@ -257,7 +266,6 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
257266
continue;
258267

259268
CTxUndo txundo;
260-
const uint256& hash = tx.GetHash();
261269
UpdateCoins(tx, state, view, txundo, pindexPrev->nHeight+1);
262270

263271
// Added

src/rpcclient.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
7272
if (strMethod == "listtransactions" && n > 2) ConvertTo<int64_t>(params[2]);
7373
if (strMethod == "listaccounts" && n > 0) ConvertTo<int64_t>(params[0]);
7474
if (strMethod == "walletpassphrase" && n > 1) ConvertTo<int64_t>(params[1]);
75+
if (strMethod == "prioritisetransaction" && n > 1) ConvertTo<double>(params[1]);
76+
if (strMethod == "prioritisetransaction" && n > 2) ConvertTo<int64_t>(params[2]);
7577
if (strMethod == "getblocktemplate" && n > 0) ConvertTo<Object>(params[0]);
7678
if (strMethod == "listsinceblock" && n > 1) ConvertTo<int64_t>(params[1]);
7779
if (strMethod == "sendmany" && n > 1) ConvertTo<Object>(params[1]);

src/rpcmining.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ Value getmininginfo(const Array& params, bool fHelp)
247247
}
248248

249249

250+
Value prioritisetransaction(const Array& params, bool fHelp)
251+
{
252+
if (fHelp || params.size() != 3)
253+
throw runtime_error(
254+
"prioritisetransaction <txid> <priority delta> <fee delta>\n"
255+
"Accepts the transaction into mined blocks at a higher (or lower) priority");
256+
257+
uint256 hash;
258+
hash.SetHex(params[0].get_str());
259+
mempool.PrioritiseTransaction(hash, params[0].get_str(), params[1].get_real(), params[2].get_int64());
260+
return true;
261+
}
262+
263+
250264
Value getblocktemplate(const Array& params, bool fHelp)
251265
{
252266
if (fHelp || params.size() > 1)

src/rpcserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ static const CRPCCommand vRPCCommands[] =
254254
{ "getblocktemplate", &getblocktemplate, true, false, false },
255255
{ "getmininginfo", &getmininginfo, true, false, false },
256256
{ "getnetworkhashps", &getnetworkhashps, true, false, false },
257+
{ "prioritisetransaction", &prioritisetransaction, true, false, false },
257258
{ "submitblock", &submitblock, false, true, false },
258259

259260
/* Raw transactions */

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ extern json_spirit::Value setgenerate(const json_spirit::Array& params, bool fHe
130130
extern json_spirit::Value getnetworkhashps(const json_spirit::Array& params, bool fHelp);
131131
extern json_spirit::Value gethashespersec(const json_spirit::Array& params, bool fHelp);
132132
extern json_spirit::Value getmininginfo(const json_spirit::Array& params, bool fHelp);
133+
extern json_spirit::Value prioritisetransaction(const json_spirit::Array& params, bool fHelp);
133134
extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
134135
extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
135136
extern json_spirit::Value estimatefee(const json_spirit::Array& params, bool fHelp);

src/txmempool.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ void CTxMemPool::removeForBlock(const std::vector<CTransaction>& vtx, unsigned i
447447
std::list<CTransaction> dummy;
448448
remove(tx, dummy, false);
449449
removeConflicts(tx, conflicts);
450+
ClearPrioritisation(tx.GetHash());
450451
}
451452
}
452453

@@ -564,6 +565,34 @@ CTxMemPool::ReadFeeEstimates(CAutoFile& filein)
564565
return true;
565566
}
566567

568+
void CTxMemPool::PrioritiseTransaction(const uint256 hash, const string strHash, double dPriorityDelta, int64_t nFeeDelta)
569+
{
570+
{
571+
LOCK(cs);
572+
std::pair<double, int64_t> &deltas = mapDeltas[hash];
573+
deltas.first += dPriorityDelta;
574+
deltas.second += nFeeDelta;
575+
}
576+
LogPrintf("PrioritiseTransaction: %s priority += %f, fee += %d\n", strHash.c_str(), dPriorityDelta, nFeeDelta);
577+
}
578+
579+
void CTxMemPool::ApplyDeltas(const uint256 hash, double &dPriorityDelta, int64_t &nFeeDelta)
580+
{
581+
LOCK(cs);
582+
std::map<uint256, std::pair<double, int64_t> >::iterator pos = mapDeltas.find(hash);
583+
if (pos == mapDeltas.end())
584+
return;
585+
const std::pair<double, int64_t> &deltas = pos->second;
586+
dPriorityDelta += deltas.first;
587+
nFeeDelta += deltas.second;
588+
}
589+
590+
void CTxMemPool::ClearPrioritisation(const uint256 hash)
591+
{
592+
LOCK(cs);
593+
mapDeltas.erase(hash);
594+
}
595+
567596

568597
CCoinsViewMemPool::CCoinsViewMemPool(CCoinsView &baseIn, CTxMemPool &mempoolIn) : CCoinsViewBacked(baseIn), mempool(mempoolIn) { }
569598

src/txmempool.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class CTxMemPool
7171
mutable CCriticalSection cs;
7272
std::map<uint256, CTxMemPoolEntry> mapTx;
7373
std::map<COutPoint, CInPoint> mapNextTx;
74+
std::map<uint256, std::pair<double, int64_t> > mapDeltas;
7475

7576
CTxMemPool();
7677
~CTxMemPool();
@@ -95,6 +96,11 @@ class CTxMemPool
9596
unsigned int GetTransactionsUpdated() const;
9697
void AddTransactionsUpdated(unsigned int n);
9798

99+
/** Affect CreateNewBlock prioritisation of transactions */
100+
void PrioritiseTransaction(const uint256 hash, const std::string strHash, double dPriorityDelta, int64_t nFeeDelta);
101+
void ApplyDeltas(const uint256 hash, double &dPriorityDelta, int64_t &nFeeDelta);
102+
void ClearPrioritisation(const uint256 hash);
103+
98104
unsigned long size()
99105
{
100106
LOCK(cs);

0 commit comments

Comments
 (0)