Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • clients/rust/gcli-v2s
  • d0p1/gcli-v2s
  • flebon/gcli-v2s
  • zicmama/gcli-v2s
  • Nicolas80/gcli-v2s
5 results
Show changes
Commits on Source (4)
  • Nicolas80's avatar
    Added extra message when the DB parsing of DbAccountId fails, so we know for... · e8da3aa9
    Nicolas80 authored
    Added extra message when the DB parsing of DbAccountId fails, so we know for which string it failed.
    e8da3aa9
  • Nicolas80's avatar
    * Adapted `impl FromStr for DbAccountId` so that it is the only one that does... · 54e82c1b
    Nicolas80 authored
    * Adapted `impl FromStr for DbAccountId` so that it is the only one that does the conversion from &str to DbAccountId
    ** It provides error with the "raw" string that couldn't be parsed
    ** All other methods use that one now
    ** In order to be able to resolve the issue from !48; we now remove any surrounding double quotes from the string (if present) before parsing into a DbAccountId
    * Replaced `impl From<String> for DbAccountId` with `impl TryFrom<String> for DbAccountId` and adapted the code to use it.
    * Made sure `impl sea_orm::TryGetable for DbAccountId` also use the correct `DbAccountId::from_str` that will now provide the detailed error message
    54e82c1b
  • Nicolas80's avatar
    * Kind of ugly; but needed to adapt fn find_direct_children_accounts to also... · 2cd0a861
    Nicolas80 authored
    * Kind of ugly; but needed to adapt fn find_direct_children_accounts to also match lines that have extra double quotes inside the parent column
    ** For that; since it seems sea-orm does the `eq` part on string values; the only way I could find is to allow any of those checks
    *** original "parent" eq address as DbAccountId (sea-orm converts address' DbAccountId to a clean string that doesn't have the extra double quotes)
    *** extra "parent" eq `format!("\"{}\"",current_account.address.to_string())` for the cases where the DB got incorrect values
    2cd0a861
  • Nicolas80's avatar
    * Added `debug!` log inside `impl From<DbAccountId> for Value` used to... · 5924a73e
    Nicolas80 authored
    * Added `debug!` log inside `impl From<DbAccountId> for Value` used to serialize DbAccountId into the String value for the db
    ** Can be seen when adding `RUST_LOG=debug ` before the `gcli` command
    * Small fix for clippy remark
    5924a73e
......@@ -729,7 +729,7 @@ where
println!();
let vault_account = if let Some(existing_vault_account) =
vault_account::find_by_id(db_tx, &DbAccountId::from(address_to_import.clone())).await?
vault_account::find_by_id(db_tx, &DbAccountId::try_from(address_to_import.clone())?).await?
{
if existing_vault_account.is_base_account() {
println!("You are trying to add {address_to_import} as a <Base> account while it already exists as a <Base> account.");
......@@ -853,7 +853,8 @@ where
println!("Trying to create derivation with address '{derivation_address}'");
println!();
let vault_account = if let Some(existing_vault_account) =
vault_account::find_by_id(db_tx, &DbAccountId::from(derivation_address.clone())).await?
vault_account::find_by_id(db_tx, &DbAccountId::try_from(derivation_address.clone())?)
.await?
{
// Existing account
println!("You are trying to derive '{derivation_path}' from parent '{parent_address}'");
......@@ -922,7 +923,7 @@ where
// Since links are made based on address / parent(address) we can just edit the existing entry and it should be fine
let mut vault_account: ActiveModel = existing_vault_account.into();
vault_account.path = Set(Some(derivation_path.clone()));
vault_account.parent = Set(Some(DbAccountId::from(parent_address.clone())));
vault_account.parent = Set(Some(DbAccountId::try_from(parent_address.clone())?));
vault_account.crypto_scheme = Set(None);
vault_account.encrypted_suri = Set(None);
vault_account.name = Set(name.clone());
......
......@@ -2,11 +2,11 @@ use crate::commands::{cesium, vault};
use crate::runtime_config::AccountId;
use crate::utils::GcliError;
use anyhow::anyhow;
use log::debug;
use sea_orm::entity::prelude::*;
use sea_orm::prelude::async_trait::async_trait;
use sea_orm::prelude::StringLen;
use sea_orm::ActiveValue::Set;
use sea_orm::PaginatorTrait;
use sea_orm::QueryFilter;
use sea_orm::TryGetError;
use sea_orm::{
......@@ -14,6 +14,7 @@ use sea_orm::{
ModelTrait, QueryOrder, RelationDef, RelationTrait, TryFromU64,
};
use sea_orm::{ActiveModelTrait, ConnectionTrait, PrimaryKeyTrait};
use sea_orm::{Condition, PaginatorTrait};
use sea_orm::{DeriveActiveEnum, EntityTrait};
use std::cell::RefCell;
use std::collections::HashMap;
......@@ -133,18 +134,16 @@ impl sea_orm::TryGetable for DbAccountId {
let value: String = res
.try_get_by(idx)
.map_err(|e| TryGetError::Null(e.to_string()))?;
Ok(DbAccountId(AccountId::from_str(&value).map_err(|e| {
TryGetError::DbErr(DbErr::Custom(e.to_string()))
})?))
DbAccountId::from_str(&value).map_err(|e| TryGetError::DbErr(DbErr::Custom(e.to_string())))
}
}
impl sea_orm::sea_query::ValueType for DbAccountId {
fn try_from(v: Value) -> Result<Self, sea_orm::sea_query::ValueTypeErr> {
match v {
Value::String(Some(value)) => Ok(DbAccountId(
AccountId::from_str(&value).map_err(|_| sea_orm::sea_query::ValueTypeErr)?,
)),
Value::String(Some(value)) => {
Ok(DbAccountId::from_str(&value).map_err(|_| sea_orm::sea_query::ValueTypeErr)?)
}
_ => Err(sea_orm::sea_query::ValueTypeErr),
}
}
......@@ -164,7 +163,12 @@ impl sea_orm::sea_query::ValueType for DbAccountId {
impl From<DbAccountId> for Value {
fn from(account_id: DbAccountId) -> Self {
Value::String(Some(Box::new(account_id.0.to_string())))
let account_id_string = account_id.0.to_string();
debug!(
"DB converting DbAccountId to string:'{}'",
account_id_string
);
Value::String(Some(Box::new(account_id_string)))
}
}
......@@ -183,19 +187,32 @@ impl Display for DbAccountId {
}
}
impl FromStr for DbAccountId {
type Err = GcliError;
impl TryFrom<String> for DbAccountId {
type Error = GcliError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(DbAccountId(
AccountId::from_str(s).map_err(|e| GcliError::Input(e.to_string()))?,
))
fn try_from(value: String) -> Result<Self, Self::Error> {
DbAccountId::from_str(&value)
}
}
impl From<String> for DbAccountId {
fn from(s: String) -> Self {
DbAccountId(AccountId::from_str(&s).expect("Invalid AccountId format"))
/// This one is used by the other methods converting from String and does potential extra cleanup of the string before parsing.
///
/// Due to a hard to reproduce issue (!48) we added trim of double quotes (") at start and end of the string.
impl FromStr for DbAccountId {
type Err = GcliError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// Remove surrounding double quotes if present
let cleaned_value = s.trim_matches('"');
Ok(DbAccountId(AccountId::from_str(cleaned_value).map_err(
|e| {
GcliError::Input(format!(
"Cannot parse DbAccountId for raw string '{}' - error: {}",
s, e
))
},
)?))
}
}
......@@ -757,6 +774,9 @@ where
Ok(Some(base_parent_account))
}
/// Fetches all the children accounts of current_account
///
/// Due to a hard to reproduce issue (!48) had to adapt the filter on Parent to also match if the Parent value has those extra double quotes around the value
async fn find_direct_children_accounts<C>(
db: &C,
current_account: &Model,
......@@ -765,7 +785,11 @@ where
C: ConnectionTrait,
{
Entity::find()
.filter(Column::Parent.eq(current_account.address.clone()))
.filter(
Condition::any()
.add(Column::Parent.eq(current_account.address.clone()))
.add(Column::Parent.eq(format!("\"{}\"", current_account.address))),
)
.order_by_asc(Column::Address)
.all(db)
.await
......@@ -921,12 +945,12 @@ where
}
None => {
let vault_account = ActiveModel {
address: Set(address.to_string().into()),
address: Set(address.to_string().try_into()?),
name: Set(name.cloned()),
path: Set(Some(derivation_path.to_string())),
crypto_scheme: Set(None),
encrypted_suri: Set(None),
parent: Set(Some(parent_address.to_string().into())),
parent: Set(Some(parent_address.to_string().try_into()?)),
};
vault_account.insert(db).await?
}
......