Skip to content

Commit d2f6d2a

Browse files
committed
Use int32_t type for most transaction size/weight values
This change gets rid of a few casts and makes the following commit diff smaller.
1 parent c92fd63 commit d2f6d2a

File tree

9 files changed

+39
-37
lines changed

9 files changed

+39
-37
lines changed

contrib/tracing/mempool_monitor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
struct added_event
2828
{
2929
u8 hash[HASH_LENGTH];
30-
u64 vsize;
30+
s32 vsize;
3131
s64 fee;
3232
};
3333
3434
struct removed_event
3535
{
3636
u8 hash[HASH_LENGTH];
3737
char reason[MAX_REMOVAL_REASON_LENGTH];
38-
u64 vsize;
38+
s32 vsize;
3939
s64 fee;
4040
u64 entry_time;
4141
};
@@ -49,11 +49,11 @@
4949
struct replaced_event
5050
{
5151
u8 replaced_hash[HASH_LENGTH];
52-
u64 replaced_vsize;
52+
s32 replaced_vsize;
5353
s64 replaced_fee;
5454
u64 replaced_entry_time;
5555
u8 replacement_hash[HASH_LENGTH];
56-
u64 replacement_vsize;
56+
s32 replacement_vsize;
5757
s64 replacement_fee;
5858
};
5959

doc/tracing.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ about the transaction.
220220

221221
Arguments passed:
222222
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
223-
2. Transaction virtual size as `uint64`
223+
2. Transaction virtual size as `int32`
224224
3. Transaction fee as `int64`
225225

226226
#### Tracepoint `mempool:removed`
@@ -231,7 +231,7 @@ about the transaction.
231231
Arguments passed:
232232
1. Transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
233233
2. Removal reason as `pointer to C-style String` (max. length 9 characters)
234-
3. Transaction virtual size as `uint64`
234+
3. Transaction virtual size as `int32`
235235
4. Transaction fee as `int64`
236236
5. Transaction mempool entry time (epoch) as `uint64`
237237

@@ -242,11 +242,11 @@ Passes information about the replaced and replacement transactions.
242242

243243
Arguments passed:
244244
1. Replaced transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
245-
2. Replaced transaction virtual size as `uint64`
245+
2. Replaced transaction virtual size as `int32`
246246
3. Replaced transaction fee as `int64`
247247
4. Replaced transaction mempool entry time (epoch) as `uint64`
248248
5. Replacement transaction ID (hash) as `pointer to unsigned chars` (i.e. 32 bytes in little-endian)
249-
6. Replacement transaction virtual size as `uint64`
249+
6. Replacement transaction virtual size as `int32`
250250
7. Replacement transaction fee as `int64`
251251

252252
Note: In cases where a single replacement transaction replaces multiple

src/consensus/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ class BlockValidationState : public ValidationState<BlockValidationResult> {};
145145
// using only serialization with and without witness data. As witness_size
146146
// is equal to total_size - stripped_size, this formula is identical to:
147147
// weight = (stripped_size * 3) + total_size.
148-
static inline int64_t GetTransactionWeight(const CTransaction& tx)
148+
static inline int32_t GetTransactionWeight(const CTransaction& tx)
149149
{
150150
return ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION);
151151
}

src/kernel/mempool_entry.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CTxMemPoolEntry
7575
mutable Parents m_parents;
7676
mutable Children m_children;
7777
const CAmount nFee; //!< Cached to avoid expensive parent-transaction lookups
78-
const size_t nTxWeight; //!< ... and avoid recomputing tx weight (also used for GetTxSize())
78+
const int32_t nTxWeight; //!< ... and avoid recomputing tx weight (also used for GetTxSize())
7979
const size_t nUsageSize; //!< ... and total memory usage
8080
const int64_t nTime; //!< Local time when entering the mempool
8181
const unsigned int entryHeight; //!< Chain height when entering the mempool
@@ -88,12 +88,14 @@ class CTxMemPoolEntry
8888
// mempool; if we remove this transaction we must remove all of these
8989
// descendants as well.
9090
uint64_t nCountWithDescendants{1}; //!< number of descendant transactions
91-
uint64_t nSizeWithDescendants; //!< ... and size
91+
// Using int64_t instead of int32_t to avoid signed integer overflow issues.
92+
int64_t nSizeWithDescendants; //!< ... and size
9293
CAmount nModFeesWithDescendants; //!< ... and total fees (all including us)
9394

9495
// Analogous statistics for ancestor transactions
9596
uint64_t nCountWithAncestors{1};
96-
uint64_t nSizeWithAncestors;
97+
// Using int64_t instead of int32_t to avoid signed integer overflow issues.
98+
int64_t nSizeWithAncestors;
9799
CAmount nModFeesWithAncestors;
98100
int64_t nSigOpCostWithAncestors;
99101

@@ -104,7 +106,7 @@ class CTxMemPoolEntry
104106
int64_t sigops_cost, LockPoints lp)
105107
: tx{tx},
106108
nFee{fee},
107-
nTxWeight(GetTransactionWeight(*tx)),
109+
nTxWeight{GetTransactionWeight(*tx)},
108110
nUsageSize{RecursiveDynamicUsage(tx)},
109111
nTime{time},
110112
entryHeight{entry_height},
@@ -121,11 +123,11 @@ class CTxMemPoolEntry
121123
const CTransaction& GetTx() const { return *this->tx; }
122124
CTransactionRef GetSharedTx() const { return this->tx; }
123125
const CAmount& GetFee() const { return nFee; }
124-
size_t GetTxSize() const
126+
int32_t GetTxSize() const
125127
{
126128
return GetVirtualTransactionSize(nTxWeight, sigOpCost, ::nBytesPerSigOp);
127129
}
128-
size_t GetTxWeight() const { return nTxWeight; }
130+
int32_t GetTxWeight() const { return nTxWeight; }
129131
std::chrono::seconds GetTime() const { return std::chrono::seconds{nTime}; }
130132
unsigned int GetHeight() const { return entryHeight; }
131133
int64_t GetSigOpCost() const { return sigOpCost; }
@@ -134,9 +136,9 @@ class CTxMemPoolEntry
134136
const LockPoints& GetLockPoints() const { return lockPoints; }
135137

136138
// Adjusts the descendant state.
137-
void UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount);
139+
void UpdateDescendantState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount);
138140
// Adjusts the ancestor state
139-
void UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
141+
void UpdateAncestorState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps);
140142
// Updates the modified fees with descendants/ancestors.
141143
void UpdateModifiedFee(CAmount fee_diff)
142144
{
@@ -152,13 +154,13 @@ class CTxMemPoolEntry
152154
}
153155

154156
uint64_t GetCountWithDescendants() const { return nCountWithDescendants; }
155-
uint64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
157+
int64_t GetSizeWithDescendants() const { return nSizeWithDescendants; }
156158
CAmount GetModFeesWithDescendants() const { return nModFeesWithDescendants; }
157159

158160
bool GetSpendsCoinbase() const { return spendsCoinbase; }
159161

160162
uint64_t GetCountWithAncestors() const { return nCountWithAncestors; }
161-
uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
163+
int64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
162164
CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
163165
int64_t GetSigOpCostWithAncestors() const { return nSigOpCostWithAncestors; }
164166

src/policy/policy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static constexpr unsigned int DEFAULT_BLOCK_MAX_WEIGHT{MAX_BLOCK_WEIGHT - 4000};
2424
/** Default for -blockmintxfee, which sets the minimum feerate for a transaction in blocks created by mining code **/
2525
static constexpr unsigned int DEFAULT_BLOCK_MIN_TX_FEE{1000};
2626
/** The maximum weight for transactions we're willing to relay/mine */
27-
static constexpr unsigned int MAX_STANDARD_TX_WEIGHT{400000};
27+
static constexpr int32_t MAX_STANDARD_TX_WEIGHT{400000};
2828
/** The minimum non-witness size for transactions we're willing to relay/mine: one larger than 64 */
2929
static constexpr unsigned int MIN_STANDARD_TX_NONWITNESS_SIZE{65};
3030
/** Maximum number of signature check operations in an IsStandard() P2SH script */

src/test/miniminer_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ BOOST_FIXTURE_TEST_CASE(miniminer_1p1c, TestChain100Setup)
137137

138138
std::vector<CTransactionRef> all_transactions{tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8};
139139
struct TxDimensions {
140-
size_t vsize; CAmount mod_fee; CFeeRate feerate;
140+
int32_t vsize; CAmount mod_fee; CFeeRate feerate;
141141
};
142142
std::map<uint256, TxDimensions> tx_dims;
143143
for (const auto& tx : all_transactions) {

src/txmempool.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendan
7575
}
7676
// descendants now contains all in-mempool descendants of updateIt.
7777
// Update and add to cached descendant map
78-
int64_t modifySize = 0;
78+
int32_t modifySize = 0;
7979
CAmount modifyFee = 0;
8080
int64_t modifyCount = 0;
8181
for (const CTxMemPoolEntry& descendant : descendants) {
@@ -91,7 +91,7 @@ void CTxMemPool::UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendan
9191
// Don't directly remove the transaction here -- doing so would
9292
// invalidate iterators in cachedDescendants. Mark it for removal
9393
// by inserting into descendants_to_remove.
94-
if (descendant.GetCountWithAncestors() > uint64_t(m_limits.ancestor_count) || descendant.GetSizeWithAncestors() > uint64_t(m_limits.ancestor_size_vbytes)) {
94+
if (descendant.GetCountWithAncestors() > uint64_t(m_limits.ancestor_count) || descendant.GetSizeWithAncestors() > m_limits.ancestor_size_vbytes) {
9595
descendants_to_remove.insert(descendant.GetTx().GetHash());
9696
}
9797
}
@@ -278,8 +278,8 @@ void CTxMemPool::UpdateAncestorsOf(bool add, txiter it, setEntries &setAncestors
278278
for (const CTxMemPoolEntry& parent : parents) {
279279
UpdateChild(mapTx.iterator_to(parent), it, add);
280280
}
281-
const int64_t updateCount = (add ? 1 : -1);
282-
const int64_t updateSize = updateCount * it->GetTxSize();
281+
const int32_t updateCount = (add ? 1 : -1);
282+
const int32_t updateSize{updateCount * it->GetTxSize()};
283283
const CAmount updateFee = updateCount * it->GetModifiedFee();
284284
for (txiter ancestorIt : setAncestors) {
285285
mapTx.modify(ancestorIt, [=](CTxMemPoolEntry& e) { e.UpdateDescendantState(updateSize, updateFee, updateCount); });
@@ -323,7 +323,7 @@ void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, b
323323
setEntries setDescendants;
324324
CalculateDescendants(removeIt, setDescendants);
325325
setDescendants.erase(removeIt); // don't update state for self
326-
int64_t modifySize = -((int64_t)removeIt->GetTxSize());
326+
int32_t modifySize = -removeIt->GetTxSize();
327327
CAmount modifyFee = -removeIt->GetModifiedFee();
328328
int modifySigOps = -removeIt->GetSigOpCost();
329329
for (txiter dit : setDescendants) {
@@ -365,19 +365,19 @@ void CTxMemPool::UpdateForRemoveFromMempool(const setEntries &entriesToRemove, b
365365
}
366366
}
367367

368-
void CTxMemPoolEntry::UpdateDescendantState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount)
368+
void CTxMemPoolEntry::UpdateDescendantState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount)
369369
{
370370
nSizeWithDescendants += modifySize;
371-
assert(int64_t(nSizeWithDescendants) > 0);
371+
assert(nSizeWithDescendants > 0);
372372
nModFeesWithDescendants = SaturatingAdd(nModFeesWithDescendants, modifyFee);
373373
nCountWithDescendants += modifyCount;
374374
assert(int64_t(nCountWithDescendants) > 0);
375375
}
376376

377-
void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
377+
void CTxMemPoolEntry::UpdateAncestorState(int32_t modifySize, CAmount modifyFee, int64_t modifyCount, int64_t modifySigOps)
378378
{
379379
nSizeWithAncestors += modifySize;
380-
assert(int64_t(nSizeWithAncestors) > 0);
380+
assert(nSizeWithAncestors > 0);
381381
nModFeesWithAncestors = SaturatingAdd(nModFeesWithAncestors, modifyFee);
382382
nCountWithAncestors += modifyCount;
383383
assert(int64_t(nCountWithAncestors) > 0);
@@ -699,7 +699,7 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
699699
// Verify ancestor state is correct.
700700
auto ancestors{AssumeCalculateMemPoolAncestors(__func__, *it, Limits::NoLimits())};
701701
uint64_t nCountCheck = ancestors.size() + 1;
702-
uint64_t nSizeCheck = it->GetTxSize();
702+
int32_t nSizeCheck = it->GetTxSize();
703703
CAmount nFeesCheck = it->GetModifiedFee();
704704
int64_t nSigOpCheck = it->GetSigOpCost();
705705

@@ -720,7 +720,7 @@ void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendhei
720720
// Check children against mapNextTx
721721
CTxMemPoolEntry::Children setChildrenCheck;
722722
auto iter = mapNextTx.lower_bound(COutPoint(it->GetTx().GetHash(), 0));
723-
uint64_t child_sizes = 0;
723+
int32_t child_sizes{0};
724724
for (; iter != mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) {
725725
txiter childit = mapTx.find(iter->second->GetHash());
726726
assert(childit != mapTx.end()); // mapNextTx points to in-mempool transactions

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ struct TxMempoolInfo
222222
CAmount fee;
223223

224224
/** Virtual size of the transaction. */
225-
size_t vsize;
225+
int32_t vsize;
226226

227227
/** The fee delta. */
228228
int64_t nFeeDelta;

test/functional/interface_usdt_mempool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535
struct added_event
3636
{
3737
u8 hash[HASH_LENGTH];
38-
u64 vsize;
38+
s32 vsize;
3939
s64 fee;
4040
};
4141
4242
struct removed_event
4343
{
4444
u8 hash[HASH_LENGTH];
4545
char reason[MAX_REMOVAL_REASON_LENGTH];
46-
u64 vsize;
46+
s32 vsize;
4747
s64 fee;
4848
u64 entry_time;
4949
};
@@ -57,11 +57,11 @@
5757
struct replaced_event
5858
{
5959
u8 replaced_hash[HASH_LENGTH];
60-
u64 replaced_vsize;
60+
s32 replaced_vsize;
6161
s64 replaced_fee;
6262
u64 replaced_entry_time;
6363
u8 replacement_hash[HASH_LENGTH];
64-
u64 replacement_vsize;
64+
s32 replacement_vsize;
6565
s64 replacement_fee;
6666
};
6767

0 commit comments

Comments
 (0)