finish implementing supports in the claim trie

This commit is contained in:
Jimmy Kiselak 2015-09-27 18:24:46 -04:00
parent 2dcc6a989e
commit 6557635e16
3 changed files with 59 additions and 14 deletions

View file

@ -112,8 +112,27 @@ bool CClaimTrieNode::haveValue(const uint256& txhash, uint32_t nOut) const
return false;
}
void CClaimTrieNode::reorderValues()
void CClaimTrieNode::reorderValues(supportMapNodeType& supports)
{
std::vector<CNodeValue>::iterator itVal;
for (itVal = values.begin(); itVal != values.end(); ++itVal)
{
itVal->nEffectiveAmount = itVal->nAmount;
}
for (supportMapNodeType::iterator itSupport = supports.begin(); itSupport != supports.end(); ++itSupport)
{
for (itVal = values.begin(); itVal != values.end(); ++itVal)
{
if (itSupport->supportTxhash == itVal->txhash && itSupport->supportnOut == itVal->nOut)
{
itVal->nEffectiveAmount += itSupport->nAmount;
break;
}
}
}
std::make_heap(values.begin(), values.end());
}
@ -920,7 +939,9 @@ bool CClaimTrieCache::insertClaimIntoTrie(const std::string name, CNodeValue val
{
CNodeValue currentTop = currentNode->values.front();
currentNode->insertValue(val);
currentNode->reorderValues();
supportMapNodeType node;
getSupportsForName(name, node);
currentNode->reorderValues(node);
if (currentTop != currentNode->values.front())
fChanged = true;
}
@ -990,7 +1011,9 @@ bool CClaimTrieCache::removeClaimFromTrie(const std::string name, uint256 txhash
if (!currentNode->values.empty())
{
currentNode->reorderValues();
supportMapNodeType node;
getSupportsForName(name, node);
currentNode->reorderValues(node);
if (currentTop != currentNode->values.front())
fChanged = true;
}
@ -1312,8 +1335,10 @@ bool CClaimTrieCache::reorderTrieNode(const std::string name) const
else
{
CNodeValue currentTop = cachedNode->second->values.front();
cachedNode->second->reorderValues();
if (currentTop != cachedNode->second->values.front())
supportMapNodeType node;
getSupportsForName(name, node);
cachedNode->second->reorderValues(node);
if (cachedNode->second->values.front() != currentTop)
fChanged = true;
}
if (fChanged)
@ -1328,6 +1353,21 @@ bool CClaimTrieCache::reorderTrieNode(const std::string name) const
return true;
}
bool CClaimTrieCache::getSupportsForName(const std::string name, supportMapNodeType& node) const
{
supportMapType::iterator cachedNode;
cachedNode = supportCache.find(name);
if (cachedNode != supportCache.end())
{
node = cachedNode->second;
return true;
}
else
{
return base->getSupportNode(name, node);
}
}
bool CClaimTrieCache::insertSupportIntoMap(const std::string name, CSupportNodeValue val) const
{
supportMapType::iterator cachedNode;
@ -1432,8 +1472,7 @@ bool CClaimTrieCache::removeSupportFromQueue(const std::string name, uint256 txh
bool CClaimTrieCache::addSupport(const std::string name, uint256 txhash, uint32_t nOut, CAmount nAmount, uint256 supportedTxhash, int supportednOut, int nHeight) const
{
std::cout << "just adding some support" << std::endl;
LogPrintf("%s: name: %s, txhash: %s, nOut: %d, nAmount: %d, supportedTxhash: %s, supportednOut: %d, nHeight: %d, nCurrentHeight: %d\n", __func__, name, txhash.GetHex(), nOut, nAmount, supportedTxhash.GetHex(), supportednOut, nHeight);
LogPrintf("%s: name: %s, txhash: %s, nOut: %d, nAmount: %d, supportedTxhash: %s, supportednOut: %d, nHeight: %d, nCurrentHeight: %d\n", __func__, name, txhash.GetHex(), nOut, nAmount, supportedTxhash.GetHex(), supportednOut, nHeight, nCurrentHeight);
assert(nHeight == nCurrentHeight);
CNodeValue val;
if (base->getInfoForName(name, val))

View file

@ -31,10 +31,11 @@ public:
uint256 txhash;
uint32_t nOut;
CAmount nAmount;
CAmount nEffectiveAmount;
int nHeight;
int nValidAtHeight;
CNodeValue() {};
CNodeValue(uint256 txhash, uint32_t nOut, CAmount nAmount, int nHeight, int nValidAtHeight) : txhash(txhash), nOut(nOut), nAmount(nAmount), nHeight(nHeight), nValidAtHeight(nValidAtHeight) {}
CNodeValue(uint256 txhash, uint32_t nOut, CAmount nAmount, int nHeight, int nValidAtHeight) : txhash(txhash), nOut(nOut), nAmount(nAmount), nEffectiveAmount(nAmount), nHeight(nHeight), nValidAtHeight(nValidAtHeight) {}
uint256 GetHash() const;
ADD_SERIALIZE_METHODS;
@ -49,9 +50,9 @@ public:
bool operator<(const CNodeValue& other) const
{
if (nAmount < other.nAmount)
if (nEffectiveAmount < other.nEffectiveAmount)
return true;
else if (nAmount == other.nAmount)
else if (nEffectiveAmount == other.nEffectiveAmount)
{
if (nHeight > other.nHeight)
return true;
@ -114,6 +115,8 @@ public:
class CClaimTrieNode;
class CClaimTrie;
typedef std::vector<CSupportNodeValue> supportMapNodeType;
typedef std::map<unsigned char, CClaimTrieNode*> nodeMapType;
typedef std::pair<std::string, CClaimTrieNode> namedNodeType;
@ -131,7 +134,7 @@ public:
bool getBestValue(CNodeValue& val) const;
bool empty() const {return children.empty() && values.empty();}
bool haveValue(const uint256& txhash, uint32_t nOut) const;
void reorderValues();
void reorderValues(supportMapNodeType& supports);
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
@ -195,7 +198,6 @@ public:
}
};
typedef std::vector<CSupportNodeValue> supportMapNodeType;
typedef std::map<std::string, supportMapNodeType> supportMapType;
typedef std::map<int, std::vector<CValueQueueEntry> > valueQueueType;
@ -328,6 +330,7 @@ private:
bool addSupportToQueue(const std::string name, uint256 txhash, uint32_t nOut, CAmount nAmount, uint256 supportedTxhash, int supportednOut, int nHeight, int nValidAtHeight) const;
bool removeSupportFromQueue(const std::string name, uint256 txhash, uint32_t nOut, uint256 supportedTxhash, int supportednOut, int nHeightToCheck, int& nValidAtHeight) const;
uint256 hashBlock;
bool getSupportsForName(const std::string name, supportMapNodeType& node) const;
};
#endif // BITCOIN_ClaimTRIE_H

View file

@ -1296,14 +1296,17 @@ BOOST_AUTO_TEST_CASE(claimtrie_supporting_claims)
CMutableTransaction tx1 = BuildTransaction(coinbases[0]);
tx1.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName << vchValue1 << OP_2DROP << OP_DROP << OP_TRUE;
tx1.vout[0].nValue = 100000000;
CMutableTransaction tx2 = BuildTransaction(coinbases[1]);
tx2.vout[0].scriptPubKey = CScript() << OP_CLAIM_NAME << vchName << vchValue2 << OP_2DROP << OP_DROP << OP_TRUE;
tx2.vout[0].nValue = 500000000;
CMutableTransaction tx3 = BuildTransaction(coinbases[2]);
std::string sTx1Hash = tx1.GetHash().ToString();
std::vector<unsigned char> vchTx1Hash(sTx1Hash.begin(), sTx1Hash.end());
uint256 tx1Hash = tx1.GetHash();
std::vector<unsigned char> vchTx1Hash(tx1Hash.begin(), tx1Hash.end());
tx3.vout[0].scriptPubKey = CScript() << OP_SUPPORT_CLAIM << vchName << vchTx1Hash << CScriptNum(0) << OP_2DROP << OP_2DROP << OP_TRUE;
tx3.vout[0].nValue = 500000000;
CNodeValue val;
std::vector<uint256> blocks_to_invalidate;