Skip to content

Commit

Permalink
Error in migrate_db_chrysalis_to_stardust() if no chrysalis data exis…
Browse files Browse the repository at this point in the history
…ts (#1215)

* error in migrate_db_chrysalis_to_stardust() if no chrysalis data exists

* Improve

* Update sdk/CHANGELOG.md

Co-authored-by: Thibault Martinez <[email protected]>

* Bump version

* ocd

* Add another check and update test

* Update check and test

---------

Co-authored-by: Thibault Martinez <[email protected]>
Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent f78be7f commit d772a9e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
6 changes: 5 additions & 1 deletion bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.0.10 - 2023-mm-dd
## 1.0.10 - 2023-09-12

### Changed

- `migrateDbChrysalisToStardust()` returns an error if no chrysalis data was found;

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iota/sdk",
"version": "1.0.9",
"version": "1.0.10",
"description": "Node.js binding to the IOTA SDK library",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.0.4 - 2023-MM-DD

### Changed

- `migrate_db_chrysalis_to_stardust()` returns an error if no chrysalis data was found;

## 1.0.3 - 2023-09-07

### Added
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/wallet/migration/chrysalis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ pub async fn migrate_db_chrysalis_to_stardust(
// `/db` will be appended to the chrysalis storage path, because that's how it was done in the chrysalis wallet
let chrysalis_storage_path = &(*storage_path_string).join("db");

if !chrysalis_storage_path.is_dir() {
return Err(crate::wallet::Error::Migration(
"no chrysalis data to migrate".to_string(),
));
}
let chrysalis_data = get_chrysalis_data(chrysalis_storage_path, password)?;

// create new accounts base on previous data
Expand Down Expand Up @@ -257,6 +262,11 @@ fn get_chrysalis_data(chrysalis_storage_path: &Path, password: Option<Password>)

chrysalis_data.insert(key.to_vec(), value);
}
if !chrysalis_data.contains_key(&b"iota-wallet-account-indexation".to_vec()) {
return Err(crate::wallet::Error::Migration(
"no chrysalis data to migrate".to_string(),
));
}
Ok(chrysalis_data)
}

Expand Down
28 changes: 28 additions & 0 deletions sdk/tests/wallet/chrysalis_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,34 @@ async fn migrate_chrysalis_stronghold() -> Result<()> {
tear_down(storage_path)
}

#[tokio::test]
async fn migrate_empty_chrysalis_db() -> Result<()> {
iota_stronghold::engine::snapshot::try_set_encrypt_work_factor(0).unwrap();
let storage_path = "migrate_empty_chrysalis_db";
setup(storage_path)?;

// Copy db so the original doesn't get modified
copy_folder("./tests/wallet/fixtures/check_existing_4_db_test", storage_path).unwrap();

assert!(matches!(
migrate_db_chrysalis_to_stardust("migrate_empty_chrysalis_db", None, None).await,
Err(iota_sdk::wallet::error::Error::Migration(msg)) if msg == "no chrysalis data to migrate"
));

// add empty /db folder
fs::create_dir("migrate_empty_chrysalis_db/db")?;
assert!(matches!(
migrate_db_chrysalis_to_stardust("migrate_empty_chrysalis_db", None, None).await,
Err(iota_sdk::wallet::error::Error::Migration(msg)) if msg == "no chrysalis data to migrate"
));

// stardust wallet data is still there
let wallet = Wallet::builder().with_storage_path(storage_path).finish().await?;
assert_eq!(wallet.get_accounts().await?.len(), 1);

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

0 comments on commit d772a9e

Please sign in to comment.