@@ -228,16 +228,35 @@ class CKeyPool
228228 }
229229};
230230
231- /* * A key allocated from the key pool. */
231+ /* * A wrapper to reserve a key from a wallet keypool
232+ *
233+ * CReserveKey is used to reserve a key from the keypool. It is passed around
234+ * during the CreateTransaction/CommitTransaction procedure.
235+ *
236+ * Instantiating a CReserveKey does not reserve a keypool key. To do so,
237+ * GetReservedKey() needs to be called on the object. Once a key has been
238+ * reserved, call KeepKey() on the CReserveKey object to make sure it is not
239+ * returned to the keypool. Call ReturnKey() to return the key to the keypool
240+ * so it can be re-used (for example, if the key was used in a new transaction
241+ * and that transaction was not completed and needed to be aborted).
242+ *
243+ * If a key is reserved and KeepKey() is not called, then the key will be
244+ * returned to the keypool when the CReserveObject goes out of scope.
245+ */
232246class CReserveKey
233247{
234248protected:
249+ // ! The wallet to reserve the keypool key from
235250 CWallet* pwallet;
251+ // ! The index of the key in the keypool
236252 int64_t nIndex{-1 };
253+ // ! The public key
237254 CPubKey vchPubKey;
255+ // ! Whether this is from the internal (change output) keypool
238256 bool fInternal {false };
239257
240258public:
259+ // ! Construct a CReserveKey object. This does NOT reserve a key from the keypool yet
241260 explicit CReserveKey (CWallet* pwalletIn)
242261 {
243262 pwallet = pwalletIn;
@@ -246,13 +265,17 @@ class CReserveKey
246265 CReserveKey (const CReserveKey&) = delete ;
247266 CReserveKey& operator =(const CReserveKey&) = delete ;
248267
268+ // ! Destructor. If a key has been reserved and not KeepKey'ed, it will be returned to the keypool
249269 ~CReserveKey ()
250270 {
251271 ReturnKey ();
252272 }
253273
254- void ReturnKey ();
274+ // ! Reserve a key from the keypool
255275 bool GetReservedKey (CPubKey &pubkey, bool internal = false );
276+ // ! Return a key to the keypool
277+ void ReturnKey ();
278+ // ! Keep the key. Do not return it to the keypool when this object goes out of scope
256279 void KeepKey ();
257280};
258281
0 commit comments