Skip to content

Commit

Permalink
Merge branch 'master' into feature/attrs_impl_legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
prasannavl committed May 24, 2022
2 parents ea0ecae + 14f496f commit a36b155
Show file tree
Hide file tree
Showing 19 changed files with 258 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ src/univalue/gen
# Only ignore unexpected patches
*.patch
!depends/patches/**/*.patch
!contrib/devtools/*.patch

#libtool object files
*.lo
Expand Down
38 changes: 38 additions & 0 deletions contrib/devtools/mocknet.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
diff --git a/src/pos.cpp b/src/pos.cpp
index 1ad8d066d..01b13b8aa 100644
--- a/src/pos.cpp
+++ b/src/pos.cpp
@@ -140,6 +140,11 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, int64_t blockTim

unsigned int nProofOfWorkLimit = UintToArith256(params.pos.diffLimit).GetCompact();

+ // Lower difficulty fork for mock network testing
+ if (fMockNetwork) {
+ return nProofOfWorkLimit;
+ }
+
int nHeight{pindexLast->nHeight + 1};
bool newDifficultyAdjust{nHeight > params.EunosHeight};

diff --git a/src/version.h b/src/version.h
index 3ab303d56..fb84cea92 100644
--- a/src/version.h
+++ b/src/version.h
@@ -9,7 +9,7 @@
* network protocol versioning
*/

-static const int PROTOCOL_VERSION = 70029;
+static const int PROTOCOL_VERSION = 100000;

//! initial proto version, to be increased after version/verack negotiation
static const int INIT_PROTO_VERSION = 209;
@@ -18,7 +18,7 @@ static const int INIT_PROTO_VERSION = 209;
static const int GETHEADERS_VERSION = 31800;

//! disconnect from peers older than this proto version
-static const int MIN_PEER_PROTO_VERSION = 70023;
+static const int MIN_PEER_PROTO_VERSION = 100000;

//! nTime field added to CAddress, starting with this version;
//! if possible, avoid requesting addresses nodes older than this
13 changes: 13 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/case_conv.hpp>

bool fMockNetwork = false;
std::string sMockFoundationPubKey;
uint64_t nMockBlockTimeSecs = 0;

std::vector<CTransactionRef> CChainParams::CreateGenesisMasternodes()
{
std::vector<CTransactionRef> mnTxs;
Expand Down Expand Up @@ -325,6 +329,15 @@ class CMainParams : public CChainParams {
/* nTxCount */ 1091894,
/* dTxRate */ 0.1841462153145931
};

if (fMockNetwork) {
consensus.pos.nTargetSpacing = nMockBlockTimeSecs;
consensus.pos.nTargetTimespanV2 = 10 * consensus.pos.nTargetSpacing;
// Add additional foundation members here for testing
if (!sMockFoundationPubKey.empty()) {
consensus.foundationMembers.insert(GetScriptForDestination(DecodeDestination(sMockFoundationPubKey, *this)));
}
}
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#include <memory>
#include <vector>


/** Used for mocking network with low difficulty for testing */
extern bool fMockNetwork;
extern std::string sMockFoundationPubKey;
extern uint64_t nMockBlockTimeSecs;

struct SeedSpec6 {
uint8_t addr[16];
uint16_t port;
Expand Down
9 changes: 8 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ void SetupServerArgs()

// Hidden Options
std::vector<std::string> hidden_args = {
"-dbcrashratio", "-forcecompactdb", "-interrupt-block=<hash|height>", "-stop-block=<hash|height>",
"-dbcrashratio", "-forcecompactdb",
"-interrupt-block=<hash|height>", "-stop-block=<hash|height>",
"-mocknet", "-mocknet-blocktime=<secs>", "-mocknet-key=<pubkey>"
// GUI args. These will be overwritten by SetupUIArgs for the GUI
"-choosedatadir", "-lang=<lang>", "-min", "-resetguisettings", "-splash"};

Expand Down Expand Up @@ -1218,6 +1220,11 @@ bool AppInitParameterInteraction()
fIsFakeNet = Params().NetworkIDString() == "regtest" && gArgs.GetArg("-dummypos", false);
CTxOut::SERIALIZE_FORCED_TO_OLD_IN_TESTS = Params().NetworkIDString() == "regtest" && gArgs.GetArg("-txnotokens", false);

fMockNetwork = gArgs.IsArgSet("-mocknet");
if (fMockNetwork) {
sMockFoundationPubKey = gArgs.GetArg("-mocknet-key", "");
nMockBlockTimeSecs = gArgs.GetArg("-mocknet-blocktime", 10);
}
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,11 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
ret.pushKV(key, result);
} else if (const auto splitValues = boost::get<OracleSplits>(&attribute.second)) {
std::string keyValue;
for (const auto& [tokenId, multiplier] : *splitValues) {
keyValue += KeyBuilder(tokenId, multiplier) + ',';
for (auto it{splitValues->begin()}; it != splitValues->end(); ++it) {
if (it != splitValues->begin()) {
keyValue += ',';
}
keyValue += KeyBuilder(it->first, it->second);
}
ret.pushKV(key, keyValue);
} else if (const auto& descendantPair = boost::get<DescendantValue>(&attribute.second)) {
Expand Down
3 changes: 0 additions & 3 deletions src/masternodes/loan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ Res CLoanView::SetLoanToken(CLoanSetLoanTokenImpl const & loanToken, DCT_ID cons

Res CLoanView::UpdateLoanToken(CLoanSetLoanTokenImpl const & loanToken, DCT_ID const & id)
{
if (loanToken.interest < 0)
return Res::Err("interest rate cannot be less than 0!");

WriteBy<LoanSetLoanTokenKey>(id, loanToken);

return Res::Ok();
Expand Down
116 changes: 93 additions & 23 deletions src/masternodes/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,10 +537,7 @@ class CCustomMetadataParseVisitor : public boost::static_visitor<Res>

Res operator()(CLoanUpdateLoanTokenMessage& obj) const {
auto res = isPostFortCanningFork();
if (!res)
return res;
res = isPostFortCanningCrunchFork();
return res ? Res::Err("called after FortCanningCrunch height") : serialize(obj);
return !res ? res : serialize(obj);
}

Res operator()(CLoanSchemeMessage& obj) const {
Expand Down Expand Up @@ -2322,9 +2319,23 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
CDataStructureV0 collateralFactor{AttributeTypes::Token, tokenId, TokenKeys::LoanCollateralFactor};
CDataStructureV0 pairKey{AttributeTypes::Token, tokenId, TokenKeys::FixedIntervalPriceId};

attributes->SetValue(collateralEnabled, true);
attributes->SetValue(collateralFactor, obj.factor);
attributes->SetValue(pairKey, obj.fixedIntervalPriceId);
auto gv = GovVariable::Create("ATTRIBUTES");
if (!gv) {
return Res::Err("Failed to create ATTRIBUTES Governance variable");
}

auto var = std::dynamic_pointer_cast<ATTRIBUTES>(gv);
if (!var) {
return Res::Err("Failed to convert ATTRIBUTES Governance variable");
}

var->SetValue(collateralEnabled, true);
var->SetValue(collateralFactor, obj.factor);
var->SetValue(pairKey, obj.fixedIntervalPriceId);

res = attributes->Import(var->Export());
if (!res)
return res;

res = attributes->Validate(mnview);
if (!res)
Expand Down Expand Up @@ -2410,9 +2421,23 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
CDataStructureV0 mintInterest{AttributeTypes::Token, id, TokenKeys::LoanMintingInterest};
CDataStructureV0 pairKey{AttributeTypes::Token, id, TokenKeys::FixedIntervalPriceId};

attributes->SetValue(mintEnabled, obj.mintable);
attributes->SetValue(mintInterest, obj.interest);
attributes->SetValue(pairKey, obj.fixedIntervalPriceId);
auto gv = GovVariable::Create("ATTRIBUTES");
if (!gv) {
return Res::Err("Failed to create ATTRIBUTES Governance variable");
}

auto var = std::dynamic_pointer_cast<ATTRIBUTES>(gv);
if (!var) {
return Res::Err("Failed to convert ATTRIBUTES Governance variable");
}

var->SetValue(mintEnabled, obj.mintable);
var->SetValue(mintInterest, obj.interest);
var->SetValue(pairKey, obj.fixedIntervalPriceId);

res = attributes->Import(var->Export());
if (!res)
return res;

res = attributes->Validate(mnview);
if (!res)
Expand Down Expand Up @@ -2458,7 +2483,16 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
if (!HasFoundationAuth())
return Res::Err("tx not from foundation member!");

auto loanToken = mnview.GetLoanToken(obj.tokenTx);
if (obj.interest < 0)
return Res::Err("interest rate cannot be less than 0!");

auto pair = mnview.GetTokenByCreationTx(obj.tokenTx);
if (!pair)
return Res::Err("Loan token (%s) does not exist!", obj.tokenTx.GetHex());

auto loanToken = height >= static_cast<uint32_t>(consensus.FortCanningCrunchHeight) ?
mnview.GetLoanTokenByID(pair->first) : mnview.GetLoanToken(obj.tokenTx);

if (!loanToken)
return Res::Err("Loan token (%s) does not exist!", obj.tokenTx.GetHex());

Expand All @@ -2468,30 +2502,66 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
if (obj.interest != loanToken->interest)
loanToken->interest = obj.interest;

auto pair = mnview.GetTokenByCreationTx(obj.tokenTx);
if (!pair)
return Res::Err("Loan token (%s) does not exist!", obj.tokenTx.GetHex());

if (obj.symbol != pair->second.symbol)
pair->second.symbol = trim_ws(obj.symbol).substr(0, CToken::MAX_TOKEN_SYMBOL_LENGTH);

if (obj.name != pair->second.name)
pair->second.name = trim_ws(obj.name).substr(0, CToken::MAX_TOKEN_NAME_LENGTH);

if (obj.fixedIntervalPriceId != loanToken->fixedIntervalPriceId) {
if (!OraclePriceFeed(mnview, obj.fixedIntervalPriceId))
return Res::Err("Price feed %s/%s does not belong to any oracle", obj.fixedIntervalPriceId.first, obj.fixedIntervalPriceId.second);

loanToken->fixedIntervalPriceId = obj.fixedIntervalPriceId;
}

if (obj.mintable != (pair->second.flags & (uint8_t)CToken::TokenFlags::Mintable))
pair->second.flags ^= (uint8_t)CToken::TokenFlags::Mintable;

res = mnview.UpdateToken(pair->second);
if (!res)
return res;

if (height >= static_cast<uint32_t>(consensus.FortCanningCrunchHeight))
{
const auto& id = pair->first.v;

auto attributes = mnview.GetAttributes();
attributes->time = time;

CDataStructureV0 mintEnabled{AttributeTypes::Token, id, TokenKeys::LoanMintingEnabled};
CDataStructureV0 mintInterest{AttributeTypes::Token, id, TokenKeys::LoanMintingInterest};
CDataStructureV0 pairKey{AttributeTypes::Token, id, TokenKeys::FixedIntervalPriceId};

auto gv = GovVariable::Create("ATTRIBUTES");
if (!gv) {
return Res::Err("Failed to create ATTRIBUTES Governance variable");
}

auto var = std::dynamic_pointer_cast<ATTRIBUTES>(gv);
if (!var) {
return Res::Err("Failed to convert ATTRIBUTES Governance variable");
}

var->SetValue(mintEnabled, obj.mintable);
var->SetValue(mintInterest, obj.interest);
var->SetValue(pairKey, obj.fixedIntervalPriceId);

res = attributes->Import(var->Export());
if (!res)
return res;

res = attributes->Validate(mnview);
if (!res)
return res;

res = attributes->Apply(mnview, height);
if (!res)
return res;

return mnview.SetVariable(*attributes);
}

if (obj.fixedIntervalPriceId != loanToken->fixedIntervalPriceId) {
if (!OraclePriceFeed(mnview, obj.fixedIntervalPriceId))
return Res::Err("Price feed %s/%s does not belong to any oracle", obj.fixedIntervalPriceId.first, obj.fixedIntervalPriceId.second);

loanToken->fixedIntervalPriceId = obj.fixedIntervalPriceId;
}

return mnview.UpdateLoanToken(*loanToken, pair->first);
}

Expand Down Expand Up @@ -2665,7 +2735,7 @@ class CCustomTxApplyVisitor : public CCustomTxVisitor
if (auto defaultScheme = mnview.GetDefaultLoanScheme()){
vault.schemeId = *defaultScheme;
} else {
return Res::Err("There is not default loan scheme");
return Res::Err("There is no default loan scheme");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/masternodes/rpc_loan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,14 @@ UniValue updateloantoken(const JSONRPCRequest& request) {
UniValue const & txInputs = request.params[2];

boost::optional<CLoanSetLoanTokenImplementation> loanToken;
CTokenImplementation tokenImpl;
boost::optional<CTokenImplementation> token;

int targetHeight;
{
LOCK(cs_main);

DCT_ID id;
auto token = pcustomcsview->GetTokenGuessId(tokenStr, id);
token = pcustomcsview->GetTokenGuessId(tokenStr, id);
if (!token) {
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Token %s does not exist!", tokenStr));
}
Expand Down Expand Up @@ -469,7 +469,7 @@ UniValue updateloantoken(const JSONRPCRequest& request) {

CDataStream metadata(DfTxMarker, SER_NETWORK, PROTOCOL_VERSION);
metadata << static_cast<unsigned char>(CustomTxType::UpdateLoanToken)
<< static_cast<CLoanSetLoanToken>(*loanToken) << loanToken->creationTx;
<< static_cast<CLoanSetLoanToken>(*loanToken) << token->creationTx;

CScript scriptMeta;
scriptMeta << OP_RETURN << ToByteVector(metadata);
Expand Down
7 changes: 5 additions & 2 deletions src/masternodes/tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Res CTokensView::RevertCreateToken(const uint256 & txid)
return Res::Ok();
}

Res CTokensView::UpdateToken(const CTokenImpl& newToken, bool isPreBayfront, const bool skipNameValidation)
Res CTokensView::UpdateToken(const CTokenImpl& newToken, bool isPreBayfront, const bool tokenSplitUpdate)
{
auto pair = GetTokenByCreationTx(newToken.creationTx);
if (!pair) {
Expand All @@ -152,7 +152,7 @@ Res CTokensView::UpdateToken(const CTokenImpl& newToken, bool isPreBayfront, con
oldToken.name = newToken.name;

// check new symbol correctness
if (!skipNameValidation) {
if (!tokenSplitUpdate) {
auto checkSymbolRes = newToken.IsValidSymbol();
if (!checkSymbolRes.ok) {
return checkSymbolRes;
Expand Down Expand Up @@ -186,6 +186,9 @@ Res CTokensView::UpdateToken(const CTokenImpl& newToken, bool isPreBayfront, con
if (!oldToken.IsFinalized() && newToken.IsFinalized()) // IsFinalized() itself was checked upthere (with Err)
oldToken.flags |= (uint8_t)CToken::TokenFlags::Finalized;

if (tokenSplitUpdate && oldToken.IsLoanToken() != newToken.IsLoanToken())
oldToken.flags ^= (uint8_t)CToken::TokenFlags::LoanToken;

if (oldToken.destructionHeight != newToken.destructionHeight) {
oldToken.destructionHeight = newToken.destructionHeight;
}
Expand Down
2 changes: 1 addition & 1 deletion src/masternodes/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class CTokensView : public virtual CStorageView
Res CreateDFIToken();
ResVal<DCT_ID> CreateToken(CTokenImpl const & token, bool isPreBayfront = false);
Res RevertCreateToken(uint256 const & txid);
Res UpdateToken(CTokenImpl const & newToken, bool isPreBayfront = false, const bool skipNameValidation = false);
Res UpdateToken(CTokenImpl const & newToken, bool isPreBayfront = false, const bool tokenSplitUpdatea = false);

Res BayfrontFlagsCleanup();
Res AddMintedTokens(DCT_ID const & id, CAmount const & amount);
Expand Down
Loading

0 comments on commit a36b155

Please sign in to comment.