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 block and transaction context #2563

Merged
merged 26 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
330f629
Add block context to reduce arguments to functions
Bushstar Oct 10, 2023
2946230
Add const qualifier
Bushstar Oct 10, 2023
9924336
Merge branch 'master' into bush/blockctx
prasannavl Oct 11, 2023
addf7fd
Merge branch 'master' into bush/blockctx
Bushstar Oct 13, 2023
d5bb80b
Post merge changes
Bushstar Oct 13, 2023
054ae22
lint: circular deps
Bushstar Oct 13, 2023
479899c
CPP format
Bushstar Oct 13, 2023
5dd723c
Add transaction context
Bushstar Oct 16, 2023
aa4e3ae
Merge branch 'master' into bush/blockctx
Bushstar Oct 16, 2023
1a73e82
Merge branch 'master' into bush/blockctx
prasannavl Oct 17, 2023
a0139c0
Lock before global view usage. Assert global view.
Bushstar Oct 17, 2023
cc88c45
Logging for CI failure
Bushstar Oct 18, 2023
508846e
Format CPP
Bushstar Oct 18, 2023
0f1aba8
Resolve undefined behaviour to fix failing test
Bushstar Oct 18, 2023
60976b0
Move assert into GetAttributes()
Bushstar Oct 20, 2023
8addf49
Add getters to BlockContext
Bushstar Oct 20, 2023
1492e79
Do not expand BlockContext in CCustomTxVisitor
Bushstar Oct 20, 2023
edcff15
Move view into BlockContext
Bushstar Oct 20, 2023
fd21084
Fix failing test
Bushstar Oct 21, 2023
40316b1
Get mnview from BlockContext in consensus
Bushstar Oct 21, 2023
67d3817
TransactionContext add Getters. Make member vars private.
Bushstar Oct 21, 2023
c6dbd12
Get vars from TransactionContext in consensus
Bushstar Oct 21, 2023
9c5fd99
Merge branch 'master' into bush/blockctx
Bushstar Oct 24, 2023
bf47160
Rename remaining names after merge
Bushstar Oct 24, 2023
0299768
Merge branch 'master' into bush/blockctx
prasannavl Oct 24, 2023
9ba1042
Rename EVMTemplate; add comments
prasannavl Oct 25, 2023
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
13 changes: 10 additions & 3 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <dfi/mn_checks.h>
#include <primitives/transaction.h>
#include <script/interpreter.h>
#include <validation.h>

// TODO remove the following dependencies
#include <chain.h>
Expand Down Expand Up @@ -178,12 +179,18 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
std::vector<unsigned char> dummy;
const auto txType = GuessCustomTxType(tx, dummy);

if (IsBelowDakotaMintTokenOrAccountToUtxos(txType, nSpendHeight) || (nSpendHeight >= chainparams.GetConsensus().DF20GrandCentralHeight && txType == CustomTxType::UpdateMasternode)) {
if (IsBelowDF6MintTokenOrAccountToUtxos(txType, nSpendHeight) || (nSpendHeight >= chainparams.GetConsensus().DF20GrandCentralHeight && txType == CustomTxType::UpdateMasternode)) {
CCustomCSView discardCache(mnview, nullptr, nullptr, nullptr);
// Note: TXs are already filtered. So we pass isEVMEnabled to false, but for future proof, refactor this enough,
// that it's propagated.
std::shared_ptr<CScopedTemplate> evmTemplate{};
auto res = ApplyCustomTx(discardCache, inputs, tx, chainparams.GetConsensus(), nSpendHeight, 0, &canSpend, 0, evmTemplate, false, false);
BlockContext blockCtx{&discardCache};
const auto txCtx = TransactionContext{
inputs,
tx,
chainparams.GetConsensus(),
static_cast<uint32_t>(nSpendHeight),
};
auto res = ApplyCustomTx(blockCtx, txCtx, &canSpend);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-txns-customtx", res.msg);
}
Expand Down
4 changes: 4 additions & 0 deletions src/dfi/consensus/accounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <dfi/balances.h>
#include <dfi/consensus/accounts.h>
#include <primitives/transaction.h>
#include <validation.h>

static ResVal<CBalances> BurntTokens(const CTransaction &tx) {
CBalances balances;
Expand All @@ -20,6 +21,8 @@ static ResVal<CBalances> BurntTokens(const CTransaction &tx) {
}

Res CAccountsConsensus::operator()(const CUtxosToAccountMessage &obj) const {
const auto &tx = txCtx.GetTransaction();

// check enough tokens are "burnt"
auto burnt = BurntTokens(tx);
if (!burnt) {
Expand Down Expand Up @@ -76,6 +79,7 @@ Res CAccountsConsensus::operator()(const CAccountToAccountMessage &obj) const {
if (auto res = SubBalanceDelShares(obj.from, SumAllTransfers(obj.to)); !res) {
return res;
}

return AddBalancesSetShares(obj.to);
}

Expand Down
25 changes: 16 additions & 9 deletions src/dfi/consensus/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
#include <dfi/consensus/governance.h>
#include <dfi/govvariables/attributes.h>
#include <dfi/masternodes.h>
#include <validation.h>

Res CGovernanceConsensus::operator()(const CGovernanceMessage &obj) const {
// check foundation auth
if (auto res = HasFoundationAuth(); !res) {
return res;
}

const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto time = txCtx.GetTime();
auto &mnview = blockCtx.GetView();

for (const auto &gov : obj.govs) {
if (!gov.second) {
return Res::Err("'%s': variable does not registered", gov.first);
Expand All @@ -23,12 +30,8 @@ Res CGovernanceConsensus::operator()(const CGovernanceMessage &obj) const {
// Add to existing ATTRIBUTES instead of overwriting.
auto govVar = mnview.GetAttributes();

if (!govVar) {
return Res::Err("%s: %s", var->GetName(), "Failed to get existing ATTRIBUTES");
}

govVar->time = time;
govVar->evmTemplate = evmTemplate;
govVar->evmTemplate = blockCtx.GetEVMTemplate();

auto newVar = std::dynamic_pointer_cast<ATTRIBUTES>(var);
if (!newVar) {
Expand Down Expand Up @@ -137,8 +140,10 @@ Res CGovernanceConsensus::operator()(const CGovernanceUnsetMessage &obj) const {
return Res::Err("tx not from foundation member");
}

const auto height = txCtx.GetHeight();
auto &mnview = blockCtx.GetView();
const auto attributes = mnview.GetAttributes();
assert(attributes);

CDataStructureV0 key{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::GovUnset};
if (!attributes->GetValue(key, false)) {
return Res::Err("Unset Gov variables not currently enabled in attributes.");
Expand Down Expand Up @@ -167,6 +172,11 @@ Res CGovernanceConsensus::operator()(const CGovernanceHeightMessage &obj) const
if (!HasFoundationAuth()) {
return Res::Err("tx not from foundation member");
}

const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
auto &mnview = blockCtx.GetView();

if (obj.startHeight <= height) {
return Res::Err("startHeight must be above the current block height");
}
Expand All @@ -179,9 +189,6 @@ Res CGovernanceConsensus::operator()(const CGovernanceHeightMessage &obj) const
if (height >= static_cast<uint32_t>(consensus.DF16FortCanningCrunchHeight) &&
obj.govVar->GetName() == "ATTRIBUTES") {
auto govVar = mnview.GetAttributes();
if (!govVar) {
return Res::Err("%s: %s", obj.govVar->GetName(), "Failed to get existing ATTRIBUTES");
}

if (height >= static_cast<uint32_t>(consensus.DF22MetachainHeight)) {
auto newVar = std::dynamic_pointer_cast<ATTRIBUTES>(obj.govVar);
Expand Down
36 changes: 35 additions & 1 deletion src/dfi/consensus/icxorders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ bool IsICXEnabled(const int height, const CCustomCSView &view, const Consensus::
if (height >= consensus.DF22MetachainHeight) {
const CDataStructureV0 enabledKey{AttributeTypes::Param, ParamIDs::Feature, DFIPKeys::ICXEnabled};
auto attributes = view.GetAttributes();
assert(attributes);
return attributes->GetValue(enabledKey, false);
}
// ICX transactions allowed before NextNetwrokUpgrade and some of these conditions
Expand All @@ -33,6 +32,7 @@ static CAmount GetDFIperBTC(const CPoolPair &BTCDFIPoolPair) {
}

CAmount CICXOrdersConsensus::CalculateTakerFee(CAmount amount) const {
auto &mnview = blockCtx.GetView();
auto tokenBTC = mnview.GetToken(CICXOrder::TOKEN_BTC);
assert(tokenBTC);
auto pair = mnview.GetPoolPair(tokenBTC->first, DCT_ID{0});
Expand All @@ -43,6 +43,7 @@ CAmount CICXOrdersConsensus::CalculateTakerFee(CAmount amount) const {

DCT_ID CICXOrdersConsensus::FindTokenByPartialSymbolName(const std::string &symbol) const {
DCT_ID res{0};
auto &mnview = blockCtx.GetView();
mnview.ForEachToken(
[&](DCT_ID id, CTokenImplementation token) {
if (token.symbol.find(symbol) == 0) {
Expand All @@ -57,6 +58,10 @@ DCT_ID CICXOrdersConsensus::FindTokenByPartialSymbolName(const std::string &symb
}

Res CICXOrdersConsensus::operator()(const CICXCreateOrderMessage &obj) const {
const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();
if (!IsICXEnabled(height, mnview, consensus)) {
return DeFiErrors::ICXDisabled();
}
Expand Down Expand Up @@ -96,6 +101,11 @@ Res CICXOrdersConsensus::operator()(const CICXCreateOrderMessage &obj) const {
}

Res CICXOrdersConsensus::operator()(const CICXMakeOfferMessage &obj) const {
const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

if (!IsICXEnabled(height, mnview, consensus)) {
return DeFiErrors::ICXDisabled();
}
Expand Down Expand Up @@ -152,6 +162,11 @@ Res CICXOrdersConsensus::operator()(const CICXMakeOfferMessage &obj) const {
}

Res CICXOrdersConsensus::operator()(const CICXSubmitDFCHTLCMessage &obj) const {
const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

if (!IsICXEnabled(height, mnview, consensus)) {
return DeFiErrors::ICXDisabled();
}
Expand Down Expand Up @@ -304,6 +319,11 @@ Res CICXOrdersConsensus::operator()(const CICXSubmitDFCHTLCMessage &obj) const {
}

Res CICXOrdersConsensus::operator()(const CICXSubmitEXTHTLCMessage &obj) const {
const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

if (!IsICXEnabled(height, mnview, consensus)) {
return DeFiErrors::ICXDisabled();
}
Expand Down Expand Up @@ -440,6 +460,11 @@ Res CICXOrdersConsensus::operator()(const CICXSubmitEXTHTLCMessage &obj) const {
}

Res CICXOrdersConsensus::operator()(const CICXClaimDFCHTLCMessage &obj) const {
const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

if (!IsICXEnabled(height, mnview, consensus)) {
return DeFiErrors::ICXDisabled();
}
Expand Down Expand Up @@ -586,6 +611,10 @@ Res CICXOrdersConsensus::operator()(const CICXCloseOrderMessage &obj) const {
return res;
}

const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

CICXCloseOrderImplemetation closeorder;
static_cast<CICXCloseOrder &>(closeorder) = obj;

Expand Down Expand Up @@ -632,6 +661,11 @@ Res CICXOrdersConsensus::operator()(const CICXCloseOfferMessage &obj) const {
return res;
}

const auto &consensus = txCtx.GetConsensus();
const auto height = txCtx.GetHeight();
const auto &tx = txCtx.GetTransaction();
auto &mnview = blockCtx.GetView();

CICXCloseOfferImplemetation closeoffer;
static_cast<CICXCloseOffer &>(closeoffer) = obj;

Expand Down
Loading