Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Db migration: Properly handle possibly null protocol params #1091

Merged
merged 6 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Ledger Nano events properly created when preparing transactions using a `SecretManager`;
- `Account::prepare_output()` when `ReturnStrategy::Gift` is used with an existing NFT output;
- `Wallet::restore_backup()` when no secret manager data is stored inside;
- Migration mismatch from `iota-rs` version;

## 1.0.2 - 2023-07-28

Expand Down
8 changes: 4 additions & 4 deletions sdk/src/wallet/migration/migrate_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ fn migrate_account(account: &mut serde_json::Value) -> Result<()> {
}

fn migrate_client_options(client_options: &mut serde_json::Value) -> Result<()> {
let protocol_parameters = &mut client_options["protocolParameters"];
if let Some(protocol_parameters) = &mut client_options.get_mut("protocolParameters") {
ConvertHrp::check(&mut protocol_parameters["bech32_hrp"])?;

ConvertHrp::check(&mut protocol_parameters["bech32_hrp"])?;

rename_keys(&mut protocol_parameters["rent_structure"]);
rename_keys(&mut protocol_parameters["rent_structure"]);
}

Ok(())
}
Expand Down
28 changes: 16 additions & 12 deletions sdk/src/wallet/migration/migrate_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ impl Migration<crate::wallet::storage::Storage> for Migrate {

if let Some(mut wallet) = storage.get::<serde_json::Value>(WALLET_INDEXATION_KEY).await? {
if let Some(client_options) = wallet.get_mut("client_options") {
let params = client_options["protocolParameters"].as_object_mut().unwrap();
if let Some(version) = params.remove("protocol_version") {
params.insert("version".to_owned(), version);
}
ConvertNetworkName::check(&mut client_options["protocolParameters"]["network_name"])?;
ConvertTokenSupply::check(&mut client_options["protocolParameters"]["token_supply"])?;
migrate_client_options(client_options)?;
}
rename_keys(&mut wallet);

Expand All @@ -69,12 +64,7 @@ impl Migration<crate::client::stronghold::StrongholdAdapter> for Migrate {
}

if let Some(mut client_options) = storage.get::<serde_json::Value>(CLIENT_OPTIONS_KEY).await? {
let params = client_options["protocolParameters"].as_object_mut().unwrap();
if let Some(version) = params.remove("protocol_version") {
params.insert("version".to_owned(), version);
}
ConvertNetworkName::check(&mut client_options["protocolParameters"]["network_name"])?;
ConvertTokenSupply::check(&mut client_options["protocolParameters"]["token_supply"])?;
migrate_client_options(&mut client_options)?;
rename_keys(&mut client_options);

storage.set(CLIENT_OPTIONS_KEY, &client_options).await?;
Expand All @@ -83,6 +73,20 @@ impl Migration<crate::client::stronghold::StrongholdAdapter> for Migrate {
}
}

fn migrate_client_options(client_options: &mut serde_json::Value) -> Result<()> {
if let Some(params) = client_options
.get_mut("protocolParameters")
.map(|p| p.as_object_mut().unwrap())
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
{
if let Some(version) = params.remove("protocol_version") {
params.insert("version".to_owned(), version);
}
ConvertNetworkName::check(&mut params["network_name"])?;
ConvertTokenSupply::check(&mut params["token_supply"])?;
}
Ok(())
}

fn migrate_account(account: &mut serde_json::Value) -> Result<()> {
for output_data in account["outputs"]
.as_object_mut()
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions sdk/tests/wallet/fixtures/check_existing_5_db_test/CURRENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000130
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
005b9a4a-311b-4093-8947-6d1a92f14e53
Binary file not shown.
Binary file not shown.
36 changes: 36 additions & 0 deletions sdk/tests/wallet/wallet_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,42 @@ async fn check_existing_db_4() -> Result<()> {
tear_down(storage_path)
}

// Db created with iota-sdk commit 37edd0706ee003a0d17c7da19ba974b17b365cfe (@iota/[email protected])
#[cfg(feature = "stronghold")]
#[tokio::test]
async fn check_existing_db_5() -> Result<()> {
let storage_path = "check_existing_5_db_test";
setup(storage_path)?;
// Copy db so the original doesn't get modified
copy_folder("./tests/wallet/fixtures/check_existing_5_db_test", storage_path).unwrap();

let wallet = Wallet::builder().with_storage_path(storage_path).finish().await?;

// Commented because it wasn't created with encrypt_work_factor 0
// wallet.set_stronghold_password("STRONGHOLD_PASSWORD".to_owned()).await?;

// TODO: Supposed to be 2 or was that a mistake?
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(wallet.get_accounts().await?.len(), 1);

let client_options = wallet.client_options().await;
assert_eq!(client_options.node_manager_builder.nodes.len(), 1);

let account = wallet.get_account("Alice").await?;

let addresses = account.addresses().await?;
// One public address
assert_eq!(addresses.len(), 1);
// Wallet was created with mnemonic: "endorse answer radar about source reunion marriage tag sausage weekend frost
// daring base attack because joke dream slender leisure group reason prepare broken river"
assert_eq!(
addresses[0].address().to_string(),
"rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy"
);
assert!(!addresses[0].internal());

tear_down(storage_path)
}

fn copy_folder(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dest)?;
for entry in fs::read_dir(src)? {
Expand Down
Loading