Skip to content

Commit a79bca2

Browse files
author
MarcoFalke
committed
Merge #19069: refactor: replace pointers by references within tx_verify.{h,cpp}
b00266f refactor: replace pointers by references within tx_verify.{h,cpp} (Sebastian Falbesoner) Pull request description: This PR gets rid of another unnecessary use of raw pointers, similar to PR #19053 (see also issue #19062 where useful commands for finding potential candidates are listed) but in the tx verification module. For the functions `CalculateSequenceLocks()` and `SequenceLocks()`, the `prevHeights` vector parameter type is changed to be passed as a reference. Note that there were no checks for null pointers -- if one would pass `nullptr` to one of the functions, the following line would immediately lead to a crash: https://github.com/bitcoin/bitcoin/blob/dcacea096e029a02a937bf96d002ca7e94c48c15/src/consensus/tx_verify.cpp#L32 ACKs for top commit: Empact: Code Review ACK bitcoin/bitcoin@b00266f Tree-SHA512: 0eb71591467905434082029128bdca4df94988c372af40dca325654f6c002c72a00c73776cb5e72d6de2b2f218649211a5dbf19300a2e01f1841d6034e0f01e0
2 parents 297466b + b00266f commit a79bca2

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

src/consensus/tx_verify.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
2727
return true;
2828
}
2929

30-
std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
30+
std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block)
3131
{
32-
assert(prevHeights->size() == tx.vin.size());
32+
assert(prevHeights.size() == tx.vin.size());
3333

3434
// Will be set to the equivalent height- and time-based nLockTime
3535
// values that would be necessary to satisfy all relative lock-
@@ -59,11 +59,11 @@ std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags
5959
// consensus-enforced meaning at this point.
6060
if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
6161
// The height of this input is not relevant for sequence locks
62-
(*prevHeights)[txinIndex] = 0;
62+
prevHeights[txinIndex] = 0;
6363
continue;
6464
}
6565

66-
int nCoinHeight = (*prevHeights)[txinIndex];
66+
int nCoinHeight = prevHeights[txinIndex];
6767

6868
if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
6969
int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
@@ -99,7 +99,7 @@ bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> loc
9999
return true;
100100
}
101101

102-
bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
102+
bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block)
103103
{
104104
return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
105105
}

src/consensus/tx_verify.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime);
6666
* Also removes from the vector of input heights any entries which did not
6767
* correspond to sequence locked inputs as they do not affect the calculation.
6868
*/
69-
std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block);
69+
std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block);
7070

7171
bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair);
7272
/**
7373
* Check if transaction is final per BIP 68 sequence numbers and can be included in a block.
7474
* Consensus critical. Takes as input a list of heights at which tx's inputs (in order) confirmed.
7575
*/
76-
bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block);
76+
bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>& prevHeights, const CBlockIndex& block);
7777

7878
#endif // BITCOIN_CONSENSUS_TX_VERIFY_H

src/test/miner_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
448448
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
449449
BOOST_CHECK(CheckFinalTx(CTransaction(tx), flags)); // Locktime passes
450450
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
451-
BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, &prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 2))); // Sequence locks pass on 2nd block
451+
BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 2))); // Sequence locks pass on 2nd block
452452

453453
// relative time locked
454454
tx.vin[0].prevout.hash = txFirst[1]->GetHash();
@@ -461,7 +461,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
461461

462462
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
463463
::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
464-
BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, &prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 1))); // Sequence locks pass 512 seconds later
464+
BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 1))); // Sequence locks pass 512 seconds later
465465
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
466466
::ChainActive().Tip()->GetAncestor(::ChainActive().Tip()->nHeight - i)->nTime -= 512; //undo tricked MTP
467467

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ bool CheckSequenceLocks(const CTxMemPool& pool, const CTransaction& tx, int flag
302302
prevheights[txinIndex] = coin.nHeight;
303303
}
304304
}
305-
lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
305+
lockPair = CalculateSequenceLocks(tx, flags, prevheights, index);
306306
if (lp) {
307307
lp->height = lockPair.first;
308308
lp->time = lockPair.second;
@@ -2172,7 +2172,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
21722172
prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
21732173
}
21742174

2175-
if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
2175+
if (!SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
21762176
LogPrintf("ERROR: %s: contains a non-BIP68-final transaction\n", __func__);
21772177
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal");
21782178
}

0 commit comments

Comments
 (0)