Skip to content

Commit

Permalink
Add time, height, consensus to block context (#2727)
Browse files Browse the repository at this point in the history
* Add time/height to block context. Alias them in transaction context.

* Add consensus to block context.

* Add comment

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Nov 29, 2023
1 parent c875196 commit b44fdc6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 48 deletions.
8 changes: 5 additions & 3 deletions src/consensus/tx_verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,14 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, c
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.
BlockContext blockCtx{&discardCache};
//
// Note: We set time to 0 here. Take care not to use time
// in Apply for MintToken or AccountToUtxos path.
BlockContext blockCtx(nSpendHeight, {}, chainparams.GetConsensus(), &discardCache);
auto txCtx = TransactionContext{
inputs,
tx,
chainparams.GetConsensus(),
static_cast<uint32_t>(nSpendHeight),
blockCtx,
};
auto res = ApplyCustomTx(blockCtx, txCtx, &canSpend);
if (!res.ok && (res.code & CustomTxErrCodes::Fatal)) {
Expand Down
10 changes: 4 additions & 6 deletions src/dfi/mn_checks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1510,15 +1510,13 @@ void TransferDomainConfig::SetToAttributesIfNotExists(ATTRIBUTES &attrs) const {

TransactionContext::TransactionContext(const CCoinsViewCache &coins,
const CTransaction &tx,
const Consensus::Params &consensus,
const uint32_t height,
const uint64_t time,
const BlockContext &blockCtx,
const uint32_t txn)
: coins(coins),
tx(tx),
consensus(consensus),
height(height),
time(time),
consensus(blockCtx.GetConsensus()),
height(blockCtx.GetHeight()),
time(blockCtx.GetTime()),
txn(txn) {
metadataValidation = height >= static_cast<uint32_t>(consensus.DF11FortCanningHeight);
}
Expand Down
24 changes: 17 additions & 7 deletions src/dfi/mn_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,21 +161,33 @@ class BlockContext {
std::optional<bool> isEvmEnabledForBlock;
std::shared_ptr<CScopedTemplate> evmTemplate{};
bool evmPreValidate{};
const uint32_t height{};
const uint64_t time{};
const Consensus::Params &consensus;

public:
explicit BlockContext(CCustomCSView *view = {},
explicit BlockContext(const uint32_t height,
const uint64_t time,
const Consensus::Params &consensus,
CCustomCSView *view = {},
const std::optional<bool> enabled = {},
const std::shared_ptr<CScopedTemplate> &evmTemplate = {},
const bool prevalidate = {})
: view(view),
isEvmEnabledForBlock(enabled),
evmTemplate(evmTemplate),
evmPreValidate(prevalidate) {}
evmPreValidate(prevalidate),
height(height),
time(time),
consensus(consensus) {}

[[nodiscard]] CCustomCSView &GetView();
[[nodiscard]] bool GetEVMEnabledForBlock();
[[nodiscard]] bool GetEVMPreValidate() const;
[[nodiscard]] const std::shared_ptr<CScopedTemplate> &GetEVMTemplate() const;
[[nodiscard]] const uint32_t &GetHeight() const;
[[nodiscard]] const uint64_t &GetTime() const;
[[nodiscard]] const Consensus::Params &GetConsensus() const;

void SetView(CCustomCSView &other);
void SetEVMPreValidate(const bool other);
Expand All @@ -186,8 +198,8 @@ class TransactionContext {
const CCoinsViewCache &coins;
const CTransaction &tx;
const Consensus::Params &consensus;
const uint32_t height{};
const uint64_t time{};
const uint32_t &height;
const uint64_t &time;
const uint32_t txn{};

std::vector<unsigned char> metadata;
Expand All @@ -198,9 +210,7 @@ class TransactionContext {
public:
TransactionContext(const CCoinsViewCache &coins,
const CTransaction &tx,
const Consensus::Params &consensus,
const uint32_t height = {},
const uint64_t time = {},
const BlockContext &blockCtx,
const uint32_t txn = {});

[[nodiscard]] const CCoinsViewCache &GetCoins() const;
Expand Down
6 changes: 2 additions & 4 deletions src/dfi/mn_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,15 +480,13 @@ void execTestTx(const CTransaction &tx, const uint32_t height, const CTransactio
if (optAuthTx) {
AddCoins(coins, *optAuthTx, height);
}
BlockContext blockCtx;
BlockContext blockCtx(height, ::ChainActive().Tip()->nTime, Params().GetConsensus());
blockCtx.SetEVMPreValidate(true);

const auto txCtx = TransactionContext{
coins,
tx,
Params().GetConsensus(),
height,
::ChainActive().Tip()->nTime,
blockCtx,
};
res = CustomTxVisit(txMessage, blockCtx, txCtx);
}
Expand Down
5 changes: 2 additions & 3 deletions src/dfi/rpc_tokens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,14 +666,13 @@ UniValue getcustomtx(const JSONRPCRequest &request) {
result.pushKV("type", ToString(guess));
if (!actualHeight) {
LOCK(cs_main);
BlockContext blockCtx;
BlockContext blockCtx(nHeight, ::ChainActive().Tip()->nTime, Params().GetConsensus());
CCoinsViewCache view(&::ChainstateActive().CoinsTip());

auto txCtx = TransactionContext{
view,
*tx,
Params().GetConsensus(),
static_cast<uint32_t>(nHeight),
blockCtx,
};

auto res = ApplyCustomTx(blockCtx, txCtx);
Expand Down
6 changes: 2 additions & 4 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ ResVal<std::unique_ptr<CBlockTemplate>> BlockAssembler::CreateNewBlock(const CSc

int nPackagesSelected = 0;
int nDescendantsUpdated = 0;
BlockContext blockCtx;
BlockContext blockCtx(nHeight, pblock->nTime, chainparams.GetConsensus());
auto &mnview = blockCtx.GetView();
if (!blockTime) {
UpdateTime(pblock, consensus, pindexPrev); // update time before tx packaging
Expand Down Expand Up @@ -879,9 +879,7 @@ void BlockAssembler::addPackageTxs(int &nPackagesSelected,
auto txCtx = TransactionContext{
coins,
tx,
chainparams.GetConsensus(),
static_cast<uint32_t>(nHeight),
pblock->nTime,
blockCtx,
};

// Copy block context and update to cache view
Expand Down
10 changes: 5 additions & 5 deletions src/test/applytx_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)

LOCK(cs_main);

BlockContext blockCtx;
BlockContext blockCtx{{}, {}, amkCheated};
auto &mnview = blockCtx.GetView();
CCoinsViewCache coinview(&::ChainstateActive().CoinsTip());

Expand Down Expand Up @@ -124,7 +124,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
auto txCtx = TransactionContext{
coinview,
tx,
amkCheated,
blockCtx,
};

res = ApplyCustomTx(blockCtx, txCtx);
Expand All @@ -148,7 +148,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
auto txCtx = TransactionContext{
coinview,
tx,
amkCheated,
blockCtx,
};

res = ApplyCustomTx(blockCtx, txCtx);
Expand All @@ -172,7 +172,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
auto txCtx = TransactionContext{
coinview,
tx,
amkCheated,
blockCtx,
};

res = ApplyCustomTx(blockCtx, txCtx);
Expand All @@ -196,7 +196,7 @@ BOOST_AUTO_TEST_CASE(apply_a2a_neg)
auto txCtx = TransactionContext{
coinview,
tx,
amkCheated,
blockCtx,
};

res = ApplyCustomTx(blockCtx, txCtx);
Expand Down
6 changes: 4 additions & 2 deletions src/txmempool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,9 @@ void CTxMemPool::rebuildAccountsView(int height, const CCoinsViewCache &coinsCac
continue;
}
auto blockCtx = BlockContext{
static_cast<uint32_t>(height),
static_cast<uint64_t>(it->GetTime()),
consensus,
&viewDuplicate,
isEvmEnabledForBlock,
{},
Expand All @@ -1296,8 +1299,7 @@ void CTxMemPool::rebuildAccountsView(int height, const CCoinsViewCache &coinsCac
auto txCtx = TransactionContext{
coinsCache,
tx,
consensus,
static_cast<uint32_t>(height),
blockCtx,
};
auto res = ApplyCustomTx(blockCtx, txCtx);

Expand Down
36 changes: 22 additions & 14 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,9 @@ static bool AcceptToMemoryPoolWorker(const CChainParams &chainparams,
const auto &consensus = chainparams.GetConsensus();

auto blockCtx = BlockContext{
static_cast<uint32_t>(height),
static_cast<uint64_t>(nAcceptTime),
consensus,
&mnview,
IsEVMEnabled(mnview, consensus),
{},
Expand All @@ -731,9 +734,7 @@ static bool AcceptToMemoryPoolWorker(const CChainParams &chainparams,
auto txCtx = TransactionContext{
view,
tx,
consensus,
static_cast<uint32_t>(height),
static_cast<uint64_t>(nAcceptTime),
blockCtx,
};

auto res = ApplyCustomTx(blockCtx, txCtx);
Expand Down Expand Up @@ -2741,13 +2742,11 @@ bool CChainState::ConnectBlock(const CBlock &block,
// Do not track burns in genesis
mnview.GetHistoryWriters().GetBurnView() = nullptr;
for (size_t i = 0; i < block.vtx.size(); ++i) {
BlockContext blockCtx{&mnview};
BlockContext blockCtx(pindex->nHeight, pindex->GetBlockTime(), chainparams.GetConsensus(), &mnview);
auto txCtx = TransactionContext{
view,
*block.vtx[i],
chainparams.GetConsensus(),
static_cast<uint32_t>(pindex->nHeight),
static_cast<uint64_t>(pindex->GetBlockTime()),
blockCtx,
static_cast<uint32_t>(i),
};
const auto res = ApplyCustomTx(blockCtx, txCtx);
Expand Down Expand Up @@ -2991,7 +2990,8 @@ bool CChainState::ConnectBlock(const CBlock &block,

const auto &consensus = chainparams.GetConsensus();

auto blockCtx = BlockContext{&accountsView, IsEVMEnabled(attributes)};
auto blockCtx =
BlockContext(pindex->nHeight, pindex->GetBlockTime(), consensus, &accountsView, IsEVMEnabled(attributes));
auto isEvmEnabledForBlock = blockCtx.GetEVMEnabledForBlock();
auto &evmTemplate = blockCtx.GetEVMTemplate();

Expand Down Expand Up @@ -3040,9 +3040,7 @@ bool CChainState::ConnectBlock(const CBlock &block,
auto txCtx = TransactionContext{
view,
tx,
consensus,
static_cast<uint32_t>(pindex->nHeight),
static_cast<uint64_t>(pindex->GetBlockTime()),
blockCtx,
static_cast<uint32_t>(i),
};

Expand Down Expand Up @@ -3182,9 +3180,7 @@ bool CChainState::ConnectBlock(const CBlock &block,
: TransactionContext{
view,
tx,
consensus,
static_cast<uint32_t>(pindex->nHeight),
static_cast<uint64_t>(pindex->GetBlockTime()),
blockCtx,
static_cast<uint32_t>(i),
};
const auto res = ApplyCustomTx(blockCtx, txCtx);
Expand Down Expand Up @@ -7355,6 +7351,18 @@ const std::shared_ptr<CScopedTemplate> &BlockContext::GetEVMTemplate() const {
return evmTemplate;
}

const uint32_t &BlockContext::GetHeight() const {
return height;
};

const uint64_t &BlockContext::GetTime() const {
return time;
};

const Consensus::Params &BlockContext::GetConsensus() const {
return consensus;
};

void BlockContext::SetView(CCustomCSView &other) {
view = &other;
}
Expand Down

0 comments on commit b44fdc6

Please sign in to comment.