Skip to content

Commit

Permalink
rpc: getwalletinfo, return wallet 'birthtime'
Browse files Browse the repository at this point in the history
And add coverage for it

Github-Pull: bitcoin#28920
Rebased-From: 1ce45ba
  • Loading branch information
furszy authored and fanquake committed Dec 18, 2023
1 parent f768692 commit 3704e3a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/wallet/rpc/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static RPCHelpMan getwalletinfo()
{RPCResult::Type::BOOL, "descriptors", "whether this wallet uses descriptors for scriptPubKey management"},
{RPCResult::Type::BOOL, "external_signer", "whether this wallet is configured to use an external signer such as a hardware wallet"},
{RPCResult::Type::BOOL, "blank", "Whether this wallet intentionally does not contain any keys, scripts, or descriptors"},
{RPCResult::Type::NUM_TIME, "birthtime", /*optional=*/true, "The start time for blocks scanning. It could be modified by (re)importing any descriptor with an earlier timestamp."},
RESULT_LAST_PROCESSED_BLOCK,
}},
},
Expand Down Expand Up @@ -132,6 +133,9 @@ static RPCHelpMan getwalletinfo()
obj.pushKV("descriptors", pwallet->IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
obj.pushKV("external_signer", pwallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER));
obj.pushKV("blank", pwallet->IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
if (int64_t birthtime = pwallet->GetBirthTime(); birthtime != UNKNOWN_TIME) {
obj.pushKV("birthtime", birthtime);
}

AppendLastProcessedBlock(obj, *pwallet);
return obj;
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,9 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
/* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
bool CanGetAddresses(bool internal = false) const;

/* Returns the time of the first created key or, in case of an import, it could be the time of the first received transaction */
int64_t GetBirthTime() const { return m_birth_time; }

/**
* Blocks until the wallet state is up-to-date to /at least/ the current
* chain at the time this function is entered
Expand Down
25 changes: 23 additions & 2 deletions test/functional/wallet_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,27 @@ def birthtime_test(self, node, miner_wallet):
self.advance_time(node, BLOCK_TIME)

# Now create a new wallet, and import the descriptor
node.createwallet(wallet_name='watch_only', disable_private_keys=True, blank=True, load_on_startup=True)
node.createwallet(wallet_name='watch_only', disable_private_keys=True, load_on_startup=True)
wallet_watch_only = node.get_wallet_rpc('watch_only')
# Blank wallets don't have a birth time
assert 'birthtime' not in wallet_watch_only.getwalletinfo()

# For a descriptors wallet: Import address with timestamp=now.
# For legacy wallet: There is no way of importing a script/address with a custom time. The wallet always imports it with birthtime=1.
# In both cases, disable rescan to not detect the transaction.
wallet_watch_only.importaddress(wallet_addr, rescan=False)
assert_equal(len(wallet_watch_only.listtransactions()), 0)

# Depending on the wallet type, the birth time changes.
wallet_birthtime = wallet_watch_only.getwalletinfo()['birthtime']
if self.options.descriptors:
# As blocks were generated every 10 min, the chain MTP timestamp is node_time - 60 min.
assert_equal(self.node_time - BLOCK_TIME * 6, wallet_birthtime)
else:
# No way of importing scripts/addresses with a custom time on a legacy wallet.
# It's always set to the beginning of time.
assert_equal(wallet_birthtime, 1)

# Rescan the wallet to detect the missing transaction
wallet_watch_only.rescanblockchain()
assert_equal(wallet_watch_only.gettransaction(tx_id)['confirmations'], 50)
Expand All @@ -65,7 +77,16 @@ def birthtime_test(self, node, miner_wallet):

# Verify the transaction is still 'confirmed' after reindex
wallet_watch_only = node.get_wallet_rpc('watch_only')
assert_equal(wallet_watch_only.gettransaction(tx_id)['confirmations'], 50)
tx_info = wallet_watch_only.gettransaction(tx_id)
assert_equal(tx_info['confirmations'], 50)

# Depending on the wallet type, the birth time changes.
if self.options.descriptors:
# For descriptors, verify the wallet updated the birth time to the transaction time
assert_equal(tx_info['time'], wallet_watch_only.getwalletinfo()['birthtime'])
else:
# For legacy, as the birth time was set to the beginning of time, verify it did not change
assert_equal(wallet_birthtime, 1)

wallet_watch_only.unloadwallet()

Expand Down

0 comments on commit 3704e3a

Please sign in to comment.