@@ -44,6 +44,12 @@ def skip_test_if_missing_module(self):
44
44
def run_test (self ):
45
45
node = self .nodes [0 ]
46
46
47
+ def assert_submitblock (block , result_str_1 , result_str_2 = None ):
48
+ block .solve ()
49
+ result_str_2 = result_str_2 or 'duplicate-invalid'
50
+ assert_equal (result_str_1 , node .submitblock (hexdata = b2x (block .serialize ())))
51
+ assert_equal (result_str_2 , node .submitblock (hexdata = b2x (block .serialize ())))
52
+
47
53
self .log .info ('getmininginfo' )
48
54
mining_info = node .getmininginfo ()
49
55
assert_equal (mining_info ['blocks' ], 200 )
@@ -96,6 +102,7 @@ def run_test(self):
96
102
bad_block = copy .deepcopy (block )
97
103
bad_block .vtx .append (bad_block .vtx [0 ])
98
104
assert_template (node , bad_block , 'bad-txns-duplicate' )
105
+ assert_submitblock (bad_block , 'bad-txns-duplicate' , 'bad-txns-duplicate' )
99
106
100
107
self .log .info ("getblocktemplate: Test invalid transaction" )
101
108
bad_block = copy .deepcopy (block )
@@ -104,12 +111,14 @@ def run_test(self):
104
111
bad_tx .rehash ()
105
112
bad_block .vtx .append (bad_tx )
106
113
assert_template (node , bad_block , 'bad-txns-inputs-missingorspent' )
114
+ assert_submitblock (bad_block , 'bad-txns-inputs-missingorspent' )
107
115
108
116
self .log .info ("getblocktemplate: Test nonfinal transaction" )
109
117
bad_block = copy .deepcopy (block )
110
118
bad_block .vtx [0 ].nLockTime = 2 ** 32 - 1
111
119
bad_block .vtx [0 ].rehash ()
112
120
assert_template (node , bad_block , 'bad-txns-nonfinal' )
121
+ assert_submitblock (bad_block , 'bad-txns-nonfinal' )
113
122
114
123
self .log .info ("getblocktemplate: Test bad tx count" )
115
124
# The tx count is immediately after the block header
@@ -128,24 +137,29 @@ def run_test(self):
128
137
bad_block = copy .deepcopy (block )
129
138
bad_block .hashMerkleRoot += 1
130
139
assert_template (node , bad_block , 'bad-txnmrklroot' , False )
140
+ assert_submitblock (bad_block , 'bad-txnmrklroot' , 'bad-txnmrklroot' )
131
141
132
142
self .log .info ("getblocktemplate: Test bad timestamps" )
133
143
bad_block = copy .deepcopy (block )
134
144
bad_block .nTime = 2 ** 31 - 1
135
145
assert_template (node , bad_block , 'time-too-new' )
146
+ assert_submitblock (bad_block , 'time-too-new' , 'time-too-new' )
136
147
bad_block .nTime = 0
137
148
assert_template (node , bad_block , 'time-too-old' )
149
+ assert_submitblock (bad_block , 'time-too-old' , 'time-too-old' )
138
150
139
151
self .log .info ("getblocktemplate: Test not best block" )
140
152
bad_block = copy .deepcopy (block )
141
153
bad_block .hashPrevBlock = 123
142
154
assert_template (node , bad_block , 'inconclusive-not-best-prevblk' )
155
+ assert_submitblock (bad_block , 'prev-blk-not-found' , 'prev-blk-not-found' )
143
156
144
157
self .log .info ('submitheader tests' )
145
158
assert_raises_rpc_error (- 22 , 'Block header decode failed' , lambda : node .submitheader (hexdata = 'xx' * 80 ))
146
159
assert_raises_rpc_error (- 22 , 'Block header decode failed' , lambda : node .submitheader (hexdata = 'ff' * 78 ))
147
160
assert_raises_rpc_error (- 25 , 'Must submit previous header' , lambda : node .submitheader (hexdata = 'ff' * 80 ))
148
161
162
+ block .nTime += 1
149
163
block .solve ()
150
164
151
165
def chain_tip (b_hash , * , status = 'headers-only' , branchlen = 1 ):
@@ -164,7 +178,8 @@ def chain_tip(b_hash, *, status='headers-only', branchlen=1):
164
178
node .submitheader (hexdata = b2x (CBlockHeader (bad_block_root ).serialize ()))
165
179
assert chain_tip (bad_block_root .hash ) in node .getchaintips ()
166
180
# Should still reject invalid blocks, even if we have the header:
167
- assert_equal (node .submitblock (hexdata = b2x (bad_block_root .serialize ())), 'invalid' )
181
+ assert_equal (node .submitblock (hexdata = b2x (bad_block_root .serialize ())), 'bad-txnmrklroot' )
182
+ assert_equal (node .submitblock (hexdata = b2x (bad_block_root .serialize ())), 'bad-txnmrklroot' )
168
183
assert chain_tip (bad_block_root .hash ) in node .getchaintips ()
169
184
# We know the header for this invalid block, so should just return early without error:
170
185
node .submitheader (hexdata = b2x (CBlockHeader (bad_block_root ).serialize ()))
@@ -175,7 +190,8 @@ def chain_tip(b_hash, *, status='headers-only', branchlen=1):
175
190
bad_block_lock .vtx [0 ].rehash ()
176
191
bad_block_lock .hashMerkleRoot = bad_block_lock .calc_merkle_root ()
177
192
bad_block_lock .solve ()
178
- assert_equal (node .submitblock (hexdata = b2x (bad_block_lock .serialize ())), 'invalid' )
193
+ assert_equal (node .submitblock (hexdata = b2x (bad_block_lock .serialize ())), 'bad-txns-nonfinal' )
194
+ assert_equal (node .submitblock (hexdata = b2x (bad_block_lock .serialize ())), 'duplicate-invalid' )
179
195
# Build a "good" block on top of the submitted bad block
180
196
bad_block2 = copy .deepcopy (block )
181
197
bad_block2 .hashPrevBlock = bad_block_lock .sha256
@@ -201,6 +217,7 @@ def chain_tip(b_hash, *, status='headers-only', branchlen=1):
201
217
assert_raises_rpc_error (- 25 , 'bad-prevblk' , lambda : node .submitheader (hexdata = b2x (CBlockHeader (bad_block2 ).serialize ())))
202
218
node .submitheader (hexdata = b2x (CBlockHeader (block ).serialize ()))
203
219
node .submitheader (hexdata = b2x (CBlockHeader (bad_block_root ).serialize ()))
220
+ assert_equal (node .submitblock (hexdata = b2x (block .serialize ())), 'duplicate' ) # valid
204
221
205
222
206
223
if __name__ == '__main__' :
0 commit comments