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

Add legacy filtering mode #1271

Merged
merged 14 commits into from
May 24, 2022
Merged
23 changes: 21 additions & 2 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,34 @@ Res ATTRIBUTES::Import(const UniValue & val) {
return Res::Ok();
}

// Keys to exclude when using the legacy filter mode, to keep things the
// same as pre 2.7.x versions, to reduce noise. Eventually, the APIs that
// cause too much noise can be deprecated and this code removed.
std::set<uint32_t> attrsVersion27TokenHiddenSet = {
TokenKeys::LoanCollateralEnabled,
TokenKeys::LoanCollateralFactor,
TokenKeys::LoanMintingEnabled,
TokenKeys::LoanMintingInterest,
TokenKeys::FixedIntervalPriceId,
TokenKeys::Ascendant,
TokenKeys::Descendant,
TokenKeys::Epitaph,
};

UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &prefix) const {
UniValue ret(UniValue::VOBJ);
for (const auto& attribute : attributes) {
auto attrV0 = boost::get<const CDataStructureV0>(&attribute.first);
if (!attrV0) {
continue;
}
if (filter == GovVarsFilter::LiveAttributes && attrV0->type != AttributeTypes::Live) {
continue;
if (filter == GovVarsFilter::LiveAttributes &&
attrV0->type != AttributeTypes::Live) {
continue;
} else if (filter == GovVarsFilter::Version2Dot7) {
if (attrV0->type == AttributeTypes::Token &&
attrsVersion27TokenHiddenSet.find(attrV0->key) != attrsVersion27TokenHiddenSet.end())
continue;
}
try {
std::string id;
Expand Down
3 changes: 2 additions & 1 deletion src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ using CAttributeType = boost::variant<CDataStructureV0, CDataStructureV1>;
using CAttributeValue = boost::variant<bool, CAmount, CBalances, CTokenPayback, CTokenCurrencyPair, OracleSplits, DescendantValue, AscendantValue>;

enum GovVarsFilter {
Legacy,
All,
NoAttributes,
AttributesOnly,
PrefixedAttributes,
LiveAttributes,
Version2Dot7,
};

class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRIBUTES>
Expand Down
45 changes: 25 additions & 20 deletions src/masternodes/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@ UniValue listgovs(const JSONRPCRequest& request) {
"\nReturns information about all governance variables including pending changes\n",
{
{"prefix", RPCArg::Type::STR, RPCArg::Optional::OMITTED,
"One of all, legacy, gov, live. Any other string is treated as a prefix of attributes to filter with. `v0/` is assumed if not explicitly provided."},
"One of all, gov, attrs, live. Defaults to the all view. Any other string is treated as\n"
"a prefix of attributes to filter with. `v0/` is assumed if not explicitly provided."},
},
RPCResult{
"[[{id:{...}},{height:{...}},...], ...] (array) Json array with JSON objects with variable information\n"
Expand All @@ -711,28 +712,31 @@ UniValue listgovs(const JSONRPCRequest& request) {
},
}.Check(request);

GovVarsFilter mode;
GovVarsFilter mode{GovVarsFilter::All};
std::string prefix;
if (request.params.size() > 0) {
prefix = request.params[0].getValStr();
}
if (prefix.empty()) {
prefix = "legacy";
}

if (prefix == "all") {
mode = GovVarsFilter::All;
} else if (prefix == "legacy") {
mode = GovVarsFilter::Legacy;
} else if (prefix == "gov") {
mode = GovVarsFilter::NoAttributes;
} else if (prefix == "live") {
mode = GovVarsFilter::LiveAttributes;
} else {
mode = GovVarsFilter::PrefixedAttributes;
const std::regex versionRegex("v[0-9].*");
if (!std::regex_match(prefix.begin(), prefix.end(), versionRegex)) {
prefix = "v0/" + prefix;
if (!prefix.empty()) {
if (prefix == "all") {
mode = GovVarsFilter::All;
} else if (prefix == "gov") {
mode = GovVarsFilter::NoAttributes;
} else if (prefix == "attrs") {
mode = GovVarsFilter::AttributesOnly;
} else if (prefix == "v/2.7") {
// Undocumented. Make be removed or deprecated without notice.
// Only here for unforeseen compatibility concern downstream
// for transitions.
mode = GovVarsFilter::Version2Dot7;
} else if (prefix == "live") {
mode = GovVarsFilter::LiveAttributes;
} else {
mode = GovVarsFilter::PrefixedAttributes;
const std::regex versionRegex("^v[0-9].*");
if (!std::regex_match(prefix.begin(), prefix.end(), versionRegex)) {
prefix = "v0/" + prefix;
}
}
}

Expand Down Expand Up @@ -762,7 +766,8 @@ UniValue listgovs(const JSONRPCRequest& request) {
}
} else {
if (mode == GovVarsFilter::LiveAttributes ||
mode == GovVarsFilter::PrefixedAttributes) {
mode == GovVarsFilter::PrefixedAttributes ||
mode == GovVarsFilter::AttributesOnly){
continue;
}
val = var->Export();
Expand Down
7 changes: 4 additions & 3 deletions test/functional/feature_setgov.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,11 +620,12 @@ def run_test(self):
assert_equal(self.nodes[0].listgovs("live"), [[{'ATTRIBUTES': {}}]])

result_all = self.nodes[0].listgovs("all")
result_legacy = self.nodes[0].listgovs("legacy")
result_27 = self.nodes[0].listgovs("v/2.7")
result = self.nodes[0].listgovs()

# For now it's all the same.
assert_equal(result, result_legacy)
# In this particular test, in the fork period, it's the same.
# TODO: Need to add more tests
assert_equal(result, result_27)
assert_equal(result, result_all)

assert_equal(result, [[{'ICX_TAKERFEE_PER_BTC': Decimal('0.00200000')}], [{'LP_DAILY_LOAN_TOKEN_REWARD': Decimal('13020.86331792')}], [{'LP_LOAN_TOKEN_SPLITS': {'1': Decimal('0.10000000'), '2': Decimal('0.20000000'), '3': Decimal('0.70000000')}}], [{'LP_DAILY_DFI_REWARD': Decimal('13427.10581184')}], [{'LOAN_LIQUIDATION_PENALTY': Decimal('0.01000000')}], [{'LP_SPLITS': {'1': Decimal('0.70000000'), '2': Decimal('0.20000000'), '3': Decimal('0.10000000')}}], [{'ORACLE_BLOCK_INTERVAL': 30}], [{'ORACLE_DEVIATION': Decimal('0.07000000')}], [{'ATTRIBUTES': {'v0/params/dfip2201/active': 'true', 'v0/params/dfip2201/premium': '0.025', 'v0/params/dfip2201/minswap': '0.001', 'v0/params/dfip2203/active': 'true', 'v0/params/dfip2203/reward_pct': '0.05', 'v0/params/dfip2203/block_period': '20160', 'v0/token/5/payback_dfi': 'true', 'v0/token/5/payback_dfi_fee_pct': '0.33', 'v0/token/5/loan_payback/1': 'true', 'v0/token/5/loan_payback/2': 'true', 'v0/token/5/loan_payback_fee_pct/1': '0.25', 'v0/token/5/dex_in_fee_pct': '0.6', 'v0/token/5/dex_out_fee_pct': '0.12', 'v0/token/5/dfip2203': 'true'}}]])
Expand Down
8 changes: 4 additions & 4 deletions test/functional/feature_token_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def check_token_split(self, token_id, token_symbol, token_suffix, multiplier, mi
assert_equal(result['destructionHeight'], self.nodes[0].getblockcount())

# Check old token in Gov vars
result = self.nodes[0].listgovs()[8][0]['ATTRIBUTES']
result = self.nodes[0].listgovs("attrs")[0][0]['ATTRIBUTES']
assert(f'v0/token/{token_id}/fixed_interval_price_id' not in result)
if collateral:
assert(f'v0/token/{token_id}/loan_collateral_enabled' not in result)
Expand Down Expand Up @@ -318,7 +318,7 @@ def check_pool_split(self, pool_id, pool_symbol, token_id, token_symbol, token_s
assert_equal(result['rewardLoanPct'], Decimal('0.00000000'))

# Validate old Gov vars removed
result = self.nodes[0].listgovs()[8][0]['ATTRIBUTES']
result = self.nodes[0].listgovs("attrs")[0][0]['ATTRIBUTES']
assert(f'v0/poolpairs/{pool_id}/token_a_fee_pct' not in result)
assert(f'v0/poolpairs/{pool_id}/token_b_fee_pct' not in result)
assert(f'v0/token/{token_id}/dex_in_fee_pct' not in result)
Expand Down Expand Up @@ -619,7 +619,7 @@ def check_govvar_deletion(self):
self.nodes[0].generate(1)

# Check splits
result = self.nodes[0].listgovs()[8][0]['ATTRIBUTES']
result = self.nodes[0].listgovs("attrs")[0][0]['ATTRIBUTES']
assert_equal(result[f'v0/oracles/splits/{split_height}'], f'{self.idTSLA}/2')
assert_equal(result[f'v0/oracles/splits/500000'], f'{self.idTSLA}/2')
assert_equal(result[f'v0/oracles/splits/1000000'], f'{self.idTSLA}/2,{self.idNVDA}/2')
Expand All @@ -628,7 +628,7 @@ def check_govvar_deletion(self):
self.nodes[0].generate(1)

# Check TSLA entries removed
result = self.nodes[0].listgovs()[8][0]['ATTRIBUTES']
result = self.nodes[0].listgovs("attrs")[0][0]['ATTRIBUTES']
assert(f'v0/oracles/splits/{split_height}' not in result)
assert(f'v0/oracles/splits/500000' not in result)
assert_equal(result[f'v0/oracles/splits/1000000'], f'{self.idNVDA}/2')
Expand Down