Skip to content

Commit

Permalink
Correct update liquidity on rewards calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Mar 22, 2021
1 parent 99d45cc commit a6b1da9
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 259 deletions.
4 changes: 4 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ struct Params {
};
PoS pos;

uint32_t blocksPerDay() const {
static const uint32_t blocks = 60 * 60 * 24 / pos.nTargetSpacing;
return blocks;
}
/**
* Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,
* (nTargetTimespan / nTargetSpacing) which is also used for BIP9 deployments.
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/gv.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class GovVariable
virtual Res Import(UniValue const &) = 0;
virtual UniValue Export() const = 0;
/// @todo it looks like Validate+Apply may be redundant. refactor for one?
virtual Res Validate(CCustomCSView const &mnview) const = 0;
virtual Res Apply(CCustomCSView &mnview, uint32_t height) = 0;
virtual Res Validate(CCustomCSView const &) const = 0;
virtual Res Apply(CCustomCSView &, uint32_t) = 0;

virtual void Serialize(CVectorWriter& s) const = 0;
virtual void Unserialize(VectorReader& s) = 0;
Expand Down
8 changes: 5 additions & 3 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,10 +767,12 @@ bool CCustomCSView::CalculateOwnerRewards(CScript const & owner, uint32_t target
if (!height || *height >= targetHeight) {
return true; // no share or target height is before a pool share' one
}
auto onLiquidity = [&]() -> CAmount {
return GetBalance(owner, poolId).nValue;
};
auto beginHeight = std::max(*height, balanceHeight);
CalculatePoolRewards(poolId, GetBalance(owner, poolId).nValue, beginHeight, targetHeight,
[&](CScript const & from, uint8_t type, CTokenAmount amount, uint32_t begin, uint32_t end) {
amount.nValue *= (end - begin);
CalculatePoolRewards(poolId, onLiquidity, beginHeight, targetHeight,
[&](CScript const & from, uint8_t type, CTokenAmount amount, uint32_t height) {
if (!from.empty()) {
auto res = SubBalance(from, amount);
if (!res) {
Expand Down
34 changes: 14 additions & 20 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
auto pairSymbol = obj.pairSymbol;
poolPair.creationTx = tx.GetHash();
poolPair.creationHeight = height;
auto& rewards = poolPair.rewards;

auto tokenA = mnview.GetToken(poolPair.idTokenA);
if (!tokenA) {
Expand Down Expand Up @@ -691,19 +692,16 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
return std::move(tokenId);
}

auto res = mnview.SetPoolPair(tokenId, height, poolPair);
if (!res) {
return res;
}

if (!obj.rewards.balances.empty()) {
auto rewards = obj.rewards;
rewards = obj.rewards;
if (!rewards.balances.empty()) {
// Check tokens exist and remove empty reward amounts
auto res = eraseEmptyBalances(rewards.balances);
// Will only fail if pool was not actually created in SetPoolPair
return !res ? res : mnview.SetPoolCustomReward(tokenId, height, rewards);
if (!res) {
return res;
}
}
return Res::Ok();

return mnview.SetPoolPair(tokenId, height, poolPair);
}

Res operator()(const CUpdatePoolPairMessage& obj) const {
Expand All @@ -712,24 +710,20 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
return Res::Err("tx not from foundation member");
}

auto res = mnview.UpdatePoolPair(obj.poolId, height, obj.status, obj.commission, obj.ownerAddress);
if (!res) {
return res;
}

if (!obj.rewards.balances.empty()) {
auto rewards = obj.rewards;
if (!rewards.balances.empty()) {
// Check for special case to wipe rewards
auto rewards = obj.rewards;
if (rewards.balances.size() == 1 && rewards.balances.cbegin()->first == DCT_ID{std::numeric_limits<uint32_t>::max()}
&& rewards.balances.cbegin()->second == std::numeric_limits<CAmount>::max()) {
rewards.balances.clear();
}
// Check if tokens exist and remove empty reward amounts
auto res = eraseEmptyBalances(rewards.balances);
// Will only fail if pool was not actually created in SetPoolPair
return !res ? res : mnview.SetPoolCustomReward(obj.poolId, height, rewards);
if (!res) {
return res;
}
}
return Res::Ok();
return mnview.UpdatePoolPair(obj.poolId, height, obj.status, obj.commission, obj.ownerAddress, obj.rewards);
}

Res operator()(const CPoolSwapMessage& obj) const {
Expand Down
Loading

0 comments on commit a6b1da9

Please sign in to comment.