@@ -508,9 +508,18 @@ CREATE TABLE IF NOT EXISTS temp_burn_blocks (
508
508
INSERT INTO temp_burn_blocks (block_hash, block_height, received_time, consensus_hash)
509
509
SELECT block_hash, block_height, received_time, consensus_hash
510
510
FROM (
511
- SELECT *,
512
- ROW_NUMBER() OVER (PARTITION BY consensus_hash ORDER BY received_time DESC) as rn
511
+ SELECT
512
+ block_hash,
513
+ block_height,
514
+ received_time,
515
+ consensus_hash,
516
+ ROW_NUMBER() OVER (
517
+ PARTITION BY consensus_hash
518
+ ORDER BY received_time DESC
519
+ ) AS rn
513
520
FROM burn_blocks
521
+ WHERE consensus_hash IS NOT NULL
522
+ AND consensus_hash <> ''
514
523
) AS ordered
515
524
WHERE rn = 1;
516
525
@@ -2672,32 +2681,46 @@ pub mod tests {
2672
2681
conn. execute_batch ( CREATE_BURN_STATE_TABLE )
2673
2682
. expect ( "Failed to create old table" ) ;
2674
2683
conn. execute_batch ( ADD_CONSENSUS_HASH )
2675
- . expect ( "Failed to add consensus hash" ) ;
2684
+ . expect ( "Failed to add consensus hash to old table " ) ;
2676
2685
conn. execute_batch ( ADD_CONSENSUS_HASH_INDEX )
2677
- . expect ( "Failed to add consensus hash index" ) ;
2686
+ . expect ( "Failed to add consensus hash index to old table " ) ;
2678
2687
2688
+ let consensus_hash = ConsensusHash ( [ 0 ; 20 ] ) ;
2689
+ let total_nmb_rows = 5 ;
2679
2690
// Fill with old data with conflicting consensus hashes
2680
- for i in 0 ..3 {
2691
+ for i in 0 ..=total_nmb_rows {
2681
2692
let now = SystemTime :: now ( ) ;
2682
2693
let received_ts = now. duration_since ( std:: time:: UNIX_EPOCH ) . unwrap ( ) . as_secs ( ) ;
2683
2694
let burn_hash = BurnchainHeaderHash ( [ i; 32 ] ) ;
2684
- let consensus_hash = ConsensusHash ( [ 0 ; 20 ] ) ; // Same consensus hash for all
2685
2695
let burn_height = i;
2686
- conn. execute (
2687
- "INSERT OR REPLACE INTO burn_blocks (block_hash, consensus_hash, block_height, received_time) VALUES (?1, ?2, ?3, ?4)" ,
2688
- params ! [
2689
- burn_hash,
2690
- consensus_hash,
2691
- u64_to_sql( burn_height. into( ) ) . unwrap( ) ,
2692
- u64_to_sql( received_ts + i as u64 ) . unwrap( ) , // Ensure increasing received_time
2693
- ]
2694
- ) . unwrap ( ) ;
2696
+ if i % 2 == 0 {
2697
+ // Make sure we have some one empty consensus hash options that will get dropped
2698
+ conn. execute (
2699
+ "INSERT OR REPLACE INTO burn_blocks (block_hash, block_height, received_time) VALUES (?1, ?2, ?3)" ,
2700
+ params ! [
2701
+ burn_hash,
2702
+ u64_to_sql( burn_height. into( ) ) . unwrap( ) ,
2703
+ u64_to_sql( received_ts + i as u64 ) . unwrap( ) , // Ensure increasing received_time
2704
+ ]
2705
+ ) . unwrap ( ) ;
2706
+ } else {
2707
+ conn. execute (
2708
+ "INSERT OR REPLACE INTO burn_blocks (block_hash, consensus_hash, block_height, received_time) VALUES (?1, ?2, ?3, ?4)" ,
2709
+ params ! [
2710
+ burn_hash,
2711
+ consensus_hash,
2712
+ u64_to_sql( burn_height. into( ) ) . unwrap( ) ,
2713
+ u64_to_sql( received_ts + i as u64 ) . unwrap( ) , // Ensure increasing received_time
2714
+ ]
2715
+ ) . unwrap ( ) ;
2716
+ } ;
2695
2717
}
2696
2718
2697
2719
// Migrate the data and make sure that the primary key conflict is resolved by using the last received time
2720
+ // and that the block height and consensus hash of the surviving row is as expected
2698
2721
conn. execute_batch ( MIGRATE_BURN_STATE_TABLE_1_TO_TABLE_2 )
2699
2722
. expect ( "Failed to migrate data" ) ;
2700
- let migrated_count: i64 = conn
2723
+ let migrated_count: u64 = conn
2701
2724
. query_row ( "SELECT COUNT(*) FROM burn_blocks;" , [ ] , |row| row. get ( 0 ) )
2702
2725
. expect ( "Failed to get row count" ) ;
2703
2726
@@ -2706,15 +2729,23 @@ pub mod tests {
2706
2729
"Expected exactly one row after migration"
2707
2730
) ;
2708
2731
2709
- let block_height: i64 = conn
2710
- . query_row ( "SELECT block_height FROM burn_blocks;" , [ ] , |row| {
2711
- row. get ( 0 )
2712
- } )
2713
- . expect ( "Failed to get block height" ) ;
2732
+ let ( block_height, hex_hash) : ( u64 , String ) = conn
2733
+ . query_row (
2734
+ "SELECT block_height, consensus_hash FROM burn_blocks;" ,
2735
+ [ ] ,
2736
+ |row| Ok ( ( row. get ( 0 ) ?, row. get ( 1 ) ?) ) ,
2737
+ )
2738
+ . expect ( "Failed to get block_height and consensus_hash" ) ;
2739
+
2740
+ assert_eq ! (
2741
+ block_height, total_nmb_rows as u64 ,
2742
+ "Expected block_height {total_nmb_rows} to be retained (has the latest received time)"
2743
+ ) ;
2714
2744
2715
2745
assert_eq ! (
2716
- block_height, 2 ,
2717
- "Expected block_height 2 to be retained (has the latest received time)"
2746
+ hex_hash,
2747
+ consensus_hash. to_hex( ) ,
2748
+ "Expected the surviving row to have the correct consensus_hash"
2718
2749
) ;
2719
2750
}
2720
2751
}
0 commit comments