mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 12:02:48 +01:00
Merge #21141: wallet: Add new format string placeholders for walletnotify
06e1fb0b170a69996a7ce1ef5203785a7bc6b278 Add new format string placeholders for walletnotify to include relevant block information for transactions (Maayan Keshet) Pull request description: This patch includes two new format placeholders for walletnotify: %b - the hash of the block containting the transaction (zeroed if a mempool transaction) %h - the height of the block containing the transaction (zero if a mempool transaction) I've included test suite changes to check and validate the above functional requirements as well as doc/help description changes. **Motivation** The walletnotify option is used to be notified of new transactions relevant to the wallet of the node. A common usage pattern is to perform afterwards additional RPC calls to determine: 1. If this is a mempool transaction or not (i.e. are there any confirmations?) 2. What block was it included in? 3. Did this transaction was seen before and is now seen again because of a fork? All of these questions can be answered with the current features, but the resulting RPC calls may be expensive in a heavily used node. As this information is readily available when calling the walletnotify callback, it makes sense to save expensive round trips by optionally sending this information at that point in time. I can definitely say we would like to use it in Fireblocks, my employer. Please let me know of any questions and suggestions. ACKs for top commit: laanwj: ACK 06e1fb0b170a69996a7ce1ef5203785a7bc6b278 Tree-SHA512: d2744e2a7a883f9c3a9fd32237110e048c4b6b11fea8221c33d10b74157f65bbc4351211f441e8c1a4af5d5d38e2ba6b1943a7673dc18860c0553d7b41e00775
This commit is contained in:
parent
a3bee9c8ec
commit
709652bff7
@ -69,7 +69,7 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const
|
|||||||
argsman.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
|
argsman.AddArg("-walletbroadcast", strprintf("Make the wallet broadcast transactions (default: %u)", DEFAULT_WALLETBROADCAST), ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
|
||||||
argsman.AddArg("-walletdir=<dir>", "Specify directory to hold wallets (default: <datadir>/wallets if it exists, otherwise <datadir>)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
|
argsman.AddArg("-walletdir=<dir>", "Specify directory to hold wallets (default: <datadir>/wallets if it exists, otherwise <datadir>)", ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::WALLET);
|
||||||
#if HAVE_SYSTEM
|
#if HAVE_SYSTEM
|
||||||
argsman.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes. %s in cmd is replaced by TxID and %w is replaced by wallet name. %w is not currently implemented on windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
|
argsman.AddArg("-walletnotify=<cmd>", "Execute command when a wallet transaction changes. %s in cmd is replaced by TxID, %w is replaced by wallet name, %b is replaced by the hash of the block including the transaction (set to 'unconfirmed' if the transaction is not included) and %h is replaced by the block height (-1 if not included). %w is not currently implemented on windows. On systems where %w is supported, it should NOT be quoted because this would break shell escaping used to invoke the command.", ArgsManager::ALLOW_ANY, OptionsCategory::WALLET);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
argsman.AddArg("-discardfee=<amt>", strprintf("The fee rate (in %s/kB) that indicates your tolerance for discarding change by adding it to the fee (default: %s). "
|
argsman.AddArg("-discardfee=<amt>", strprintf("The fee rate (in %s/kB) that indicates your tolerance for discarding change by adding it to the fee (default: %s). "
|
||||||
|
@ -938,6 +938,14 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
|
|||||||
if (!strCmd.empty())
|
if (!strCmd.empty())
|
||||||
{
|
{
|
||||||
ReplaceAll(strCmd, "%s", hash.GetHex());
|
ReplaceAll(strCmd, "%s", hash.GetHex());
|
||||||
|
if (confirm.status == CWalletTx::Status::CONFIRMED)
|
||||||
|
{
|
||||||
|
ReplaceAll(strCmd, "%b", confirm.hashBlock.GetHex());
|
||||||
|
ReplaceAll(strCmd, "%h", ToString(confirm.block_height));
|
||||||
|
} else {
|
||||||
|
ReplaceAll(strCmd, "%b", "unconfirmed");
|
||||||
|
ReplaceAll(strCmd, "%h", "-1");
|
||||||
|
}
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
// Substituting the wallet name isn't currently supported on windows
|
// Substituting the wallet name isn't currently supported on windows
|
||||||
// because windows shell escaping has not been implemented yet:
|
// because windows shell escaping has not been implemented yet:
|
||||||
|
@ -19,7 +19,7 @@ from test_framework.util import (
|
|||||||
FILE_CHAR_START = 32 if os.name == 'nt' else 1
|
FILE_CHAR_START = 32 if os.name == 'nt' else 1
|
||||||
FILE_CHAR_END = 128
|
FILE_CHAR_END = 128
|
||||||
FILE_CHARS_DISALLOWED = '/\\?%*:|"<>' if os.name == 'nt' else '/'
|
FILE_CHARS_DISALLOWED = '/\\?%*:|"<>' if os.name == 'nt' else '/'
|
||||||
|
UNCONFIRMED_HASH_STRING = 'unconfirmed'
|
||||||
|
|
||||||
def notify_outputname(walletname, txid):
|
def notify_outputname(walletname, txid):
|
||||||
return txid if os.name == 'nt' else '{}_{}'.format(walletname, txid)
|
return txid if os.name == 'nt' else '{}_{}'.format(walletname, txid)
|
||||||
@ -46,7 +46,7 @@ class NotificationsTest(DashTestFramework):
|
|||||||
self.extra_args[0].append("-alertnotify=echo > {}".format(os.path.join(self.alertnotify_dir, '%s')))
|
self.extra_args[0].append("-alertnotify=echo > {}".format(os.path.join(self.alertnotify_dir, '%s')))
|
||||||
self.extra_args[0].append("-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s')))
|
self.extra_args[0].append("-blocknotify=echo > {}".format(os.path.join(self.blocknotify_dir, '%s')))
|
||||||
self.extra_args[1].append("-rescan")
|
self.extra_args[1].append("-rescan")
|
||||||
self.extra_args[1].append("-walletnotify=echo > {}".format(os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s'))))
|
self.extra_args[1].append("-walletnotify=echo %h_%b > {}".format(os.path.join(self.walletnotify_dir, notify_outputname('%w', '%s'))))
|
||||||
|
|
||||||
# -chainlocknotify on node0, -instantsendnotify on node1
|
# -chainlocknotify on node0, -instantsendnotify on node1
|
||||||
self.extra_args[0].append("-chainlocknotify=echo > {}".format(os.path.join(self.chainlocknotify_dir, '%s')))
|
self.extra_args[0].append("-chainlocknotify=echo > {}".format(os.path.join(self.chainlocknotify_dir, '%s')))
|
||||||
@ -78,12 +78,9 @@ class NotificationsTest(DashTestFramework):
|
|||||||
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
|
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
|
||||||
|
|
||||||
# directory content should equal the generated transaction hashes
|
# directory content should equal the generated transaction hashes
|
||||||
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count)))
|
tx_details = list(map(lambda t: (t['txid'], t['blockheight'], t['blockhash']), self.nodes[1].listtransactions("*", block_count)))
|
||||||
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
|
|
||||||
self.stop_node(1)
|
self.stop_node(1)
|
||||||
|
self.expect_wallet_notify(tx_details)
|
||||||
for tx_file in os.listdir(self.walletnotify_dir):
|
|
||||||
os.remove(os.path.join(self.walletnotify_dir, tx_file))
|
|
||||||
|
|
||||||
self.log.info("test -walletnotify after rescan")
|
self.log.info("test -walletnotify after rescan")
|
||||||
# restart node to rescan to force wallet notifications
|
# restart node to rescan to force wallet notifications
|
||||||
@ -94,10 +91,8 @@ class NotificationsTest(DashTestFramework):
|
|||||||
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
|
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
|
||||||
|
|
||||||
# directory content should equal the generated transaction hashes
|
# directory content should equal the generated transaction hashes
|
||||||
txids_rpc = list(map(lambda t: notify_outputname(self.wallet, t['txid']), self.nodes[1].listtransactions("*", block_count)))
|
tx_details = list(map(lambda t: (t['txid'], t['blockheight'], t['blockhash']), self.nodes[1].listtransactions("*", block_count)))
|
||||||
assert_equal(sorted(txids_rpc), sorted(os.listdir(self.walletnotify_dir)))
|
self.expect_wallet_notify(tx_details)
|
||||||
for tx_file in os.listdir(self.walletnotify_dir):
|
|
||||||
os.remove(os.path.join(self.walletnotify_dir, tx_file))
|
|
||||||
|
|
||||||
|
|
||||||
self.log.info("test -chainlocknotify")
|
self.log.info("test -chainlocknotify")
|
||||||
@ -145,6 +140,27 @@ class NotificationsTest(DashTestFramework):
|
|||||||
|
|
||||||
# TODO: add test for `-alertnotify` large fork notifications
|
# TODO: add test for `-alertnotify` large fork notifications
|
||||||
|
|
||||||
|
def expect_wallet_notify(self, tx_details):
|
||||||
|
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) >= len(tx_details), timeout=10)
|
||||||
|
# Should have no more and no less files than expected
|
||||||
|
assert_equal(sorted(notify_outputname(self.wallet, tx_id) for tx_id, _, _ in tx_details), sorted(os.listdir(self.walletnotify_dir)))
|
||||||
|
# Should now verify contents of each file
|
||||||
|
for tx_id, blockheight, blockhash in tx_details:
|
||||||
|
fname = os.path.join(self.walletnotify_dir, notify_outputname(self.wallet, tx_id))
|
||||||
|
with open(fname, 'rt', encoding='utf-8') as f:
|
||||||
|
text = f.read()
|
||||||
|
# Universal newline ensures '\n' on 'nt'
|
||||||
|
assert_equal(text[-1], '\n')
|
||||||
|
text = text[:-1]
|
||||||
|
if os.name == 'nt':
|
||||||
|
# On Windows, echo as above will append a whitespace
|
||||||
|
assert_equal(text[-1], ' ')
|
||||||
|
text = text[:-1]
|
||||||
|
expected = str(blockheight) + '_' + blockhash
|
||||||
|
assert_equal(text, expected)
|
||||||
|
|
||||||
|
for tx_file in os.listdir(self.walletnotify_dir):
|
||||||
|
os.remove(os.path.join(self.walletnotify_dir, tx_file))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
NotificationsTest().main()
|
NotificationsTest().main()
|
||||||
|
Loading…
Reference in New Issue
Block a user