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

fix: custom toml parsing of Option values #2053

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 1 addition & 3 deletions z2/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ impl Chain {
Self::Zq2ProtoTestnet => ContractUpgradesBlockHeights {
deposit_v3: Some(8406000),
},
_ => ContractUpgradesBlockHeights {
deposit_v3: Some(0),
},
_ => ContractUpgradesBlockHeights::default(),
}
}

Expand Down
3 changes: 1 addition & 2 deletions z2/src/chain/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,7 @@ impl ChainNode {
);
ctx.insert(
"contract_upgrade_block_heights",
&serde_json::from_value::<toml::Value>(json!(contract_upgrade_block_heights))?
.to_string(),
&contract_upgrade_block_heights.to_toml().to_string(),
);
// convert json to toml formatting
let toml_servers: toml::Value = serde_json::from_value(api_servers)?;
Expand Down
22 changes: 22 additions & 0 deletions zilliqa/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use anyhow::{anyhow, Result};
use libp2p::{Multiaddr, PeerId};
use rand::{distributions::Alphanumeric, Rng};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::json;

use crate::{
crypto::{Hash, NodePublicKey},
Expand Down Expand Up @@ -536,3 +537,24 @@ pub fn total_native_token_supply_default() -> Amount {
pub struct ContractUpgradesBlockHeights {
pub deposit_v3: Option<u64>,
}

impl ContractUpgradesBlockHeights {
// toml doesnt like Option types. Map items in struct and remove keys for None values
pub fn to_toml(&self) -> toml::Value {
toml::Value::Table(
json!(self)
.as_object()
.unwrap()
.clone()
.into_iter()
.filter_map(|(k, v)| {
if v.is_null() {
None // Skip null values
} else {
Some((k, toml::Value::Integer(v.as_u64().unwrap() as i64)))
}
})
.collect(),
)
}
}
Loading