Skip to content

Commit

Permalink
[config] Allow network config to be loaded from genesis setup
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored and aptos-bot committed May 10, 2022
1 parent 1571612 commit a7e699d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ publish = false
edition = "2018"

[dependencies]
anyhow = "1.0.57"
bcs = "0.1.3"
get_if_addrs = { version = "0.5.3", default-features = false }
mirai-annotations = "1.12.0"
Expand Down
22 changes: 22 additions & 0 deletions config/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ mod test_config;
pub use test_config::*;
mod api_config;
pub use api_config::*;
use aptos_crypto::{ed25519::Ed25519PrivateKey, x25519};
use aptos_types::account_address::AccountAddress;

/// Represents a deprecated config that provides no field verification.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
Expand Down Expand Up @@ -158,6 +160,26 @@ impl WaypointConfig {
}
}

#[derive(Deserialize, Serialize)]
pub struct IdentityBlob {
account_address: AccountAddress,
account_key: Ed25519PrivateKey,
/// Optional consensus key. Only used for validators
consensus_key: Option<Ed25519PrivateKey>,
network_key: x25519::PrivateKey,
}

impl IdentityBlob {
pub fn from_file(path: &Path) -> anyhow::Result<IdentityBlob> {
Ok(serde_yaml::from_str(&fs::read_to_string(path)?)?)
}

pub fn to_file(&self, path: &Path) -> anyhow::Result<()> {
let mut file = File::open(path)?;
Ok(file.write_all(serde_yaml::to_string(self)?.as_bytes())?)
}
}

#[derive(Clone, Copy, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RoleType {
Expand Down
22 changes: 21 additions & 1 deletion config/src/config/network_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{
config::{Error, SecureBackend},
config::{Error, IdentityBlob, SecureBackend},
keys::ConfigKey,
network_id::NetworkId,
utils,
Expand Down Expand Up @@ -148,6 +148,10 @@ impl NetworkConfig {
.expect("Unable to convert key");
Some(key)
}
Identity::FromFile(config) => {
let identity_blob: IdentityBlob = IdentityBlob::from_file(&config.path).unwrap();
Some(identity_blob.network_key)
}
Identity::None => None,
};
key.expect("identity key should be present")
Expand Down Expand Up @@ -212,6 +216,10 @@ impl NetworkConfig {
.value;
Some(peer_id)
}
Identity::FromFile(config) => {
let identity_blob: IdentityBlob = IdentityBlob::from_file(&config.path).unwrap();
Some(identity_blob.account_address)
}
Identity::None => None,
}
.expect("peer id should be present")
Expand All @@ -234,6 +242,7 @@ impl NetworkConfig {
config.peer_id = peer_id;
}
}
Identity::FromFile(_) => (),
};
}

Expand Down Expand Up @@ -300,6 +309,7 @@ pub enum DiscoveryMethod {
pub enum Identity {
FromConfig(IdentityFromConfig),
FromStorage(IdentityFromStorage),
FromFile(IdentityFromFile),
None,
}

Expand All @@ -316,6 +326,10 @@ impl Identity {
peer_id_name,
})
}

pub fn from_file(path: PathBuf) -> Self {
Identity::FromFile(IdentityFromFile { path })
}
}

/// The identity is stored within the config.
Expand All @@ -336,6 +350,12 @@ pub struct IdentityFromStorage {
pub peer_id_name: String,
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct IdentityFromFile {
pub path: PathBuf,
}

#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RateLimitConfig {
Expand Down

0 comments on commit a7e699d

Please sign in to comment.