42
42
from .authproxy import JSONRPCException
43
43
44
44
class BitcoinTestFramework (object ):
45
+ """Base class for a bitcoin test script.
46
+
47
+ Individual bitcoin test scripts should subclass this class and override the following methods:
48
+
49
+ - __init__()
50
+ - add_options()
51
+ - setup_chain()
52
+ - setup_network()
53
+ - run_test()
54
+
55
+ The main() method should not be overridden.
56
+
57
+ This class also contains various public and private helper methods."""
58
+
59
+ # Methods to override in subclass test scripts.
45
60
46
61
TEST_EXIT_PASSED = 0
47
62
TEST_EXIT_FAILED = 1
@@ -52,9 +67,6 @@ def __init__(self):
52
67
self .setup_clean_chain = False
53
68
self .nodes = None
54
69
55
- def run_test (self ):
56
- raise NotImplementedError
57
-
58
70
def add_options (self , parser ):
59
71
pass
60
72
@@ -65,24 +77,6 @@ def setup_chain(self):
65
77
else :
66
78
self ._initialize_chain (self .options .tmpdir , self .num_nodes , self .options .cachedir )
67
79
68
- def start_node (self , i , dirname , extra_args = None , rpchost = None , timewait = None , binary = None , stderr = None ):
69
- return start_node (i , dirname , extra_args , rpchost , timewait , binary , stderr )
70
-
71
- def start_nodes (self , num_nodes , dirname , extra_args = None , rpchost = None , timewait = None , binary = None ):
72
- return start_nodes (num_nodes , dirname , extra_args , rpchost , timewait , binary )
73
-
74
- def stop_node (self , num_node ):
75
- stop_node (self .nodes [num_node ], num_node )
76
-
77
- def stop_nodes (self ):
78
- stop_nodes (self .nodes )
79
-
80
- def setup_nodes (self ):
81
- extra_args = None
82
- if hasattr (self , "extra_args" ):
83
- extra_args = self .extra_args
84
- self .nodes = start_nodes (self .num_nodes , self .options .tmpdir , extra_args )
85
-
86
80
def setup_network (self ):
87
81
self .setup_nodes ()
88
82
@@ -93,27 +87,16 @@ def setup_network(self):
93
87
connect_nodes_bi (self .nodes , i , i + 1 )
94
88
self .sync_all ()
95
89
96
- def split_network (self ):
97
- """
98
- Split the network of four nodes into nodes 0/1 and 2/3.
99
- """
100
- disconnect_nodes (self .nodes [1 ], 2 )
101
- disconnect_nodes (self .nodes [2 ], 1 )
102
- self .sync_all ([self .nodes [:2 ], self .nodes [2 :]])
103
-
104
- def sync_all (self , node_groups = None ):
105
- if not node_groups :
106
- node_groups = [self .nodes ]
90
+ def setup_nodes (self ):
91
+ extra_args = None
92
+ if hasattr (self , "extra_args" ):
93
+ extra_args = self .extra_args
94
+ self .nodes = start_nodes (self .num_nodes , self .options .tmpdir , extra_args )
107
95
108
- [ sync_blocks ( group ) for group in node_groups ]
109
- [ sync_mempools ( group ) for group in node_groups ]
96
+ def run_test ( self ):
97
+ raise NotImplementedError
110
98
111
- def join_network (self ):
112
- """
113
- Join the (previously split) network halves together.
114
- """
115
- connect_nodes_bi (self .nodes , 1 , 2 )
116
- self .sync_all ()
99
+ # Main function. This should not be overridden by the subclass test scripts.
117
100
118
101
def main (self ):
119
102
@@ -209,6 +192,45 @@ def main(self):
209
192
logging .shutdown ()
210
193
sys .exit (self .TEST_EXIT_FAILED )
211
194
195
+ # Public helper methods. These can be accessed by the subclass test scripts.
196
+
197
+ def start_node (self , i , dirname , extra_args = None , rpchost = None , timewait = None , binary = None , stderr = None ):
198
+ return start_node (i , dirname , extra_args , rpchost , timewait , binary , stderr )
199
+
200
+ def start_nodes (self , num_nodes , dirname , extra_args = None , rpchost = None , timewait = None , binary = None ):
201
+ return start_nodes (num_nodes , dirname , extra_args , rpchost , timewait , binary )
202
+
203
+ def stop_node (self , num_node ):
204
+ stop_node (self .nodes [num_node ], num_node )
205
+
206
+ def stop_nodes (self ):
207
+ stop_nodes (self .nodes )
208
+
209
+ def split_network (self ):
210
+ """
211
+ Split the network of four nodes into nodes 0/1 and 2/3.
212
+ """
213
+ disconnect_nodes (self .nodes [1 ], 2 )
214
+ disconnect_nodes (self .nodes [2 ], 1 )
215
+ self .sync_all ([self .nodes [:2 ], self .nodes [2 :]])
216
+
217
+ def join_network (self ):
218
+ """
219
+ Join the (previously split) network halves together.
220
+ """
221
+ connect_nodes_bi (self .nodes , 1 , 2 )
222
+ self .sync_all ()
223
+
224
+ def sync_all (self , node_groups = None ):
225
+ if not node_groups :
226
+ node_groups = [self .nodes ]
227
+
228
+ for group in node_groups :
229
+ sync_blocks (group )
230
+ sync_mempools (group )
231
+
232
+ # Private helper methods. These should not be accessed by the subclass test scripts.
233
+
212
234
def _start_logging (self ):
213
235
# Add logger and logging handlers
214
236
self .log = logging .getLogger ('TestFramework' )
0 commit comments