Skip to content

Commit 6b7bf90

Browse files
committed
Merge bitcoin/bitcoin#28825: fuzz: Minor improvements to tx_package_eval target
6a91791 fuzz: allow fake and duplicate inputs in tx_package_eval target (Greg Sanders) a0626cc fuzz: allow reaching MempoolAcceptResult::ResultType::DIFFERENT_WITNESS in tx_package_eval target (Greg Sanders) Pull request description: Exercises `DIFFERENT_WITNESS` by using "blank" WSH() and allowing witness to determine wtxid, and attempts to make invalid/duplicate inputs. ACKs for top commit: dergoegge: Coverage looks good to me ACK 6a91791 Tree-SHA512: db894f5f5b81c6b454874baf11f296462832285f41ccb09f23c0db92b9abc98f8ecacd72fc8f60dc92cb7947f543a2e55bed2fd210b0e8ca7c7d5389d90b14af
2 parents eb2ab3d + 6a91791 commit 6b7bf90

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/test/fuzz/package_eval.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void initialize_tx_pool()
4040
g_setup = testing_setup.get();
4141

4242
for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
43-
COutPoint prevout{MineBlock(g_setup->m_node, P2WSH_OP_TRUE)};
43+
COutPoint prevout{MineBlock(g_setup->m_node, P2WSH_EMPTY)};
4444
if (i < COINBASE_MATURITY) {
4545
// Remember the txids to avoid expensive disk access later on
4646
g_outpoints_coinbase_init_mature.push_back(prevout);
@@ -195,7 +195,8 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
195195
// Create input
196196
const auto sequence = ConsumeSequence(fuzzed_data_provider);
197197
const auto script_sig = CScript{};
198-
const auto script_wit_stack = std::vector<std::vector<uint8_t>>{WITNESS_STACK_ELEM_OP_TRUE};
198+
const auto script_wit_stack = fuzzed_data_provider.ConsumeBool() ? P2WSH_EMPTY_TRUE_STACK : P2WSH_EMPTY_TWO_STACK;
199+
199200
CTxIn in;
200201
in.prevout = outpoint;
201202
in.nSequence = sequence;
@@ -204,17 +205,30 @@ FUZZ_TARGET(tx_package_eval, .init = initialize_tx_pool)
204205

205206
tx_mut.vin.push_back(in);
206207
}
208+
209+
// Duplicate an input
210+
bool dup_input = fuzzed_data_provider.ConsumeBool();
211+
if (dup_input) {
212+
tx_mut.vin.push_back(tx_mut.vin.back());
213+
}
214+
215+
// Refer to a non-existant input
216+
if (fuzzed_data_provider.ConsumeBool()) {
217+
tx_mut.vin.emplace_back();
218+
}
219+
207220
const auto amount_fee = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(0, amount_in);
208221
const auto amount_out = (amount_in - amount_fee) / num_out;
209222
for (int i = 0; i < num_out; ++i) {
210-
tx_mut.vout.emplace_back(amount_out, P2WSH_OP_TRUE);
223+
tx_mut.vout.emplace_back(amount_out, P2WSH_EMPTY);
211224
}
212225
// TODO vary transaction sizes to catch size-related issues
213226
auto tx = MakeTransactionRef(tx_mut);
214227
// Restore previously removed outpoints, except in-package outpoints
215228
if (!last_tx) {
216229
for (const auto& in : tx->vin) {
217-
Assert(outpoints.insert(in.prevout).second);
230+
// It's a fake input, or a new input, or a duplicate
231+
Assert(in == CTxIn() || outpoints.insert(in.prevout).second || dup_input);
218232
}
219233
// Cache the in-package outpoints being made
220234
for (size_t i = 0; i < tx->vout.size(); ++i) {

src/test/util/script.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ static const CScript P2WSH_OP_TRUE{
1818
return hash;
1919
}())};
2020

21+
static const std::vector<uint8_t> EMPTY{};
22+
static const CScript P2WSH_EMPTY{
23+
CScript{}
24+
<< OP_0
25+
<< ToByteVector([] {
26+
uint256 hash;
27+
CSHA256().Write(EMPTY.data(), EMPTY.size()).Finalize(hash.begin());
28+
return hash;
29+
}())};
30+
static const std::vector<std::vector<uint8_t>> P2WSH_EMPTY_TRUE_STACK{{static_cast<uint8_t>(OP_TRUE)}, {}};
31+
static const std::vector<std::vector<uint8_t>> P2WSH_EMPTY_TWO_STACK{{static_cast<uint8_t>(OP_2)}, {}};
32+
2133
/** Flags that are not forbidden by an assert in script validation */
2234
bool IsValidFlagCombination(unsigned flags);
2335

0 commit comments

Comments
 (0)