wallet: Move restorewallet() logic to the wallet section #23721

pull w0xlt wants to merge 3 commits into bitcoin:master from w0xlt:restore_wallet_gui changing 11 files +91 −32
  1. w0xlt commented at 2:14 am on December 9, 2021: contributor

    Currently restorewallet() logic is written in the RPC layer and it can´t be reused by GUI. So it moves this to the wallet section and then, GUI can access it.

    This is necessary to implement the “Restore Wallet” menu item in the GUI (which is already implemented in https://github.com/bitcoin-core/gui/pull/471 ).

    This commit also simplifies error handling and adds a new behavior: if the restore fails, the invalid wallet folder is removed.

  2. DrahtBot added the label interfaces on Dec 9, 2021
  3. DrahtBot added the label RPC/REST/ZMQ on Dec 9, 2021
  4. DrahtBot added the label Wallet on Dec 9, 2021
  5. DrahtBot commented at 3:22 am on December 9, 2021: 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:

    • #23497 (Add src/node/ and src/wallet/ code to node:: and wallet:: namespaces by ryanofsky)

    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.

  6. shaavan commented at 1:50 pm on December 10, 2021: contributor

    Concept ACK

    Wow, this PR was a tough nut to crack! It took me quite a while to understand it thoroughly. That’s why I have attached my notes so that other reviewers might have an easier time reviewing this PR.

    I agree with the changes introduced in this PR. Though I have some suggestions that might help improve this PR.

    1. I suggest you split the PR into two commits: First, introduce the RestoreWallet function, and second, use it.
    2. Due to the difference introduced in implementation, this snippet of code is getting duplicated. Though I can’t find a solution to avoid this, it also doesn’t feel intuitively correct. Maybe other reviewers might have some suggestions to improve upon it.

    Notes:

    • Introduced new pure virtual function restoreWallet in src/interfaces/wallet.h file. This function is then overridden in src/wallet/interfaces.h file.
    • This new function allows the creation of a new wallet using backed-up wallet’s data. For this purpose, it uses the (newly introduced) function RestoreWallet.
    • RestoreWallet is defined under src/wallet/wallet.h file. This function does preliminary checks for:
      1. If backup_file exist.
      2. If first, wallet_path (i.e., where wallet should be created) is empty, and, second, the directory can be created there. After these steps, the LoadWallet function is called, creating a wallet variable. Finally, if the wallet variable does not exist (if(!wallet)), the wallet_file and wallet_path are removed from the system. Finally, the wallet variable is returned if everything works out correctly.
    • The LoadWalletHelper in src/wallet/rpc/util.cpp, which used to return {wallet, warning} has now been reduced to HandleWalletError. Reason:
      1. This function had two use cases: for restoreWallet() in src/wallet/rpc/backup.cpp file, and for loadWallet() in src/wallet/rpc/wallet.cpp file.
      2. Now, the restoreWallet() function was updated to use a more suitable RestoreWallet function, and loadWallet() is still using the LoadWallet function.
      3. This change leads to duplicating the following steps for both functions and removing them from the LoadWalletHelper function.
    0    DatabaseOptions options;
    1    DatabaseStatus status;
    2    options.require_existing = true;
    3    bilingual_str error;
    4    std::vector<bilingual_str> warnings;
    
    • Finally, this PR introduced some minor changes to the test/functional/wallet_backup.py file:
      1. Changed the assert_raises_rpc_error code from -8 (RPC_INVALID_PARAMETER) to -4 (RPC_WALLET_ERROR)
      2. Updated code to make the error message more verbose, better explaining the cause of the error.
  7. in src/wallet/wallet.cpp:360 in 33763d02ef outdated
    356@@ -357,6 +357,35 @@ std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string&
    357     return wallet;
    358 }
    359 
    360+std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const std::string& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
    


    achow101 commented at 2:22 pm on December 10, 2021:

    In 33763d02ef1011ae71143c5e6e313c72d9b790a6 “refactor: Move restorewallet() logic to the wallet section”

    options does not need to be an argument to this function. The DatabaseOptions for loading a restored wallet are always going to be the same so you can make that object inside of this function.


    w0xlt commented at 0:48 am on December 15, 2021:
    Done in 4807f73
  8. in src/wallet/wallet.cpp:385 in 33763d02ef outdated
    377+    fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
    378+
    379+    auto wallet = LoadWallet(context, wallet_name, load_on_start, options, status, error, warnings);
    380+
    381+    if (!wallet) {
    382+        fs::remove(wallet_file);
    


    achow101 commented at 2:22 pm on December 10, 2021:

    In 33763d02ef1011ae71143c5e6e313c72d9b790a6 “refactor: Move restorewallet() logic to the wallet section”

    This is a change in behavior. I would prefer to have this added in its own commit.


    w0xlt commented at 0:49 am on December 15, 2021:
    Done in a3bdbf6 Also added a more one test for this new behavior.
  9. in src/wallet/rpc/backup.cpp:1892 in 33763d02ef outdated
    1905 
    1906-    fs::copy_file(backup_file, wallet_file, fs::copy_option::fail_if_exists);
    1907+    const std::shared_ptr<CWallet> wallet = RestoreWallet(context, fs::PathToString(backup_file), wallet_name, load_on_start, options, status, error, warnings);
    1908 
    1909-    auto [wallet, warnings] = LoadWalletHelper(context, request.params[2], wallet_name);
    1910+    HandleWalletError(wallet, status, error);
    


    achow101 commented at 2:26 pm on December 10, 2021:

    In 33763d02ef1011ae71143c5e6e313c72d9b790a6 “refactor: Move restorewallet() logic to the wallet section”

    HandleWalletError does not handle FAILED_BAD_PATH or FAILED_ALREADY_EXISTS, which results in a slightly incorrect RPC_WALLET_ERROR code. There should be additional error handling for those cases to return better error codes.


    w0xlt commented at 0:50 am on December 15, 2021:
    Done in 4053a21, which introduced additional error handling.
  10. in src/wallet/rpc/util.cpp:125 in 33763d02ef outdated
    121@@ -122,16 +122,8 @@ std::string LabelFromValue(const UniValue& value)
    122     return label;
    123 }
    124 
    125-std::tuple<std::shared_ptr<CWallet>, std::vector<bilingual_str>> LoadWalletHelper(WalletContext& context, UniValue load_on_start_param, const std::string wallet_name)
    126+void HandleWalletError(const std::shared_ptr<CWallet> wallet, DatabaseStatus& status, bilingual_str& error)
    


    achow101 commented at 2:26 pm on December 10, 2021:

    In 33763d02ef1011ae71143c5e6e313c72d9b790a6 “refactor: Move restorewallet() logic to the wallet section”

    It would be easier to review this diff if this refactor were in its own commit.


    w0xlt commented at 0:50 am on December 15, 2021:
    Done in 4053a21
  11. refactor: Implement restorewallet() logic in the wallet section
    Currently restorewallet() logic is written in the RPC layer
    and it can´t be reused by GUI. So it reimplements this in the
    wallet and interface sections and then, GUI can access it.
    4807f73f48
  12. Rspigler commented at 11:43 pm on December 14, 2021: contributor
    As a followup, documentation should be updated
  13. w0xlt force-pushed on Dec 14, 2021
  14. w0xlt force-pushed on Dec 15, 2021
  15. w0xlt force-pushed on Dec 15, 2021
  16. w0xlt force-pushed on Dec 15, 2021
  17. refactor: Move restorewallet() RPC logic to the wallet section
    It also simplifies restorewallet() and loadwallet() RPC error handling.
    abbb7eccef
  18. refactor: remove the wallet folder if the restore fails 62fa61fa4a
  19. in src/wallet/db.h:223 in 4053a210a6 outdated
    219@@ -220,6 +220,7 @@ enum class DatabaseStatus {
    220     FAILED_LOAD,
    221     FAILED_VERIFY,
    222     FAILED_ENCRYPT,
    223+    FAILED_INVALID_BACKCUP_FILE,
    


    achow101 commented at 6:28 pm on December 15, 2021:

    In 4053a210a6126f85779e5ccab248d13a74d27897 “refactor: Move restorewallet() RPC logic to the wallet section”

    nit: typo

    0    FAILED_INVALID_BACKUP_FILE,
    

    w0xlt commented at 10:01 pm on December 15, 2021:
    Fixed in abbb7ec
  20. w0xlt force-pushed on Dec 15, 2021
  21. achow101 commented at 11:16 pm on December 15, 2021: member
    ACK 62fa61fa4a33ff4d108a65d656ffe2cac4330824
  22. shaavan approved
  23. shaavan commented at 7:31 am on December 16, 2021: contributor

    crACK 62fa61fa4a33ff4d108a65d656ffe2cac4330824

    Great work, @w0xlt!

  24. MarcoFalke removed the label RPC/REST/ZMQ on Dec 16, 2021
  25. MarcoFalke removed the label interfaces on Dec 16, 2021
  26. MarcoFalke added the label Refactoring on Dec 16, 2021
  27. MarcoFalke merged this on Dec 16, 2021
  28. MarcoFalke closed this on Dec 16, 2021

  29. MarcoFalke renamed this:
    wallet, refactor: Move restorewallet() logic to the wallet section
    wallet: Move restorewallet() logic to the wallet section
    on Dec 16, 2021
  30. MarcoFalke removed the label Refactoring on Dec 16, 2021
  31. MarcoFalke commented at 7:59 am on December 16, 2021: member
    None of this is a refactor. Every commit is a behavior change.
  32. sidhujag referenced this in commit 4f9fa8c482 on Dec 16, 2021
  33. w0xlt deleted the branch on Dec 16, 2021
  34. DrahtBot locked this on Dec 16, 2022

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-07-01 13:12 UTC

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