Declarative construction of test data #36692

issue purpleKarrot opened this issue on October 29, 2026
  1. purpleKarrot commented at 11:19 AM on October 29, 2026: contributor

    This is a tracking issue for individual refactorings of unit tests.

    Rationale: Primitive types like CTxOut currently provide read and write access to their data members like scriptPubKey. Consensus validation does not require write access. Hence, it is forced to depend on functionality that it does not use, which is a violation of the Interface Segregation Principle (ISP). A whole category of bugs could be eliminated if this functionality was removed.


    One case where write access to those data members is extensively used, is the imperative building of test data in unit tests like this:

    https://github.com/bitcoin/bitcoin/blob/9bf4815b94b9eaf8807f4e569091778da6c338d5/src/bench/ccoins_caching.cpp#L36-L52

    The imperative building could be replaced with declarative construction:

    const CTransaction tx_1{
        std::array{
            CTxIn{COutPoint{dummyTransactions[0].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0)},
            CTxIn{COutPoint{dummyTransactions[1].GetHash(), 0}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
            CTxIn{COutPoint{dummyTransactions[1].GetHash(), 1}, CScript{} << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4)},
        },
        std::array{
            CTxOut{90 * COIN, CScript{} << OP_1},
            CTxOut{},
        },
    };
    

    This example shows several advantages:

    • It reduces the amount of necessary boilerplate.
    • It is necessarily honest: The fact that the outputs contain an empty element is plain to see rather than hidden in a resize(2) call.
    • It removes the need for a dedicated "builder" type, like CMutableTransaction in this case.

    Another case where write access is currently used, is the reuse of test data, like in this example:

    https://github.com/bitcoin/bitcoin/blob/9bf4815b94b9eaf8807f4e569091778da6c338d5/src/test/script_tests.cpp#L1287-L1339

    The test data txFrom is constructed in line 1287 and later modified in lines 1315 and 1331, through the scriptPubKey reference defined in line 1289. Notably, each block of code that uses a different state of the test data is introduced by a comment containing the word "case" ("Single signature case", "P2SH, single-signature case", "Hardest case: Multisig 2-of-3").

    Separating the individual "cases" into actually independent test cases would remove the need for write access in addition to providing additional advantages:

    • Easier to reason about: The test input data is visible in a single place. It does not depend on modifications elsewhere or side effects of other tests.
    • Easier to parallelize: Individual test cases can run concurrently.

    Refactoring the imperative code in unit tests is a precondition for solving the ISP issues of the primitive types. The two examples above show that such a refactoring is self-contained (it only affects test code, and in most cases only individual tests, so it can be done one test at a time). It also shows that it provides benefits for each individual test on its own.

  2. maflcko commented at 11:32 AM on October 29, 2026: member

    This change is tedious to review, but I think worth it. Having tests that use mutable structs over hundreds of lines and separate unit test cases and each case may modify some fields of the struct leads to test bugs (copy-paste errors) and makes it harder to read the test, because more time is spent on trying to figure out the current state than the test logic itself.

    So rough Concept ACK, but obviously the approach may differ on a case-by-case basis.

  3. maflcko added the label Refactoring on Oct 29, 2026
  4. maflcko added the label Tests on Oct 29, 2026
  5. maflcko commented at 1:04 PM on October 29, 2026: member

    declarative construction:

    const CTransaction tx_1{ std::array{ CTxIn{...}...

    btw, I am not sure on forcing the call sites to specify a container. This is verbose and distracting in test code. If this is done, the ctor should simply accept a std::vector<T> vin/out. This keeps all call sites clean and possibly also avoids a copy on the stack (not that performance is relevant here)? Of course, fancy callers can still do fancy stuff like passing (std::views::concat(a, b) | std::ranges::to<std::vector>());, but then the fancy stuff only requires verbose code at the fancy call sites. So my preference would be to keep this plain and simple C++11.

  6. fanquake referenced this in commit 18953bd01f on Oct 29, 2026
  7. maflcko added the label Brainstorming on Oct 29, 2026
  8. maflcko added the label Tracking Issue on Oct 29, 2026
  9. purpleKarrot commented at 3:31 PM on October 29, 2026: contributor

    the ctor should simply accept a std::vector<T> vin/out. This keeps all call sites clean and possibly also avoids a copy @maflcko, the problem is that moving a vector into place allows the following:

    std::vector<CTxIn> inputs = ...;
    CTxIn& backdoor = inputs.back();
    
    const CTransaction tx{std::move(inputs), ...};
    
    backdoor = ...; // <- modify `tx`, potentially breaking its invariant
    

    So it is better to not try to avoid the copy. But if a copy needs to be made, why should we enforce the type of the temporary? The compiler cannot deduce template arguments from bare braced initializer lists, which I would not consider a big problem here. I think it actually helps readability to specify a type because it communicates to the reader: "I am creating a container" rather than "I am creating some object". Using std::array seems to have less overhead than std::vector here and is easier to type than std::initializer_list<CTxIn>, though all three would work in the same way.

    If performance of the copy becomes a concern, we should strive for making construction constexpr (not proposed here).

  10. maflcko commented at 4:00 PM on October 29, 2026: member

    Right, but I'd still prefer the plain and less verbose C++11 ctor here. Recall the goal is to mostly catch accidental bugs in tests, not intentional bugs. If we are worried about the mutable reference backdoor, I think it can be trivially fixed in C++11 by just doing the copy (performance doesn't matter for this test-only code). While clang-tidy use-after-free doesn't catch such partial moves, I am sure human or LLM reviewers will catch it, so I wouldn't worry too much about it.

  11. alexanderwiederin commented at 3:56 PM on October 30, 2026: contributor

    To summarise where the constructor discussion stands as I understand it: the aliasing concern can be addressed by either design, as noted above, so it does not decide between them. What remains:

    • Literal lists: explicit std::array{...} versus plain braces (with one element per line and trailing commas). The explicit container makes it clear that the inputs and outputs are each a collection, and where each one ends. Plain braces are less verbose, and since every element already names its type, the container name adds little. For an empty list, however, a {} doesn't say what it is, whereas std::array<CTxIn, 0>{} is unambiguous.

    • Flexibility: the range constructor accepts any range directly, including views of derived inputs. The vector constructor needs those converted first: through the iterator-pair constructor on a separate line in C++20, or inline with | std::ranges::to<std::vector>() in C++23. If it also copies to avoid aliasing, the elements are copied twice. For example, with the range constructor, a child spending output 0 of every parent (as in txpackage_tests) could be written as:

      const CTransaction child{
          package | std::views::transform([](const CTransactionRef& p) { 
              return CTxIn{COutPoint{p->GetHash(), 0}};
          }),
          std::array{
              CTxOut{47 * COIN, spk2},
          },
      };
      
    • Complexity: a constrained template versus a plain C++11 constructor.

    Let me know if I've missed or misrepresented anything.

  12. fanquake referenced this in commit 3b1ba022ef on Nov 5, 2026
  13. yuvicc commented at 4:51 AM on November 6, 2026: contributor

    Thanks for the summary @alexanderwiederin. Going through your point and also while converting #36750 and #36788, almost every call site I converted is a literal list while empty case came up once in script_p2sh_tests, for that named-argument comment seems enough:

    const CTransaction coinbase_tx{{CTxIn{COutPoint{}}}, /*vout=*/{}};
    

    Regarding flexibility, in the txpackage_tests example, the parents are created in the same loop that adds the child's inputs:

    for (int i{0}; i < 24; ++i) {
        auto parent = MakeTransactionRef(CreateValidMempoolTransaction(...));
        package.emplace_back(parent);
        child.vin.emplace_back(COutPoint(parent->GetHash(), 0));
    }
    

    With a vector ctor, child.vin would just become a local std::vector<CTxIn> filled in that loop and passed in afterwards. No extra conversion line, and it reads no worse than the transform. I agree the extra copy is real, but for test callers I don't think it matters. I think the template's flexibility is rarely used in practice, so the simpler constructor seems the better trade.

    Looks like aliasing is already possible on master through CTransaction(CMutableTransaction&&), which moves tx.vin/tx.vout into place (transaction.cpp#L96, and #36696 keeps this as well. That said aliasing can't be deciding between the designs.

    On the side of the range constructor: following #35569, it avoids tying the interface to std::vector, so the representation could change later.

    ~So my weak preference is the plain std::vector ctor.~ I now lean towards the range ctor.

  14. purpleKarrot commented at 10:06 AM on November 6, 2026: contributor

    @yuvicc, I think your example can show the advantage of a constructor accepting ranges, because the loop can be refactored to:

    auto package = iota(0, 24) | transform(CreateValidMempoolTransaction);
    auto tx = CTransaction{
      package | transform([](auto& tx) { return COutPoint{tx.GetHash(), 0}; }),
      ...
    };
    
  15. sedited referenced this in commit e71cebc016 on Nov 6, 2026
  16. sedited referenced this in commit 4ef7904c39 on Nov 8, 2026
  17. yuvicc commented at 6:32 AM on November 9, 2026: contributor

    I've opened #36798 which could be a a good example of why the second part of this issue, splitting shared-state tests into independent cases, matters as much as the constructor.

  18. fanquake referenced this in commit 44901d8f64 on Nov 9, 2026
  19. alexanderwiederin commented at 10:54 AM on November 10, 2026: contributor

    On the range-vs-vector question, I lean towards the range ctor for caller flexibility (e.g. passing views directly) and for not adding another std::vector to the interface. No strong opinion though.

    Since no one seems to consider this blocking, I suggest going with the author’s choice so this can move forward, unless someone feels otherwise.

  20. fanquake referenced this in commit 0b9c2c30bf on Nov 10, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me