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 ,
@@ -62,6 +63,7 @@ def run_test(self):
6263 self ._test_getnetworkhashps ()
6364 self ._test_stopatheight ()
6465 self ._test_waitforblockheight ()
66+ self ._test_utxo_set_hash ()
6567 assert self .nodes [0 ].verifychain (4 , 0 )
6668
6769 def mine_chain (self ):
@@ -73,6 +75,10 @@ def mine_chain(self):
7375 self .nodes [0 ].generatetoaddress (1 , address )
7476 assert_equal (self .nodes [0 ].getblockchaininfo ()['blocks' ], 200 )
7577
78+ def mine_block (self ):
79+ address = self .nodes [0 ].get_deterministic_priv_key ().address
80+ self .nodes [0 ].generatetoaddress (1 , address )
81+
7682 def _test_getblockchaininfo (self ):
7783 self .log .info ("Test getblockchaininfo" )
7884
@@ -204,6 +210,8 @@ def _test_getchaintxstats(self):
204210
205211 def _test_gettxoutsetinfo (self ):
206212 node = self .nodes [0 ]
213+
214+ wait_until (lambda : self .nodes [0 ].getblockcount () == 200 )
207215 res = node .gettxoutsetinfo ()
208216
209217 assert_equal (res ['total_amount' ], Decimal ('8725.00000000' ))
@@ -233,7 +241,6 @@ def _test_gettxoutsetinfo(self):
233241
234242 self .log .info ("Test that gettxoutsetinfo() returns the same result after invalidate/reconsider block" )
235243 node .reconsiderblock (b1hash )
236-
237244 res3 = node .gettxoutsetinfo ()
238245 # The field 'disk_size' is non-deterministic and can thus not be
239246 # compared between res and res3. Everything else should be the same.
@@ -333,6 +340,31 @@ def assert_waitforheight(height, timeout=2):
333340 assert_waitforheight (current_height )
334341 assert_waitforheight (current_height + 1 )
335342
343+ def _test_utxo_set_hash (self ):
344+ self .restart_node (0 )
345+ node = self .nodes [0 ]
346+
347+ self .log .info ("Test that gettxoutsetinfo() utxo set hash is unchanged when rolling back a new block" )
348+
349+ # Test consistency of hashing
350+ res = node .gettxoutsetinfo ()
351+ hash_at_207 = res ['utxo_set_hash' ]
352+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] == hash_at_207 )
353+
354+ # Hash is updated with new block
355+ self .mine_block ()
356+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] != hash_at_207 )
357+
358+ # Hash is rolled back to previous block if invalidated
359+ b208hash = node .getblockhash (208 )
360+ node .invalidateblock (b208hash )
361+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] == hash_at_207 )
362+
363+ # Hash persists restart
364+ self .stop_node (0 )
365+ self .start_node (0 )
366+ assert (node .gettxoutsetinfo ()['utxo_set_hash' ] == hash_at_207 )
367+
336368
337369if __name__ == '__main__' :
338370 BlockchainTest ().main ()
0 commit comments