Remove unnecessary height field in removeClaim method

This commit is contained in:
Ariel Rodriguez 2018-11-08 19:29:28 -03:00 committed by Brannon King
parent cccdce02df
commit 15be5dacce
6 changed files with 138 additions and 123 deletions

View file

@ -144,7 +144,8 @@ def main():
command.extend(lines)
command.extend(['-style=file', '-fallback-style=none'])
p = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=None, stdin=subprocess.PIPE)
stderr=None,
stdin=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
sys.exit(p.returncode);

View file

@ -1653,20 +1653,20 @@ bool CClaimTrieCache::removeClaimFromQueue(const std::string& name, const COutPo
return false;
}
bool CClaimTrieCache::undoAddClaim(const std::string& name, const COutPoint& outPoint, int nHeight) const
bool CClaimTrieCache::undoAddClaim(const std::string& name, const COutPoint& outPoint) const
{
int throwaway;
return removeClaim(name, outPoint, nHeight, throwaway, false);
return removeClaim(name, outPoint, throwaway, false);
}
bool CClaimTrieCache::spendClaim(const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight) const
bool CClaimTrieCache::spendClaim(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight) const
{
return removeClaim(name, outPoint, nHeight, nValidAtHeight, true);
return removeClaim(name, outPoint, nValidAtHeight, true);
}
bool CClaimTrieCache::removeClaim(const std::string& name, const COutPoint& outPoint, int nHeight, int& nValidAtHeight, bool fCheckTakeover) const
bool CClaimTrieCache::removeClaim(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight, bool fCheckTakeover) const
{
LogPrintf("%s: name: %s, txhash: %s, nOut: %s, nHeight: %s, nCurrentHeight: %s\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, nHeight, nCurrentHeight);
LogPrintf("%s: name: %s, txhash: %s, nOut: %s, nCurrentHeight: %s\n", __func__, name, outPoint.hash.GetHex(), outPoint.n, nCurrentHeight);
bool removed = false;
CClaimValue claim;
if (removeClaimFromQueue(name, outPoint, claim))
@ -1680,7 +1680,7 @@ bool CClaimTrieCache::removeClaim(const std::string& name, const COutPoint& outP
if (removed == true)
{
nValidAtHeight = claim.nValidAtHeight;
int expirationHeight = nHeight + base->nExpirationTime;
int expirationHeight = claim.nHeight + base->nExpirationTime;
removeFromExpirationQueue(name, outPoint, expirationHeight);
claimsToDelete.insert(claim);
}

View file

@ -212,7 +212,6 @@ struct nameOutPointHeightType
nameOutPointHeightType(std::string name, COutPoint outPoint, int nHeight)
: name(name), outPoint(outPoint), nHeight(nHeight) {}
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
@ -323,6 +322,7 @@ public:
bool getLastTakeoverForName(const std::string& name, int& lastTakeoverHeight) const;
claimsForNameType getClaimsForName(const std::string& name) const;
CAmount getEffectiveAmountForClaim(const std::string& name, const uint160& claimId, std::vector<CSupportValue>* supports = NULL) const;
CAmount getEffectiveAmountForClaim(const claimsForNameType& claims, const uint160& claimId, std::vector<CSupportValue>* supports = NULL) const;
@ -484,10 +484,8 @@ public:
bool addClaim(const std::string& name, const COutPoint& outPoint,
uint160 claimId, CAmount nAmount, int nHeight) const;
bool undoAddClaim(const std::string& name, const COutPoint& outPoint,
int nHeight) const;
bool spendClaim(const std::string& name, const COutPoint& outPoint,
int nHeight, int& nValidAtHeight) const;
bool undoAddClaim(const std::string& name, const COutPoint& outPoint) const;
bool spendClaim(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight) const;
bool undoSpendClaim(const std::string& name, const COutPoint& outPoint,
uint160 claimId, CAmount nAmount, int nHeight,
int nValidAtHeight) const;
@ -578,9 +576,7 @@ protected:
bool clear() const;
bool removeClaim(const std::string& name, const COutPoint& outPoint,
int nHeight, int& nValidAtHeight, bool fCheckTakeover) const;
bool removeClaim(const std::string& name, const COutPoint& outPoint, int& nValidAtHeight, bool fCheckTakeover) const;
bool addClaimToQueues(const std::string& name, CClaimValue& claim) const;
bool removeClaimFromQueue(const std::string& name, const COutPoint& outPoint,
CClaimValue& claim) const;

View file

@ -969,7 +969,6 @@ bool CheckTransaction(const CTransaction& tx, CValidationState &state)
return state.DoS(100, false, REJECT_INVALID, "bad-txns-claimscriptsize-toolarge");
if (ClaimNameSize(txout.scriptPubKey) > MAX_CLAIM_NAME_SIZE)
return state.DoS(100, false, REJECT_INVALID, "bad-txns-claimscriptname-toolarge");
}
// Check for duplicate inputs
@ -2194,8 +2193,7 @@ bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockI
}
std::string name(vvchParams[0].begin(), vvchParams[0].end());
LogPrintf("%s: (txid: %s, nOut: %d) Trying to remove %s from the claim trie due to its block being disconnected\n", __func__, hash.ToString(), i, name.c_str());
if (!trieCache.undoAddClaim(name, COutPoint(hash, i), pindex->nHeight))
{
if (!trieCache.undoAddClaim(name, COutPoint(hash, i))) {
LogPrintf("%s: Could not find the claim in the trie or the cache\n", __func__);
}
}
@ -2625,8 +2623,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
std::string name(vvchParams[0].begin(), vvchParams[0].end());
int nValidAtHeight;
LogPrintf("%s: Removing %s from the claim trie. Tx: %s, nOut: %d\n", __func__, name, txin.prevout.hash.GetHex(), txin.prevout.n);
if (trieCache.spendClaim(name, COutPoint(txin.prevout.hash, txin.prevout.n), coins->nHeight, nValidAtHeight))
{
if (trieCache.spendClaim(name, COutPoint(txin.prevout.hash, txin.prevout.n), nValidAtHeight)) {
mClaimUndoHeights[i] = nValidAtHeight;
std::pair<std::string, uint160> entry(name, claimId);
spentClaims.push_back(entry);
@ -4219,7 +4216,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
// check level 1: verify block validity
if (nCheckLevel >= 1 && !CheckBlock(block, state))
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
// check level 2: verify undo validity
if (nCheckLevel >= 2 && pindex) {
CBlockUndo undo;

View file

@ -294,8 +294,7 @@ CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& s
}
std::string name(vvchParams[0].begin(), vvchParams[0].end());
int throwaway;
if (trieCache.spendClaim(name, COutPoint(txin.prevout.hash, txin.prevout.n), nTxinHeight, throwaway))
{
if (trieCache.spendClaim(name, COutPoint(txin.prevout.hash, txin.prevout.n), throwaway)) {
std::pair<std::string, uint160> entry(name, claimId);
spentClaims.push_back(entry);
}

View file

@ -30,13 +30,13 @@ is_claim_in_queue(std::string name, const CTransaction &tx)
{
COutPoint outPoint(tx.GetHash(), 0);
int validAtHeight;
bool have_claim = pclaimTrie->haveClaimInQueue(name,outPoint,validAtHeight);
bool have_claim = pclaimTrie->haveClaimInQueue(name, outPoint, validAtHeight);
if (have_claim){
return true;
}
else{
boost::test_tools::predicate_result res(false);
res.message()<<"Is not a claim in queue.";
res.message() << "Is not a claim in queue.";
return res;
}
}
@ -47,14 +47,14 @@ is_best_claim(std::string name, const CTransaction &tx)
{
CClaimValue val;
COutPoint outPoint(tx.GetHash(), 0);
bool have_claim = pclaimTrie->haveClaim(name,outPoint);
bool have_claim = pclaimTrie->haveClaim(name, outPoint);
bool have_info = pclaimTrie->getInfoForName(name, val);
if (have_claim && have_info && val.outPoint == outPoint){
return true;
}
else{
boost::test_tools::predicate_result res(false);
res.message()<<"Is not best claim";
res.message() << "Is not best claim";
return res;
}
}
@ -209,7 +209,7 @@ struct ClaimTrieChainFixture{
CMutableTransaction Spend(const CTransaction &prev){
uint32_t prevout = 0;
CMutableTransaction tx = BuildTransaction(prev,prevout);
CMutableTransaction tx = BuildTransaction(prev, prevout);
tx.vout[0].scriptPubKey = CScript() << OP_TRUE;
tx.vout[0].nValue = 1;
@ -218,13 +218,12 @@ struct ClaimTrieChainFixture{
}
//make claim at the current block
CMutableTransaction MakeClaim(const CTransaction &prev, std::string name, std::string value,
CAmount quantity)
CMutableTransaction MakeClaim(const CTransaction& prev, std::string name, std::string value, CAmount quantity)
{
uint32_t prevout = 0;
CMutableTransaction tx = BuildTransaction(prev,prevout);
tx.vout[0].scriptPubKey = ClaimNameScript(name,value);
tx.vout[0].scriptPubKey = ClaimNameScript(name, value);
tx.vout[0].nValue = quantity;
CommitTx(tx);
@ -243,7 +242,7 @@ struct ClaimTrieChainFixture{
uint32_t prevout = 0;
CMutableTransaction tx = BuildTransaction(prev,prevout);
tx.vout[0].scriptPubKey = SupportClaimScript(name,claimId);
tx.vout[0].scriptPubKey = SupportClaimScript(name, claimId);
tx.vout[0].nValue = quantity;
CommitTx(tx);
@ -257,7 +256,7 @@ struct ClaimTrieChainFixture{
uint32_t prevout = 0;
CMutableTransaction tx = BuildTransaction(prev,prevout);
tx.vout[0].scriptPubKey = UpdateClaimScript(name,claimId,value);
tx.vout[0].scriptPubKey = UpdateClaimScript(name, claimId, value);
tx.vout[0].nValue = quantity;
CommitTx(tx);
@ -374,16 +373,16 @@ BOOST_AUTO_TEST_CASE(claim_test)
CMutableTransaction tx5 = fixture.MakeClaim(fixture.GetCoinbase(),"test","two",1);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_claim_in_queue("test",tx5));
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(is_best_claim("test", tx4));
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(is_best_claim("test", tx4));
BOOST_CHECK(is_claim_in_queue("test",tx5));
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(is_best_claim("test", tx4));
fixture.DecrementBlocks(1);
// check claim takeover, note that CClaimTrie.nProportionalDelayFactor is set to 1
@ -401,7 +400,7 @@ BOOST_AUTO_TEST_CASE(claim_test)
fixture.DecrementBlocks(10);
BOOST_CHECK(is_claim_in_queue("test",tx7));
BOOST_CHECK(is_best_claim("test",tx6));
BOOST_CHECK(is_best_claim("test", tx6));
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx6));
fixture.DecrementBlocks(10);
@ -421,13 +420,13 @@ BOOST_AUTO_TEST_CASE(spend_claim_test)
CMutableTransaction tx1 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
CMutableTransaction tx2 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx1));
BOOST_CHECK(is_best_claim("test", tx1));
fixture.Spend(tx1);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx2));
BOOST_CHECK(is_best_claim("test", tx2));
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx1));
BOOST_CHECK(is_best_claim("test", tx1));
fixture.DecrementBlocks(1);
@ -489,7 +488,7 @@ BOOST_AUTO_TEST_CASE(support_test)
fixture.IncrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
CMutableTransaction s4 = fixture.MakeSupport(fixture.GetCoinbase(),tx3,"test",10); //10 delay
CMutableTransaction s4 = fixture.MakeSupport(fixture.GetCoinbase(), tx3, "test", 10); //10 delay
fixture.IncrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
@ -501,7 +500,7 @@ BOOST_AUTO_TEST_CASE(support_test)
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
fixture.DecrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(is_best_claim("test", tx4));
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
fixture.DecrementBlocks(10);
}
@ -582,7 +581,7 @@ BOOST_AUTO_TEST_CASE(support_spend_test)
CMutableTransaction tx2 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
CMutableTransaction s1 = fixture.MakeSupport(fixture.GetCoinbase(),tx1,"test",2);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx1));
BOOST_CHECK(is_best_claim("test", tx1));
CMutableTransaction sp1 = fixture.Spend(s1);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx2));
@ -622,7 +621,7 @@ BOOST_AUTO_TEST_CASE(support_spend_test)
fixture.CommitTx(tx);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx4));
BOOST_CHECK(is_best_claim("test", tx4));
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx5));
}
@ -641,7 +640,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test)
//update preserves claim id
ClaimTrieChainFixture fixture;
CMutableTransaction tx1 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
CMutableTransaction u1 = fixture.MakeUpdate(tx1,"test","one",ClaimIdHash(tx1.GetHash(),0),2);
CMutableTransaction u1 = fixture.MakeUpdate(tx1, "test", "one", ClaimIdHash(tx1.GetHash(), 0), 2);
fixture.IncrementBlocks(1);
CClaimValue val;
pclaimTrie->getInfoForName("test",val);
@ -651,8 +650,8 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test)
// update preserves supports
CMutableTransaction tx2 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1);
CMutableTransaction s1 = fixture.MakeSupport(fixture.GetCoinbase(),tx2,"test",1);
CMutableTransaction u2 = fixture.MakeUpdate(tx2,"test","one",ClaimIdHash(tx2.GetHash(),0),1);
CMutableTransaction s1 = fixture.MakeSupport(fixture.GetCoinbase(), tx2, "test", 1);
CMutableTransaction u2 = fixture.MakeUpdate(tx2, "test", "one", ClaimIdHash(tx2.GetHash(), 0), 1);
fixture.IncrementBlocks(1);
BOOST_CHECK(best_claim_effective_amount_equals("test",2));
fixture.DecrementBlocks(1);
@ -661,7 +660,7 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test)
CMutableTransaction tx3 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
CMutableTransaction tx4 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1);
fixture.IncrementBlocks(10);
CMutableTransaction u3 = fixture.MakeUpdate(tx3,"test","one",ClaimIdHash(tx3.GetHash(),0),2);
CMutableTransaction u3 = fixture.MakeUpdate(tx3, "test", "one", ClaimIdHash(tx3.GetHash(), 0), 2);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",u3));
BOOST_CHECK_EQUAL(2U, pclaimTrie->getClaimsForName("test").claims.size());
@ -678,14 +677,14 @@ BOOST_AUTO_TEST_CASE(claimtrie_update_test)
BOOST_CHECK(is_best_claim("test",tx6));
fixture.DecrementBlocks(1);
BOOST_CHECK(is_best_claim("test",tx5));
BOOST_CHECK(is_best_claim("test", tx5));
fixture.DecrementBlocks(10);
// update on losing claim happens with delay , and wins
CMutableTransaction tx7 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",3);
CMutableTransaction tx8 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",2);
fixture.IncrementBlocks(10);
BOOST_CHECK(is_best_claim("test",tx7));
BOOST_CHECK(is_best_claim("test", tx7));
CMutableTransaction tx;
tx.nVersion = 1;
@ -977,7 +976,7 @@ BOOST_AUTO_TEST_CASE(hardfork_support_test)
ClaimTrieChainFixture fixture;
int blocks_before_fork = 10;
fixture.IncrementBlocks(fixture.expirationForkHeight - chainActive.Height() - blocks_before_fork-1);
fixture.IncrementBlocks(fixture.expirationForkHeight - chainActive.Height() - blocks_before_fork - 1);
// Create claim and support it before the fork height
CMutableTransaction tx1 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1);
CMutableTransaction s1 = fixture.MakeSupport(fixture.GetCoinbase(),tx1,"test",2);
@ -1900,6 +1899,29 @@ BOOST_AUTO_TEST_CASE(claimtrienode_serialize_unserialize)
BOOST_CHECK(n1 == n2);
}
BOOST_AUTO_TEST_CASE(claimtrienode_remove_invalid_claim)
{
uint160 hash160;
CClaimTrieNode n1;
CClaimTrieNode n2;
CClaimValue throwaway;
CClaimValue v1(COutPoint(uint256S("0000000000000000000000000000000000000000000000000000000000000001"), 0), hash160, 50, 0, 100);
CClaimValue v2(COutPoint(uint256S("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"), 1), hash160, 100, 1, 101);
n1.insertClaim(v1);
n2.insertClaim(v2);
bool invalidClaim = n2.removeClaim(v1.outPoint, throwaway);
BOOST_CHECK(invalidClaim == false);
invalidClaim = n1.removeClaim(v2.outPoint, throwaway);
BOOST_CHECK(invalidClaim == false);
}
BOOST_AUTO_TEST_CASE(invalid_claimid_test)
{
ClaimTrieChainFixture fixture;