Skip to content

Commit

Permalink
feat(node): complete implementation of v3 → v4 chain state migration
Browse files Browse the repository at this point in the history
  • Loading branch information
aesedepece committed Nov 11, 2024
1 parent 792cd39 commit a8872c6
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions node/src/storage_mngr/node_migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,27 @@ fn migrate_chain_state_v3_to_v4(old_chain_state_bytes: &[u8]) -> Vec<u8> {
let db_version: u32 = 4;
let db_version_bytes = db_version.to_le_bytes();

// FIXME: the protocol info migration is not 100% correct, for reasons unknown.
// Extra fields in ChainState v4:
let protocol_info = ProtocolInfo::default();
let protocol_info_bytes = serialize(&protocol_info).unwrap();
let stakes = StakesTracker::default();
let stakes_bytes = serialize(&stakes).unwrap();

[
&db_version_bytes,
&old_chain_state_bytes[4..5],
&protocol_info_bytes,
&old_chain_state_bytes[5..],
&stakes_bytes,
]
.concat()
// This little trick makes sure that we don't run a migration on a v4 chain state that was
// incorrectly tagged as v3 in the context of a development release, by checking if the bytes
// match a pattern where the environment enum is stored at a certain position
if old_chain_state_bytes[5..9] == [0x02, 0x00, 0x00, 0x00] {
// Extra fields in ChainState v4:
let protocol_info = ProtocolInfo::default();
let protocol_info_bytes = serialize(&protocol_info).unwrap();
let stakes = StakesTracker::default();
let stakes_bytes = serialize(&stakes).unwrap();

[
&db_version_bytes,
&old_chain_state_bytes[4..5],
&protocol_info_bytes,
&old_chain_state_bytes[5..],
&stakes_bytes,
]
.concat()
} else {
[&db_version_bytes, &old_chain_state_bytes[4..]].concat()
}
}

fn migrate_chain_state(mut bytes: Vec<u8>) -> Result<ChainState, failure::Error> {
Expand Down Expand Up @@ -208,7 +214,7 @@ pub fn put_chain_state_in_batch<K>(
where
K: serde::Serialize + 'static,
{
let db_version: u32 = 3;
let db_version: u32 = 4;
// The first byte of the ChainState db_version must never be 0 or 1,
// because that can be confused with version 0.
assert!(db_version.to_le_bytes()[0] >= 2);
Expand Down

0 comments on commit a8872c6

Please sign in to comment.