Implement a hard fork for extended claim expiration times. Added unit tests for extended claim expiration.

This commit is contained in:
lbrynaut 2018-03-21 12:20:49 -05:00 committed by Umpei Kay Kurokawa
parent cd10b981b1
commit dc34766423
7 changed files with 178 additions and 9 deletions

View file

@ -131,6 +131,9 @@ public:
consensus.powLimit = uint256S("0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 150; //retarget every block
consensus.nPowTargetSpacing = 150;
consensus.nOriginalClaimExpirationTime = 262974;
consensus.nExtendedClaimExpirationTime = 2102400;
consensus.nExtendedClaimExpirationForkHeight = 400155;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016
@ -215,6 +218,9 @@ public:
consensus.powLimit = uint256S("0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 150;
consensus.nPowTargetSpacing = 150;
consensus.nOriginalClaimExpirationTime = 262974;
consensus.nExtendedClaimExpirationTime = 2102400;
consensus.nExtendedClaimExpirationForkHeight = 278160;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains
@ -292,6 +298,9 @@ public:
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 1;//14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 1;
consensus.nOriginalClaimExpirationTime = 500;
consensus.nExtendedClaimExpirationTime = 600;
consensus.nExtendedClaimExpirationForkHeight = 800;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains

View file

@ -218,6 +218,7 @@ bool CClaimTrie::supportQueueEmpty() const
void CClaimTrie::setExpirationTime(int t)
{
nExpirationTime = t;
LogPrintf("%s: Expiration time is now %d\n", __func__, nExpirationTime);
}
void CClaimTrie::clear()
@ -1119,6 +1120,8 @@ bool CClaimTrie::ReadFromDisk(bool check)
LogPrintf("%s: Couldn't read the best block's hash\n", __func__);
if (!db.Read(CURRENT_HEIGHT, nCurrentHeight))
LogPrintf("%s: Couldn't read the current height\n", __func__);
setExpirationTime(Params().GetConsensus().GetExpirationTime(nCurrentHeight-1));
boost::scoped_ptr<CDBIterator> pcursor(const_cast<CDBWrapper*>(&db)->NewIterator());
pcursor->SeekToFirst();
@ -1699,8 +1702,17 @@ bool CClaimTrieCache::removeClaim(const std::string& name, const COutPoint& outP
void CClaimTrieCache::addToExpirationQueue(int nExpirationHeight, nameOutPointType& entry) const
{
expirationQueueType::iterator itQueueRow = getExpirationQueueCacheRow(nExpirationHeight, true);
itQueueRow->second.push_back(entry);
// NOTE: To disable expiration completely after fork, set this define to enable check
// #define CLAIM_EXPIRATION_DISABLED_AFTER_FORK
#ifdef CLAIM_EXPIRATION_DISABLED_AFTER_FORK
const Consensus::Params& consensusParams = Params().GetConsensus();
if ((nExpirationHeight < consensusParams.nExtendedClaimExpirationForkHeight) ||
(base->nCurrentHeight < consensusParams.nExtendedClaimExpirationForkHeight))
#endif
{
expirationQueueType::iterator itQueueRow = getExpirationQueueCacheRow(nExpirationHeight, true);
itQueueRow->second.push_back(entry);
}
}
void CClaimTrieCache::removeFromExpirationQueue(const std::string& name, const COutPoint& outPoint, int nHeight) const
@ -1715,10 +1727,11 @@ void CClaimTrieCache::removeFromExpirationQueue(const std::string& name, const C
if (name == itQueue->name && outPoint == itQueue->outPoint)
break;
}
}
if (itQueue != itQueueRow->second.end())
{
itQueueRow->second.erase(itQueue);
if (itQueue != itQueueRow->second.end())
{
itQueueRow->second.erase(itQueue);
}
}
}
@ -2140,6 +2153,7 @@ bool CClaimTrieCache::incrementBlock(insertUndoType& insertUndo, claimQueueRowTy
CClaimValue claim;
assert(removeClaimFromTrie(itEntry->name, itEntry->outPoint, claim, true));
expireUndo.push_back(std::make_pair(itEntry->name, claim));
LogPrintf("Expiring claim %s: %s, nHeight: %d, nValidAtHeight: %d\n", claim.claimId.GetHex(), itEntry->name, claim.nHeight, claim.nValidAtHeight);
}
itExpirationRow->second.clear();
}
@ -2191,6 +2205,7 @@ bool CClaimTrieCache::incrementBlock(insertUndoType& insertUndo, claimQueueRowTy
CSupportValue support;
assert(removeSupportFromMap(itEntry->name, itEntry->outPoint, support, true));
expireSupportUndo.push_back(std::make_pair(itEntry->name, support));
LogPrintf("Expiring support %s: %s, nHeight: %d, nValidAtHeight: %d\n", support.supportedClaimId.GetHex(), itEntry->name, support.nHeight, support.nValidAtHeight);
}
itSupportExpirationRow->second.clear();
}

View file

@ -6,6 +6,7 @@
#include "uint256.h"
#include "util.h"
#include "dbwrapper.h"
#include "chainparams.h"
#include "primitives/transaction.h"
#include <string>
@ -299,7 +300,7 @@ class CClaimTrie
public:
CClaimTrie(bool fMemory = false, bool fWipe = false, int nProportionalDelayFactor = 32)
: db(GetDataDir() / "claimtrie", 100, fMemory, fWipe, false)
, nCurrentHeight(0), nExpirationTime(262974)
, nCurrentHeight(0), nExpirationTime(Params().GetConsensus().nOriginalClaimExpirationTime)
, nProportionalDelayFactor(nProportionalDelayFactor)
, root(uint256S("0000000000000000000000000000000000000000000000000000000000000001"))
{}

View file

@ -58,6 +58,14 @@ struct Params {
bool fPowNoRetargeting;
int64_t nPowTargetSpacing;
int64_t nPowTargetTimespan;
int64_t nOriginalClaimExpirationTime;
int64_t nExtendedClaimExpirationTime;
int64_t nExtendedClaimExpirationForkHeight;
int64_t GetExpirationTime(int64_t nHeight) const {
return nHeight < nExtendedClaimExpirationForkHeight ?
nOriginalClaimExpirationTime :
nExtendedClaimExpirationTime;
}
int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; }
};
} // namespace Consensus

View file

@ -1229,6 +1229,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
LogPrintf("* Using %.1fMiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
LogPrintf("* Using %.1fMiB for in-memory UTXO set\n", nCoinCacheUsage * (1.0 / 1024 / 1024));
const Consensus::Params& consensusParams = Params().GetConsensus();
bool fLoaded = false;
while (!fLoaded) {
bool fReset = fReindex;
@ -1266,7 +1268,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
// If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around).
if (!mapBlockIndex.empty() && mapBlockIndex.count(chainparams.GetConsensus().hashGenesisBlock) == 0)
if (!mapBlockIndex.empty() && mapBlockIndex.count(consensusParams.hashGenesisBlock) == 0)
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
// Initialize the block index (no-op if non-empty database was already loaded)
@ -1422,6 +1424,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
RandAddSeedPerfmon();
pclaimTrie->setExpirationTime(consensusParams.GetExpirationTime(chainActive.Height()));
//// debug print
LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size());
LogPrintf("nBestHeight = %d\n", chainActive.Height());
@ -1437,7 +1441,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
StartNode(threadGroup, scheduler);
// Monitor the chain, and alert if we get blocks much quicker or slower than expected
int64_t nPowTargetSpacing = Params().GetConsensus().nPowTargetSpacing;
int64_t nPowTargetSpacing = consensusParams.nPowTargetSpacing;
CScheduler::Function f = boost::bind(&PartitionCheck, &IsInitialBlockDownload,
boost::ref(cs_main), boost::cref(pindexBestHeader), nPowTargetSpacing);
scheduler.scheduleEvery(f, nPowTargetSpacing);

View file

@ -2171,12 +2171,22 @@ bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockI
if (op == OP_CLAIM_NAME)
{
assert(vvchParams.size() == 2);
std::string name(vvchParams[0].begin(), vvchParams[0].end());
std::string value(vvchParams[1].begin(), vvchParams[1].end());
claimId = ClaimIdHash(hash, i);
LogPrintf("--- %s[%lu]: OP_CLAIM_NAME \"%s\" = \"%s\" with claimId %s and tx prevout %s at index %d\n",
__func__, pindex->nHeight, name, SanitizeString(value),
claimId.GetHex(), hash.ToString(), i);
}
else if (op == OP_UPDATE_CLAIM)
{
assert(vvchParams.size() == 3);
std::string name(vvchParams[0].begin(), vvchParams[0].end());
std::string value(vvchParams[1].begin(), vvchParams[1].end());
claimId = uint160(vvchParams[1]);
LogPrintf("--- %s[%lu]: OP_UPDATE_CLAIM \"%s\" = \"%s\" with claimId %s and tx prevout %s at index %d\n",
__func__, pindex->nHeight, name, SanitizeString(value),
claimId.GetHex(), hash.ToString(), i);
}
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());
@ -2190,6 +2200,8 @@ bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockI
assert(vvchParams.size() == 2);
std::string name(vvchParams[0].begin(), vvchParams[0].end());
uint160 supportedClaimId(vvchParams[1]);
LogPrintf("--- %s[%lu]: OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n",
__func__, pindex->nHeight, name, supportedClaimId.GetHex(), hash.ToString(), i);
LogPrintf("%s: (txid: %s, nOut: %d) Removing support for claim id %s on %s due to its block being disconnected\n", __func__, hash.ToString(), i, supportedClaimId.ToString(), name.c_str());
if (!trieCache.undoAddSupport(name, COutPoint(hash, i), pindex->nHeight))
LogPrintf("%s: Something went wrong removing support for name %s in hash %s\n", __func__, name.c_str(), hash.ToString());
@ -2220,6 +2232,12 @@ bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockI
assert(trieCache.finalizeDecrement());
trieCache.setBestBlock(pindex->pprev->GetBlockHash());
assert(trieCache.getMerkleHash() == pindex->pprev->hashClaimTrie);
if (pindex->nHeight == Params().GetConsensus().nExtendedClaimExpirationForkHeight)
{
LogPrintf("Decremented past the extended claim expiration hard fork height");
pclaimTrie->setExpirationTime(Params().GetConsensus().GetExpirationTime(pindex->nHeight-1));
}
if (pfClean) {
*pfClean = fClean;
@ -2481,6 +2499,14 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
}
// v 13 LBRYcrd hard fork to extend expiration time
if (pindex->nHeight == Params().GetConsensus().nExtendedClaimExpirationForkHeight)
{
LogPrintf("Incremented past the extended claim expiration hard fork height");
pclaimTrie->setExpirationTime(chainparams.GetConsensus().GetExpirationTime(pindex->nHeight));
}
int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
LogPrint("bench", " - Fork checks: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeForks * 0.000001);
@ -2573,12 +2599,22 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
if (op == OP_CLAIM_NAME)
{
assert(vvchParams.size() == 2);
std::string name(vvchParams[0].begin(), vvchParams[0].end());
std::string value(vvchParams[1].begin(), vvchParams[1].end());
claimId = ClaimIdHash(txin.prevout.hash, txin.prevout.n);
LogPrintf("+++ %s[%lu]: OP_CLAIM_NAME \"%s\" = \"%s\" with claimId %s and tx prevout %s at index %d\n",
__func__, pindex->nHeight, name, SanitizeString(value),
claimId.GetHex(), txin.prevout.hash.GetHex(), txin.prevout.n);
}
else if (op == OP_UPDATE_CLAIM)
{
assert(vvchParams.size() == 3);
std::string name(vvchParams[0].begin(), vvchParams[0].end());
std::string value(vvchParams[1].begin(), vvchParams[1].end());
claimId = uint160(vvchParams[1]);
LogPrintf("+++ %s[%lu]: OP_UPDATE_CLAIM \"%s\" = \"%s\" with claimId %s and tx prevout %s at index %d\n",
__func__, pindex->nHeight, name, SanitizeString(value),
claimId.GetHex(), txin.prevout.hash.GetHex(), txin.prevout.n);
}
std::string name(vvchParams[0].begin(), vvchParams[0].end());
int nValidAtHeight;
@ -2595,6 +2631,9 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
assert(vvchParams.size() == 2);
std::string name(vvchParams[0].begin(), vvchParams[0].end());
uint160 supportedClaimId(vvchParams[1]);
LogPrintf("+++ %s[%lu]: OP_SUPPORT_CLAIM \"%s\" with claimId %s and tx prevout %s at index %d\n",
__func__, pindex->nHeight, name,
supportedClaimId.GetHex(), txin.prevout.hash.GetHex(), txin.prevout.n);
int nValidAtHeight;
LogPrintf("%s: Removing support for %s in %s. Tx: %s, nOut: %d, removed txid: %s\n", __func__, supportedClaimId.ToString(), name, txin.prevout.hash.ToString(), txin.prevout.n,tx.GetHash().ToString());
if (trieCache.spendSupport(name, COutPoint(txin.prevout.hash, txin.prevout.n), coins->nHeight, nValidAtHeight))

View file

@ -683,6 +683,7 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_update)
fixture.DecrementBlocks(11);
}
/*
expiration
check claims expire and loses claim
@ -844,4 +845,96 @@ BOOST_AUTO_TEST_CASE(claimtriebranching_get_claim_by_id)
BOOST_CHECK(claimValue2.claimId != claimId);
}
/*
expiration
check claims do not expire post ExpirationForkHeight
check supports work post ExpirationForkHeight
*/
BOOST_AUTO_TEST_CASE(claimtriebranching_no_expire)
{
ClaimTrieChainFixture fixture;
// To activate the expiration hard fork, the expiration time must
// be set to 10 years of blocks (not used as 10 years, but rather
// a sentinel value for hard fork activation) and we must advance
// past nExtendedClaimExpirationForkHeight. This is purposely set
// low enough for testing in the Regtest chain parameters.
const int expirationForkHeight =
Params(CBaseChainParams::REGTEST).GetConsensus().nExtendedClaimExpirationForkHeight;
const int originalExpiration =
Params(CBaseChainParams::REGTEST).GetConsensus().nOriginalClaimExpirationTime;
const int extendedExpiration =
Params(CBaseChainParams::REGTEST).GetConsensus().nExtendedClaimExpirationTime;
BOOST_CHECK_EQUAL(pclaimTrie->nExpirationTime, originalExpiration);
// First create a claim and make sure it expires pre-fork
CMutableTransaction tx1 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",3);
fixture.IncrementBlocks(originalExpiration+1);
BOOST_CHECK(!is_best_claim("test",tx1));
fixture.DecrementBlocks(originalExpiration);
BOOST_CHECK(is_best_claim("test",tx1));
fixture.IncrementBlocks(originalExpiration);
BOOST_CHECK(!is_best_claim("test",tx1));
// Create a claim 1 block before the fork height that will expire after the fork height
fixture.IncrementBlocks(expirationForkHeight - chainActive.Height() -2);
CMutableTransaction tx2 = fixture.MakeClaim(fixture.GetCoinbase(),"test2","one",3);
fixture.IncrementBlocks(1);
BOOST_CHECK_EQUAL(chainActive.Height(), expirationForkHeight -1);
// Disable future expirations and fast-forward past the fork height
fixture.IncrementBlocks(1);
BOOST_CHECK_EQUAL(chainActive.Height(), expirationForkHeight);
BOOST_CHECK_EQUAL(pclaimTrie->nExpirationTime, extendedExpiration);
// make sure decrementing to before the fork height will apppropriately set back the
// expiration time to the original expiraiton time
fixture.DecrementBlocks(1);
BOOST_CHECK_EQUAL(pclaimTrie->nExpirationTime, originalExpiration);
fixture.IncrementBlocks(1);
// make sure that claim created 1 block before the fork expires as expected
// at the original expiration times
BOOST_CHECK(is_best_claim("test2", tx2));
fixture.IncrementBlocks(originalExpiration-1);
BOOST_CHECK(!is_best_claim("test2", tx2));
fixture.DecrementBlocks(originalExpiration-1);
// This first claim is still expired since it's pre-fork, even
// after fork activation
BOOST_CHECK(!is_best_claim("test",tx1));
// This new claim created at the fork height cannot expire at original expiration
BOOST_CHECK_EQUAL(chainActive.Height(), expirationForkHeight);
CMutableTransaction tx3 = fixture.MakeClaim(fixture.GetCoinbase(),"test","one",1);
fixture.IncrementBlocks(1);
fixture.IncrementBlocks(originalExpiration);
BOOST_CHECK(is_best_claim("test",tx3));
BOOST_CHECK(!is_best_claim("test",tx1));
fixture.DecrementBlocks(originalExpiration);
// but it expires at the extended expiration
fixture.IncrementBlocks(extendedExpiration);
BOOST_CHECK(!is_best_claim("test",tx3));
fixture.DecrementBlocks(extendedExpiration);
// Ensure that we cannot update the original pre-fork expired claim
CMutableTransaction u1 = fixture.MakeUpdate(tx1,"test","two",ClaimIdHash(tx1.GetHash(),0), 3);
fixture.IncrementBlocks(1);
BOOST_CHECK(!is_best_claim("test",u1));
// Ensure that supports for the expired claim don't support it
CMutableTransaction s1 = fixture.MakeSupport(fixture.GetCoinbase(),u1,"test",10);
BOOST_CHECK(!is_best_claim("test",u1));
// Ensure that we can update the new post-fork claim
CMutableTransaction u2 = fixture.MakeUpdate(tx3,"test","two",ClaimIdHash(tx3.GetHash(),0), 1);
fixture.IncrementBlocks(1);
BOOST_CHECK(is_best_claim("test",u2));
// Ensure that supports for the new post-fork claim
CMutableTransaction s2 = fixture.MakeSupport(fixture.GetCoinbase(),u2,"test",3);
BOOST_CHECK(is_best_claim("test",u2));
}
BOOST_AUTO_TEST_SUITE_END()