In commit “Key pool: Move TopUp call” (d195dcf841e4037a517ead16db063db9d496b42b)
re: #17373 (review)
It’s a belt-and-suspenders as not all ScriptPubKeyMans are guaranteed to do this.
Would encourage adding this TopUp call in a new commit (see below), or possibly not doing it in this PR. Commit title “Key pool: Move TopUp call” is misleading. This isn’t moving a TopUp call so much as removing a useless TopUp call in one place, and adding a new, slightly justified “belt and suspenders” TopUp call in a different place.
Substantively, it looks like this is a good location for the the topup. I think in general the wallet, not the key manager should be responsible for any opportunistic, unchecked topups so we don’t have bugs caused by differences between key manager implementations. Also, since the wallet not the key manager is responsible for locking and unlocking, this would avoid key manager methods having different undocumented side effects depending on what wallet state they are called under.
Following this design would lead to three small commits:
- One commit removing useless topup from
CWallet::GetNewChangeDestination
.
- One commit moving opportunistic topup from
LegacyScriptPubKeyMan::ReserveKeyFromKeyPool
to ReserveDestination::GetReservedDestination
for greater key manager simplicity and goodness.
- One commit moving the other opportunistic topup from
LegacyScriptPubKeyMan::GetNewDestination
to CWallet::GetNewDestination
Relatedly, I want to emphasize how much wasted time and confusion undocumented “belt and suspenders” wallet code causes for me. I can understand code that does the right things, and I can understand code that does the wrong things, but when I see code checking for conditions that will never happen, or just not doing anything, I have 0 indication (positive or negative) code is doing what the author intended, and I waste time going back questioning fundamental assumptions to see if some inscrutable piece of code is superfluous or has some important effect in a case I never thought about. Personally, I prefer code that triggers errors or logs warnings when unexpected conditions happen instead of just silently continuing and “working anyway”. But in cases where you prefer the belt and suspenders style, having simple “This condition will never happen” or “This code should not be necessary” comments is hugely helpful.