Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose live dex statistics #1214

Merged
merged 29 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
46ef729
Live dex statistics (#1192)
bvbfan Apr 27, 2022
3cce1a4
Force reindex when dex live does not present
bvbfan Apr 27, 2022
55aadfe
Merge branch 'master' into backport/dex_main
prasannavl May 5, 2022
f1ab5d5
Add test for swap overflow
dcorral May 6, 2022
cea51f0
Refactor poolswap tests
dcorral May 9, 2022
42eb17a
Remove unused variables fixes lint error
dcorral May 9, 2022
4dc5f7a
Merge branch 'master' into backport/dex_main
prasannavl May 9, 2022
e312aa4
Exclude attributes from merkle root
bvbfan May 11, 2022
53ebcb7
Merge branch 'master' into backport/dex_main
Bushstar May 22, 2022
ceada94
Merge branch 'master' into backport/dex_main
prasannavl May 22, 2022
13a8178
Merge branch 'master' into backport/dex_main
Mixa84 May 30, 2022
53ecb6a
Erase attributes from undos so it is not part of merkle root
Mixa84 May 31, 2022
eac30a5
Merge branch 'master' into backport/dex_main
Mixa84 May 31, 2022
5a6bdfa
Merge branch 'master' into backport/dex_main
Mixa84 Jun 1, 2022
3190001
Set token split via SetValue
Bushstar Jun 1, 2022
3bd08aa
Fix test
Mixa84 Jun 1, 2022
dad3d41
Merge branch 'master' into backport/dex_main
Mixa84 Jun 7, 2022
c3db655
Add -dexstats flag for optional usage
Mixa84 Jun 13, 2022
e51cddb
Move SetDexStatsLastHeight at the end of ConnectBlock in the view wit…
Mixa84 Jun 14, 2022
90a9d9b
Merge branch 'master' into backport/dex_main
Mixa84 Jun 14, 2022
2ab35e8
Fix test
Mixa84 Jun 14, 2022
5f2b885
Merge branch 'master' into backport/dex_main
prasannavl Jun 22, 2022
a064522
Merge branch 'master' into backport/dex_main
prasannavl Jun 23, 2022
c595730
Merge branch 'master' into backport/dex_main
prasannavl Jun 25, 2022
b49b457
Merge branch 'master' into backport/dex_main
prasannavl Jun 27, 2022
c020701
Fix economy keys
prasannavl Jun 27, 2022
f9f7949
Fix lints
prasannavl Jun 27, 2022
2258935
Refund DUSD use SetValue
Bushstar Jun 27, 2022
e1da63d
Update src/masternodes/mn_checks.cpp
prasannavl Jun 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <key_io.h>
#include <masternodes/accountshistory.h>
#include <masternodes/anchors.h>
#include <masternodes/govvariables/attributes.h>
#include <masternodes/masternodes.h>
#include <masternodes/vaulthistory.h>
#include <miner.h>
Expand Down Expand Up @@ -505,6 +506,7 @@ void SetupServerArgs()
gArgs.AddArg("-greatworldheight", "Great World fork activation height (regtest only)", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::CHAINPARAMS);
gArgs.AddArg("-jellyfish_regtest", "Configure the regtest network for jellyfish testing", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
gArgs.AddArg("-simulatemainnet", "Configure the regtest network to mainnet target timespan and spacing ", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
gArgs.AddArg("-dexstats", strprintf("Enable storing live dex data in DB (default: %u)", DEFAULT_DEXSTATS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
#ifdef USE_UPNP
#if USE_UPNP
gArgs.AddArg("-upnp", "Use UPnP to map the listening port (default: 1 when listening and no -proxy)", ArgsManager::ALLOW_ANY, OptionsCategory::CONNECTION);
Expand Down Expand Up @@ -1706,6 +1708,7 @@ bool AppInitMain(InitInterfaces& interfaces)
pcustomcsDB = std::make_unique<CStorageLevelDB>(GetDataDir() / "enhancedcs", nCustomCacheSize, false, fReset || fReindexChainState);
pcustomcsview.reset();
pcustomcsview = std::make_unique<CCustomCSView>(*pcustomcsDB.get());

if (!fReset && !fReindexChainState) {
if (!pcustomcsDB->IsEmpty() && pcustomcsview->GetDbVersion() != CCustomCSView::DbVersion) {
strLoadError = _("Account database is unsuitable").translated;
Expand Down Expand Up @@ -1760,6 +1763,24 @@ bool AppInitMain(InitInterfaces& interfaces)
}
assert(::ChainActive().Tip() != nullptr);
}

auto dexStats = gArgs.GetBoolArg("-dexstats", DEFAULT_DEXSTATS);
pcustomcsview->SetDexStatsEnabled(dexStats);

if (!fReset && !fReindexChainState && !pcustomcsDB->IsEmpty() && dexStats) {
// force reindex if there is no dex data at the tip
PoolHeightKey anyPoolSwap{DCT_ID{}, ~0u};
auto it = pcustomcsview->LowerBound<CPoolPairView::ByPoolSwap>(anyPoolSwap);
auto shouldReindex = it.Valid();
auto lastHeight = pcustomcsview->GetDexStatsLastHeight();
if (lastHeight.has_value())
shouldReindex &= !(*lastHeight == ::ChainActive().Tip()->nHeight);

if (shouldReindex) {
strLoadError = _("Live dex needs reindex").translated;
break;
}
}
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database").translated;
Expand Down
23 changes: 18 additions & 5 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ const std::map<uint8_t, std::map<uint8_t, std::string>>& ATTRIBUTES::displayKeys
{EconomyKeys::DFIP2203Current, "dfip2203_current"},
{EconomyKeys::DFIP2203Burned, "dfip2203_burned"},
{EconomyKeys::DFIP2203Minted, "dfip2203_minted"},
{EconomyKeys::DexTokens, "dex"},
{EconomyKeys::DFIP2206FCurrent, "dfip2206f_current"},
{EconomyKeys::DFIP2206FBurned, "dfip2206f_burned"},
{EconomyKeys::DFIP2206FMinted, "dfip2206f_minted"},
Expand Down Expand Up @@ -607,7 +608,7 @@ Res ATTRIBUTES::RefundFuturesContracts(CCustomCSView &mnview, const uint32_t hei
}
}

attributes[liveKey] = balances;
SetValue(liveKey, std::move(balances));

return Res::Ok();
}
Expand Down Expand Up @@ -661,7 +662,7 @@ Res ATTRIBUTES::RefundFuturesDUSD(CCustomCSView &mnview, const uint32_t height)
}
}

attributes[liveKey] = balances;
SetValue(liveKey, std::move(balances));

return Res::Ok();
}
Expand Down Expand Up @@ -696,7 +697,7 @@ Res ATTRIBUTES::Import(const UniValue & val) {
const auto& [id, multiplier] = *(splitValue->begin());
tokenSplits.insert(id);

attributes[attribute] = *splitValue;
SetValue(attribute, *splitValue);
return Res::Ok();
}

Expand All @@ -708,11 +709,11 @@ Res ATTRIBUTES::Import(const UniValue & val) {
} else {
newAttr.key = TokenKeys::PaybackDFIFeePCT;
}
attributes[newAttr] = value;
SetValue(newAttr, value);
return Res::Ok();
}
}
attributes[attribute] = value;
SetValue(attribute, value);
return Res::Ok();
}
);
Expand Down Expand Up @@ -800,6 +801,18 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
result.pushKV("paybackfees", AmountsToJSON(paybacks->tokensFee.balances));
result.pushKV("paybacktokens", AmountsToJSON(paybacks->tokensPayback.balances));
ret.pushKV(key, result);
} else if (const auto balances = std::get_if<CDexBalances>(&attribute.second)) {
for (const auto& pool : *balances) {
auto& dexTokenA = pool.second.totalTokenA;
auto& dexTokenB = pool.second.totalTokenB;
auto poolkey = KeyBuilder(key, pool.first.v);
ret.pushKV(KeyBuilder(poolkey, "total_commission_a"), ValueFromUint(dexTokenA.commissions));
ret.pushKV(KeyBuilder(poolkey, "total_commission_b"), ValueFromUint(dexTokenB.commissions));
ret.pushKV(KeyBuilder(poolkey, "fee_burn_a"), ValueFromUint(dexTokenA.feeburn));
ret.pushKV(KeyBuilder(poolkey, "fee_burn_b"), ValueFromUint(dexTokenB.feeburn));
ret.pushKV(KeyBuilder(poolkey, "total_swap_a"), ValueFromUint(dexTokenA.swaps));
ret.pushKV(KeyBuilder(poolkey, "total_swap_b"), ValueFromUint(dexTokenB.swaps));
}
} else if (const auto splitValues = std::get_if<OracleSplits>(&attribute.second)) {
std::string keyValue;
for (auto it{splitValues->begin()}; it != splitValues->end(); ++it) {
Expand Down
43 changes: 37 additions & 6 deletions src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ enum OracleIDs : uint8_t {
};

enum EconomyKeys : uint8_t {
PaybackDFITokens = 'a',
PaybackTokens = 'b',
DFIP2203Current = 'c',
DFIP2203Burned = 'd',
DFIP2203Minted = 'e',
PaybackDFITokens = 'a',
PaybackTokens = 'b',
DFIP2203Current = 'c',
DFIP2203Burned = 'd',
DFIP2203Minted = 'e',
DFIP2206FCurrent = 'f',
DFIP2206FBurned = 'g',
DFIP2206FMinted = 'h',
DexTokens = 'i',
};

enum DFIPKeys : uint8_t {
Expand Down Expand Up @@ -150,17 +151,46 @@ struct CFeeDir {

ResVal<CScript> GetFutureSwapContractAddress(const std::string& contract);

struct CDexTokenInfo {
struct CTokenInfo {
uint64_t swaps;
uint64_t feeburn;
uint64_t commissions;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(swaps);
READWRITE(feeburn);
READWRITE(commissions);
}
};

CTokenInfo totalTokenA;
CTokenInfo totalTokenB;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(totalTokenA);
READWRITE(totalTokenB);
}
};

enum FeeDirValues : uint8_t {
Both,
In,
Out
};

using CDexBalances = std::map<DCT_ID, CDexTokenInfo>;
using OracleSplits = std::map<uint32_t, int32_t>;
using DescendantValue = std::pair<uint32_t, int32_t>;
using AscendantValue = std::pair<uint32_t, std::string>;
using CAttributeType = std::variant<CDataStructureV0, CDataStructureV1>;
using CAttributeValue = std::variant<bool, CAmount, CBalances, CTokenPayback, CTokenCurrencyPair, OracleSplits, DescendantValue, AscendantValue, CFeeDir>;
using CAttributeValue = std::variant<bool, CAmount, CBalances, CTokenPayback, CTokenCurrencyPair, OracleSplits, DescendantValue, AscendantValue, CFeeDir, CDexBalances>;

enum GovVarsFilter {
All,
Expand Down Expand Up @@ -260,6 +290,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
READWRITE(attributes);
}


uint32_t time{0};

// For formatting in export
Expand Down
24 changes: 23 additions & 1 deletion src/masternodes/gv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,29 @@

Res CGovView::SetVariable(GovVariable const & var)
{
return WriteBy<ByName>(var.GetName(), var) ? Res::Ok() : Res::Err("can't write to DB");
auto WriteVar = [this](GovVariable const & var) {
return WriteBy<ByName>(var.GetName(), var) ? Res::Ok() : Res::Err("can't write to DB");
};
if (var.GetName() != "ATTRIBUTES") {
return WriteVar(var);
}
auto attributes = GetAttributes();
if (!attributes) {
return WriteVar(var);
}
auto& current = dynamic_cast<const ATTRIBUTES&>(var);
if (current.changed.empty()) {
return Res::Ok();
}
for (auto& key : current.changed) {
auto it = current.attributes.find(key);
if (it == current.attributes.end()) {
attributes->attributes.erase(key);
} else {
attributes->attributes[key] = it->second;
}
}
return WriteVar(*attributes);
}

std::shared_ptr<GovVariable> CGovView::GetVariable(std::string const & name) const
Expand Down
51 changes: 47 additions & 4 deletions src/masternodes/masternodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,29 @@ std::vector<CAnchorConfirmDataPlus> CAnchorConfirmsView::GetAnchorConfirmData()
return confirms;
}

/*
* CSettingsView
*/

void CSettingsView::SetDexStatsLastHeight(const int32_t height)
{
WriteBy<KVSettings>(DEX_STATS_LAST_HEIGHT, height);
}

std::optional<int32_t> CSettingsView::GetDexStatsLastHeight()
{
return ReadBy<KVSettings, int32_t>(DEX_STATS_LAST_HEIGHT);
}

void CSettingsView::SetDexStatsEnabled(const bool enabled)
{
WriteBy<KVSettings>(DEX_STATS_ENABLED, enabled);
}

std::optional<bool> CSettingsView::GetDexStatsEnabled()
{
return ReadBy<KVSettings, bool>(DEX_STATS_ENABLED);
}
/*
* CCustomCSView
*/
Expand Down Expand Up @@ -1049,14 +1072,34 @@ Res CCustomCSView::PopulateCollateralData(CCollateralLoans& result, CVaultId con
}

uint256 CCustomCSView::MerkleRoot() {
auto& rawMap = GetStorage().GetRaw();
auto rawMap = GetStorage().GetRaw();
if (rawMap.empty()) {
return {};
}
auto isAttributes = [](const TBytes& key) {
MapKV map = {std::make_pair(key, TBytes{})};
// Attributes should not be part of merkle root
static const std::string attributes("ATTRIBUTES");
auto it = NewKVIterator<CGovView::ByName>(attributes, map);
return it.Valid() && it.Key() == attributes;
};

auto it = NewKVIterator<CUndosView::ByUndoKey>(UndoKey{}, rawMap);
for (; it.Valid(); it.Next()) {
CUndo value = it.Value();
auto& map = value.before;
for (auto it = map.begin(); it != map.end();) {
isAttributes(it->first) ? map.erase(it++) : ++it;
}
auto key = std::make_pair(CUndosView::ByUndoKey::prefix(), static_cast<const UndoKey&>(it.Key()));
rawMap[DbTypeToBytes(key)] = DbTypeToBytes(value);
}

std::vector<uint256> hashes;
for (const auto& it : rawMap) {
auto value = it.second ? *it.second : TBytes{};
hashes.push_back(Hash2(it.first, value));
for (const auto& [key, value] : rawMap) {
if (!isAttributes(key)) {
hashes.push_back(Hash2(key, value ? *value : TBytes{}));
}
}
return ComputeMerkleRoot(std::move(hashes));
}
Expand Down
19 changes: 18 additions & 1 deletion src/masternodes/masternodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ class CAnchorConfirmsView : public virtual CStorageView
struct BtcTx { static constexpr uint8_t prefix() { return 'x'; } };
};

class CSettingsView : public virtual CStorageView
{

public:
const std::string DEX_STATS_LAST_HEIGHT = "DexStatsLastHeight";
const std::string DEX_STATS_ENABLED = "DexStatsEnabled";

void SetDexStatsLastHeight(int32_t height);
std::optional<int32_t> GetDexStatsLastHeight();
void SetDexStatsEnabled(bool enabled);
std::optional<bool> GetDexStatsEnabled();

struct KVSettings { static constexpr uint8_t prefix() { return '0'; } };
};

class CCollateralLoans { // in USD

double calcRatio(uint64_t maxRatio) const;
Expand Down Expand Up @@ -351,6 +366,7 @@ class CCustomCSView
, public CICXOrderView
, public CLoanView
, public CVaultView
, public CSettingsView
{
void CheckPrefixes()
{
Expand Down Expand Up @@ -379,7 +395,8 @@ class CCustomCSView
CLoanView :: LoanSetCollateralTokenCreationTx, LoanSetCollateralTokenKey, LoanSetLoanTokenCreationTx,
LoanSetLoanTokenKey, LoanSchemeKey, DefaultLoanSchemeKey, DelayedLoanSchemeKey,
DestroyLoanSchemeKey, LoanInterestByVault, LoanTokenAmount, LoanLiquidationPenalty, LoanInterestV2ByVault,
CVaultView :: VaultKey, OwnerVaultKey, CollateralKey, AuctionBatchKey, AuctionHeightKey, AuctionBidKey
CVaultView :: VaultKey, OwnerVaultKey, CollateralKey, AuctionBatchKey, AuctionHeightKey, AuctionBidKey,
CSettingsView :: KVSettings
>();
}
private:
Expand Down
Loading