Skip to content

Commit ad2f0f0

Browse files
committed
Updated logic of "unlock limits"; still incorrect yet
1 parent 3afe082 commit ad2f0f0

File tree

3 files changed

+31
-41
lines changed

3 files changed

+31
-41
lines changed

src/evo/creditpool.cpp

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,31 @@ CreditPoolCb CCreditPoolManager::getCreditPool(const CBlockIndex* block_index, c
120120
blockUnlocked += unlocked;
121121
indexes.add(index);
122122
}
123-
LogPrintf("getCreditPool block %s unlocked %lld\n", block_hash.ToString(), blockUnlocked);
124123

125-
CreditPoolCb pool{cbTx.assetLockedAmount, prev.totalUnlocked + blockUnlocked, indexes};
124+
const CAmount locked = cbTx.assetLockedAmount;
125+
const CAmount previousLimit = prev.currentLimit;
126+
if (previousLimit < blockUnlocked) {
127+
throw std::runtime_error(strprintf("%s: getCreditPool failed because previous block %s exceed limits", __func__, block_hash.ToString()));
128+
}
129+
// # max(100, min(.10 * assetlockpool, 1000))
130+
CAmount currentLimitCandidate = locked;
131+
if (currentLimitCandidate > LimitAmountLow) {
132+
currentLimitCandidate = std::max(LimitAmountLow, locked / 10);
133+
}
134+
currentLimitCandidate = std::min(currentLimitCandidate, LimitAmountHigh);
135+
136+
CAmount currentLimit = previousLimit - blockUnlocked + currentLimitCandidate / LimitBlocksToTrace;
137+
if (currentLimit > locked) {
138+
currentLimit = locked;
139+
}
140+
141+
if (currentLimit || previousLimit || locked) {
142+
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,
143+
currentLimit / COIN, currentLimit % COIN,
144+
previousLimit / COIN, previousLimit % COIN);
145+
}
146+
147+
CreditPoolCb pool{locked, currentLimit, indexes};
126148
addToCache(block_hash, block_height, pool);
127149
return pool;
128150
}
@@ -138,33 +160,6 @@ CreditPoolCbDiff::CreditPoolCbDiff(const CreditPoolCb& starter, const CBlockInde
138160
, pindex(pindex)
139161
{
140162
assert(pindex);
141-
const CAmount prevLocked = pool.locked;
142-
CAmount latelyUnlocked = pool.totalUnlocked;
143-
const CBlockIndex* other_block = pindex;
144-
for (size_t i = 0; i < CCreditPoolManager::LimitBlocksToTrace; ++i) {
145-
other_block = other_block->pprev;
146-
if (other_block == nullptr) break;
147-
}
148-
if (other_block) {
149-
CreditPoolCb agedPool = creditPoolManager->getCreditPool(other_block, consensusParams);
150-
latelyUnlocked -= agedPool.totalUnlocked;
151-
}
152-
sessionLimit = prevLocked;
153-
154-
// # max(100, min(.10 * assetlockpool, 1000))
155-
if ((sessionLimit + latelyUnlocked > (prevLocked + latelyUnlocked) / 10) && (sessionLimit + latelyUnlocked > CCreditPoolManager::LimitAmountLow)) {
156-
sessionLimit = std::max<CAmount>(0, (latelyUnlocked + prevLocked) / 10 - latelyUnlocked);
157-
if (sessionLimit > prevLocked) sessionLimit = prevLocked;
158-
}
159-
if (sessionLimit + latelyUnlocked > CCreditPoolManager::LimitAmountHigh) {
160-
sessionLimit = CCreditPoolManager::LimitAmountHigh - latelyUnlocked;
161-
}
162-
163-
if (prevLocked || latelyUnlocked || sessionLimit) {
164-
LogPrintf("CreditPoolCb init on height %d: %d.%08d %d.%08d limited by %d.%08d\n", pindex->nHeight, prevLocked / COIN, prevLocked % COIN,
165-
latelyUnlocked / COIN, latelyUnlocked % COIN,
166-
sessionLimit / COIN, sessionLimit % COIN);
167-
}
168163
}
169164

170165

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

src/evo/creditpool.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,17 @@ struct SkipSet {
5151
};
5252

5353
struct CreditPoolCb {
54-
CAmount locked;
54+
CAmount locked{0};
5555

56-
// KNST incorrect temporary
57-
// CAN OVERFLOW SOMEDAY
58-
// SUBSTRACT EACH BLOCK: (MAX / 576)
59-
CAmount totalUnlocked;
56+
CAmount currentLimit{0};
6057

61-
SkipSet indexes;
58+
SkipSet indexes{};
6259

6360
SERIALIZE_METHODS(CreditPoolCb, obj)
6461
{
6562
READWRITE(
6663
obj.locked,
67-
obj.totalUnlocked,
64+
obj.currentLimit,
6865
obj.indexes
6966
);
7067
}
@@ -74,8 +71,6 @@ struct CreditPoolCbDiff {
7471
CAmount sessionLocked{0};
7572
CAmount sessionUnlocked{0};
7673

77-
CAmount sessionLimit{0};
78-
7974
const CBlockIndex *pindex{nullptr};
8075
CreditPoolCbDiff(const CreditPoolCb& starter, const CBlockIndex *pindex, const Consensus::Params& consensusParams);
8176

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
171171
std::optional<CreditPoolCbDiff> creditPoolDiff;
172172
if (fV19Active_context) {
173173
const CreditPoolCb creditPool = creditPoolManager->getCreditPool(pindexPrev, chainparams.GetConsensus());
174-
LogPrintf("CreateNewBlock(): credit pool stats. locked: %lld unlocked: %lld indexes: %lld\n",
175-
creditPool.locked, creditPool.totalUnlocked,
174+
LogPrintf("CreateNewBlock(): credit pool stats. locked: %lld current limit: %lld indexes: %lld\n",
175+
creditPool.locked, creditPool.currentLimit,
176176
creditPool.indexes.size());
177177
creditPoolDiff.emplace(creditPool, pindexPrev, chainparams.GetConsensus());
178178
}

0 commit comments

Comments
 (0)