10
10
from test_framework .test_framework import BitcoinTestFramework
11
11
from test_framework .util import (
12
12
assert_equal ,
13
+ assert_greater_than_or_equal ,
13
14
assert_raises_rpc_error ,
14
15
connect_nodes_bi ,
15
16
p2p_port ,
@@ -33,26 +34,34 @@ def _test_connection_count(self):
33
34
assert_equal (self .nodes [0 ].getconnectioncount (), 2 )
34
35
35
36
def _test_getnettotals (self ):
36
- # check that getnettotals totalbytesrecv and totalbytessent
37
- # are consistent with getpeerinfo
37
+ # getnettotals totalbytesrecv and totalbytessent should be
38
+ # consistent with getpeerinfo. Since the RPC calls are not atomic,
39
+ # and messages might have been recvd or sent between RPC calls, call
40
+ # getnettotals before and after and verify that the returned values
41
+ # from getpeerinfo are bounded by those values.
42
+ net_totals_before = self .nodes [0 ].getnettotals ()
38
43
peer_info = self .nodes [0 ].getpeerinfo ()
44
+ net_totals_after = self .nodes [0 ].getnettotals ()
39
45
assert_equal (len (peer_info ), 2 )
40
- net_totals = self .nodes [0 ].getnettotals ()
41
- assert_equal (sum ([peer ['bytesrecv' ] for peer in peer_info ]),
42
- net_totals ['totalbytesrecv' ])
43
- assert_equal (sum ([peer ['bytessent' ] for peer in peer_info ]),
44
- net_totals ['totalbytessent' ])
46
+ peers_recv = sum ([peer ['bytesrecv' ] for peer in peer_info ])
47
+ peers_sent = sum ([peer ['bytessent' ] for peer in peer_info ])
48
+
49
+ assert_greater_than_or_equal (peers_recv , net_totals_before ['totalbytesrecv' ])
50
+ assert_greater_than_or_equal (net_totals_after ['totalbytesrecv' ], peers_recv )
51
+ assert_greater_than_or_equal (peers_sent , net_totals_before ['totalbytessent' ])
52
+ assert_greater_than_or_equal (net_totals_after ['totalbytessent' ], peers_sent )
53
+
45
54
# test getnettotals and getpeerinfo by doing a ping
46
55
# the bytes sent/received should change
47
56
# note ping and pong are 32 bytes each
48
57
self .nodes [0 ].ping ()
49
- wait_until (lambda : (net_totals [ 'totalbytessent' ] + 32 * 2 ) == self .nodes [0 ].getnettotals ()['totalbytessent' ], timeout = 1 )
50
- wait_until (lambda : (net_totals [ 'totalbytesrecv' ] + 32 * 2 ) == self .nodes [0 ].getnettotals ()['totalbytesrecv' ], timeout = 1 )
58
+ wait_until (lambda : (self .nodes [0 ].getnettotals ()['totalbytessent' ] >= net_totals_after [ 'totalbytessent' ] + 32 * 2 ) , timeout = 1 )
59
+ wait_until (lambda : (self .nodes [0 ].getnettotals ()['totalbytesrecv' ] >= net_totals_after [ 'totalbytesrecv' ] + 32 * 2 ) , timeout = 1 )
51
60
52
61
peer_info_after_ping = self .nodes [0 ].getpeerinfo ()
53
62
for before , after in zip (peer_info , peer_info_after_ping ):
54
- assert_equal ( before ['bytesrecv_per_msg' ]['pong' ] + 32 , after ['bytesrecv_per_msg' ]['pong' ])
55
- assert_equal ( before ['bytessent_per_msg' ]['ping' ] + 32 , after ['bytessent_per_msg' ]['ping' ])
63
+ assert_greater_than_or_equal ( after ['bytesrecv_per_msg' ]['pong' ], before ['bytesrecv_per_msg' ]['pong' ] + 32 )
64
+ assert_greater_than_or_equal ( after ['bytessent_per_msg' ]['ping' ], before ['bytessent_per_msg' ]['ping' ] + 32 )
56
65
57
66
def _test_getnetworkinginfo (self ):
58
67
assert_equal (self .nodes [0 ].getnetworkinfo ()['networkactive' ], True )
@@ -78,8 +87,7 @@ def _test_getaddednodeinfo(self):
78
87
assert_equal (len (added_nodes ), 1 )
79
88
assert_equal (added_nodes [0 ]['addednode' ], ip_port )
80
89
# check that a non-existent node returns an error
81
- assert_raises_rpc_error (- 24 , "Node has not been added" ,
82
- self .nodes [0 ].getaddednodeinfo , '1.1.1.1' )
90
+ assert_raises_rpc_error (- 24 , "Node has not been added" , self .nodes [0 ].getaddednodeinfo , '1.1.1.1' )
83
91
84
92
def _test_getpeerinfo (self ):
85
93
peer_info = [x .getpeerinfo () for x in self .nodes ]
0 commit comments