Skip to content

Commit

Permalink
Updated logic of "unlock limits"; still incorrect yet
Browse files Browse the repository at this point in the history
  • Loading branch information
knst committed Nov 24, 2022
1 parent 3afe082 commit ad2f0f0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
55 changes: 25 additions & 30 deletions src/evo/creditpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,31 @@ CreditPoolCb CCreditPoolManager::getCreditPool(const CBlockIndex* block_index, c
blockUnlocked += unlocked;
indexes.add(index);
}
LogPrintf("getCreditPool block %s unlocked %lld\n", block_hash.ToString(), blockUnlocked);

CreditPoolCb pool{cbTx.assetLockedAmount, prev.totalUnlocked + blockUnlocked, indexes};
const CAmount locked = cbTx.assetLockedAmount;
const CAmount previousLimit = prev.currentLimit;
if (previousLimit < blockUnlocked) {
throw std::runtime_error(strprintf("%s: getCreditPool failed because previous block %s exceed limits", __func__, block_hash.ToString()));
}
// # max(100, min(.10 * assetlockpool, 1000))
CAmount currentLimitCandidate = locked;
if (currentLimitCandidate > LimitAmountLow) {
currentLimitCandidate = std::max(LimitAmountLow, locked / 10);
}
currentLimitCandidate = std::min(currentLimitCandidate, LimitAmountHigh);

CAmount currentLimit = previousLimit - blockUnlocked + currentLimitCandidate / LimitBlocksToTrace;
if (currentLimit > locked) {
currentLimit = locked;
}

if (currentLimit || previousLimit || locked) {
LogPrintf("getCreditPool asset unlock limits on height: %d locked: %d.%08d limit: %d.%08d previous: %d.%08d\n", block_index->nHeight, locked / COIN, locked % COIN,
currentLimit / COIN, currentLimit % COIN,
previousLimit / COIN, previousLimit % COIN);
}

CreditPoolCb pool{locked, currentLimit, indexes};
addToCache(block_hash, block_height, pool);
return pool;
}
Expand All @@ -138,33 +160,6 @@ CreditPoolCbDiff::CreditPoolCbDiff(const CreditPoolCb& starter, const CBlockInde
, pindex(pindex)
{
assert(pindex);
const CAmount prevLocked = pool.locked;
CAmount latelyUnlocked = pool.totalUnlocked;
const CBlockIndex* other_block = pindex;
for (size_t i = 0; i < CCreditPoolManager::LimitBlocksToTrace; ++i) {
other_block = other_block->pprev;
if (other_block == nullptr) break;
}
if (other_block) {
CreditPoolCb agedPool = creditPoolManager->getCreditPool(other_block, consensusParams);
latelyUnlocked -= agedPool.totalUnlocked;
}
sessionLimit = prevLocked;

// # max(100, min(.10 * assetlockpool, 1000))
if ((sessionLimit + latelyUnlocked > (prevLocked + latelyUnlocked) / 10) && (sessionLimit + latelyUnlocked > CCreditPoolManager::LimitAmountLow)) {
sessionLimit = std::max<CAmount>(0, (latelyUnlocked + prevLocked) / 10 - latelyUnlocked);
if (sessionLimit > prevLocked) sessionLimit = prevLocked;
}
if (sessionLimit + latelyUnlocked > CCreditPoolManager::LimitAmountHigh) {
sessionLimit = CCreditPoolManager::LimitAmountHigh - latelyUnlocked;
}

if (prevLocked || latelyUnlocked || sessionLimit) {
LogPrintf("CreditPoolCb init on height %d: %d.%08d %d.%08d limited by %d.%08d\n", pindex->nHeight, prevLocked / COIN, prevLocked % COIN,
latelyUnlocked / COIN, latelyUnlocked % COIN,
sessionLimit / COIN, sessionLimit % COIN);
}
}


Expand Down Expand Up @@ -199,7 +194,7 @@ bool CreditPoolCbDiff::unlock(const CTransaction& tx, CValidationState& state)
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "failed-creditpool-duplicated-index");
}
pool.indexes.add(index);
if (sessionUnlocked + toUnlock > sessionLimit ) {
if (sessionUnlocked + toUnlock > pool.currentLimit) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "failed-creditpool-unloock-too-much");
}

Expand Down
13 changes: 4 additions & 9 deletions src/evo/creditpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,17 @@ struct SkipSet {
};

struct CreditPoolCb {
CAmount locked;
CAmount locked{0};

// KNST incorrect temporary
// CAN OVERFLOW SOMEDAY
// SUBSTRACT EACH BLOCK: (MAX / 576)
CAmount totalUnlocked;
CAmount currentLimit{0};

SkipSet indexes;
SkipSet indexes{};

SERIALIZE_METHODS(CreditPoolCb, obj)
{
READWRITE(
obj.locked,
obj.totalUnlocked,
obj.currentLimit,
obj.indexes
);
}
Expand All @@ -74,8 +71,6 @@ struct CreditPoolCbDiff {
CAmount sessionLocked{0};
CAmount sessionUnlocked{0};

CAmount sessionLimit{0};

const CBlockIndex *pindex{nullptr};
CreditPoolCbDiff(const CreditPoolCb& starter, const CBlockIndex *pindex, const Consensus::Params& consensusParams);

Expand Down
4 changes: 2 additions & 2 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
std::optional<CreditPoolCbDiff> creditPoolDiff;
if (fV19Active_context) {
const CreditPoolCb creditPool = creditPoolManager->getCreditPool(pindexPrev, chainparams.GetConsensus());
LogPrintf("CreateNewBlock(): credit pool stats. locked: %lld unlocked: %lld indexes: %lld\n",
creditPool.locked, creditPool.totalUnlocked,
LogPrintf("CreateNewBlock(): credit pool stats. locked: %lld current limit: %lld indexes: %lld\n",
creditPool.locked, creditPool.currentLimit,
creditPool.indexes.size());
creditPoolDiff.emplace(creditPool, pindexPrev, chainparams.GetConsensus());
}
Expand Down

0 comments on commit ad2f0f0

Please sign in to comment.