-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(indexer)!: add indexed id to fix implicit aliases and nfts (#1075)
* Add indexed id to fix implicit aliases and nfts. Add migration binary. * Move migrations to a CLI command * Check for indexed id kind * Make crypto dep mandatory * Small fix * Mongo doesn't want to hear that it has to match * Use pipeline for update many * Fix details level --------- Co-authored-by: /alex/ <alexander.schmidt@iota.org>
- Loading branch information
Showing
11 changed files
with
244 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
mod api; | ||
mod cli; | ||
mod config; | ||
mod migrations; | ||
mod process; | ||
#[cfg(feature = "inx")] | ||
mod stardust_inx; | ||
|
120 changes: 120 additions & 0 deletions
120
src/bin/inx-chronicle/migrations/migrate_1_0_0_beta_31.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use chronicle::{ | ||
db::{collections::OutputCollection, MongoDb, MongoDbCollection, MongoDbCollectionExt}, | ||
types::stardust::block::output::{AliasId, NftId, OutputId}, | ||
}; | ||
use futures::TryStreamExt; | ||
use mongodb::{bson::doc, options::IndexOptions, IndexModel}; | ||
use serde::Deserialize; | ||
|
||
pub const PREV_VERSION: &str = "1.0.0-beta.30"; | ||
|
||
pub async fn migrate(db: &MongoDb) -> eyre::Result<()> { | ||
let collection = db.collection::<OutputCollection>(); | ||
|
||
#[derive(Deserialize)] | ||
struct Res { | ||
output_id: OutputId, | ||
} | ||
|
||
// Convert the outputs with implicit IDs | ||
let outputs = collection | ||
.aggregate::<Res>( | ||
[ | ||
doc! { "$match": { "$or": [ | ||
{ "output.alias_id": AliasId::implicit() }, | ||
{ "output.nft_id": NftId::implicit() } | ||
] } }, | ||
doc! { "$project": { | ||
"output_id": "$_id" | ||
} }, | ||
], | ||
None, | ||
) | ||
.await? | ||
.map_ok(|res| res.output_id) | ||
.try_collect::<Vec<_>>() | ||
.await?; | ||
|
||
for output_id in outputs { | ||
// Alias and nft are the same length so both can be done this way since they are just serialized as bytes | ||
let id = AliasId::from(output_id); | ||
collection | ||
.update_one( | ||
doc! { "_id": output_id }, | ||
doc! { "$set": { "details.indexed_id": id } }, | ||
None, | ||
) | ||
.await?; | ||
} | ||
|
||
// Get the outputs that don't have implicit IDs | ||
collection | ||
.update_many( | ||
doc! { | ||
"output.kind": "alias", | ||
"output.alias_id": { "$ne": AliasId::implicit() }, | ||
}, | ||
vec![doc! { "$set": { | ||
"details.indexed_id": "$output.alias_id", | ||
} }], | ||
None, | ||
) | ||
.await?; | ||
|
||
collection | ||
.update_many( | ||
doc! { | ||
"output.kind": "nft", | ||
"output.nft_id": { "$ne": NftId::implicit() }, | ||
}, | ||
vec![doc! { "$set": { | ||
"details.indexed_id": "$output.nft_id", | ||
} }], | ||
None, | ||
) | ||
.await?; | ||
|
||
collection | ||
.update_many( | ||
doc! { "output.kind": "foundry" }, | ||
vec![doc! { "$set": { | ||
"details.indexed_id": "$output.foundry_id", | ||
} }], | ||
None, | ||
) | ||
.await?; | ||
|
||
collection | ||
.collection() | ||
.drop_index("output_alias_id_index", None) | ||
.await?; | ||
|
||
collection | ||
.collection() | ||
.drop_index("output_foundry_id_index", None) | ||
.await?; | ||
|
||
collection.collection().drop_index("output_nft_id_index", None).await?; | ||
|
||
collection | ||
.create_index( | ||
IndexModel::builder() | ||
.keys(doc! { "details.indexed_id": 1 }) | ||
.options( | ||
IndexOptions::builder() | ||
.name("output_indexed_id_index".to_string()) | ||
.partial_filter_expression(doc! { | ||
"details.indexed_id": { "$exists": true }, | ||
}) | ||
.build(), | ||
) | ||
.build(), | ||
None, | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use chronicle::db::MongoDb; | ||
use eyre::bail; | ||
|
||
pub mod migrate_1_0_0_beta_31; | ||
|
||
pub async fn migrate(version: &str, db: &MongoDb) -> eyre::Result<()> { | ||
let curr_version = std::env!("CARGO_PKG_VERSION"); | ||
match version { | ||
"1.0.0-beta.31" => { | ||
if migrate_1_0_0_beta_31::PREV_VERSION == curr_version { | ||
migrate_1_0_0_beta_31::migrate(db).await?; | ||
} else { | ||
bail!("cannot migrate to {} from {}", version, curr_version); | ||
} | ||
} | ||
_ => bail!("cannot migrate version {}", version), | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.