From 9cdd498dabd7dca10224b71213e6ee19b6d0da3d Mon Sep 17 00:00:00 2001 From: Mihailo Milenkovic Date: Wed, 25 May 2022 16:48:52 +0200 Subject: [PATCH] Make setcollateraltoken and set/updateloantoken work old way before token migration to gov var (#1289) * setcollateraltoken and set/updateloantoken work old way before FCC + 2 height (migration happens on end of FCC +1) * Add FCC height check for better code readability * Fix tests Co-authored-by: Prasanna Loganathar --- src/masternodes/mn_checks.cpp | 11 +++-- .../feature_loan_setcollateraltoken.py | 8 ++++ test/functional/feature_loan_setloantoken.py | 3 ++ test/functional/feature_token_lock.py | 0 test/functional/feature_token_split.py | 46 ++++++++++++++++++- 5 files changed, 62 insertions(+), 6 deletions(-) mode change 100644 => 100755 test/functional/feature_token_lock.py diff --git a/src/masternodes/mn_checks.cpp b/src/masternodes/mn_checks.cpp index a89d8acf59..3e26e10707 100644 --- a/src/masternodes/mn_checks.cpp +++ b/src/masternodes/mn_checks.cpp @@ -879,6 +879,9 @@ class CCustomTxVisitor : public boost::static_visitor tokenCurrency = std::move(trimmed); return Res::Ok(); } + bool IsTokensMigratedToGovVar() const { + return static_cast(height) > consensus.FortCanningCrunchHeight + 1; + } }; class CCustomTxApplyVisitor : public CCustomTxVisitor @@ -2308,7 +2311,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor if (!HasFoundationAuth()) return Res::Err("tx not from foundation member!"); - if (height >= static_cast(consensus.FortCanningCrunchHeight)) + if (height >= static_cast(consensus.FortCanningCrunchHeight) && IsTokensMigratedToGovVar()) { const auto& tokenId = obj.idToken.v; @@ -2410,7 +2413,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor if (!tokenId) return std::move(tokenId); - if (height >= static_cast(consensus.FortCanningCrunchHeight)) + if (height >= static_cast(consensus.FortCanningCrunchHeight) && IsTokensMigratedToGovVar()) { const auto& id = tokenId.val->v; @@ -2490,7 +2493,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor if (!pair) return Res::Err("Loan token (%s) does not exist!", obj.tokenTx.GetHex()); - auto loanToken = height >= static_cast(consensus.FortCanningCrunchHeight) ? + auto loanToken = (height >= static_cast(consensus.FortCanningCrunchHeight) && IsTokensMigratedToGovVar()) ? mnview.GetLoanTokenByID(pair->first) : mnview.GetLoanToken(obj.tokenTx); if (!loanToken) @@ -2515,7 +2518,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor if (!res) return res; - if (height >= static_cast(consensus.FortCanningCrunchHeight)) + if (height >= static_cast(consensus.FortCanningCrunchHeight) && IsTokensMigratedToGovVar()) { const auto& id = pair->first.v; diff --git a/test/functional/feature_loan_setcollateraltoken.py b/test/functional/feature_loan_setcollateraltoken.py index f187b50f3a..1a63402686 100755 --- a/test/functional/feature_loan_setcollateraltoken.py +++ b/test/functional/feature_loan_setcollateraltoken.py @@ -207,6 +207,14 @@ def run_test(self): # Move to fork height self.nodes[0].generate(150 - self.nodes[0].getblockcount()) + # Check errors on FCC + assert_raises_rpc_error(-32600, "setCollateralToken factor must be lower or equal than 1.00000000", self.nodes[0].setcollateraltoken, { + 'token': idDFI, + 'factor': 1.01, + 'fixedIntervalPriceId': "DFI/USD"}) + + self.nodes[0].generate(1) + # Check errors assert_raises_rpc_error(-32600, "Percentage exceeds 100%", self.nodes[0].setcollateraltoken, { 'token': idDFI, diff --git a/test/functional/feature_loan_setloantoken.py b/test/functional/feature_loan_setloantoken.py index d5a37df2ad..0e14c9e896 100755 --- a/test/functional/feature_loan_setloantoken.py +++ b/test/functional/feature_loan_setloantoken.py @@ -155,6 +155,9 @@ def run_test(self): # Move to fork height self.nodes[0].generate(110 - self.nodes[0].getblockcount()) + # Move to FCC +1 + self.nodes[0].generate(1) + assert_raises_rpc_error(-32600, 'token symbol should be non-empty and starts with a letter', self.nodes[0].setloantoken, { 'symbol': "", 'name': "Google", diff --git a/test/functional/feature_token_lock.py b/test/functional/feature_token_lock.py old mode 100644 new mode 100755 diff --git a/test/functional/feature_token_split.py b/test/functional/feature_token_split.py index ae643679a3..0126d6bf5f 100755 --- a/test/functional/feature_token_split.py +++ b/test/functional/feature_token_split.py @@ -364,8 +364,41 @@ def check_pool_split(self, pool_id, pool_symbol, token_id, token_symbol, token_s def token_split(self): - # Move to GW - self.nodes[0].generate(151 - self.nodes[0].getblockcount()) + # Move to FCC + self.nodes[0].generate(149 - self.nodes[0].getblockcount()) + + # Mined in 150 height, still using old code + self.nodes[0].updateloantoken(self.idGOOGL, { + 'name': 'AAAA', + 'interest': 1 + }) + + self.nodes[0].generate(1) + + result = self.nodes[0].getgov('ATTRIBUTES')['ATTRIBUTES'] + assert_equal(result, {}) + + token = self.nodes[0].getloantoken(self.idGOOGL) + assert_equal(token['token']['1']['name'], 'AAAA') + assert_equal(token['interest'], Decimal(1)) + + # Mining height 151 - migration of token to gov var + self.nodes[0].generate(1) + + result = self.nodes[0].getgov('ATTRIBUTES')['ATTRIBUTES'] + assert_equal(result[f'v0/token/{self.idGOOGL}/loan_minting_enabled'], 'true') + assert_equal(result[f'v0/token/{self.idGOOGL}/loan_minting_interest'], '1') + assert_equal(result[f'v0/token/{self.idGOOGL}/fixed_interval_price_id'], f"{self.symbolGOOGL}/USD") + + token = self.nodes[0].getloantoken(self.idGOOGL) + assert_equal(token['token']['1']['name'], 'AAAA') + assert_equal(token['interest'], Decimal(1)) + + # Mined in 152 height, using new code + self.nodes[0].updateloantoken(self.idGOOGL, { + 'name': self.symbolGOOGL, + 'interest': 0 + }) # Set extra Gov vars for token self.nodes[0].setgov({"ATTRIBUTES":{f'v0/token/{self.idTSLA}/dfip2203':'true', @@ -377,6 +410,15 @@ def token_split(self): f'v0/token/{self.idTSLA}/loan_payback_fee_pct/{self.idDUSD}': '0.25'}}) self.nodes[0].generate(1) + result = self.nodes[0].getgov('ATTRIBUTES')['ATTRIBUTES'] + assert_equal(result[f'v0/token/{self.idGOOGL}/loan_minting_enabled'], 'true') + assert_equal(result[f'v0/token/{self.idGOOGL}/loan_minting_interest'], '0') + assert_equal(result[f'v0/token/{self.idGOOGL}/fixed_interval_price_id'], f"{self.symbolGOOGL}/USD") + + token = self.nodes[0].getloantoken(self.idGOOGL) + assert_equal(token['token']['1']['name'], self.symbolGOOGL) + assert_equal(token['interest'], Decimal(0)) + # Make sure we cannot make a token with '/' in its symbol assert_raises_rpc_error(-32600, "token symbol should not contain '/'", self.nodes[0].createtoken, { 'symbol': 'bad/v1',