Skip to content

Commit

Permalink
Add finalized block depth to Gov vars (#2188)
Browse files Browse the repository at this point in the history
* Add finalized block depth to Gov vars

* varname change: `finality_count`

* Adding missing rename

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Jul 19, 2023
1 parent bf16610 commit a5fa496
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
51 changes: 50 additions & 1 deletion src/masternodes/govvariables/attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const std::map<std::string, uint8_t> &ATTRIBUTES::allowedTypes() {
{"gov", AttributeTypes::Governance},
{"consortium", AttributeTypes::Consortium},
{"transferdomain", AttributeTypes::Transfer },
{"evm", AttributeTypes::EVMType },
{"vaults", AttributeTypes::Vaults },
};
return types;
Expand All @@ -75,6 +76,7 @@ const std::map<uint8_t, std::string> &ATTRIBUTES::displayTypes() {
{AttributeTypes::Governance, "gov" },
{AttributeTypes::Consortium, "consortium" },
{AttributeTypes::Transfer, "transferdomain"},
{AttributeTypes::EVMType, "evm" },
{AttributeTypes::Vaults, "vaults" },
};
return types;
Expand Down Expand Up @@ -132,6 +134,20 @@ const std::map<uint8_t, std::string> &ATTRIBUTES::displayOracleIDs() {
return params;
}

const std::map<std::string, uint8_t> &ATTRIBUTES::allowedEVMIDs() {
static const std::map<std::string, uint8_t> params{
{"block", EVMIDs::Block},
};
return params;
}

const std::map<uint8_t, std::string> &ATTRIBUTES::displayEVMIDs() {
static const std::map<uint8_t, std::string> params{
{EVMIDs::Block, "block"},
};
return params;
}

const std::map<std::string, uint8_t> &ATTRIBUTES::allowedGovernanceIDs() {
static const std::map<std::string, uint8_t> params{
{"proposals", GovernanceIDs::Proposals},
Expand Down Expand Up @@ -230,6 +246,10 @@ const std::map<uint8_t, std::map<std::string, uint8_t>> &ATTRIBUTES::allowedKeys
{"mint-tokens-to-address", DFIPKeys::MintTokens},
{"transferdomain", DFIPKeys::TransferDomain},
}},
{AttributeTypes::EVMType,
{
{"finality_count", EVMKeys::Finalized},
}},
{AttributeTypes::Governance,
{
{"fee_redistribution", GovernanceKeys::FeeRedistribution},
Expand Down Expand Up @@ -317,6 +337,10 @@ const std::map<uint8_t, std::map<uint8_t, std::string>> &ATTRIBUTES::displayKeys
{DFIPKeys::MintTokens, "mint-tokens-to-address"},
{DFIPKeys::TransferDomain, "transferdomain"},
}},
{AttributeTypes::EVMType,
{
{EVMKeys::Finalized, "finality_count"},
}},
{AttributeTypes::Live,
{
{EconomyKeys::PaybackDFITokens, "dfi_payback_tokens"},
Expand Down Expand Up @@ -663,6 +687,10 @@ const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(
{
{OracleIDs::Splits, VerifySplit},
}},
{AttributeTypes::EVMType,
{
{EVMKeys::Finalized, VerifyUInt32},
}},
{AttributeTypes::Governance,
{
{GovernanceKeys::FeeRedistribution, VerifyBool},
Expand Down Expand Up @@ -794,7 +822,13 @@ Res ATTRIBUTES::ProcessVariable(const std::string &key,
return DeFiErrors::GovVarVariableInvalidKey("locks", allowedLocksIDs());
}
typeId = id->second;
} else if (type == AttributeTypes::Oracles) {
} else if (type == AttributeTypes::EVMType) {
auto id = allowedEVMIDs().find(keys[2]);
if (id == allowedEVMIDs().end()) {
return DeFiErrors::GovVarVariableInvalidKey("evm", allowedEVMIDs());
}
typeId = id->second;
}else if (type == AttributeTypes::Oracles) {
auto id = allowedOracleIDs().find(keys[2]);
if (id == allowedOracleIDs().end()) {
return DeFiErrors::GovVarVariableInvalidKey("oracles", allowedOracleIDs());
Expand Down Expand Up @@ -897,6 +931,13 @@ Res ATTRIBUTES::ProcessVariable(const std::string &key,
} else {
return DeFiErrors::GovVarVariableUnsupportedParamType();
}
} else if (type == AttributeTypes::EVMType) {
if (typeId == EVMIDs::Block) {
if (typeKey != EVMKeys::Finalized)
return DeFiErrors::GovVarVariableUnsupportedTransferType(typeKey);
} else {
return DeFiErrors::GovVarVariableUnsupportedGovType();
}
} else if (type == AttributeTypes::Governance) {
if (typeId == GovernanceIDs::Proposals) {
if (typeKey != GovernanceKeys::FeeRedistribution && typeKey != GovernanceKeys::FeeBurnPct &&
Expand Down Expand Up @@ -1228,6 +1269,8 @@ UniValue ATTRIBUTES::ExportFiltered(GovVarsFilter filter, const std::string &pre
if (attrV0->type == AttributeTypes::Param || attrV0->type == AttributeTypes::Live ||
attrV0->type == AttributeTypes::Locks) {
id = displayParamsIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::EVMType) {
id = displayEVMIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::Oracles) {
id = displayOracleIDs().at(attrV0->typeId);
} else if (attrV0->type == AttributeTypes::Governance) {
Expand Down Expand Up @@ -1647,6 +1690,12 @@ Res ATTRIBUTES::Validate(const CCustomCSView &view) const {
}
break;

case AttributeTypes::EVMType:
if (view.GetLastHeight() < Params().GetConsensus().NextNetworkUpgradeHeight) {
return Res::Err("Cannot be set before NextNetworkUpgrade");
}
break;

case AttributeTypes::Transfer:
if (view.GetLastHeight() < Params().GetConsensus().NextNetworkUpgradeHeight) {
return Res::Err("Cannot be set before NextNetworkUpgrade");
Expand Down
11 changes: 11 additions & 0 deletions src/masternodes/govvariables/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ enum AttributeTypes : uint8_t {
Governance = 'g',
Consortium = 'c',
Transfer = 'b',
EVMType = 'e',
Vaults = 'v',
};

Expand All @@ -43,6 +44,14 @@ enum OracleIDs : uint8_t {
Splits = 'a',
};

enum EVMIDs : uint8_t {
Block = 'a',
};

enum EVMKeys : uint8_t {
Finalized = 'a',
};

enum GovernanceIDs : uint8_t {
Global = 'a',
Proposals = 'b',
Expand Down Expand Up @@ -444,6 +453,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<uint8_t, std::string> &displayConsortiumIDs();
static const std::map<uint8_t, std::string> &displayGovernanceIDs();
static const std::map<uint8_t, std::string> &displayTransferIDs();
static const std::map<uint8_t, std::string> &displayEVMIDs();
static const std::map<uint8_t, std::string> &displayVaultIDs();
static const std::map<uint8_t, std::map<uint8_t, std::string>> &displayKeys();

Expand All @@ -469,6 +479,7 @@ class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRI
static const std::map<std::string, uint8_t> &allowedConsortiumIDs();
static const std::map<std::string, uint8_t> &allowedGovernanceIDs();
static const std::map<std::string, uint8_t> &allowedTransferIDs();
static const std::map<std::string, uint8_t> &allowedEVMIDs();
static const std::map<std::string, uint8_t> &allowedVaultIDs();
static const std::map<uint8_t, std::map<std::string, uint8_t>> &allowedKeys();
static const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>>
Expand Down
3 changes: 0 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2599,9 +2599,6 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl

txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated

// Variable to tally total gas used in the block
uint64_t totalGas{};

// Execute TXs
for (unsigned int i = 0; i < block.vtx.size(); i++)
{
Expand Down
8 changes: 8 additions & 0 deletions test/functional/feature_evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ def run_test(self):
block_txs = self.nodes[0].getblock(self.nodes[0].getblockhash(self.nodes[0].getblockcount()))['tx']
assert_equal(len(block_txs), 1)

# Test setting of finalized block
self.nodes[0].setgov({"ATTRIBUTES": {'v0/evm/block/finality_count': '100'}})
self.nodes[0].generate(1)

# Check Gov var is present
attrs = self.nodes[0].getgov("ATTRIBUTES")['ATTRIBUTES']
assert_equal(attrs['v0/evm/block/finality_count'], '100')

# Test that node should not crash without chainId param
key_pair = KeyPair.from_node(self.nodes[0])
self.test_tx_without_chainid(self.nodes[0], key_pair, web3)
Expand Down

0 comments on commit a5fa496

Please sign in to comment.