mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-24 18:23:26 -03:00
refactor: Reserve vectors in fuzz tests
* Since the main LIMITED_WHILE stated `outpoints.size() < 200'000`, I've presized outpoints accordingly. * `tx_mut.vin` and `tx_mut.vout` weren't caught by the clang-tidy, but addressed them anyway.
This commit is contained in:
parent
152fefe7a2
commit
11f3bc229c
2 changed files with 5 additions and 0 deletions
|
@ -38,6 +38,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
|
||||||
|
|
||||||
TxOrphanage orphanage;
|
TxOrphanage orphanage;
|
||||||
std::vector<COutPoint> outpoints; // Duplicates are tolerated
|
std::vector<COutPoint> outpoints; // Duplicates are tolerated
|
||||||
|
outpoints.reserve(200'000);
|
||||||
|
|
||||||
// initial outpoints used to construct transactions later
|
// initial outpoints used to construct transactions later
|
||||||
for (uint8_t i = 0; i < 4; i++) {
|
for (uint8_t i = 0; i < 4; i++) {
|
||||||
|
@ -55,12 +56,14 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
|
||||||
const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 256);
|
const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 256);
|
||||||
// pick outpoints from outpoints as input. We allow input duplicates on purpose, given we are not
|
// pick outpoints from outpoints as input. We allow input duplicates on purpose, given we are not
|
||||||
// running any transaction validation logic before adding transactions to the orphanage
|
// running any transaction validation logic before adding transactions to the orphanage
|
||||||
|
tx_mut.vin.reserve(num_in);
|
||||||
for (uint32_t i = 0; i < num_in; i++) {
|
for (uint32_t i = 0; i < num_in; i++) {
|
||||||
auto& prevout = PickValue(fuzzed_data_provider, outpoints);
|
auto& prevout = PickValue(fuzzed_data_provider, outpoints);
|
||||||
// try making transactions unique by setting a random nSequence, but allow duplicate transactions if they happen
|
// try making transactions unique by setting a random nSequence, but allow duplicate transactions if they happen
|
||||||
tx_mut.vin.emplace_back(prevout, CScript{}, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, CTxIn::SEQUENCE_FINAL));
|
tx_mut.vin.emplace_back(prevout, CScript{}, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, CTxIn::SEQUENCE_FINAL));
|
||||||
}
|
}
|
||||||
// output amount will not affect txorphanage
|
// output amount will not affect txorphanage
|
||||||
|
tx_mut.vout.reserve(num_out);
|
||||||
for (uint32_t i = 0; i < num_out; i++) {
|
for (uint32_t i = 0; i < num_out; i++) {
|
||||||
tx_mut.vout.emplace_back(CAmount{0}, CScript{});
|
tx_mut.vout.emplace_back(CAmount{0}, CScript{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ template<typename B = uint8_t>
|
||||||
{
|
{
|
||||||
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
|
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
|
||||||
std::vector<std::string> r;
|
std::vector<std::string> r;
|
||||||
|
r.reserve(n_elements);
|
||||||
for (size_t i = 0; i < n_elements; ++i) {
|
for (size_t i = 0; i < n_elements; ++i) {
|
||||||
r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
|
r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
|
||||||
}
|
}
|
||||||
|
@ -90,6 +91,7 @@ template <typename T>
|
||||||
{
|
{
|
||||||
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
|
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
|
||||||
std::vector<T> r;
|
std::vector<T> r;
|
||||||
|
r.reserve(n_elements);
|
||||||
for (size_t i = 0; i < n_elements; ++i) {
|
for (size_t i = 0; i < n_elements; ++i) {
|
||||||
r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
|
r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue