Skip to content

wallet: Enumerate walletdb keys - #16475

Merged
fanquake merged 2 commits into
bitcoin:masterfrom
maflcko:1907-walletDbKeys
Jul 30, 2019
Merged

fanquake merged 2 commits into
bitcoin:masterfrom
maflcko:1907-walletDbKeys

Conversation

@maflcko

@maflcko maflcko commented Jul 27, 2019 •

Copy link
Copy Markdown
Member

It is nice to see all the keys that exists in a single enum

Also, rename CWalletKey to OldKey and update the outdated documentation

@maflcko
maflcko force-pushed the 1907-walletDbKeys branch 2 times, most recently from fa59e42 to fa01c0e Compare July 27, 2019 20:12
@DrahtBot

Copy link
Copy Markdown
Contributor

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #16451 (Remove CMerkleTx by jnewbery)
  • #16341 (Introduce ScriptPubKeyMan interface and use it for key and script management (aka wallet boxes) by achow101)
  • #15931 (Remove GetDepthInMainChain dependency on locked chain interface by ariard)
  • #15709 (Do not add setting as unknown by Bushstar)
  • #9381 (Remove CWalletTx merging logic from AddToWallet 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.

@jnewbery

Copy link
Copy Markdown
Contributor

Concept ACK. This seems like an improvement to code self-documentation.

I think this would be even more helpful if the DBKeys namespace had documentation for each of the keys and what they're used for.

We could go further and change this to an enum, which would ensure that only known keys are read/written, and would allow us to tidy the long sequence of if-else statments in ReadKeyValue() into a switch statement.

@promag promag left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK fa6f22b. @jnewbery suggestions are great followups, I think this is good enough.

I think you should change commit order because then it makes sense to name wkey as OLD_KEY.

Comment thread src/wallet/wallet.h
ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

fa6f22b

Could go further and just implement Unserialize?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I kept the changes to a minimum. Happy to add a commit for this, if other think it is useful.

@meshcollider meshcollider left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

utACK fa6f22b

Agree with John though, the PR title says enumerate so why not use an enum?

@maflcko

maflcko commented Jul 29, 2019

Copy link
Copy Markdown
Member Author

why not use an enum

The underlying type on an enum in cpp can only be integer, not std::string.

I could switch it to an enum and then add helper methods for std::string -> enum and enum -> std::string, but I don't see the benefit.

We could go further and change this to an enum, which would ensure that only known keys are read/written

I don't understand how this would prevent anyone from inlining a string literal (bypassing the enum).

@maflcko

maflcko commented Jul 29, 2019

Copy link
Copy Markdown
Member Author

and would allow us to tidy the long sequence of if-else statments in ReadKeyValue() into a switch statement.

You'd still have to do the if-else statements elsewhere, so I don't see the benefit. I know a log(N) map lookup would also work, but I think the computational complexity shouldn't be a concern here.

@laanwj

laanwj commented Jul 29, 2019

Copy link
Copy Markdown
Member

ACK fa6f22b, I'm a big fan of this kind of change as it prevents typos, which can happen with 'magic' strings in the code.

@jnewbery

Copy link
Copy Markdown
Contributor

I don't understand how this would prevent anyone from inlining a string literal (bypassing the enum).

The idea would be to change WriteIC and EraseIC to take the enum rather than a string.

Changing this to an enum was a suggested follow-up. This PR is a useful change by itself.

@achow101 achow101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Code review ACK fa6f22b

Comment thread src/wallet/walletdb.cpp
}
} else if (strType != "bestblock" && strType != "bestblock_nomerkle" &&
strType != "minversion" && strType != "acentry" && strType != "version") {
} else if (strType != DBKeys::BESTBLOCK && strType != DBKeys::BESTBLOCK_NOMERKLE &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(maybe for a followup) I think it would be better to define a const set of the types that are unused but kept around to avoid triggering unknown record warnings. Then we could change this if to be checking membership in that set.

Comment thread src/wallet/walletdb.cpp
{
return (strType== "key" || strType == "wkey" ||
strType == "mkey" || strType == "ckey");
return (strType == DBKeys::KEY || strType == DBKeys::OLD_KEY ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(maybe for a followup) I think it would be better to define a const set of the types that are keys. Then we could change this function to be returning membership in that set.

@maflcko

maflcko commented Jul 29, 2019

Copy link
Copy Markdown
Member Author

Thanks for the plethora of suggestions of follow-up refactorings. To not invalidate the ACKs and to not scope creep this change, which should be minimal, but still an improvement, I will leave them for later.

@fanquake fanquake left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK fa6f22b - I had a quick look over, definitely prefer this to strings floating around everywhere.

5 ACKs, including both the wallet maintainers, means I'll merge this now and all the follow ups can be done in a new PR. I like the idea of using a const set of types.

@fanquake
fanquake merged commit fa6f22b into bitcoin:master Jul 30, 2019
fanquake added a commit that referenced this pull request Jul 30, 2019
fa6f22b wallet: Rename CWalletKey to OldKey (MarcoFalke)
fa6dc7f wallet: Enumerate walletdb keys (MarcoFalke)

Pull request description:

  It is nice to see all the keys that exists in a single enum

  Also, rename CWalletKey to OldKey and update the outdated documentation

ACKs for top commit:
  laanwj:
    ACK fa6f22b, I'm a big fan of this kind of change as it prevents typos, which can happen with 'magic' strings in the code.
  promag:
    ACK fa6f22b. @jnewbery suggestions are great followups, I think this is good enough.
  meshcollider:
    utACK fa6f22b
  achow101:
    Code review ACK fa6f22b
  fanquake:
    ACK fa6f22b - I had a quick look over, definitely prefer this to strings floating around everywhere.

Tree-SHA512: 8ac3abd5a0d22dac1d77b8f97fe1e16c2608d650f3e9d6dd1df2fd5aeb35ef6643dfd4cd5c162404bb0100343c927d66df04dc695507ffc84a6c667e603acc54
@maflcko
maflcko deleted the 1907-walletDbKeys branch July 30, 2019 12:12
@jnewbery

Copy link
Copy Markdown
Contributor

post-merge ACK fa6f22b

Potential follow-up PRs:

kwvg added a commit to kwvg/dash that referenced this pull request Dec 12, 2021
UdjinM6 added a commit to dashpay/dash that referenced this pull request Dec 12, 2021
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants