refactor: use Span in random.* #24213

pull PastaPastaPasta wants to merge 1 commits into bitcoin:master from PastaPastaPasta:refactor-random changing 12 files +21 −19
  1. PastaPastaPasta commented at 11:51 AM on January 31, 2022: contributor

    ~This PR does two things~

    1. use a Span<unsigned char> for GetRandBytes and GetStrongRandBytes

    ~2. make GetRand a template for which any integral type can be used, where the default behavior is to return a random integral up to the max of the integral unless a max is provided. This simplifies a lot of code from GetRand(std::numeric_limits<uint64_t>::max() -> GetRand<uint64_t>()~

    MarcoFalke this was inspired by your comment here: #24185 (comment) about using Span, so hopefully I'll be able to get this PR done and merged 😂

    ~Also, if requested I could revert the GetRand(std::numeric_limits<uint64_t>::max() -> GetRand<uint64_t>() related changes if it ends up causing too many conflicts~

  2. maflcko commented at 12:21 PM on January 31, 2022: member

    This PR does two things

    This needs to be split into two commits for review

  3. DrahtBot added the label P2P on Jan 31, 2022
  4. DrahtBot added the label Refactoring on Jan 31, 2022
  5. DrahtBot added the label RPC/REST/ZMQ on Jan 31, 2022
  6. DrahtBot added the label Utils/log/libs on Jan 31, 2022
  7. DrahtBot added the label UTXO Db and Indexes on Jan 31, 2022
  8. DrahtBot added the label Wallet on Jan 31, 2022
  9. PastaPastaPasta commented at 12:33 PM on January 31, 2022: contributor

    This PR does two things

    This needs to be split into two commits for review

    Done, thanks

  10. PastaPastaPasta force-pushed on Jan 31, 2022
  11. PastaPastaPasta force-pushed on Jan 31, 2022
  12. DrahtBot commented at 8:49 PM on January 31, 2022: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #24185 (refactor: only use explicit reinterpret/const casts, not implicit by PastaPastaPasta)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  13. DrahtBot added the label Needs rebase on Mar 2, 2022
  14. PastaPastaPasta force-pushed on Mar 6, 2022
  15. DrahtBot removed the label Needs rebase on Mar 6, 2022
  16. PastaPastaPasta commented at 5:11 PM on March 6, 2022: contributor

    Any opinion on this? @MarcoFalke and others?

  17. in src/dbwrapper.cpp:230 in d35019566a outdated
     226 | @@ -227,7 +227,7 @@ const unsigned int CDBWrapper::OBFUSCATE_KEY_NUM_BYTES = 8;
     227 |  std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
     228 |  {
     229 |      std::vector<uint8_t> ret(OBFUSCATE_KEY_NUM_BYTES);
     230 | -    GetRandBytes(ret.data(), OBFUSCATE_KEY_NUM_BYTES);
     231 | +    GetRandBytes(ret);
    


    jonatack commented at 5:36 PM on March 6, 2022:

    eeae84c5eb missing span headers here in src/dbwrapper.cpp and in src/key.cpp, src/random.h, src/rpc/request.cpp, maybe reverify if didn't miss any others


    PastaPastaPasta commented at 3:30 PM on March 7, 2022:

    Should it really be included in files like dbwrapper.cpp and key.cpp and request.cpp? It seems to be that it makes more sense to just explicitly include it in src/random.h.

    If this is in developer notes or a strong opinion of yours, I'll happily do it, but seems better to just do it in random.h (applied and squashed random.h include)


    jonatack commented at 3:05 PM on March 9, 2022:

    Yes, the developer notes specify the guideline and reason:

    - Every `.cpp` and `.h` file should `#include` every header file it directly uses classes, functions or other
      definitions from, even if those headers are already included indirectly through other headers.
    
      - *Rationale*: Excluding headers because they are already indirectly included results in compilation
        failures when those indirect dependencies change. Furthermore, it obscures what the real code
        dependencies are.
    

    PastaPastaPasta commented at 5:47 PM on March 14, 2022:

    I agree with the rule, but I'm not sure I agree with the application. I'm not really "using" span in these cpp files, I'm simply passing something to a function. If we change random.h to not use span, then we need to change the calling files anyway.

    My local CLion for instance sees these includes are completely unused when I add them, so I see no reason to add them right now


    jonatack commented at 6:39 PM on March 14, 2022:

    Oh, you're right, now that you've added the header to random.h. Though key.cpp uses Span (not in your changes), so could add the header in 32b6173e30b514437f6b16aed261d118e416ae6e if you're so inclined. Otherwise LGTM.

  18. in src/addrdb.cpp:51 in d35019566a outdated
      47 | @@ -48,8 +48,7 @@ template <typename Data>
      48 |  bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version)
      49 |  {
      50 |      // Generate random temporary filename
      51 | -    uint16_t randv = 0;
      52 | -    GetRandBytes((unsigned char*)&randv, sizeof(randv));
      53 | +    uint16_t randv = GetRand<uint16_t>();
    


    jonatack commented at 6:02 PM on March 6, 2022:

    d35019566a

        const uint16_t randv{GetRand<uint16_t>()};
    

    PastaPastaPasta commented at 3:33 PM on March 7, 2022:

    applied and squashed

  19. in src/net_processing.cpp:4430 in d35019566a outdated
    4425 | -            GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
    4426 | -        }
    4427 | +        uint64_t nonce;
    4428 | +        do {
    4429 | +            nonce = GetRand<uint64_t>();
    4430 | +        } while (nonce == 0);
    


    jonatack commented at 6:05 PM on March 6, 2022:

    d35019566a what is the reason for the do/while style change?


    PastaPastaPasta commented at 3:31 PM on March 7, 2022:

    It avoids a redundant assignment, and I think it makes more sense, although I would be happy to revert this change if you would strongly want that


    jonatack commented at 3:08 PM on March 9, 2022:

    I wouldn't needlessly change it unless other reviewers prefer the change, but no strong opinion.


    laanwj commented at 10:31 AM on April 6, 2022:

    I think the old code is slightly more elegant. There's no need to distinguish the first iteration here, so let's not.

  20. jonatack commented at 6:22 PM on March 6, 2022: contributor

    Approach ACK, debug-builds cleanly, unit tests pass, feature_taproot.py passes (cf the CI failure)

  21. PastaPastaPasta force-pushed on Mar 7, 2022
  22. DrahtBot added the label Needs rebase on Mar 14, 2022
  23. PastaPastaPasta force-pushed on Mar 14, 2022
  24. DrahtBot removed the label Needs rebase on Mar 14, 2022
  25. jonatack commented at 7:18 PM on March 14, 2022: contributor

    ACK b989b5a43284b49c49e11d45c12c1daccc80e17a

  26. maflcko removed the label Wallet on Mar 15, 2022
  27. maflcko removed the label UTXO Db and Indexes on Mar 15, 2022
  28. maflcko removed the label RPC/REST/ZMQ on Mar 15, 2022
  29. maflcko removed the label P2P on Mar 15, 2022
  30. maflcko removed the label Utils/log/libs on Mar 15, 2022
  31. DrahtBot added the label Needs rebase on Mar 23, 2022
  32. refactor: use Span in random.* 3ae7791bca
  33. PastaPastaPasta commented at 11:02 PM on March 23, 2022: contributor

    I have rebased. @jonatack please re-review.

  34. PastaPastaPasta force-pushed on Mar 23, 2022
  35. DrahtBot removed the label Needs rebase on Mar 24, 2022
  36. jonatack commented at 11:25 AM on March 24, 2022: contributor

    re-ACK 19f2c35200321f0aeb728c51b75dcb47d9cca3a5 per git range-diff 8234cda b989b5a 19f2c35, rebase only

  37. PastaPastaPasta commented at 11:03 PM on April 3, 2022: contributor

    @MarcoFalke would you be able to review this PR?

  38. laanwj commented at 10:35 AM on April 6, 2022: member

    Concept and code review ACK on the span commit. I find the other one changing GetRandInt more risky and harder to review.

    Edit: might want to split this into different PRs as the changes, apart from both being related to random, are orthogonal.

  39. in src/net.cpp:2172 in 19f2c35200 outdated
    2167 | @@ -2168,7 +2168,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
    2168 |  
    2169 |              if (fFeeler) {
    2170 |                  // Add small amount of random noise before connection to avoid synchronization.
    2171 | -                int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
    2172 | +                int randsleep = GetRand<int>(FEELER_SLEEP_WINDOW * 1000);
    2173 |                  if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
    


    maflcko commented at 11:26 AM on April 8, 2022:

    This can probably use the exiting native chrono random stuff.

    Further, I am wondering if it makes sense to add a FastRandomContext member to connman and use that for all noise generation instead of generating a new one each time. A commit like fa73e9d781d4898dbfd960aab997d99c62bc01ed can then be used to provide the chrono random stuff.

  40. in src/init.cpp:1260 in 19f2c35200 outdated
    1256 | @@ -1257,7 +1257,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
    1257 |      assert(!node.banman);
    1258 |      node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
    1259 |      assert(!node.connman);
    1260 | -    node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), *node.addrman, args.GetBoolArg("-networkactive", true));
    1261 | +    node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(), GetRand<uint64_t>(), *node.addrman, args.GetBoolArg("-networkactive", true));
    


    maflcko commented at 11:28 AM on April 8, 2022:

    Maybe the fast context could then also be used to remove those args?

  41. in src/net_processing.cpp:4429 in 19f2c35200 outdated
    4428 | -        while (nonce == 0) {
    4429 | -            GetRandBytes({(unsigned char*)&nonce, sizeof(nonce)});
    4430 | -        }
    4431 | +        uint64_t nonce;
    4432 | +        do {
    4433 | +            nonce = GetRand<uint64_t>();
    


    maflcko commented at 11:31 AM on April 8, 2022:

    I wonder if it makes sense to use a single FastRandomContext member for all entropy drawn inside PeerManagerImpl, instead of creating a new context for each call.

  42. PastaPastaPasta force-pushed on Apr 19, 2022
  43. PastaPastaPasta renamed this:
    refactor: use Span in random.*; make GetRand a template, remove GetRandInt
    refactor: use Span in random.*
    on Apr 19, 2022
  44. PastaPastaPasta commented at 6:51 PM on April 19, 2022: contributor

    @laanwj Thanks for the review, I've dropped that other commit from this PR. Please re-ACK

  45. laanwj added the label Utils/log/libs on Apr 21, 2022
  46. laanwj commented at 1:48 PM on April 21, 2022: member

    Thank you! Code review re-ACK 3ae7791bcaa88f5c68592673b8926ee807242ce7

  47. laanwj merged this on Apr 21, 2022
  48. laanwj closed this on Apr 21, 2022

  49. PastaPastaPasta deleted the branch on Apr 21, 2022
  50. sidhujag referenced this in commit ad8500793a on Apr 22, 2022
  51. Fabcien referenced this in commit 2cdd79b987 on Nov 21, 2023
  52. bitcoin locked this on Jan 10, 2024

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-04-13 15:14 UTC

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