Skip to content

Commit

Permalink
Minimize undo data size
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Fieroni <[email protected]>
  • Loading branch information
bvbfan committed Apr 14, 2021
1 parent 700f399 commit 1fcfa10
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 131 deletions.
12 changes: 5 additions & 7 deletions src/masternodes/govvariables/lp_splits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ UniValue LP_SPLITS::Export() const {
Res LP_SPLITS::Validate(const CCustomCSView & mnview) const {
CAmount total{0};
for (auto const & kv : splits) {
auto pool = mnview.GetPoolPair(kv.first);

if (!pool)
if (!mnview.HasPoolPair(kv.first))
return Res::Err("pool with id=%s not found", kv.first.ToString());

if (kv.second < 0 || kv.second > COIN)
Expand All @@ -50,15 +48,15 @@ Res LP_SPLITS::Validate(const CCustomCSView & mnview) const {
}

Res LP_SPLITS::Apply(CCustomCSView & mnview, uint32_t height) {
mnview.ForEachPoolPair([&] (const DCT_ID poolId, CPoolPair pool) {
mnview.ForEachPoolId([&] (DCT_ID poolId) {
// we ought to reset previous value:
pool.rewardPct = 0;
CAmount rewardPct = 0;
auto it = splits.find(poolId);
if (it != splits.end()) {
pool.rewardPct = it->second;
rewardPct = it->second;
}

mnview.SetPoolPair(poolId, height, pool);
mnview.SetRewardPct(poolId, height, rewardPct);
return true;
});
return Res::Ok();
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ bool CCustomCSView::CalculateOwnerRewards(CScript const & owner, uint32_t target
if (balanceHeight >= targetHeight) {
return false;
}
ForEachPoolPair([&] (DCT_ID const & poolId, CLazySerialize<CPoolPair>) {
ForEachPoolId([&] (DCT_ID const & poolId) {
auto height = GetShare(poolId, owner);
if (!height || *height >= targetHeight) {
return true; // no share or target height is before a pool share' one
Expand Down
261 changes: 162 additions & 99 deletions src/masternodes/poolpairs.cpp

Large diffs are not rendered by default.

27 changes: 15 additions & 12 deletions src/masternodes/poolpairs.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ class CPoolPair : public CPoolPairMessage
CPoolPair(CPoolPairMessage const & msg = {}) : CPoolPairMessage(msg) {}
virtual ~CPoolPair() = default;

CBalances rewards;
CAmount reserveA = 0, reserveB = 0, totalLiquidity = 0;
CAmount blockCommissionA = 0, blockCommissionB = 0;
// temporary values, not serialized
CAmount reserveA = 0;
CAmount reserveB = 0;
CAmount totalLiquidity = 0;
CAmount blockCommissionA = 0;
CAmount blockCommissionB = 0;

CAmount rewardPct = 0; // pool yield farming reward %%
bool swapEvent = false;

// serialized
CBalances rewards;
uint256 creationTx;
uint32_t creationHeight = -1;

Expand Down Expand Up @@ -130,16 +135,9 @@ class CPoolPair : public CPoolPairMessage
if (!ser_action.ForRead()) ioProofer();

READWRITEAS(CPoolPairMessage, *this);
READWRITE(reserveA);
READWRITE(reserveB);
READWRITE(totalLiquidity);
READWRITE(blockCommissionA);
READWRITE(blockCommissionB);
READWRITE(rewardPct);
READWRITE(swapEvent);
READWRITE(rewards);
READWRITE(creationTx);
READWRITE(creationHeight);
READWRITE(rewards);

if (ser_action.ForRead()) ioProofer();
}
Expand Down Expand Up @@ -202,7 +200,8 @@ class CPoolPairView : public virtual CStorageView
boost::optional<CPoolPair> GetPoolPair(const DCT_ID &poolId) const;
boost::optional<std::pair<DCT_ID, CPoolPair> > GetPoolPair(DCT_ID const & tokenA, DCT_ID const & tokenB) const;

void ForEachPoolPair(std::function<bool(DCT_ID const &, CLazySerialize<CPoolPair>)> callback, DCT_ID const & start = DCT_ID{0});
void ForEachPoolId(std::function<bool(DCT_ID const &)> callback, DCT_ID const & start = DCT_ID{0});
void ForEachPoolPair(std::function<bool(DCT_ID const &, CPoolPair)> callback, DCT_ID const & start = DCT_ID{0});
void ForEachPoolShare(std::function<bool(DCT_ID const &, CScript const &, uint32_t)> callback, PoolShareKey const &startKey = {});

Res SetShare(DCT_ID const & poolId, CScript const & provider, uint32_t height);
Expand All @@ -213,6 +212,8 @@ class CPoolPairView : public virtual CStorageView
void CalculatePoolRewards(DCT_ID const & poolId, std::function<CAmount()> onLiquidity, uint32_t begin, uint32_t end, std::function<void(uint8_t, CTokenAmount, uint32_t)> onReward);

Res SetDailyReward(uint32_t height, CAmount reward);
Res SetRewardPct(DCT_ID const & poolId, uint32_t height, CAmount rewardPct);
bool HasPoolPair(DCT_ID const & poolId) const;

CAmount UpdatePoolRewards(std::function<CTokenAmount(CScript const &, DCT_ID)> onGetBalance, std::function<Res(CScript const &, CScript const &, CTokenAmount)> onTransfer, int nHeight = 0);

Expand All @@ -222,6 +223,8 @@ class CPoolPairView : public virtual CStorageView
struct ByShare { static const unsigned char prefix; }; // lsTokenID+accountID -> {}
struct ByIDPair { static const unsigned char prefix; }; // lsTokenID -> tokenA+tokenB
struct ByPoolSwap { static const unsigned char prefix; };
struct ByReserves { static const unsigned char prefix; };
struct ByRewardPct { static const unsigned char prefix; };
struct ByPoolReward { static const unsigned char prefix; };
struct ByDailyReward { static const unsigned char prefix; };
struct ByCustomReward { static const unsigned char prefix; };
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/rpc_accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ UniValue outputEntryToJSON(COutputEntry const & entry, CBlockIndex const * index

static void onPoolRewards(CCustomCSView & view, CScript const & owner, uint32_t begin, uint32_t end, std::function<void(uint32_t, DCT_ID, uint8_t, CTokenAmount)> onReward) {
CCustomCSView mnview(view);
view.ForEachPoolPair([&] (DCT_ID const & poolId, CLazySerialize<CPoolPair>) {
view.ForEachPoolId([&] (DCT_ID const & poolId) {
auto height = view.GetShare(poolId, owner);
if (!height || *height >= end) {
return true; // no share or target height is before a pool share' one
Expand Down
4 changes: 2 additions & 2 deletions src/masternodes/rpc_poolpair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ UniValue listpoolpairs(const JSONRPCRequest& request) {
LOCK(cs_main);

UniValue ret(UniValue::VOBJ);
pcustomcsview->ForEachPoolPair([&](DCT_ID const & id, CLazySerialize<CPoolPair> pool) {
pcustomcsview->ForEachPoolPair([&](DCT_ID const & id, CPoolPair pool) {
const auto token = pcustomcsview->GetToken(id);
if (token) {
ret.pushKVs(poolToJSON(id, pool.get(), *token, verbose));
ret.pushKVs(poolToJSON(id, pool, *token, verbose));
}

limit--;
Expand Down
13 changes: 5 additions & 8 deletions src/test/liquidity_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,7 @@ BOOST_AUTO_TEST_CASE(math_liquidity_and_trade)

void SetPoolRewardPct(CCustomCSView & mnview, DCT_ID idPool, CAmount pct)
{
auto optPool = mnview.GetPoolPair(idPool);
BOOST_REQUIRE(optPool);
optPool->rewardPct = pct;
mnview.SetPoolPair(idPool, 1, *optPool);
BOOST_REQUIRE(mnview.SetRewardPct(idPool, 1, pct));
}

void SetPoolTradeFees(CCustomCSView & mnview, DCT_ID idPool, CAmount A, CAmount B)
Expand Down Expand Up @@ -274,7 +271,7 @@ BOOST_AUTO_TEST_CASE(math_rewards)

}
// create shares
mnview.ForEachPoolPair([&] (DCT_ID const & idPool, CLazySerialize<CPoolPair>) {
mnview.ForEachPoolId([&] (DCT_ID const & idPool) {
// printf("pool id = %s\n", idPool.ToString().c_str());
for (int i = 0; i < ProvidersCount; ++i) {
CScript shareAddress = CScript(idPool.v * ProvidersCount + i);
Expand Down Expand Up @@ -303,7 +300,7 @@ BOOST_AUTO_TEST_CASE(math_rewards)
/// DCT_ID{10} - 0

// set "traded fees" here too, just to estimate proc.load
cache.ForEachPoolPair([&] (DCT_ID const & idPool, CLazySerialize<CPoolPair>) {
cache.ForEachPoolId([&] (DCT_ID const & idPool) {
SetPoolTradeFees(cache, idPool, idPool.v * COIN, idPool.v * COIN*2);
return true;
});
Expand Down Expand Up @@ -373,7 +370,7 @@ BOOST_AUTO_TEST_CASE(owner_rewards)
}

// create shares
mnview.ForEachPoolPair([&] (DCT_ID const & idPool, CLazySerialize<CPoolPair>) {
mnview.ForEachPoolId([&] (DCT_ID const & idPool) {
for (int i = 0; i < PoolCount; ++i) {
Res res = AddPoolLiquidity(mnview, idPool, idPool.v*COIN, idPool.v*COIN, shareAddress[i]);
BOOST_CHECK(res.ok);
Expand All @@ -382,12 +379,12 @@ BOOST_AUTO_TEST_CASE(owner_rewards)
});

mnview.ForEachPoolPair([&] (DCT_ID const & idPool, CPoolPair pool) {
pool.rewardPct = COIN/(idPool.v + 1);
pool.blockCommissionA = idPool.v * COIN;
pool.blockCommissionB = idPool.v * COIN * 2;
pool.swapEvent = true;
pool.ownerAddress = shareAddress[0];
mnview.SetPoolPair(idPool, 1, pool);
mnview.SetRewardPct(idPool, 1, COIN/(idPool.v + 1));
return true;
});

Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_poolpair.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def run_test(self):
}, [])
except JSONRPCException as e:
errorString = e.error['message']
assert("Error, there is already a poolpairwith same tokens, but different poolId" in errorString)
assert("Error, there is already a poolpair with same tokens, but different poolId" in errorString)

# Creating another one
trPP = self.nodes[0].createpoolpair({
Expand Down

0 comments on commit 1fcfa10

Please sign in to comment.