@@ -17,73 +17,71 @@ def skip_test_if_missing_module(self):
17
17
self .skip_if_no_wallet ()
18
18
19
19
def setup_network (self ):
20
- self .alert_filename = os .path .join (self .options .tmpdir , "alert.txt" )
21
- self .block_filename = os .path .join (self .options .tmpdir , "blocks.txt" )
22
- self .tx_filename = os .path .join (self .options .tmpdir , "transactions.txt" )
20
+ self .alertnotify_dir = os .path .join (self .options .tmpdir , "alertnotify" )
21
+ self .blocknotify_dir = os .path .join (self .options .tmpdir , "blocknotify" )
22
+ self .walletnotify_dir = os .path .join (self .options .tmpdir , "walletnotify" )
23
+ os .mkdir (self .alertnotify_dir )
24
+ os .mkdir (self .blocknotify_dir )
25
+ os .mkdir (self .walletnotify_dir )
23
26
24
27
# -alertnotify and -blocknotify on node0, walletnotify on node1
25
28
self .extra_args = [["-blockversion=2" ,
26
- "-alertnotify=echo %%s >> %s" % self .alert_filename ,
27
- "-blocknotify=echo %%s >> %s" % self .block_filename ],
29
+ "-alertnotify=echo > {}" . format ( os . path . join ( self .alertnotify_dir , '%s' )) ,
30
+ "-blocknotify=echo > {}" . format ( os . path . join ( self .blocknotify_dir , '%s' )) ],
28
31
["-blockversion=211" ,
29
32
"-rescan" ,
30
- "-walletnotify=echo %%s >> %s" % self .tx_filename ]]
33
+ "-walletnotify=echo > {}" . format ( os . path . join ( self .walletnotify_dir , '%s' )) ]]
31
34
super ().setup_network ()
32
35
33
36
def run_test (self ):
34
37
self .log .info ("test -blocknotify" )
35
38
block_count = 10
36
39
blocks = self .nodes [1 ].generate (block_count )
37
40
38
- # wait at most 10 seconds for expected file size before reading the content
39
- wait_until (lambda : os . path . isfile ( self . block_filename ) and os .stat (self .block_filename ). st_size >= ( block_count * 65 ) , timeout = 10 )
41
+ # wait at most 10 seconds for expected number of files before reading the content
42
+ wait_until (lambda : len ( os .listdir (self .blocknotify_dir )) == block_count , timeout = 10 )
40
43
41
- # file content should equal the generated blocks hashes
42
- with open (self .block_filename , 'r' , encoding = "utf-8" ) as f :
43
- assert_equal (sorted (blocks ), sorted (l .strip () for l in f .read ().splitlines ()))
44
+ # directory content should equal the generated blocks hashes
45
+ assert_equal (sorted (blocks ), sorted (os .listdir (self .blocknotify_dir )))
44
46
45
47
self .log .info ("test -walletnotify" )
46
- # wait at most 10 seconds for expected file size before reading the content
47
- wait_until (lambda : os . path . isfile ( self . tx_filename ) and os .stat (self .tx_filename ). st_size >= ( block_count * 65 ) , timeout = 10 )
48
+ # wait at most 10 seconds for expected number of files before reading the content
49
+ wait_until (lambda : len ( os .listdir (self .walletnotify_dir )) == block_count , timeout = 10 )
48
50
49
- # file content should equal the generated transaction hashes
51
+ # directory content should equal the generated transaction hashes
50
52
txids_rpc = list (map (lambda t : t ['txid' ], self .nodes [1 ].listtransactions ("*" , block_count )))
51
- with open ( self .tx_filename , 'r' , encoding = "ascii" ) as f :
52
- assert_equal ( sorted ( txids_rpc ), sorted ( l . strip () for l in f . read (). splitlines ()))
53
- os .remove (self .tx_filename )
53
+ assert_equal ( sorted ( txids_rpc ), sorted ( os . listdir ( self .walletnotify_dir )))
54
+ for tx_file in os . listdir ( self . walletnotify_dir ):
55
+ os .remove (os . path . join ( self .walletnotify_dir , tx_file ) )
54
56
55
57
self .log .info ("test -walletnotify after rescan" )
56
58
# restart node to rescan to force wallet notifications
57
59
self .restart_node (1 )
58
60
connect_nodes_bi (self .nodes , 0 , 1 )
59
61
60
- wait_until (lambda : os . path . isfile ( self . tx_filename ) and os .stat (self .tx_filename ). st_size >= ( block_count * 65 ) , timeout = 10 )
62
+ wait_until (lambda : len ( os .listdir (self .walletnotify_dir )) == block_count , timeout = 10 )
61
63
62
- # file content should equal the generated transaction hashes
64
+ # directory content should equal the generated transaction hashes
63
65
txids_rpc = list (map (lambda t : t ['txid' ], self .nodes [1 ].listtransactions ("*" , block_count )))
64
- with open (self .tx_filename , 'r' , encoding = "ascii" ) as f :
65
- assert_equal (sorted (txids_rpc ), sorted (l .strip () for l in f .read ().splitlines ()))
66
+ assert_equal (sorted (txids_rpc ), sorted (os .listdir (self .walletnotify_dir )))
66
67
67
68
# Mine another 41 up-version blocks. -alertnotify should trigger on the 51st.
68
69
self .log .info ("test -alertnotify" )
69
70
self .nodes [1 ].generate (41 )
70
71
self .sync_all ()
71
72
72
73
# Give bitcoind 10 seconds to write the alert notification
73
- wait_until (lambda : os . path . isfile ( self . alert_filename ) and os .path . getsize (self .alert_filename ), timeout = 10 )
74
+ wait_until (lambda : len ( os .listdir (self .alertnotify_dir ) ), timeout = 10 )
74
75
75
- with open (self .alert_filename , 'r' , encoding = 'utf8' ) as f :
76
- alert_text = f . read ( )
76
+ for notify_file in os . listdir (self .alertnotify_dir ) :
77
+ os . remove ( os . path . join ( self . alertnotify_dir , notify_file ) )
77
78
78
79
# Mine more up-version blocks, should not get more alerts:
79
80
self .nodes [1 ].generate (2 )
80
81
self .sync_all ()
81
82
82
- with open (self .alert_filename , 'r' , encoding = 'utf8' ) as f :
83
- alert_text2 = f .read ()
84
-
85
83
self .log .info ("-alertnotify should not continue notifying for more unknown version blocks" )
86
- assert_equal (alert_text , alert_text2 )
84
+ assert_equal (len ( os . listdir ( self . alertnotify_dir )), 0 )
87
85
88
86
if __name__ == '__main__' :
89
87
NotificationsTest ().main ()
0 commit comments