Skip to content

Commit

Permalink
fix: fix NodeIdentityHasNoAddress when creating new node identity (#4002
Browse files Browse the repository at this point in the history
)

Description
---
- sets node id address to multiaddr empty if not given. 

Motivation and Context
---
The node identity address must be set to something, comms will set the tor address later if in tor mode, otherwise public_address must be set in config.

How Has This Been Tested?
---
Started base node with no identity, create a new identity works
  • Loading branch information
sdbondi committed Apr 5, 2022
1 parent 6bd5d44 commit 76b526e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion applications/launchpad/backend/src/docker/workspace.rs
Expand Up @@ -256,7 +256,7 @@ impl TariWorkspace {
) -> Result<Option<NodeIdentity>, DockerWrapperError> {
if let Some(id_file_path) = self.config.id_path(root_path, image) {
debug!("Loading or creating identity file {}", id_file_path.to_string_lossy());
let id = setup_node_identity(id_file_path, &None, true, PeerFeatures::COMMUNICATION_NODE)?
let id = setup_node_identity(id_file_path, None, true, PeerFeatures::COMMUNICATION_NODE)?
.as_ref()
.clone();
Ok(Some(id))
Expand Down
22 changes: 10 additions & 12 deletions applications/tari_app_utilities/src/identity_management.rs
Expand Up @@ -48,7 +48,7 @@ const REQUIRED_IDENTITY_PERMS: u32 = 0o100600;
/// A NodeIdentity wrapped in an atomic reference counter on success, the exit code indicating the reason on failure
pub fn setup_node_identity<P: AsRef<Path>>(
identity_file: P,
public_address: &Option<Multiaddr>,
public_address: Option<&Multiaddr>,
create_id: bool,
peer_features: PeerFeatures,
) -> Result<Arc<NodeIdentity>, ExitError> {
Expand Down Expand Up @@ -94,7 +94,11 @@ pub fn setup_node_identity<P: AsRef<Path>>(

debug!(target: LOG_TARGET, "Existing node id not found. {}. Creating new ID", e);

match create_new_node_identity(&identity_file, public_address.clone(), peer_features) {
match create_new_node_identity(
&identity_file,
public_address.cloned().unwrap_or_else(Multiaddr::empty),
peer_features,
) {
Ok(id) => {
info!(
target: LOG_TARGET,
Expand All @@ -106,10 +110,10 @@ pub fn setup_node_identity<P: AsRef<Path>>(
Ok(Arc::new(id))
},
Err(e) => {
error!(target: LOG_TARGET, "Could not create new node id. {:?}.", e);
error!(target: LOG_TARGET, "Could not create new node id. {}.", e);
Err(ExitError::new(
ExitCode::ConfigError,
&format!("Could not create new node id. {:?}.", e),
&format!("Could not create new node id. {}.", e),
))
},
}
Expand Down Expand Up @@ -153,14 +157,10 @@ fn load_node_identity<P: AsRef<Path>>(path: P) -> Result<NodeIdentity, IdentityE
/// Result containing the node identity, string will indicate reason on error
fn create_new_node_identity<P: AsRef<Path>>(
path: P,
public_addr: Option<Multiaddr>,
public_addr: Multiaddr,
features: PeerFeatures,
) -> Result<NodeIdentity, IdentityError> {
let node_identity = NodeIdentity::random(
&mut OsRng,
public_addr.ok_or(IdentityError::NodeIdentityHasNoAddress)?,
features,
);
let node_identity = NodeIdentity::random(&mut OsRng, public_addr, features);
save_as_json(&path, &node_identity)?;
Ok(node_identity)
}
Expand Down Expand Up @@ -268,8 +268,6 @@ pub enum IdentityError {
NotFound,
#[error("Path is not a file")]
NotFile,
#[error("NodeIdentity has no public address")]
NodeIdentityHasNoAddress,
#[error("Malformed identity file: {0}")]
JsonError(#[from] json5::Error),
#[error(transparent)]
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_base_node/src/main.rs
Expand Up @@ -140,7 +140,7 @@ fn main_inner() -> Result<(), ExitError> {
// Load or create the Node identity
let node_identity = setup_node_identity(
&config.base_node_identity_file,
&config.comms_public_address,
config.comms_public_address.as_ref(),
bootstrap.create_id,
PeerFeatures::COMMUNICATION_NODE,
)?;
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_validator_node/src/main.rs
Expand Up @@ -104,7 +104,7 @@ async fn run_node(config: GlobalConfig, create_id: bool) -> Result<(), ExitError
fs::create_dir_all(&config.comms_peer_db_path).map_err(|err| ExitError::new(ExitCode::ConfigError, &err))?;
let node_identity = setup_node_identity(
&config.base_node_identity_file,
&config.comms_public_address,
config.comms_public_address.as_ref(),
create_id,
PeerFeatures::NONE,
)?;
Expand Down

0 comments on commit 76b526e

Please sign in to comment.