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 force-pushed on Jul 1, 2018
  3. Empact force-pushed on Jul 1, 2018
  4. fanquake added the label Refactoring on Jul 1, 2018
  5. Empact force-pushed on Jul 2, 2018
  6. Empact force-pushed on Jul 2, 2018
  7. Empact force-pushed on Jul 2, 2018
  8. fanquake commented at 7:11 am on July 2, 2018: member

    Both Windows builds failed with:

     0In file included from /home/travis/build/bitcoin/bitcoin/depends/i686-w64-mingw32/share/../include/boost/thread/win32/condition_variable.hpp:9:0,
     1                 from /home/travis/build/bitcoin/bitcoin/depends/i686-w64-mingw32/share/../include/boost/thread/condition_variable.hpp:14,
     2                 from ./util.h:35,
     3                 from ./init.h:11,
     4                 from init.cpp:10:
     5/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*)'
     6                 __declspec(dllimport) void* __stdcall GetModuleHandleA(char const*);
     7                                                       ^~~~~~~~~~~~~~~~
     8In file included from /usr/share/mingw-w64/include/windows.h:71:0,
     9                 from /usr/share/mingw-w64/include/winsock2.h:23,
    10                 from ./compat.h:39,
    11                 from ./util.h:17,
    12                 from ./init.h:11,
    13                 from init.cpp:10:
    14init.cpp:1245:5: error: expected identifier before numeric constant
    15     ERROR = 2,
    16     ^
    17init.cpp:1245:5: error: expected '}' before numeric constant
    18init.cpp:1245:5: error: expected unqualified-id before numeric constant
    19init.cpp:1246:1: error: expected declaration before '}' token
    20 };
    21 ^
    22make[2]: *** [libbitcoin_server_a-init.o] Error 1
    23Makefile:5820: recipe for target 'libbitcoin_server_a-init.o' failed
    
  9. Empact force-pushed on Jul 2, 2018
  10. 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.
  11. 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

    0if (fShutdownRequested) {
    1} else if (success) {
    2} else if (fReset || fatal_err) {
    3   return InitError( );
    4} else {
    5}
    
  12. Empact force-pushed on Jul 2, 2018
  13. 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.
  14. jimpo commented at 10:36 pm on July 2, 2018: contributor

    utACK fb28c7f5cf903c0c21f823b2e179a39bd69c9549

    Yes, I like this approach more.

  15. Empact force-pushed on Jul 5, 2018
  16. Empact commented at 3:50 pm on July 5, 2018: member
    Rebased for #13235
  17. laanwj commented at 4:13 pm on July 5, 2018: member
    Needs rebase for #13577
  18. Empact force-pushed on Jul 6, 2018
  19. Empact commented at 4:17 am on July 6, 2018: member
    Rebased for #13577
  20. DrahtBot commented at 7:27 am on July 6, 2018: member

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

    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.

  21. 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:

    0static bool AppInitLockBlockIndex(...) try 
    1{
    2    ...
    3    return true;
    4} catch (std::exception& e) 
    5{
    6    ...
    7    return false;
    8}
    

    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)
  22. Empact force-pushed on Jul 9, 2018
  23. Empact force-pushed on Jul 9, 2018
  24. 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
  25. 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.

  26. Empact force-pushed on Jul 9, 2018
  27. 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.
  28. promag commented at 10:54 pm on July 16, 2018: member
    utACK a2dbe2a.
  29. MarcoFalke commented at 6:18 pm on September 7, 2018: member

    Locally I get:

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

    Let’s see what travis says

  30. MarcoFalke closed this on Sep 7, 2018

  31. MarcoFalke reopened this on Sep 7, 2018

  32. Empact force-pushed on Sep 10, 2018
  33. Empact commented at 9:06 pm on September 10, 2018: member
    @MarcoFalke rebased and added the lock declaration to AppInitLoadBlockIndex
  34. scravy commented at 9:31 pm on September 10, 2018: contributor
    Concept ACK
  35. DrahtBot added the label Needs rebase on Nov 9, 2018
  36. Empact force-pushed on Nov 10, 2018
  37. Empact commented at 7:16 pm on November 10, 2018: member
    Rebased for #14437
  38. DrahtBot removed the label Needs rebase on Nov 10, 2018
  39. Empact commented at 10:49 pm on November 10, 2018: member
    Appveyor failure looks unrelated
  40. Empact force-pushed on Jan 17, 2019
  41. 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.
  42. DrahtBot added the label Needs rebase on Mar 7, 2019
  43. Empact force-pushed on Mar 9, 2019
  44. Empact commented at 3:30 pm on March 9, 2019: member
    Rebase for #15402
  45. DrahtBot removed the label Needs rebase on Mar 9, 2019
  46. Empact force-pushed on Mar 9, 2019
  47. Empact force-pushed on Mar 9, 2019
  48. DrahtBot added the label Needs rebase on May 7, 2019
  49. 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
  50. Empact force-pushed on May 17, 2019
  51. Empact commented at 7:06 pm on May 17, 2019: member
    Rebased
  52. DrahtBot removed the label Needs rebase on May 17, 2019
  53. 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.
  54. DrahtBot added the label Needs rebase on May 20, 2019
  55. DrahtBot commented at 8:33 am on May 20, 2019: member
  56. Empact commented at 9:12 pm on May 28, 2019: member
    Closing as rebasing this is too much a chore. :P
  57. Empact closed this on May 28, 2019

  58. laanwj removed the label Needs rebase on Oct 24, 2019
  59. DrahtBot 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: 2024-10-04 22:12 UTC

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