#include "util.h"
#include "utilmoneystr.h"
#include "version.h"
+#define _COINBASE_MATURITY 100
using namespace std;
LOCK(cs);
mapTx.insert(entry);
const CTransaction& tx = mapTx.find(hash)->GetTx();
- for (unsigned int i = 0; i < tx.vin.size(); i++)
- mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
+ if (!tx.IsCoinImport()) {
+ for (unsigned int i = 0; i < tx.vin.size(); i++)
+ mapNextTx[tx.vin[i].prevout] = CInPoint(&tx, i);
+ }
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
mapSproutNullifiers[nf] = &tx;
return true;
}
+void CTxMemPool::addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
+{
+ LOCK(cs);
+ const CTransaction& tx = entry.GetTx();
+ std::vector<CMempoolAddressDeltaKey> inserted;
+
+ uint256 txhash = tx.GetHash();
+ for (unsigned int j = 0; j < tx.vin.size(); j++) {
+ const CTxIn input = tx.vin[j];
+ const CTxOut &prevout = view.GetOutputFor(input);
+ if (prevout.scriptPubKey.IsPayToScriptHash()) {
+ vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22);
+ CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, j, 1);
+ CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
+ mapAddress.insert(make_pair(key, delta));
+ inserted.push_back(key);
+ }
+ else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
+ vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23);
+ CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, j, 1);
+ CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
+ mapAddress.insert(make_pair(key, delta));
+ inserted.push_back(key);
+ }
+ else if (prevout.scriptPubKey.IsPayToPublicKey()) {
+ vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.begin()+34);
+ CMempoolAddressDeltaKey key(1, Hash160(hashBytes), txhash, j, 1);
+ CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
+ mapAddress.insert(make_pair(key, delta));
+ inserted.push_back(key);
+ }
+ else if (prevout.scriptPubKey.IsPayToCryptoCondition()) {
+ vector<unsigned char> hashBytes(prevout.scriptPubKey.begin(), prevout.scriptPubKey.end());
+ CMempoolAddressDeltaKey key(1, Hash160(hashBytes), txhash, j, 1);
+ CMempoolAddressDelta delta(entry.GetTime(), prevout.nValue * -1, input.prevout.hash, input.prevout.n);
+ mapAddress.insert(make_pair(key, delta));
+ inserted.push_back(key);
+ } }
+
+ for (unsigned int k = 0; k < tx.vout.size(); k++) {
+ const CTxOut &out = tx.vout[k];
+ if (out.scriptPubKey.IsPayToScriptHash()) {
+ vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
+ CMempoolAddressDeltaKey key(2, uint160(hashBytes), txhash, k, 0);
+ mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
+ inserted.push_back(key);
+ }
+ else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
+ vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
+ std::pair<addressDeltaMap::iterator,bool> ret;
+ CMempoolAddressDeltaKey key(1, uint160(hashBytes), txhash, k, 0);
+ mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
+ inserted.push_back(key);
+ }
+ else if (out.scriptPubKey.IsPayToPublicKey()) {
+ vector<unsigned char> hashBytes(out.scriptPubKey.begin()+1, out.scriptPubKey.begin()+34);
+ std::pair<addressDeltaMap::iterator,bool> ret;
+ CMempoolAddressDeltaKey key(1, Hash160(hashBytes), txhash, k, 0);
+ mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
+ inserted.push_back(key);
+ }
+ else if (out.scriptPubKey.IsPayToCryptoCondition()) {
+ vector<unsigned char> hashBytes(out.scriptPubKey.begin(), out.scriptPubKey.end());
+ std::pair<addressDeltaMap::iterator,bool> ret;
+ CMempoolAddressDeltaKey key(1, Hash160(hashBytes), txhash, k, 0);
+ mapAddress.insert(make_pair(key, CMempoolAddressDelta(entry.GetTime(), out.nValue)));
+ inserted.push_back(key);
+ }
+ }
+
+ mapAddressInserted.insert(make_pair(txhash, inserted));
+}
+
+bool CTxMemPool::getAddressIndex(std::vector<std::pair<uint160, int> > &addresses,
+ std::vector<std::pair<CMempoolAddressDeltaKey, CMempoolAddressDelta> > &results)
+{
+ LOCK(cs);
+ for (std::vector<std::pair<uint160, int> >::iterator it = addresses.begin(); it != addresses.end(); it++) {
+ addressDeltaMap::iterator ait = mapAddress.lower_bound(CMempoolAddressDeltaKey((*it).second, (*it).first));
+ while (ait != mapAddress.end() && (*ait).first.addressBytes == (*it).first && (*ait).first.type == (*it).second) {
+ results.push_back(*ait);
+ ait++;
+ }
+ }
+ return true;
+}
+
+bool CTxMemPool::removeAddressIndex(const uint256 txhash)
+{
+ LOCK(cs);
+ addressDeltaMapInserted::iterator it = mapAddressInserted.find(txhash);
+
+ if (it != mapAddressInserted.end()) {
+ std::vector<CMempoolAddressDeltaKey> keys = (*it).second;
+ for (std::vector<CMempoolAddressDeltaKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
+ mapAddress.erase(*mit);
+ }
+ mapAddressInserted.erase(it);
+ }
+
+ return true;
+}
+
+void CTxMemPool::addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
+{
+ LOCK(cs);
+
+ const CTransaction& tx = entry.GetTx();
+ std::vector<CSpentIndexKey> inserted;
+
+ uint256 txhash = tx.GetHash();
+ for (unsigned int j = 0; j < tx.vin.size(); j++) {
+ const CTxIn input = tx.vin[j];
+ const CTxOut &prevout = view.GetOutputFor(input);
+ uint160 addressHash;
+ int addressType;
+
+ if (prevout.scriptPubKey.IsPayToScriptHash()) {
+ addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22));
+ addressType = 2;
+ }
+ else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
+ addressHash = uint160(vector<unsigned char> (prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23));
+ addressType = 1;
+ }
+ else if (prevout.scriptPubKey.IsPayToPublicKey()) {
+ addressHash = Hash160(vector<unsigned char> (prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.begin()+34));
+ addressType = 1;
+ }
+ else if (prevout.scriptPubKey.IsPayToCryptoCondition()) {
+ addressHash = Hash160(vector<unsigned char> (prevout.scriptPubKey.begin(), prevout.scriptPubKey.end()));
+ addressType = 1;
+ }
+ else {
+ addressHash.SetNull();
+ addressType = 0;
+ }
+
+ CSpentIndexKey key = CSpentIndexKey(input.prevout.hash, input.prevout.n);
+ CSpentIndexValue value = CSpentIndexValue(txhash, j, -1, prevout.nValue, addressType, addressHash);
+
+ mapSpent.insert(make_pair(key, value));
+ inserted.push_back(key);
+
+ }
+
+ mapSpentInserted.insert(make_pair(txhash, inserted));
+}
+
+bool CTxMemPool::getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
+{
+ LOCK(cs);
+ mapSpentIndex::iterator it;
+
+ it = mapSpent.find(key);
+ if (it != mapSpent.end()) {
+ value = it->second;
+ return true;
+ }
+ return false;
+}
+
+bool CTxMemPool::removeSpentIndex(const uint256 txhash)
+{
+ LOCK(cs);
+ mapSpentIndexInserted::iterator it = mapSpentInserted.find(txhash);
+
+ if (it != mapSpentInserted.end()) {
+ std::vector<CSpentIndexKey> keys = (*it).second;
+ for (std::vector<CSpentIndexKey>::iterator mit = keys.begin(); mit != keys.end(); mit++) {
+ mapSpent.erase(*mit);
+ }
+ mapSpentInserted.erase(it);
+ }
+
+ return true;
+}
void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& removed, bool fRecursive)
{
mapTx.erase(hash);
nTransactionsUpdated++;
minerPolicyEstimator->removeTx(hash);
+ removeAddressIndex(hash);
+ removeSpentIndex(hash);
}
}
}
+extern uint64_t ASSETCHAINS_TIMELOCKGTE;
+int64_t komodo_block_unlocktime(uint32_t nHeight);
+
void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
{
+ // Remove transactions spending a coinbase which are now immature
+ extern char ASSETCHAINS_SYMBOL[KOMODO_ASSETCHAIN_MAXLEN];
+ if ( ASSETCHAINS_SYMBOL[0] == 0 )
+ COINBASE_MATURITY = _COINBASE_MATURITY;
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
LOCK(cs);
list<CTransaction> transactionsToRemove;
if (it2 != mapTx.end())
continue;
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
- if (nCheckFrequency != 0) assert(coins);
- if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
+ if (nCheckFrequency != 0) assert(coins);
+ if (!coins || (coins->IsCoinBase() && (((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY) &&
+ ((signed long)nMemPoolHeight < komodo_block_unlocktime(coins->nHeight) &&
+ coins->IsAvailable(0) && coins->vout[0].nValue >= ASSETCHAINS_TIMELOCKGTE))) {
transactionsToRemove.push_back(tx);
break;
}
}
break;
default:
- throw runtime_error("Unknown shielded type " + type);
+ throw runtime_error("Unknown shielded type");
break;
}
}
}
}
+int32_t komodo_validate_interest(const CTransaction &tx,int32_t txheight,uint32_t nTime,int32_t dispflag);
+extern char ASSETCHAINS_SYMBOL[];
+
void CTxMemPool::removeExpired(unsigned int nBlockHeight)
{
+ CBlockIndex *tipindex;
// Remove expired txs from the mempool
LOCK(cs);
list<CTransaction> transactionsToRemove;
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++)
{
const CTransaction& tx = it->GetTx();
- if (IsExpiredTx(tx, nBlockHeight)) {
+ tipindex = chainActive.LastTip();
+ if (IsExpiredTx(tx, nBlockHeight) || (ASSETCHAINS_SYMBOL[0] == 0 && tipindex != 0 && komodo_validate_interest(tx,tipindex->GetHeight()+1,tipindex->GetMedianTimePast() + 777,0)) < 0)
+ {
transactionsToRemove.push_back(tx);
}
}
i++;
}
- boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
+ boost::unordered_map<uint256, SproutMerkleTree, CCoinsKeyHasher> intermediates;
BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
assert(!pcoins->GetNullifier(nf, SPROUT));
}
- ZCIncrementalMerkleTree tree;
+ SproutMerkleTree tree;
auto it = intermediates.find(joinsplit.anchor);
if (it != intermediates.end()) {
tree = it->second;
intermediates.insert(std::make_pair(tree.root(), tree));
}
for (const SpendDescription &spendDescription : tx.vShieldedSpend) {
- ZCSaplingIncrementalMerkleTree tree;
+ SaplingMerkleTree tree;
assert(pcoins->GetSaplingAnchorAt(spendDescription.anchor, tree));
assert(!pcoins->GetNullifier(spendDescription.nullifier, SAPLING));