Skip to content

Commit ca77d06

Browse files
PastaPastaPastaknstUdjinM6
authored
refactor: make GetTxPayload return an Optional T instead of taking in a T& return (dashpay#5733)
## Issue being fixed or feature implemented We should avoid return by reference; especially return by reference with a bool flag indicating validity. ## What was done? Instead we use a std::optional ## How Has This Been Tested? Unit tests pass ## Breaking Changes Should be none ## Checklist: _Go over all the following points, and put an `x` in all the boxes that apply._ - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_ --------- Co-authored-by: Konstantin Akimov <[email protected]> Co-authored-by: UdjinM6 <[email protected]>
1 parent a7eeda5 commit ca77d06

21 files changed

+288
-326
lines changed

src/bloom.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ bool CBloomFilter::CheckSpecialTransactionMatchesAndUpdate(const CTransaction &t
126126
}
127127
switch(tx.nType) {
128128
case(TRANSACTION_PROVIDER_REGISTER): {
129-
CProRegTx proTx;
130-
if (GetTxPayload(tx, proTx)) {
131-
if(contains(proTx.collateralOutpoint) ||
132-
contains(proTx.keyIDOwner) ||
133-
contains(proTx.keyIDVoting) ||
134-
CheckScript(proTx.scriptPayout)) {
129+
if (const auto opt_proTx = GetTxPayload<CProRegTx>(tx)) {
130+
if(contains(opt_proTx->collateralOutpoint) ||
131+
contains(opt_proTx->keyIDOwner) ||
132+
contains(opt_proTx->keyIDVoting) ||
133+
CheckScript(opt_proTx->scriptPayout)) {
135134
if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
136135
insert(tx.GetHash());
137136
return true;
@@ -140,47 +139,43 @@ bool CBloomFilter::CheckSpecialTransactionMatchesAndUpdate(const CTransaction &t
140139
return false;
141140
}
142141
case(TRANSACTION_PROVIDER_UPDATE_SERVICE): {
143-
CProUpServTx proTx;
144-
if (GetTxPayload(tx, proTx)) {
145-
if(contains(proTx.proTxHash)) {
142+
if (const auto opt_proTx = GetTxPayload<CProUpServTx>(tx)) {
143+
if(contains(opt_proTx->proTxHash)) {
146144
return true;
147145
}
148-
if(CheckScript(proTx.scriptOperatorPayout)) {
146+
if(CheckScript(opt_proTx->scriptOperatorPayout)) {
149147
if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
150-
insert(proTx.proTxHash);
148+
insert(opt_proTx->proTxHash);
151149
return true;
152150
}
153151
}
154152
return false;
155153
}
156154
case(TRANSACTION_PROVIDER_UPDATE_REGISTRAR): {
157-
CProUpRegTx proTx;
158-
if (GetTxPayload(tx, proTx)) {
159-
if(contains(proTx.proTxHash))
155+
if (const auto opt_proTx = GetTxPayload<CProUpRegTx>(tx)) {
156+
if(contains(opt_proTx->proTxHash))
160157
return true;
161-
if(contains(proTx.keyIDVoting) ||
162-
CheckScript(proTx.scriptPayout)) {
158+
if(contains(opt_proTx->keyIDVoting) ||
159+
CheckScript(opt_proTx->scriptPayout)) {
163160
if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
164-
insert(proTx.proTxHash);
161+
insert(opt_proTx->proTxHash);
165162
return true;
166163
}
167164
}
168165
return false;
169166
}
170167
case(TRANSACTION_PROVIDER_UPDATE_REVOKE): {
171-
CProUpRevTx proTx;
172-
if (GetTxPayload(tx, proTx)) {
173-
if(contains(proTx.proTxHash))
168+
if (const auto opt_proTx = GetTxPayload<CProUpRevTx>(tx)) {
169+
if(contains(opt_proTx->proTxHash))
174170
return true;
175171
}
176172
return false;
177173
}
178174
case(TRANSACTION_ASSET_LOCK): {
179175
// inputs of Asset Lock transactions are standard. But some outputs are special
180-
CAssetLockPayload assetLockTx;
181-
if (GetTxPayload(tx, assetLockTx)) {
176+
if (const auto opt_assetlockTx = GetTxPayload<CAssetLockPayload>(tx)) {
182177
bool fFound = false;
183-
const auto& extraOuts = assetLockTx.getCreditOutputs();
178+
const auto& extraOuts = opt_assetlockTx->getCreditOutputs();
184179
for (unsigned int i = 0; i < extraOuts.size(); ++i)
185180
{
186181
fFound = ProcessTxOut(extraOuts[i], tx.GetHash(), i) || fFound;

src/core_write.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,40 +266,40 @@ void TxToUniv(const CTransaction& tx, const uint256& hashBlock, UniValue& entry,
266266
}
267267

268268
if (tx.nType == TRANSACTION_PROVIDER_REGISTER) {
269-
if (CProRegTx proTx; GetTxPayload(tx, proTx)) {
270-
entry.pushKV("proRegTx", proTx.ToJson());
269+
if (const auto opt_proTx = GetTxPayload<CProRegTx>(tx)) {
270+
entry.pushKV("proRegTx", opt_proTx->ToJson());
271271
}
272272
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_SERVICE) {
273-
if (CProUpServTx proTx; GetTxPayload(tx, proTx)) {
274-
entry.pushKV("proUpServTx", proTx.ToJson());
273+
if (const auto opt_proTx = GetTxPayload<CProUpServTx>(tx)) {
274+
entry.pushKV("proUpServTx", opt_proTx->ToJson());
275275
}
276276
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REGISTRAR) {
277-
if (CProUpRegTx proTx; GetTxPayload(tx, proTx)) {
278-
entry.pushKV("proUpRegTx", proTx.ToJson());
277+
if (const auto opt_proTx = GetTxPayload<CProUpRegTx>(tx)) {
278+
entry.pushKV("proUpRegTx", opt_proTx->ToJson());
279279
}
280280
} else if (tx.nType == TRANSACTION_PROVIDER_UPDATE_REVOKE) {
281-
if (CProUpRevTx proTx; GetTxPayload(tx, proTx)) {
282-
entry.pushKV("proUpRevTx", proTx.ToJson());
281+
if (const auto opt_proTx = GetTxPayload<CProUpRevTx>(tx)) {
282+
entry.pushKV("proUpRevTx", opt_proTx->ToJson());
283283
}
284284
} else if (tx.nType == TRANSACTION_COINBASE) {
285-
if (CCbTx cbTx; GetTxPayload(tx, cbTx)) {
286-
entry.pushKV("cbTx", cbTx.ToJson());
285+
if (const auto opt_cbTx = GetTxPayload<CCbTx>(tx)) {
286+
entry.pushKV("cbTx", opt_cbTx->ToJson());
287287
}
288288
} else if (tx.nType == TRANSACTION_QUORUM_COMMITMENT) {
289-
if (llmq::CFinalCommitmentTxPayload qcTx; GetTxPayload(tx, qcTx)) {
290-
entry.pushKV("qcTx", qcTx.ToJson());
289+
if (const auto opt_qcTx = GetTxPayload<llmq::CFinalCommitmentTxPayload>(tx)) {
290+
entry.pushKV("qcTx", opt_qcTx->ToJson());
291291
}
292292
} else if (tx.nType == TRANSACTION_MNHF_SIGNAL) {
293-
if (MNHFTxPayload mnhfTx; GetTxPayload(tx, mnhfTx)) {
294-
entry.pushKV("mnhfTx", mnhfTx.ToJson());
293+
if (const auto opt_mnhfTx = GetTxPayload<MNHFTxPayload>(tx)) {
294+
entry.pushKV("mnhfTx", opt_mnhfTx->ToJson());
295295
}
296296
} else if (tx.nType == TRANSACTION_ASSET_LOCK) {
297-
if (CAssetLockPayload assetLockTx; GetTxPayload(tx, assetLockTx)) {
298-
entry.pushKV("assetLockTx", assetLockTx.ToJson());
297+
if (const auto opt_assetLockTx = GetTxPayload<CAssetLockPayload>(tx)) {
298+
entry.pushKV("assetLockTx", opt_assetLockTx->ToJson());
299299
}
300300
} else if (tx.nType == TRANSACTION_ASSET_UNLOCK) {
301-
if (CAssetUnlockPayload assetUnlockTx; GetTxPayload(tx, assetUnlockTx)) {
302-
entry.pushKV("assetUnlockTx", assetUnlockTx.ToJson());
301+
if (const auto opt_assetUnlockTx = GetTxPayload<CAssetUnlockPayload>(tx)) {
302+
entry.pushKV("assetUnlockTx", opt_assetUnlockTx->ToJson());
303303
}
304304
}
305305

src/evo/assetlocktx.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,21 @@ bool CheckAssetLockTx(const CTransaction& tx, TxValidationState& state)
6060

6161
if (returnAmount == 0) return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-no-return");
6262

63-
CAssetLockPayload assetLockTx;
64-
if (!GetTxPayload(tx, assetLockTx)) {
63+
const auto opt_assetLockTx = GetTxPayload<CAssetLockPayload>(tx);
64+
if (!opt_assetLockTx.has_value()) {
6565
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-payload");
6666
}
6767

68-
if (assetLockTx.getVersion() == 0 || assetLockTx.getVersion() > CAssetLockPayload::CURRENT_VERSION) {
68+
if (opt_assetLockTx->getVersion() == 0 || opt_assetLockTx->getVersion() > CAssetLockPayload::CURRENT_VERSION) {
6969
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-version");
7070
}
7171

72-
if (assetLockTx.getCreditOutputs().empty()) {
72+
if (opt_assetLockTx->getCreditOutputs().empty()) {
7373
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-emptycreditoutputs");
7474
}
7575

7676
CAmount creditOutputsAmount = 0;
77-
for (const CTxOut& out : assetLockTx.getCreditOutputs()) {
77+
for (const CTxOut& out : opt_assetLockTx->getCreditOutputs()) {
7878
if (out.nValue == 0 || !MoneyRange(out.nValue) || !MoneyRange(creditOutputsAmount + out.nValue)) {
7979
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetlocktx-credit-outofrange");
8080
}
@@ -158,10 +158,11 @@ bool CheckAssetUnlockTx(const CTransaction& tx, gsl::not_null<const CBlockIndex*
158158
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-too-many-outs");
159159
}
160160

161-
CAssetUnlockPayload assetUnlockTx;
162-
if (!GetTxPayload(tx, assetUnlockTx)) {
161+
const auto opt_assetUnlockTx = GetTxPayload<CAssetUnlockPayload>(tx);
162+
if (!opt_assetUnlockTx) {
163163
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-payload");
164164
}
165+
auto& assetUnlockTx = *opt_assetUnlockTx;
165166

166167
if (assetUnlockTx.getVersion() == 0 || assetUnlockTx.getVersion() > CAssetUnlockPayload::CURRENT_VERSION) {
167168
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-version");
@@ -187,11 +188,11 @@ bool CheckAssetUnlockTx(const CTransaction& tx, gsl::not_null<const CBlockIndex*
187188

188189
bool GetAssetUnlockFee(const CTransaction& tx, CAmount& txfee, TxValidationState& state)
189190
{
190-
CAssetUnlockPayload assetUnlockTx;
191-
if (!GetTxPayload(tx, assetUnlockTx)) {
191+
const auto opt_assetUnlockTx = GetTxPayload<CAssetUnlockPayload>(tx);
192+
if (!opt_assetUnlockTx) {
192193
return state.Invalid(TxValidationResult::TX_BAD_SPECIAL, "bad-assetunlocktx-payload");
193194
}
194-
const CAmount txfee_aux = assetUnlockTx.getFee();
195+
const CAmount txfee_aux = opt_assetUnlockTx->getFee();
195196
if (txfee_aux == 0 || !MoneyRange(txfee_aux)) {
196197
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-assetunlock-fee-outofrange");
197198
}

src/evo/cbtx.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ bool CheckCbTx(const CTransaction& tx, const CBlockIndex* pindexPrev, TxValidati
2929
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-invalid");
3030
}
3131

32-
CCbTx cbTx;
33-
if (!GetTxPayload(tx, cbTx)) {
32+
const auto opt_cbTx = GetTxPayload<CCbTx>(tx);
33+
if (!opt_cbTx) {
3434
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-payload");
3535
}
36+
const auto& cbTx = *opt_cbTx;
3637

3738
if (cbTx.nVersion == CCbTx::Version::INVALID || cbTx.nVersion >= CCbTx::Version::UNKNOWN) {
3839
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-version");
@@ -68,10 +69,11 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, const
6869

6970
int64_t nTime1 = GetTimeMicros();
7071

71-
CCbTx cbTx;
72-
if (!GetTxPayload(*block.vtx[0], cbTx)) {
72+
const auto opt_cbTx = GetTxPayload<CCbTx>(*block.vtx[0]);
73+
if (!opt_cbTx) {
7374
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-payload");
7475
}
76+
auto cbTx = *opt_cbTx;
7577

7678
int64_t nTime2 = GetTimeMicros(); nTimePayload += nTime2 - nTime1;
7779
LogPrint(BCLog::BENCHMARK, " - GetTxPayload: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimePayload * 0.000001);
@@ -255,23 +257,23 @@ bool CalcCbTxMerkleRootQuorums(const CBlock& block, const CBlockIndex* pindexPre
255257
const auto& tx = block.vtx[i];
256258

257259
if (tx->nVersion == 3 && tx->nType == TRANSACTION_QUORUM_COMMITMENT) {
258-
llmq::CFinalCommitmentTxPayload qc;
259-
if (!GetTxPayload(*tx, qc)) {
260+
const auto opt_qc = GetTxPayload<llmq::CFinalCommitmentTxPayload>(*tx);
261+
if (!opt_qc) {
260262
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-payload-calc-cbtx-quorummerkleroot");
261263
}
262-
if (qc.commitment.IsNull()) {
264+
if (opt_qc->commitment.IsNull()) {
263265
// having null commitments is ok but we don't use them here, move to the next tx
264266
continue;
265267
}
266-
const auto& llmq_params_opt = llmq::GetLLMQParams(qc.commitment.llmqType);
268+
const auto& llmq_params_opt = llmq::GetLLMQParams(opt_qc->commitment.llmqType);
267269
if (!llmq_params_opt.has_value()) {
268270
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-qc-commitment-type-calc-cbtx-quorummerkleroot");
269271
}
270272
const auto& llmq_params = llmq_params_opt.value();
271-
auto qcHash = ::SerializeHash(qc.commitment);
273+
auto qcHash = ::SerializeHash(opt_qc->commitment);
272274
if (llmq::utils::IsQuorumRotationEnabled(llmq_params, pindexPrev)) {
273-
auto& map_indexed_hashes = qcIndexedHashes[qc.commitment.llmqType];
274-
map_indexed_hashes[qc.commitment.quorumIndex] = qcHash;
275+
auto& map_indexed_hashes = qcIndexedHashes[opt_qc->commitment.llmqType];
276+
map_indexed_hashes[opt_qc->commitment.quorumIndex] = qcHash;
275277
} else {
276278
auto& vec_hashes = qcHashes[llmq_params.type];
277279
if (vec_hashes.size() == size_t(llmq_params.signingActiveQuorumCount)) {
@@ -328,17 +330,17 @@ bool CheckCbTxBestChainlock(const CBlock& block, const CBlockIndex* pindex, cons
328330
return true;
329331
}
330332

331-
CCbTx cbTx;
332-
if (!GetTxPayload(*block.vtx[0], cbTx)) {
333+
const auto opt_cbTx = GetTxPayload<CCbTx>(*block.vtx[0]);
334+
if (!opt_cbTx) {
333335
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-payload");
334336
}
335337

336-
if (cbTx.nVersion < CCbTx::Version::CLSIG_AND_BALANCE) {
338+
if (opt_cbTx->nVersion < CCbTx::Version::CLSIG_AND_BALANCE) {
337339
return true;
338340
}
339341

340342
auto best_clsig = chainlock_handler.GetBestChainLock();
341-
if (best_clsig.getHeight() == pindex->nHeight - 1 && cbTx.bestCLHeightDiff == 0 && cbTx.bestCLSignature == best_clsig.getSig()) {
343+
if (best_clsig.getHeight() == pindex->nHeight - 1 && opt_cbTx->bestCLHeightDiff == 0 && opt_cbTx->bestCLSignature == best_clsig.getSig()) {
342344
// matches our best clsig which still hold values for the previous block
343345
return true;
344346
}
@@ -347,29 +349,29 @@ bool CheckCbTxBestChainlock(const CBlock& block, const CBlockIndex* pindex, cons
347349
// If std::optional prevBlockCoinbaseChainlock is empty, then up to the previous block, coinbase Chainlock is null.
348350
if (prevBlockCoinbaseChainlock.has_value()) {
349351
// Previous block Coinbase has a non-null Chainlock: current block's Chainlock must be non-null and at least as new as the previous one
350-
if (!cbTx.bestCLSignature.IsValid()) {
352+
if (!opt_cbTx->bestCLSignature.IsValid()) {
351353
// IsNull() doesn't exist for CBLSSignature: we assume that a non valid BLS sig is null
352354
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-null-clsig");
353355
}
354356
int prevBlockCoinbaseCLHeight = pindex->nHeight - static_cast<int>(prevBlockCoinbaseChainlock.value().second) - 1;
355-
int curBlockCoinbaseCLHeight = pindex->nHeight - static_cast<int>(cbTx.bestCLHeightDiff) - 1;
357+
int curBlockCoinbaseCLHeight = pindex->nHeight - static_cast<int>(opt_cbTx->bestCLHeightDiff) - 1;
356358
if (curBlockCoinbaseCLHeight < prevBlockCoinbaseCLHeight) {
357359
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-older-clsig");
358360
}
359361
}
360362

361363
// IsNull() doesn't exist for CBLSSignature: we assume that a valid BLS sig is non-null
362-
if (cbTx.bestCLSignature.IsValid()) {
363-
int curBlockCoinbaseCLHeight = pindex->nHeight - static_cast<int>(cbTx.bestCLHeightDiff) - 1;
364-
if (best_clsig.getHeight() == curBlockCoinbaseCLHeight && best_clsig.getSig() == cbTx.bestCLSignature) {
364+
if (opt_cbTx->bestCLSignature.IsValid()) {
365+
int curBlockCoinbaseCLHeight = pindex->nHeight - static_cast<int>(opt_cbTx->bestCLHeightDiff) - 1;
366+
if (best_clsig.getHeight() == curBlockCoinbaseCLHeight && best_clsig.getSig() == opt_cbTx->bestCLSignature) {
365367
// matches our best (but outdated) clsig, no need to verify it again
366368
return true;
367369
}
368370
uint256 curBlockCoinbaseCLBlockHash = pindex->GetAncestor(curBlockCoinbaseCLHeight)->GetBlockHash();
369-
if (!chainlock_handler.VerifyChainLock(llmq::CChainLockSig(curBlockCoinbaseCLHeight, curBlockCoinbaseCLBlockHash, cbTx.bestCLSignature))) {
371+
if (!chainlock_handler.VerifyChainLock(llmq::CChainLockSig(curBlockCoinbaseCLHeight, curBlockCoinbaseCLBlockHash, opt_cbTx->bestCLSignature))) {
370372
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-invalid-clsig");
371373
}
372-
} else if (cbTx.bestCLHeightDiff != 0) {
374+
} else if (opt_cbTx->bestCLHeightDiff != 0) {
373375
// Null bestCLSignature is allowed only with bestCLHeightDiff = 0
374376
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cbtx-cldiff");
375377
}
@@ -450,12 +452,7 @@ std::optional<CCbTx> GetCoinbaseTx(const CBlockIndex* pindex)
450452
}
451453

452454
CTransactionRef cbTx = block.vtx[0];
453-
CCbTx cbTxPayload;
454-
if (!GetTxPayload(*cbTx, cbTxPayload)) {
455-
return std::nullopt;
456-
}
457-
458-
return cbTxPayload;
455+
return GetTxPayload<CCbTx>(*cbTx);
459456
}
460457

461458
std::optional<std::pair<CBLSSignature, uint32_t>> GetNonNullCoinbaseChainlock(const CBlockIndex* pindex)

src/evo/creditpool.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ std::unique_ptr<CCreditPoolManager> creditPoolManager;
3232

3333
static bool GetDataFromUnlockTx(const CTransaction& tx, CAmount& toUnlock, uint64_t& index, TxValidationState& state)
3434
{
35-
CAssetUnlockPayload assetUnlockTx;
36-
if (!GetTxPayload(tx, assetUnlockTx)) {
35+
const auto opt_assetUnlockTx = GetTxPayload<CAssetUnlockPayload>(tx);
36+
if (!opt_assetUnlockTx.has_value()) {
3737
return state.Invalid(TxValidationResult::TX_CONSENSUS, "failed-creditpool-unlock-payload");
3838
}
3939

40-
index = assetUnlockTx.getIndex();
41-
toUnlock = assetUnlockTx.getFee();
40+
index = opt_assetUnlockTx->getIndex();
41+
toUnlock = opt_assetUnlockTx->getFee();
4242
for (const CTxOut& txout : tx.vout) {
4343
if (!MoneyRange(txout.nValue)) {
4444
return state.Invalid(TxValidationResult::TX_CONSENSUS, "failed-creditpool-unlock-txout-outofrange");
@@ -143,14 +143,13 @@ CCreditPool CCreditPoolManager::ConstructCreditPool(const CBlockIndex* const blo
143143
AddToCache(block_index->GetBlockHash(), block_index->nHeight, emptyPool);
144144
return emptyPool;
145145
}
146-
CAmount locked{0};
147-
{
148-
CCbTx cbTx;
149-
if (!GetTxPayload(block->vtx[0]->vExtraPayload, cbTx)) {
150-
throw std::runtime_error(strprintf("%s: failed-getcreditpool-cbtx-payload", __func__));
146+
CAmount locked = [&, func=__func__]() {
147+
const auto opt_cbTx = GetTxPayload<CCbTx>(block->vtx[0]->vExtraPayload);
148+
if (!opt_cbTx) {
149+
throw std::runtime_error(strprintf("%s: failed-getcreditpool-cbtx-payload", func));
151150
}
152-
locked = cbTx.creditPoolBalance;
153-
}
151+
return opt_cbTx->creditPoolBalance;
152+
}();
154153

155154
// We use here sliding window with LimitBlocksToTrace to determine
156155
// current limits for asset unlock transactions.
@@ -235,7 +234,7 @@ CCreditPoolDiff::CCreditPoolDiff(CCreditPool starter, const CBlockIndex *pindexP
235234

236235
bool CCreditPoolDiff::Lock(const CTransaction& tx, TxValidationState& state)
237236
{
238-
if (CAssetLockPayload assetLockTx; !GetTxPayload(tx, assetLockTx)) {
237+
if (const auto opt_assetLockTx = GetTxPayload<CAssetLockPayload>(tx); !opt_assetLockTx) {
239238
return state.Invalid(TxValidationResult::TX_CONSENSUS, "failed-creditpool-lock-payload");
240239
}
241240

0 commit comments

Comments
 (0)