Skip to content

Commit fa3b83e

Browse files
committed
CRC: drop rows that do not have a cosensus hash when migrating from old burn block state table to new table
Signed-off-by: Jacinta Ferrant <jacinta.ferrant@gmail.com>
1 parent cfacfed commit fa3b83e

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

stacks-signer/src/signerdb.rs

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,18 @@ CREATE TABLE IF NOT EXISTS temp_burn_blocks (
508508
INSERT INTO temp_burn_blocks (block_hash, block_height, received_time, consensus_hash)
509509
SELECT block_hash, block_height, received_time, consensus_hash
510510
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
513520
FROM burn_blocks
521+
WHERE consensus_hash IS NOT NULL
522+
AND consensus_hash <> ''
514523
) AS ordered
515524
WHERE rn = 1;
516525
@@ -2672,32 +2681,46 @@ pub mod tests {
26722681
conn.execute_batch(CREATE_BURN_STATE_TABLE)
26732682
.expect("Failed to create old table");
26742683
conn.execute_batch(ADD_CONSENSUS_HASH)
2675-
.expect("Failed to add consensus hash");
2684+
.expect("Failed to add consensus hash to old table");
26762685
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");
26782687

2688+
let consensus_hash = ConsensusHash([0; 20]);
2689+
let total_nmb_rows = 5;
26792690
// Fill with old data with conflicting consensus hashes
2680-
for i in 0..3 {
2691+
for i in 0..=total_nmb_rows {
26812692
let now = SystemTime::now();
26822693
let received_ts = now.duration_since(std::time::UNIX_EPOCH).unwrap().as_secs();
26832694
let burn_hash = BurnchainHeaderHash([i; 32]);
2684-
let consensus_hash = ConsensusHash([0; 20]); // Same consensus hash for all
26852695
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+
};
26952717
}
26962718

26972719
// 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
26982721
conn.execute_batch(MIGRATE_BURN_STATE_TABLE_1_TO_TABLE_2)
26992722
.expect("Failed to migrate data");
2700-
let migrated_count: i64 = conn
2723+
let migrated_count: u64 = conn
27012724
.query_row("SELECT COUNT(*) FROM burn_blocks;", [], |row| row.get(0))
27022725
.expect("Failed to get row count");
27032726

@@ -2706,15 +2729,23 @@ pub mod tests {
27062729
"Expected exactly one row after migration"
27072730
);
27082731

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+
);
27142744

27152745
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"
27182749
);
27192750
}
27202751
}

0 commit comments

Comments
 (0)