Extract AppInitLoadBlockIndex from AppInitMain #13582

pull Empact wants to merge 1 commits into bitcoin:master from Empact:app-init-load-block-index changing 1 files +140 −136
  1. Empact commented at 8:23 PM on July 1, 2018: member

    AppInitMain goes from ~650 lines to ~500. This also replaces constructs like while(false) and using break vs return with a simple bool result for more explicit operation.

    Prompted by looking into #13577

    Suggest: git diff --color-moved=dimmed_zebra --color-moved-ws=allow-indentation-change head^ to aid review.

  2. Empact cross-referenced this on Jul 1, 2018 from issue logging: avoid nStart may be used uninitialized in AppInitMain warning by mruddy
  3. Empact force-pushed on Jul 1, 2018
  4. Empact force-pushed on Jul 1, 2018
  5. fanquake added the label Refactoring on Jul 1, 2018
  6. Empact force-pushed on Jul 2, 2018
  7. Empact force-pushed on Jul 2, 2018
  8. Empact force-pushed on Jul 2, 2018
  9. fanquake commented at 7:11 AM on July 2, 2018: member

    Both Windows builds failed with:

    In file included from /home/travis/build/bitcoin/bitcoin/depends/i686-w64-mingw32/share/../include/boost/thread/win32/condition_variable.hpp:9:0,
                     from /home/travis/build/bitcoin/bitcoin/depends/i686-w64-mingw32/share/../include/boost/thread/condition_variable.hpp:14,
                     from ./util.h:35,
                     from ./init.h:11,
                     from init.cpp:10:
    /home/travis/build/bitcoin/bitcoin/depends/i686-w64-mingw32/share/../include/boost/thread/win32/thread_primitives.hpp:177:55: warning: conflicts with previous declaration 'void* boost::detail::win32::GetModuleHandleA(const char*)'
                     __declspec(dllimport) void* __stdcall GetModuleHandleA(char const*);
                                                           ^~~~~~~~~~~~~~~~
    In file included from /usr/share/mingw-w64/include/windows.h:71:0,
                     from /usr/share/mingw-w64/include/winsock2.h:23,
                     from ./compat.h:39,
                     from ./util.h:17,
                     from ./init.h:11,
                     from init.cpp:10:
    init.cpp:1245:5: error: expected identifier before numeric constant
         ERROR = 2,
         ^
    init.cpp:1245:5: error: expected '}' before numeric constant
    init.cpp:1245:5: error: expected unqualified-id before numeric constant
    init.cpp:1246:1: error: expected declaration before '}' token
     };
     ^
    make[2]: *** [libbitcoin_server_a-init.o] Error 1
    Makefile:5820: recipe for target 'libbitcoin_server_a-init.o' failed
    
  10. Empact force-pushed on Jul 2, 2018
  11. Empact commented at 7:29 AM on July 2, 2018: member

    Renamed ERROR to FATAL to avoid windows conflict. Note this was in an enum class so seems Windows is applying an ERROR macro or the like.

  12. jimpo commented at 6:12 PM on July 2, 2018: contributor

    utACK 67ed865.

    Nice refactor. Personally, I find the enum a bit heavy and think it would be simpler to return a bool and an additional bool fatal_err outparam, but I don't feel too strongly. Check in the false return case could just be

    if (fShutdownRequested) {
    } else if (success) {
    } else if (fReset || fatal_err) {
       return InitError( );
    } else {
    }
    
  13. Empact force-pushed on Jul 2, 2018
  14. Empact commented at 8:10 PM on July 2, 2018: member

    Thanks for the review @jimpo. I see what you mean - switched to a boolean return, and made fReset an out arg as it was already being used to trigger the retry behavior, so now it can be toggled within the function to disable retry. Lmk if you have thoughts on that.

  15. jimpo commented at 10:36 PM on July 2, 2018: contributor

    utACK fb28c7f5cf903c0c21f823b2e179a39bd69c9549

    Yes, I like this approach more.

  16. Empact force-pushed on Jul 5, 2018
  17. Empact commented at 3:50 PM on July 5, 2018: member

    Rebased for #13235

  18. laanwj commented at 4:13 PM on July 5, 2018: member

    Needs rebase for #13577

  19. Empact force-pushed on Jul 6, 2018
  20. Empact commented at 4:17 AM on July 6, 2018: member

    Rebased for #13577

  21. DrahtBot commented at 7:27 AM on July 6, 2018: 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:

    • #15329 (Fix InitError() and InitWarning() content by hebasto)
    • #15218 (validation: Flush state after initial sync by andrewtoth)
    • #12980 (Allow quicker shutdowns during LoadBlockIndex() by jonasschnelli)

    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.

  22. in src/init.cpp:1242 in 463994c4fe outdated
    1225 | @@ -1226,6 +1226,136 @@ bool AppInitLockDataDirectory()
    1226 |      return true;
    1227 |  }
    1228 |  
    1229 | +static bool AppInitLoadBlockIndex(const CChainParams& chainparams, int64_t nCoinDBCache, int64_t nBlockTreeDBCache, bool fReindexChainState, bool& fReset, std::string& strLoadError)
    1230 | +{
    1231 | +    try {
    


    l2a5b1 commented at 9:06 PM on July 8, 2018:

    I really like this refactor, but I wanted to comment briefly on the try block.

    I suspect that you kept the try block to keep things simple, but alternatively you can also use a function try-block if you like the notational convenience that it provides.

    A function try-block will save one level of indentation in the function body, which could be nice with a function body of this size.

    Function try-blocks are not commonly used, but it is perfectly fine to use them (at least according to Stroustrup).

    With a function-try block this function would look something like this:

    static bool AppInitLockBlockIndex(...) try 
    {
        ...
        return true;
    } catch (std::exception& e) 
    {
        ...
        return false;
    }
    

    Of course this comment is not critical, feel free to ignore it.


    Empact commented at 3:36 AM on July 9, 2018:

    Wow, TIL. Read up on them and I'm into it. If this is accepted, this will be the first use of function-try-block in the codebase. :P


    Empact commented at 10:59 PM on July 9, 2018:

    Switched back as per #13582 (comment)

  23. Empact force-pushed on Jul 9, 2018
  24. Empact force-pushed on Jul 9, 2018
  25. Empact commented at 3:44 AM on July 9, 2018: member

    Switched to using function-try-block. If people aren't into that, maybe we should discourage them in the developer-notes? https://en.cppreference.com/w/cpp/language/function-try-block

  26. promag commented at 8:45 AM on July 9, 2018: member

    Travis failed with unrelated error, restarted.

    Regarding function-try-block I'm -0 if the reasoning is just to save one level of indentation in the function body.

  27. Empact force-pushed on Jul 9, 2018
  28. Empact commented at 2:59 PM on July 9, 2018: member

    Yeah, on second thought, switched back to regular try/catch. The framing of the function body and the consistent treatment help with the reading of the function IMO.

  29. Empact cross-referenced this on Jul 14, 2018 from issue [moveonly] Extract RescanWallet to handle a simple rescan by Empact
  30. promag commented at 10:54 PM on July 16, 2018: member

    utACK a2dbe2a.

  31. DrahtBot cross-referenced this on Aug 31, 2018 from issue Minor style enhancement in documentation by fedsten
  32. MarcoFalke commented at 6:18 PM on September 7, 2018: member

    Locally I get:

      CXX      libbitcoin_server_a-init.o
    init.cpp:1252:14: warning: calling function 'LoadBlockIndex' requires holding mutex 'cs_main' exclusively [-Wthread-safety-analysis]
            if (!LoadBlockIndex(chainparams)) {
                 ^
    1 warning generated.
    

    Let's see what travis says

  33. MarcoFalke closed this on Sep 7, 2018

  34. MarcoFalke reopened this on Sep 7, 2018

  35. Empact force-pushed on Sep 10, 2018
  36. Empact commented at 9:06 PM on September 10, 2018: member

    @MarcoFalke rebased and added the lock declaration to AppInitLoadBlockIndex

  37. scravy commented at 9:31 PM on September 10, 2018: contributor

    Concept ACK

  38. DrahtBot cross-referenced this on Sep 21, 2018 from issue Allow quicker shutdowns during LoadBlockIndex() by jonasschnelli
  39. DrahtBot cross-referenced this on Sep 21, 2018 from issue Refactor: separate wallet from node by ryanofsky
  40. DrahtBot cross-referenced this on Sep 27, 2018 from issue Multiprocess bitcoin by ryanofsky
  41. DrahtBot cross-referenced this on Sep 30, 2018 from issue [logs] Fix a few log messages related to duration measurement by romanz
  42. DrahtBot cross-referenced this on Oct 9, 2018 from issue Refactor: Start to separate wallet from node by ryanofsky
  43. DrahtBot added the label Needs rebase on Nov 9, 2018
  44. Empact force-pushed on Nov 10, 2018
  45. Empact commented at 7:16 PM on November 10, 2018: member

    Rebased for #14437

  46. DrahtBot removed the label Needs rebase on Nov 10, 2018
  47. Empact commented at 10:49 PM on November 10, 2018: member

    Appveyor failure looks unrelated

  48. Empact force-pushed on Jan 17, 2019
  49. Empact commented at 12:51 PM on January 17, 2019: member

    Reworked to minimize git diff --color-moved=dimmed_zebra --color-moved-ws=allow-indentation-change head^. Now is a minimal extraction.

  50. DrahtBot added the label Needs rebase on Mar 7, 2019
  51. Empact force-pushed on Mar 9, 2019
  52. Empact commented at 3:30 PM on March 9, 2019: member

    Rebase for #15402

  53. DrahtBot removed the label Needs rebase on Mar 9, 2019
  54. Empact force-pushed on Mar 9, 2019
  55. Empact force-pushed on Mar 9, 2019
  56. DrahtBot added the label Needs rebase on May 7, 2019
  57. Extract AppInitLoadBlockIndex from AppInitMain
    AppInitMain goes from ~650 lines to ~500. This also replaces
    constructs like `while(false)` and using `break` vs `return` with
    more explicit operation.
    c95a277d05
  58. Empact force-pushed on May 17, 2019
  59. Empact commented at 7:06 PM on May 17, 2019: member

    Rebased

  60. DrahtBot removed the label Needs rebase on May 17, 2019
  61. in src/init.cpp:1274 in c95a277d05
    1269 | +        }
    1270 | +
    1271 | +        // If the loaded chain has a wrong genesis, bail out immediately
    1272 | +        // (we're likely using a testnet datadir, or the other way around).
    1273 | +        if (!mapBlockIndex.empty() && !LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
    1274 | +            fReset = true; // don't retry
    


    jb55 commented at 2:36 PM on May 18, 2019:

    was this added? I don't see it in the original block. not saying it's wrong, but just wondering.


    Empact commented at 9:12 PM on May 28, 2019:

    It's that way because there were 2 ways to exit the prior codeblock: break and return. This is special handling for the one return case.

  62. DrahtBot added the label Needs rebase on May 20, 2019
  63. DrahtBot commented at 8:33 AM on May 20, 2019: contributor

    <!--cf906140f33d8803c4a75a2196329ecb-->Needs rebase

  64. Empact commented at 9:12 PM on May 28, 2019: member

    Closing as rebasing this is too much a chore. :P

  65. Empact closed this on May 28, 2019

  66. laanwj removed the label Needs rebase on Oct 24, 2019
  67. bitcoin locked this on Dec 16, 2021

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-05-19 06:54 UTC