3131 assert_raises_rpc_error ,
3232 assert_is_hex_string ,
3333 assert_is_hash_string ,
34+ wait_until ,
3435)
3536from test_framework .blocktools import (
3637 create_block ,
@@ -63,6 +64,7 @@ def run_test(self):
6364 self ._test_getnetworkhashps ()
6465 self ._test_stopatheight ()
6566 self ._test_waitforblockheight ()
67+ self ._test_utxo_set_hash ()
6668 assert self .nodes [0 ].verifychain (4 , 0 )
6769
6870 def mine_chain (self ):
@@ -74,6 +76,10 @@ def mine_chain(self):
7476 self .nodes [0 ].generatetoaddress (1 , address )
7577 assert_equal (self .nodes [0 ].getblockchaininfo ()['blocks' ], 200 )
7678
79+ def mine_block (self ):
80+ address = self .nodes [0 ].get_deterministic_priv_key ().address
81+ self .nodes [0 ].generatetoaddress (1 , address )
82+
7783 def _test_getblockchaininfo (self ):
7884 self .log .info ("Test getblockchaininfo" )
7985
@@ -205,6 +211,8 @@ def _test_getchaintxstats(self):
205211
206212 def _test_gettxoutsetinfo (self ):
207213 node = self .nodes [0 ]
214+
215+ wait_until (lambda : self .nodes [0 ].getblockcount () == 200 )
208216 res = node .gettxoutsetinfo ()
209217
210218 assert_equal (res ['total_amount' ], Decimal ('8725.00000000' ))
@@ -234,7 +242,6 @@ def _test_gettxoutsetinfo(self):
234242
235243 self .log .info ("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block" )
236244 node .reconsiderblock (b1hash )
237-
238245 res3 = node .gettxoutsetinfo ()
239246 # The field 'disk_size' is non-deterministic and can thus not be
240247 # compared between res and res3. Everything else should be the same.
@@ -334,6 +341,31 @@ def assert_waitforheight(height, timeout=2):
334341 assert_waitforheight (current_height )
335342 assert_waitforheight (current_height + 1 )
336343
344+ def _test_utxo_set_hash (self ):
345+ self .restart_node (0 )
346+ node = self .nodes [0 ]
347+
348+ self .log .info ("Test that gettxoutsetinfo() utxo set hash is unchanged when rolling back a new block" )
349+
350+ # Test consistency of hashing
351+ res = node .gettxoutsetinfo ()
352+ hash_at_207 = res ['utxo_set_hash' ]
353+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] == hash_at_207 )
354+
355+ # Hash is updated with new block
356+ self .mine_block ()
357+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] != hash_at_207 )
358+
359+ # Hash is rolled back to previous block if invalidated
360+ b208hash = node .getblockhash (208 )
361+ node .invalidateblock (b208hash )
362+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] == hash_at_207 )
363+
364+ # Hash persists restart
365+ self .stop_node (0 )
366+ self .start_node (0 )
367+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] == hash_at_207 )
368+
337369
338370if __name__ == '__main__' :
339371 BlockchainTest ().main ()
0 commit comments