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(configs): fill in missing values in ExperimentalConfig with defaults #10472

Merged
merged 3 commits into from
Jan 23, 2024

Conversation

marcelo-gonzalez
Copy link
Contributor

@marcelo-gonzalez marcelo-gonzalez commented Jan 19, 2024

the command neard --home {home_dir} init --chain-id mainnet --download-config downloads the config at
https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/mainnet/config.json and then saves a config file built from that one to {home_dir}/config.json. This is expected to work even when particular fields are missing by filling them in with defaults, which is why many config fields are marked with some sort of #[serde(default)] or #[serde(default = default_fn)]. But the ExperimentalConfig doesn't fill in these defaults. The existing #[serde(default)] over that field in near_network::config_json::Config will have us fill it in if it's totally missing, but we get an error if it's present with some fields missing, which is the case today:

Error: Failed to initialize configs

Caused by:
    config.json file issue: Failed to deserialize config from /tmp/n/config.json: Error("missing field `network_config_overrides`", line: 98, column: 5)

Fix it by adding a #[serde(default)] to each of the fields in ExperimentalConfig

the command `neard --home {home_dir} init --chain-id mainnet --download-config`
downloads the config at
https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/mainnet/config.json
and then saves a config file built from that one to
{home_dir}/config.json. This is expected to work even when particular
fields are missing by filling them in with defaults, which is why many
config fields are marked with some sort of #[serde(default)] or
doesn't fill in these defaults. The existing #[serde(default)] over
that field in `near_network::config_json::Config` will have us fill it
in if it's totally missing, but we get an error if it's present with
some fields missing, which is the case today:

Error: Failed to initialize configs

Caused by:
    config.json file issue: Failed to deserialize config from /tmp/n/config.json: Error("missing field `network_config_overrides`", line: 98, column: 5)

Fix it by adding a #[serde(default)] to the ExperimentalConfig struct
itself, which is how, for example, the fact that the `archive` field
is missing in the above config url is not a problem, because
`nearcore::config::Config` is marked with this attribute.
Copy link

codecov bot commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ab45005) 71.97% compared to head (3edae63) 71.96%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #10472      +/-   ##
==========================================
- Coverage   71.97%   71.96%   -0.01%     
==========================================
  Files         720      720              
  Lines      145126   145138      +12     
  Branches   145126   145138      +12     
==========================================
  Hits       104455   104455              
- Misses      35870    35884      +14     
+ Partials     4801     4799       -2     
Flag Coverage Δ
backward-compatibility 0.08% <0.00%> (-0.01%) ⬇️
db-migration 0.08% <0.00%> (-0.01%) ⬇️
genesis-check 1.27% <100.00%> (+0.01%) ⬆️
integration-tests 36.85% <100.00%> (-0.03%) ⬇️
linux 71.40% <100.00%> (+<0.01%) ⬆️
linux-nightly 71.51% <100.00%> (+<0.01%) ⬆️
macos 55.39% <100.00%> (+<0.01%) ⬆️
pytests 1.49% <100.00%> (+0.01%) ⬆️
sanity-checks 1.28% <100.00%> (+0.01%) ⬆️
unittests 68.01% <100.00%> (-0.02%) ⬇️
upgradability 0.13% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -197,6 +197,7 @@ pub struct Config {
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
#[serde(default)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit risky. It is easy to add a flag where a default value (false, 0 etc.) is not a “disabled experiment”. Can we make the following change instead?

-pub experimental: ExperimentalConfig,
+pub experimental: Option<ExperimentalConfig>,

Copy link
Collaborator

@nagisa nagisa Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or if not, then at least lets annotate each field individually as is done in Config above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh yea that is a good thing to think of, good point. Although, are we not protected by the explicit listing out of each struct field in the impl Default for ExperimentalConfig? Like, if I make this change:

diff --git a/chain/network/src/config_json.rs b/chain/network/src/config_json.rs
index 1225e32ae..2a327bd7f 100644
--- a/chain/network/src/config_json.rs
+++ b/chain/network/src/config_json.rs
@@ -226,6 +226,7 @@ pub struct ExperimentalConfig {
     /// See `NetworkConfig`.
     /// Fields set here will override the NetworkConfig fields.
     pub network_config_overrides: NetworkConfigOverrides,
+    pub behave_properly: bool,
 }
 
 /// Overrides values from NetworkConfig.

and then try to build it, I get this:

% cargo build -p neard
   Compiling near-network v0.0.0 (/Users/marcelo/nearcore/chain/network)
error[E0063]: missing field `behave_properly` in initializer of `ExperimentalConfig`
   --> chain/network/src/config_json.rs:251:9
    |
251 |         ExperimentalConfig {
    |         ^^^^^^^^^^^^^^^^^^ missing `behave_properly`

For more information about this error, try `rustc --explain E0063`.
error: could not compile `near-network` (lib) due to previous error

But maybe there is some other way this could go wrong... @nagisa wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern is more that people might forget to add the field into the config file(s), which might very read

{"...": "...","experimental": {}, "...": "..." }

for a large majority of users.

Picking a wrong default value by itself is still a bug in implementation of course, but when #[serde(default)] is on top of the struct it can be a little difficult for a developer to realize that such defaulting can occur at all. Meanwhile if you had to add #[serde(default)] on top of the field you’d think about what it is doing there, and if you forgot to add it tests would fail with parse errors or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah agreed makes it more obvious if the fields are the ones marked w #[serde(default)]. updated it, PTAL. Went with that instead of the experimental: Option<ExperimentalConfig> because now at least we don't have to change stuff where it's referred to, but if you can think of a reason that would be better, we could just do it that way too.

also while we're lookin at this kind of thing, looks like near_store::config::StoreConfig uses this #[serde(default)] over the struct pattern too..

@marcelo-gonzalez marcelo-gonzalez added this pull request to the merge queue Jan 23, 2024
Merged via the queue into near:master with commit 64e11a5 Jan 23, 2024
26 checks passed
@marcelo-gonzalez marcelo-gonzalez deleted the network-config branch January 23, 2024 17:45
posvyatokum pushed a commit that referenced this pull request Jan 24, 2024
…lts (#10472)

the command `neard --home {home_dir} init --chain-id mainnet
--download-config` downloads the config at

https://s3-us-west-1.amazonaws.com/build.nearprotocol.com/nearcore-deploy/mainnet/config.json
and then saves a config file built from that one to
{home_dir}/config.json. This is expected to work even when particular
fields are missing by filling them in with defaults, which is why many
config fields are marked with some sort of `#[serde(default)]` or
`#[serde(default = default_fn)]`. But the `ExperimentalConfig` doesn't
fill in these defaults. The existing `#[serde(default)]` over that field
in `near_network::config_json::Config` will have us fill it in if it's
totally missing, but we get an error if it's present with some fields
missing, which is the case today:

```
Error: Failed to initialize configs

Caused by:
    config.json file issue: Failed to deserialize config from /tmp/n/config.json: Error("missing field `network_config_overrides`", line: 98, column: 5)
```

Fix it by adding a `#[serde(default)]` to each of the fields in
`ExperimentalConfig`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants