Skip to content

Commit 22dccea

Browse files
glozowinstagibbs
andcommitted
[fuzz] txorphan byte accounting
Co-authored-by: Greg Sanders <[email protected]>
1 parent 982ce10 commit 22dccea

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

src/test/fuzz/txorphan.cpp

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
7575
return new_tx;
7676
}();
7777

78+
const auto wtxid{tx->GetWitnessHash()};
79+
7880
// Trigger orphanage functions that are called using parents. ptx_potential_parent is a tx we constructed in a
7981
// previous loop and potentially the parent of this tx.
8082
if (ptx_potential_parent) {
@@ -94,6 +96,9 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
9496
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS)
9597
{
9698
NodeId peer_id = fuzzed_data_provider.ConsumeIntegral<NodeId>();
99+
const auto total_bytes_start{orphanage.TotalOrphanUsage()};
100+
const auto total_peer_bytes_start{orphanage.UsageByPeer(peer_id)};
101+
const auto tx_weight{GetTransactionWeight(*tx)};
97102

98103
CallOneOf(
99104
fuzzed_data_provider,
@@ -113,12 +118,29 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
113118
bool add_tx = orphanage.AddTx(tx, peer_id);
114119
// have_tx == true -> add_tx == false
115120
Assert(!have_tx || !add_tx);
121+
122+
if (add_tx) {
123+
Assert(orphanage.UsageByPeer(peer_id) == tx_weight + total_peer_bytes_start);
124+
Assert(orphanage.TotalOrphanUsage() == tx_weight + total_bytes_start);
125+
Assert(tx_weight <= MAX_STANDARD_TX_WEIGHT);
126+
} else {
127+
// Peer may have been added as an announcer.
128+
if (orphanage.UsageByPeer(peer_id) == tx_weight + total_peer_bytes_start) {
129+
Assert(orphanage.HaveTxFromPeer(wtxid, peer_id));
130+
} else {
131+
// Otherwise, there must not be any change to the peer byte count.
132+
Assert(orphanage.UsageByPeer(peer_id) == total_peer_bytes_start);
133+
}
134+
135+
// Regardless, total bytes should not have changed.
136+
Assert(orphanage.TotalOrphanUsage() == total_bytes_start);
137+
}
116138
}
117139
have_tx = orphanage.HaveTx(tx->GetWitnessHash());
118140
{
119141
bool add_tx = orphanage.AddTx(tx, peer_id);
120142
// if have_tx is still false, it must be too big
121-
Assert(!have_tx == (GetTransactionWeight(*tx) > MAX_STANDARD_TX_WEIGHT));
143+
Assert(!have_tx == (tx_weight > MAX_STANDARD_TX_WEIGHT));
122144
Assert(!have_tx || !add_tx);
123145
}
124146
},
@@ -132,23 +154,46 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
132154
Assert(have_tx || !added_announcer);
133155
// have_tx_and_peer == true -> added_announcer == false
134156
Assert(!have_tx_and_peer || !added_announcer);
157+
158+
// Total bytes should not have changed. If peer was added as announcer, byte
159+
// accounting must have been updated.
160+
Assert(orphanage.TotalOrphanUsage() == total_bytes_start);
161+
if (added_announcer) {
162+
Assert(orphanage.UsageByPeer(peer_id) == tx_weight + total_peer_bytes_start);
163+
} else {
164+
Assert(orphanage.UsageByPeer(peer_id) == total_peer_bytes_start);
165+
}
135166
}
136167
},
137168
[&] {
138169
bool have_tx = orphanage.HaveTx(tx->GetWitnessHash());
170+
bool have_tx_and_peer{orphanage.HaveTxFromPeer(wtxid, peer_id)};
139171
// EraseTx should return 0 if m_orphans doesn't have the tx
140172
{
173+
auto bytes_from_peer_before{orphanage.UsageByPeer(peer_id)};
141174
Assert(have_tx == orphanage.EraseTx(tx->GetWitnessHash()));
175+
if (have_tx) {
176+
Assert(orphanage.TotalOrphanUsage() == total_bytes_start - tx_weight);
177+
if (have_tx_and_peer) {
178+
Assert(orphanage.UsageByPeer(peer_id) == bytes_from_peer_before - tx_weight);
179+
} else {
180+
Assert(orphanage.UsageByPeer(peer_id) == bytes_from_peer_before);
181+
}
182+
} else {
183+
Assert(orphanage.TotalOrphanUsage() == total_bytes_start);
184+
}
142185
}
143186
have_tx = orphanage.HaveTx(tx->GetWitnessHash());
187+
have_tx_and_peer = orphanage.HaveTxFromPeer(wtxid, peer_id);
144188
// have_tx should be false and EraseTx should fail
145189
{
146-
Assert(!have_tx && !orphanage.EraseTx(tx->GetWitnessHash()));
190+
Assert(!have_tx && !have_tx_and_peer && !orphanage.EraseTx(wtxid));
147191
}
148192
},
149193
[&] {
150194
orphanage.EraseForPeer(peer_id);
151195
Assert(!orphanage.HaveTxFromPeer(tx->GetWitnessHash(), peer_id));
196+
Assert(orphanage.UsageByPeer(peer_id) == 0);
152197
},
153198
[&] {
154199
// test mocktime and expiry

0 commit comments

Comments
 (0)