Skip to content

Conversation

achow101
Copy link
Member

The BerkeleyBatch class has 4 static functions that operate on BerkeleyDatabase or BerkeleyEnvironment. It doesn't make sense for these to be standalone nor for them to be static functions. So instead, move them from BerkeleyBatch into BerkeleyDatabase and make them member functions instead of static.

BerkeleyBatch::VerifyEnvironment and BerkeleyBatch::VerifyDatabaseFile are combined into a single BerkeleyDatabase::Verify function that operates on that BerkeleyDatabase object.

BerkeleyBatch::Rewrite and BerkeleyBatch::PeriodicFlush both took a BerkeleyDatabase as an argument and did stuff on it. So we just make it a member function so it doesn't need to take a database as an argument.

Part of #18971

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review almost-ACK 893ec59. Suggested some tweaks

@@ -112,7 +112,7 @@ static bool SalvageWallet(const fs::path& path)
// Initialize the environment before recovery
bilingual_str error_string;
try {
WalletBatch::VerifyEnvironment(path, error_string);
database->Verify(error_string);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "walletdb: Combine VerifyDatabaseFile and VerifyEnvironment" (ae1a9d9)

It doesn't seem like there is a point to calling Verify here in SalvageWallet when ExecuteWalletToolFunc already called it immediately before calling this. It seems like this SalvageWallet function could just be dropped and ExecuteWalletToolFunc just call RecoverDatabaseFile directly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the database is closed before we reach this function, we need to reopen it before doing the recovery function. We could remove this and only close the database for info but that seems a bit like a hack.

Maybe something to think about for a followup.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "walletdb: Combine VerifyDatabaseFile and VerifyEnvironment" (8f1bcf8)

Since the database is closed before we reach this function, we need to reopen it before doing the recovery function. We could remove this and only close the database for info but that seems a bit like a hack.

Maybe something to think about for a followup.

It seems like the unintended change in behavior described #19324 (comment) is back.

Instead of salvage command just recovering wallet data, now it tries to verify before recovering. Maybe this change is good because verifying before recovering is good for some reason. Maybe it is bad because it prevents salvage command from working some cases. Or maybe it is ok but slows the salvage operation down unnecessarily. I can't tell if it's good or bad but it seems unintended and not mentioned in the PR description or any commit message and ignored in reviews.

It looks like previous behavior could be restored by deleting the SalvageWallet function and just calling RecoverDatabaseFile function instead (previously suggested #19324 (comment))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Seems like there was a rebase error at some point. I'll make a followup that handles this and a few other related suggestions.

@achow101 achow101 force-pushed the bdb-batch-rm-statics branch from 893ec59 to 4693b11 Compare June 18, 2020 23:51
@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 19, 2020

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@promag
Copy link
Contributor

promag commented Jun 20, 2020

Code review ACK 4693b11.

No strong opinion whether wallet tool info and salvage should verify.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 4693b11. VerifyNotInUse rename is wonky but other than that this is an improvement. Main difference since last review is reverting wallet tool behavior change

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

@achow101 achow101 force-pushed the bdb-batch-rm-statics branch from 192218c to 514ed76 Compare June 24, 2020 17:48
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 514ed76 📁

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK 514ed7641f1df5cad04feba61a5fa12b1c5e9056 📁
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUi4Mgv9HZbkdS5ffjQogWmlFeLv7NxfV0pmKHDWps9i1P/5QpyHNfHbSZR3Roi7
kEFZMRMftn0L6flvX4zxOoq1ZbVuE/2JeEwIUKs8Zq3vYM/4egmbuSyaVKruXkRT
lMKJk1FVYVLWAM7cx2rETlWpQRc9zahCcwI/AnXvbnux3+iQBt5ij9BEIR+42KRF
UB7Ty7sW45l809Or9p6lCW8MoxNzNX4VUHBZMd+b3lIbYwRwGIS5JqULTmKkymz+
gUBdA0Wx98EBHuG46MZukGsrmAF+aR5AuuBDsny9Oi+cUaA8YAGL8VxpeFzNTJfr
PITxStmdAIpqlTHt3tNz6BBqjIlnNy7GbndYQiBhsOOmrrSMB+fCgyu2TRpn28+y
ztA1uqQBqI8s/iDklLYbxhYieGJuiGCfUajpLdLsxl55/C3cw9PkuaBrbKSSeBzF
t6ezgu+OZT22iprRiutkcrlkWqUhScv4sMcu4K7zeOE1eP/LBu2CCGBrnav7RF1R
DVJhXzNB
=uigF
-----END PGP SIGNATURE-----

Timestamp of file with hash 1639c2f3437a4a8f4fe84a0eaa5966db2964f9df9c75f3885e2308bb1c24d313 -

if (!WalletBatch::VerifyEnvironment(path, error)) {
tfm::format(std::cerr, "%s\nError loading %s. Is wallet being used by other process?\n", error.original, name);
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in commit 17924a5

This change seems unrelated. Maybe put it into a preceding commit and explain what it does? I presume it removes a redundant check that is done as part of LoadWallet?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is it unrelated? This function is being removed from WalletBatch.

Copy link
Member

@maflcko maflcko Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wrong. VerifyEnvironment is called via LoadWallet -> LoadWalletInternal -> CWallet::Verify, which is different from (edit: this was wrong) the LoadWallet call below.

So why is it ok to remove this call to VerifyEnvironment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VerifyEnvironment would just call BerkeleyEnvironment::Open. A couple of other things also call that in their normal operation. In particular, CWallet::LoadWallet creates a WalletBatch which creates a BerkeleyBatch which calls BerkeleyEnvironment::Open in its constructor.

In the cases where we need to open the environment and aren't using the higher level stuff, a direct call to BerkeleyEnvironment::Open is needed which is why it is added to SalvageWallet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With "unrelated" I meant "can be split up into its own atomic and logical commit without depending on other changes in this pull request".

See the following commit on current master, which compiles fine and runs all tests fine as well:

$ git show 
commit 43d120ec8a7a999a48ba31e2621efec06ca668f6 (HEAD)
Author: MarcoFalke <falke.marco@gmail.com>
Date:   Thu Jul 2 07:53:05 2020 -0400

    wallet: Remove redundant WalletBatch::VerifyEnvironment
    
    It is redundant because LoadWallet will call CWallet::LoadWallet ->
    LoadWalletInternal -> CWallet::Verify, which calls VerifyEnvironment
    
    For SalvageWallet it is redundant with the newly added call to Open

diff --git a/src/wallet/salvage.cpp b/src/wallet/salvage.cpp
index d42950ee42..e6e62332c0 100644
--- a/src/wallet/salvage.cpp
+++ b/src/wallet/salvage.cpp
@@ -20,6 +20,11 @@ bool RecoverDatabaseFile(const fs::path& file_path)
     std::string filename;
     std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
 
+    if (!env->Open(true /* retry */)) {
+        tfm::format(std::cerr, "Error initializing wallet database environment %s!", env->Directory());
+        return false;
+    }
+
     // Recovery procedure:
     // move wallet file to walletfilename.timestamp.bak
     // Call Salvage with fAggressive=true to
diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp
index 77ed6beb5d..430a866a7c 100644
--- a/src/wallet/wallettool.cpp
+++ b/src/wallet/wallettool.cpp
@@ -140,11 +140,6 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
             tfm::format(std::cerr, "Error: no wallet file at %s\n", name);
             return false;
         }
-        bilingual_str error;
-        if (!WalletBatch::VerifyEnvironment(path, error)) {
-            tfm::format(std::cerr, "%s\nError loading %s. Is wallet being used by other process?\n", error.original, name);
-            return false;
-        }
 
         if (command == "info") {
             std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
diff --git a/test/functional/tool_wallet.py b/test/functional/tool_wallet.py
index 524e1593ba..18f0beb598 100755
--- a/test/functional/tool_wallet.py
+++ b/test/functional/tool_wallet.py
@@ -71,8 +71,7 @@ class ToolWalletTest(BitcoinTestFramework):
         self.assert_raises_tool_error('Error: two methods provided (info and create). Only one method should be provided.', 'info', 'create')
         self.assert_raises_tool_error('Error parsing command line arguments: Invalid parameter -foo', '-foo')
         self.assert_raises_tool_error(
-            'Error initializing wallet database environment "{}"!\nError loading wallet.dat. Is wallet being used by other process?'
-            .format(os.path.join(self.nodes[0].datadir, self.chain, 'wallets')),
+            'Error loading wallet.dat. Is wallet being used by another process?',
             '-wallet=wallet.dat',
             'info',
         )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if I need to push again.

if (!env->Open(true /* retry */)) {
tfm::format(std::cerr, "Error initializing wallet database environment %s!", env->Directory());
return false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same commit

This also seems unrelated. Is it related to the change in ExecuteWalletToolFunc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -20,6 +20,11 @@ bool RecoverDatabaseFile(const fs::path& file_path)
std::string filename;
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);

if (!env->Open(true /* retry */)) {
tfm::format(std::cerr, "Error initializing wallet database environment %s!", env->Directory());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in commit 17924a5:

Any reason to write to stderr when all other failures are written to the debug log? Since this is a pure utility function, I'd prefer if this returned the error string to the caller in the future, so that the caller can decide how to display the error

Copy link
Member Author

@achow101 achow101 Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was to largely retain the previous behavior of this error going to stderr. I think that cleaning up this function to return error strings and remove the LogPrintfs in a followup.

achow101 added 3 commits July 1, 2020 12:32
Combine these two functions into a single Verify function that is a
member of WalletDatabase. Additionally, these are no longer static.
Make PeriodicFlush a non-static member of WalletDatabase instead of
WalletBatch.
Make Rewrite actually a member of BerkeleyDatabase instead of a static
function in BerkeleyBatch
@achow101 achow101 force-pushed the bdb-batch-rm-statics branch from 514ed76 to d8e9ca6 Compare July 1, 2020 16:36
@achow101
Copy link
Member Author

achow101 commented Jul 1, 2020

Apparently the python linter on master fails here, so I've pushed a change to fix that.

@maflcko
Copy link
Member

maflcko commented Jul 2, 2020

re-ACK d8e9ca6 only change is test fixup 🤞

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

re-ACK d8e9ca66d1 only change is test fixup 🤞
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjlKwwAwqUvc8WhUv1aYY1m9LSQcs/3dHt5UFdx7DRoPH2NBB4t2uZfxn/P8rcx
xqfjWlLj4i6Ia5g0HhAunAo6aU7yooL2t+ZGCtPVprwNzzcYQyZkDiRe+lRNBIzG
40lMOWuVpfVcIeYiDQcWtLf2uDyVkhWoSYmQBXsd6xn3tik5sLQMEVXOm12WfVDI
wxpEyH/d6FkWOJPpZjWA5Hqce7BH7IbZkDb9dbJR3qEor+++NgjBUDjr1OtHqdcQ
UjUOJw3LC93EjzMd2uLnjJaJMTxEEfTBRYXAM/vQrusAJo8PR76NXz1rUa2WDnLJ
484QwqBxAPUYRmszSG7UoD2lM2GpxdWoZLdjscKe8SuwWjqe1yn2CRn3rUAEiZpR
m7A4rlD54pwODWKD9HRyoZ4CJ22ljG9KjIhQaDpRxIuHbpV4xBOlRQGzpThmuk57
Z2jeHaILIa0ypYA0oW5ZgLtg77vOO0Qfeu9B53HuITGH6W133nGTnVkbQvGmZv2+
6SU9QC9G
=sVVQ
-----END PGP SIGNATURE-----

Timestamp of file with hash 7e2f395c7afce74dc863a67c21bbf3c3b75edbd4fb9a35f91c5cc23e1674a2a6 -

@maflcko
Copy link
Member

maflcko commented Jul 2, 2020

@promag or @ryanofsky Mind to re-ACK?

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK d8e9ca6, good stuff.

@maflcko maflcko merged commit 171f4a5 into bitcoin:master Jul 5, 2020
@Sjors
Copy link
Member

Sjors commented Jul 6, 2020

There's a stray VerifyEnvironment and VerifyDatabaseFile in walletdb.h. Maybe fix those in #19325?

maflcko pushed a commit that referenced this pull request Jul 14, 2020
…Batch abstract class

b82f0ca walletdb: Add MakeBatch function to BerkeleyDatabase and use it (Andrew Chow)
eac9200 walletdb: Refactor DatabaseBatch abstract class from BerkeleyBatch (Andrew Chow)

Pull request description:

  In order to support alternative database systems, we need to have a generic `Batch` class. This PR adds a `DatabaseBatch` abstract class which is implemented by `BerkeleyBatch`. `DatabaseBatch` is now the class that is used by `WalletBatch` to interact with the database. To be able to get the correct type of `DatabaseBatch`, `BerkeleyDatabase` now has a `MakeBatch` function which returns a newly constructed `std::unique_ptr<DatabaseBatch>`. For `BerkeleyDatabase`, that will be `std::unique_ptr<BerkeleyBatch>`.

  The `Read`, `Write`, `Erase`, and `Exists` template functions are moved from `BerkeleyBatch`.

  Part of #18971

  Requires #19308 and #19324

ACKs for top commit:
  Sjors:
    re-utACK b82f0ca
  MarcoFalke:
    ACK b82f0ca 🌘
  meshcollider:
    LGTM, utACK b82f0ca

Tree-SHA512: 6d2d41631c0983391dbecd702e881c6775b155c90b275df97f7157e42608ed251744f9d7ce5173d02a6c5cc38d90b611880fac7fa635d3d8c4d590681f56ac6a
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Jul 14, 2020
…atabaseBatch abstract class

b82f0ca walletdb: Add MakeBatch function to BerkeleyDatabase and use it (Andrew Chow)
eac9200 walletdb: Refactor DatabaseBatch abstract class from BerkeleyBatch (Andrew Chow)

Pull request description:

  In order to support alternative database systems, we need to have a generic `Batch` class. This PR adds a `DatabaseBatch` abstract class which is implemented by `BerkeleyBatch`. `DatabaseBatch` is now the class that is used by `WalletBatch` to interact with the database. To be able to get the correct type of `DatabaseBatch`, `BerkeleyDatabase` now has a `MakeBatch` function which returns a newly constructed `std::unique_ptr<DatabaseBatch>`. For `BerkeleyDatabase`, that will be `std::unique_ptr<BerkeleyBatch>`.

  The `Read`, `Write`, `Erase`, and `Exists` template functions are moved from `BerkeleyBatch`.

  Part of bitcoin#18971

  Requires bitcoin#19308 and bitcoin#19324

ACKs for top commit:
  Sjors:
    re-utACK b82f0ca
  MarcoFalke:
    ACK b82f0ca 🌘
  meshcollider:
    LGTM, utACK b82f0ca

Tree-SHA512: 6d2d41631c0983391dbecd702e881c6775b155c90b275df97f7157e42608ed251744f9d7ce5173d02a6c5cc38d90b611880fac7fa635d3d8c4d590681f56ac6a
maflcko pushed a commit to bitcoin-core/gui that referenced this pull request Aug 14, 2020
…ous declarations

0e279fe walletdb: Remove unused static functions from walletdb.h (Andrew Chow)
9f536d4 wallettool: Have RecoverDatabaseFile return errors and warnings (Andrew Chow)
06e263a Call RecoverDatabaseFile directly from wallettool (Andrew Chow)

Pull request description:

  Followup to #19324 addressing some comments.

  Removes the `SalvageWallet` function in wallettool and instead directly calls `RecoverDatabaseFile` as suggested in bitcoin/bitcoin#19324 (comment)

  Removes the `LogPrintf`s and `tfm::format`s in `RecoverDatabaseFile` as noted in bitcoin/bitcoin#19324 (comment)

  Removes the declarations of `VerifyEnvironment` and `VerifyDatabaseFile` that were forgotten in `walletdb.h` as noted in bitcoin/bitcoin#19324 (comment)

ACKs for top commit:
  meshcollider:
    Code review ACK 0e279fe
  ryanofsky:
    Code review ACK 0e279fe, just dropped last commit

Tree-SHA512: ffd01f30536c2eab4bf40ba363c3ea916ecef3c8f0c5262040b40498776ffb00f95240204a40e38415d6931800851d0a3fa63ee91efc1d329b60ac317da0363d
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Aug 16, 2020
…extraneous declarations

0e279fe walletdb: Remove unused static functions from walletdb.h (Andrew Chow)
9f536d4 wallettool: Have RecoverDatabaseFile return errors and warnings (Andrew Chow)
06e263a Call RecoverDatabaseFile directly from wallettool (Andrew Chow)

Pull request description:

  Followup to bitcoin#19324 addressing some comments.

  Removes the `SalvageWallet` function in wallettool and instead directly calls `RecoverDatabaseFile` as suggested in bitcoin#19324 (comment)

  Removes the `LogPrintf`s and `tfm::format`s in `RecoverDatabaseFile` as noted in bitcoin#19324 (comment)

  Removes the declarations of `VerifyEnvironment` and `VerifyDatabaseFile` that were forgotten in `walletdb.h` as noted in bitcoin#19324 (comment)

ACKs for top commit:
  meshcollider:
    Code review ACK 0e279fe
  ryanofsky:
    Code review ACK 0e279fe, just dropped last commit

Tree-SHA512: ffd01f30536c2eab4bf40ba363c3ea916ecef3c8f0c5262040b40498776ffb00f95240204a40e38415d6931800851d0a3fa63ee91efc1d329b60ac317da0363d
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Dec 7, 2020
Summary:
```
The BerkeleyBatch class has 4 static functions that operate on
BerkeleyDatabase or BerkeleyEnvironment. It doesn't make sense for these
to be standalone nor for them to be static functions. So instead, move
them from BerkeleyBatch into BerkeleyDatabase and make them member
functions instead of static.

BerkeleyBatch::VerifyEnvironment and BerkeleyBatch::VerifyDatabaseFile
are combined into a single BerkeleyDatabase::Verify function that
operates on that BerkeleyDatabase object.

BerkeleyBatch::Rewrite and BerkeleyBatch::PeriodicFlush both took a
BerkeleyDatabase as an argument and did stuff on it. So we just make it
a member function so it doesn't need to take a database as an argument.
```

Backport of core [[bitcoin/bitcoin#19324 | PR19324]].

Depends on D8616.

Test Plan:
  ninja all check-all

Reviewers: #bitcoin_abc, majcosta

Reviewed By: #bitcoin_abc, majcosta

Differential Revision: https://reviews.bitcoinabc.org/D8617
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jun 8, 2021
Summary:
VerifyEnvironment and VerifyDatabaseFile were removed in D8617/[[bitcoin/bitcoin#19324 | PR19324]], but their declarations weren't. Remove those.

This is a backport of [[bitcoin/bitcoin#19457 | core#19457]] [3/3]
bitcoin/bitcoin@0e279fe

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D9079
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants