Skip to content

Commit

Permalink
refactor: introduce CbTx version enum class, adjust version names
Browse files Browse the repository at this point in the history
  • Loading branch information
UdjinM6 committed Nov 21, 2023
1 parent 9861bc7 commit b137954
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/evo/cbtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool CheckCbTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidati
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-payload");
}

if (cbTx.nVersion == 0 || cbTx.nVersion > CCbTx::CB_V20_VERSION) {
if (cbTx.nVersion == CbTxVersion::INVALID || cbTx.nVersion >= CbTxVersion::UNKNOWN) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-version");
}

Expand All @@ -43,12 +43,12 @@ bool CheckCbTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidati
}

bool fDIP0008Active = pindexPrev->nHeight >= Params().GetConsensus().DIP0008Height;
if (fDIP0008Active && cbTx.nVersion < CCbTx::CB_V19_VERSION) {
if (fDIP0008Active && cbTx.nVersion < CbTxVersion::MERKLE_ROOT_QUORUMS) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-version");
}

bool isV20 = llmq::utils::IsV20Active(pindexPrev);
if ((isV20 && cbTx.nVersion < CCbTx::CB_V20_VERSION) || (!isV20 && cbTx.nVersion >= CCbTx::CB_V20_VERSION)) {
if ((isV20 && cbTx.nVersion < CbTxVersion::BEST_CHAINLOCK) || (!isV20 && cbTx.nVersion >= CbTxVersion::BEST_CHAINLOCK)) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-version");
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, const
int64_t nTime3 = GetTimeMicros(); nTimeMerkleMNL += nTime3 - nTime2;
LogPrint(BCLog::BENCHMARK, " - CalcCbTxMerkleRootMNList: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeMerkleMNL * 0.000001);

if (cbTx.nVersion >= 2) {
if (cbTx.nVersion >= CbTxVersion::MERKLE_ROOT_QUORUMS) {
if (!CalcCbTxMerkleRootQuorums(block, pindex->pprev, quorum_block_processor, calculatedMerkleRoot, state)) {
// pass the state returned by the function above
return false;
Expand Down Expand Up @@ -332,7 +332,7 @@ bool CheckCbTxBestChainlock(const CBlock& block, const CBlockIndex* pindex, cons
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-payload");
}

if (cbTx.nVersion < CCbTx::CB_V20_VERSION) {
if (cbTx.nVersion < CbTxVersion::BEST_CHAINLOCK) {
return true;
}

Expand Down Expand Up @@ -433,7 +433,7 @@ bool CalcCbTxBestChainlock(const llmq::CChainLocksHandler& chainlock_handler, co
std::string CCbTx::ToString() const
{
return strprintf("CCbTx(nVersion=%d, nHeight=%d, merkleRootMNList=%s, merkleRootQuorums=%s, bestCLHeightDiff=%d, bestCLSig=%s, creditPoolBalance=%d.%08d)",
nVersion, nHeight, merkleRootMNList.ToString(), merkleRootQuorums.ToString(), bestCLHeightDiff, bestCLSignature.ToString(),
static_cast<uint16_t>(nVersion), nHeight, merkleRootMNList.ToString(), merkleRootQuorums.ToString(), bestCLHeightDiff, bestCLSignature.ToString(),
creditPoolBalance / COIN, creditPoolBalance % COIN);
}

Expand Down Expand Up @@ -467,7 +467,7 @@ std::optional<std::pair<CBLSSignature, uint32_t>> GetNonNullCoinbaseChainlock(co

CCbTx& cbtx = opt_cbtx.value();

if (cbtx.nVersion < CCbTx::CB_V20_VERSION) {
if (cbtx.nVersion < CbTxVersion::BEST_CHAINLOCK) {
return std::nullopt;
}

Expand Down
22 changes: 14 additions & 8 deletions src/evo/cbtx.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,21 @@ class CChainLocksHandler;
// Forward declaration from core_io to get rid of circular dependency
UniValue ValueFromAmount(const CAmount& amount);

enum class CbTxVersion : uint16_t {
INVALID = 0,
MERKLE_ROOT_MNLIST = 1,
MERKLE_ROOT_QUORUMS = 2,
BEST_CHAINLOCK = 3,
UNKNOWN,
};
template<> struct is_serializable_enum<CbTxVersion> : std::true_type {};

// coinbase transaction
class CCbTx
{
public:
static constexpr auto SPECIALTX_TYPE = TRANSACTION_COINBASE;
static constexpr uint16_t CB_V19_VERSION = 2;
static constexpr uint16_t CB_V20_VERSION = 3;

uint16_t nVersion{CB_V19_VERSION};
CbTxVersion nVersion{CbTxVersion::MERKLE_ROOT_QUORUMS};
int32_t nHeight{0};
uint256 merkleRootMNList;
uint256 merkleRootQuorums;
Expand All @@ -45,9 +51,9 @@ class CCbTx
{
READWRITE(obj.nVersion, obj.nHeight, obj.merkleRootMNList);

if (obj.nVersion >= CB_V19_VERSION) {
if (obj.nVersion >= CbTxVersion::MERKLE_ROOT_QUORUMS) {
READWRITE(obj.merkleRootQuorums);
if (obj.nVersion >= CB_V20_VERSION) {
if (obj.nVersion >= CbTxVersion::BEST_CHAINLOCK) {
READWRITE(COMPACTSIZE(obj.bestCLHeightDiff));
READWRITE(obj.bestCLSignature);
READWRITE(obj.creditPoolBalance);
Expand All @@ -65,9 +71,9 @@ class CCbTx
obj.pushKV("version", (int)nVersion);
obj.pushKV("height", nHeight);
obj.pushKV("merkleRootMNList", merkleRootMNList.ToString());
if (nVersion >= CB_V19_VERSION) {
if (nVersion >= CbTxVersion::MERKLE_ROOT_QUORUMS) {
obj.pushKV("merkleRootQuorums", merkleRootQuorums.ToString());
if (nVersion >= CB_V20_VERSION) {
if (nVersion >= CbTxVersion::BEST_CHAINLOCK) {
obj.pushKV("bestCLHeightDiff", static_cast<int>(bestCLHeightDiff));
obj.pushKV("bestCLSignature", bestCLSignature.ToString());
obj.pushKV("creditPoolBalance", ValueFromAmount(creditPoolBalance));
Expand Down
2 changes: 1 addition & 1 deletion src/evo/simplifiedmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ UniValue CSimplifiedMNListDiff::ToJson(bool extended) const
CCbTx cbTxPayload;
if (GetTxPayload(*cbTx, cbTxPayload)) {
obj.pushKV("merkleRootMNList", cbTxPayload.merkleRootMNList.ToString());
if (cbTxPayload.nVersion >= 2) {
if (cbTxPayload.nVersion >= CbTxVersion::MERKLE_ROOT_QUORUMS) {
obj.pushKV("merkleRootQuorums", cbTxPayload.merkleRootQuorums.ToString());
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
CCbTx cbTx;

if (fV20Active_context) {
cbTx.nVersion = CCbTx::CB_V20_VERSION;
cbTx.nVersion = CbTxVersion::BEST_CHAINLOCK;
} else if (fDIP0008Active_context) {
cbTx.nVersion = CCbTx::CB_V19_VERSION;
cbTx.nVersion = CbTxVersion::MERKLE_ROOT_QUORUMS;
} else {
cbTx.nVersion = 1;
cbTx.nVersion = CbTxVersion::MERKLE_ROOT_MNLIST;
}

cbTx.nHeight = nHeight;
Expand Down

0 comments on commit b137954

Please sign in to comment.