-
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(api): missing base token data in info response (#807)
* Query node for base token information at startup * Add node configuration collection * Change message * Clippy + Format * Fix feature gates * Nit * Fix docs * Fix merge * Add test * PR comments * Improve test * PR comments * PR suggestions * PR comments 2 * Fix test
- Loading branch information
Showing
10 changed files
with
353 additions
and
15 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
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2022 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use mongodb::{bson::doc, error::Error, options::FindOneOptions}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
db::{ | ||
mongodb::{MongoDbCollection, MongoDbCollectionExt}, | ||
MongoDb, | ||
}, | ||
types::{node::NodeConfiguration, tangle::MilestoneIndex}, | ||
}; | ||
|
||
/// The corresponding MongoDb document representation to store [`NodeConfiguration`]s. | ||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] | ||
pub struct ConfigurationUpdateDocument { | ||
#[serde(rename = "_id")] | ||
pub ledger_index: MilestoneIndex, | ||
#[serde(flatten)] | ||
pub config: NodeConfiguration, | ||
} | ||
|
||
/// A collection to store [`NodeConfiguration`]s. | ||
pub struct ConfigurationUpdateCollection { | ||
collection: mongodb::Collection<ConfigurationUpdateDocument>, | ||
} | ||
|
||
impl MongoDbCollection for ConfigurationUpdateCollection { | ||
const NAME: &'static str = "stardust_configuration_updates"; | ||
type Document = ConfigurationUpdateDocument; | ||
|
||
fn instantiate(_db: &MongoDb, collection: mongodb::Collection<Self::Document>) -> Self { | ||
Self { collection } | ||
} | ||
|
||
fn collection(&self) -> &mongodb::Collection<Self::Document> { | ||
&self.collection | ||
} | ||
} | ||
|
||
impl ConfigurationUpdateCollection { | ||
/// Gets the latest node configuration. | ||
pub async fn get_latest_node_configuration(&self) -> Result<Option<ConfigurationUpdateDocument>, Error> { | ||
self.find_one(doc! {}, FindOneOptions::builder().sort(doc! { "_id": -1 }).build()) | ||
.await | ||
} | ||
|
||
/// Gets the node configuration that was valid for the given ledger index. | ||
pub async fn get_node_configuration_for_ledger_index( | ||
&self, | ||
ledger_index: MilestoneIndex, | ||
) -> Result<Option<ConfigurationUpdateDocument>, Error> { | ||
self.find_one( | ||
doc! { "_id": { "$lte": ledger_index } }, | ||
FindOneOptions::builder().sort(doc! { "_id": -1 }).build(), | ||
) | ||
.await | ||
} | ||
|
||
/// Inserts or updates a node configuration for a given ledger index. | ||
pub async fn upsert_node_configuration( | ||
&self, | ||
ledger_index: MilestoneIndex, | ||
config: NodeConfiguration, | ||
) -> Result<(), Error> { | ||
if let Some(latest_config) = self.get_node_configuration_for_ledger_index(ledger_index).await? { | ||
if latest_config.ledger_index == ledger_index { | ||
if latest_config.config != config { | ||
self.replace_one(doc! {}, ConfigurationUpdateDocument { ledger_index, config }, None) | ||
.await?; | ||
} | ||
} else { | ||
self.insert_one(ConfigurationUpdateDocument { ledger_index, config }, None) | ||
.await?; | ||
} | ||
} else { | ||
self.insert_one(ConfigurationUpdateDocument { ledger_index, config }, None) | ||
.await?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Add the node configuration to this collection if it is newer and different. | ||
pub async fn update_latest_node_configuration( | ||
&self, | ||
ledger_index: MilestoneIndex, | ||
config: NodeConfiguration, | ||
) -> Result<(), Error> { | ||
if let Some(latest_config) = self.get_latest_node_configuration().await? { | ||
if latest_config.ledger_index >= ledger_index { | ||
return Ok(()); | ||
} else if latest_config.config != config { | ||
self.insert_one(ConfigurationUpdateDocument { ledger_index, config }, None) | ||
.await?; | ||
} | ||
} else { | ||
self.insert_one(ConfigurationUpdateDocument { ledger_index, config }, 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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2022 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Module containing the node models. | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// The [`NodeConfiguration`] type. | ||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[allow(missing_docs)] | ||
pub struct NodeConfiguration { | ||
pub base_token: BaseToken, | ||
} | ||
|
||
/// The [`BaseToken`] type. | ||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[allow(missing_docs)] | ||
pub struct BaseToken { | ||
pub name: String, | ||
pub ticker_symbol: String, | ||
pub unit: String, | ||
pub subunit: String, | ||
pub decimals: u32, | ||
pub use_metric_prefix: bool, | ||
} |
Oops, something went wrong.