From 5caf413ac9d87d25f6df6e7c1a9358a93b01b3c1 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 19:14:28 +0800 Subject: [PATCH 1/7] Add files via upload --- node/router/src/routing.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/node/router/src/routing.rs b/node/router/src/routing.rs index d2d2457c62..fc1475fda8 100644 --- a/node/router/src/routing.rs +++ b/node/router/src/routing.rs @@ -41,7 +41,9 @@ pub trait Routing: // Start listening for inbound connections. async fn enable_listener(&self) { - self.tcp().enable_listener().await.expect("Failed to enable the TCP listener"); + if let Err(e) = self.tcp().enable_listener().await { + eprintln!("Failed to enable the TCP listener: {:?}", e); + } } /// Initialize a new instance of the heartbeat. From cc78daeaec895fc3687432893713bdbdf6ba16b1 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 19:14:58 +0800 Subject: [PATCH 2/7] reset commit --- node/router/src/routing.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/node/router/src/routing.rs b/node/router/src/routing.rs index fc1475fda8..d2d2457c62 100644 --- a/node/router/src/routing.rs +++ b/node/router/src/routing.rs @@ -41,9 +41,7 @@ pub trait Routing: // Start listening for inbound connections. async fn enable_listener(&self) { - if let Err(e) = self.tcp().enable_listener().await { - eprintln!("Failed to enable the TCP listener: {:?}", e); - } + self.tcp().enable_listener().await.expect("Failed to enable the TCP listener"); } /// Initialize a new instance of the heartbeat. From 7c81d96ddd1b849c996a6dd403ac91c5a4dac108 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 19:17:35 +0800 Subject: [PATCH 3/7] Optimize error handling Error Handling Improvement: In the `enable_listener` method, use if let Err(e) for error handling instead of directly using expect. This can prevent the program from crashing when an error occurs. The expect method will cause the program to crash and print the specified error message when an error is encountered. This is useful during development and debugging because it helps quickly locate the problem. However, in a production environment, directly crashing may not be the best choice as it can lead to service interruptions. --- node/router/src/routing.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/node/router/src/routing.rs b/node/router/src/routing.rs index d2d2457c62..fc1475fda8 100644 --- a/node/router/src/routing.rs +++ b/node/router/src/routing.rs @@ -41,7 +41,9 @@ pub trait Routing: // Start listening for inbound connections. async fn enable_listener(&self) { - self.tcp().enable_listener().await.expect("Failed to enable the TCP listener"); + if let Err(e) = self.tcp().enable_listener().await { + eprintln!("Failed to enable the TCP listener: {:?}", e); + } } /// Initialize a new instance of the heartbeat. From 3f03b4f7b8fde02b021e8eb40f24e90373d5abda Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 19:48:44 +0800 Subject: [PATCH 4/7] Add files via upload --- node/router/src/routing.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/node/router/src/routing.rs b/node/router/src/routing.rs index fc1475fda8..d2d2457c62 100644 --- a/node/router/src/routing.rs +++ b/node/router/src/routing.rs @@ -41,9 +41,7 @@ pub trait Routing: // Start listening for inbound connections. async fn enable_listener(&self) { - if let Err(e) = self.tcp().enable_listener().await { - eprintln!("Failed to enable the TCP listener: {:?}", e); - } + self.tcp().enable_listener().await.expect("Failed to enable the TCP listener"); } /// Initialize a new instance of the heartbeat. From 224f9c5f35816f295dd5656a4d1d35b8ccb7dd89 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 20:39:34 +0800 Subject: [PATCH 5/7] Add files via upload --- cli/src/commands/account.rs | 99 ++++++++++++++++++++++--------------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/cli/src/commands/account.rs b/cli/src/commands/account.rs index 29fd401ea0..1ca2d5cd1b 100644 --- a/cli/src/commands/account.rs +++ b/cli/src/commands/account.rs @@ -108,53 +108,72 @@ impl Account { bail!("Cannot specify both the '--seed' and '--vanity' flags"); } - match vanity { - // Generate a vanity account for the specified network. - Some(vanity) => match network { - MainnetV0::ID => Self::new_vanity::(vanity.as_str(), discreet), - TestnetV0::ID => Self::new_vanity::(vanity.as_str(), discreet), - CanaryV0::ID => Self::new_vanity::(vanity.as_str(), discreet), - unknown_id => bail!("Unknown network ID ({unknown_id})"), - }, - // Generate a seeded account for the specified network. - None => match network { - MainnetV0::ID => Self::new_seeded::(seed, discreet), - TestnetV0::ID => Self::new_seeded::(seed, discreet), - CanaryV0::ID => Self::new_seeded::(seed, discreet), - unknown_id => bail!("Unknown network ID ({unknown_id})"), - }, - } + Self::generate_account(network, seed, vanity, discreet) } Self::Sign { network, message, seed, raw, private_key, private_key_file } => { - let key = match (private_key, private_key_file) { - (Some(private_key), None) => private_key, - (None, Some(private_key_file)) => { - let path = private_key_file.parse::().map_err(|e| anyhow!("Invalid path - {e}"))?; - std::fs::read_to_string(path)?.trim().to_string() - } - (None, None) => bail!("Missing the '--private-key' or '--private-key-file' argument"), - (Some(_), Some(_)) => { - bail!("Cannot specify both the '--private-key' and '--private-key-file' flags") - } - }; - + let key = Self::get_private_key(private_key, private_key_file)?; // Sign the message for the specified network. - match network { - MainnetV0::ID => Self::sign::(key, message, seed, raw), - TestnetV0::ID => Self::sign::(key, message, seed, raw), - CanaryV0::ID => Self::sign::(key, message, seed, raw), - unknown_id => bail!("Unknown network ID ({unknown_id})"), - } + Self::process_message(network, key, message, seed, raw, Self::sign) } Self::Verify { network, address, signature, message, raw } => { // Verify the signature for the specified network. - match network { - MainnetV0::ID => Self::verify::(address, signature, message, raw), - TestnetV0::ID => Self::verify::(address, signature, message, raw), - CanaryV0::ID => Self::verify::(address, signature, message, raw), - unknown_id => bail!("Unknown network ID ({unknown_id})"), - } + Self::process_message(network, address, signature, message, raw, Self::verify) + } + } + } + + fn generate_account(network: u16, seed: Option, vanity: Option, discreet: bool) -> Result { + match vanity { + // Generate a vanity account for the specified network. + Some(vanity) => Self::new_vanity_account(network, &vanity, discreet), + // Generate a seeded account for the specified network. + None => Self::new_seeded_account(network, seed, discreet), + } + } + + fn new_vanity_account(network: u16, vanity: &str, discreet: bool) -> Result { + Self::execute_for_network(network, |net| Self::new_vanity::(vanity, discreet)) + } + + fn new_seeded_account(network: u16, seed: Option, discreet: bool) -> Result { + Self::execute_for_network(network, |net| Self::new_seeded::(seed, discreet)) + } + + fn get_private_key(private_key: Option, private_key_file: Option) -> Result { + match (private_key, private_key_file) { + (Some(private_key), None) => Ok(private_key), + (None, Some(private_key_file)) => { + let path = private_key_file.parse::().map_err(|e| anyhow!("Invalid path - {e}"))?; + Ok(std::fs::read_to_string(path)?.trim().to_string()) } + (None, None) => bail!("Missing the '--private-key' or '--private-key-file' argument"), + (Some(_), Some(_)) => bail!("Cannot specify both the '--private-key' and '--private-key-file' flags"), + } + } + + fn process_message( + network: u16, + key: String, + message: String, + seed: Option, + raw: bool, + func: F, + ) -> Result + where + F: Fn(String, String, Option, bool) -> Result, + { + Self::execute_for_network(network, |net| func::(key, message, seed, raw)) + } + + fn execute_for_network(network: u16, func: F) -> Result + where + F: FnOnce(&dyn Network) -> Result, + { + match network { + MainnetV0::ID => func(&MainnetV0), + TestnetV0::ID => func(&TestnetV0), + CanaryV0::ID => func(&CanaryV0), + unknown_id => bail!("Unknown network ID ({unknown_id})"), } } From dbf86cf785781252bdea69c8078e01724df3fe75 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 20:42:17 +0800 Subject: [PATCH 6/7] feat: add Remarks --- cli/src/commands/account.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/src/commands/account.rs b/cli/src/commands/account.rs index 1ca2d5cd1b..b711e15559 100644 --- a/cli/src/commands/account.rs +++ b/cli/src/commands/account.rs @@ -131,10 +131,12 @@ impl Account { } } + // Generate a vanity account for the specified network. fn new_vanity_account(network: u16, vanity: &str, discreet: bool) -> Result { Self::execute_for_network(network, |net| Self::new_vanity::(vanity, discreet)) } + // Generate a seeded account for the specified network. fn new_seeded_account(network: u16, seed: Option, discreet: bool) -> Result { Self::execute_for_network(network, |net| Self::new_seeded::(seed, discreet)) } From cf570c14bc86065d94a1fc08c5c02b070c02ec65 Mon Sep 17 00:00:00 2001 From: Jack Date: Sat, 13 Jul 2024 20:45:24 +0800 Subject: [PATCH 7/7] Small code refactor --- cli/src/commands/account.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/src/commands/account.rs b/cli/src/commands/account.rs index b711e15559..2f97baaf73 100644 --- a/cli/src/commands/account.rs +++ b/cli/src/commands/account.rs @@ -153,6 +153,7 @@ impl Account { } } + // Abstract the logic for processing messages fn process_message( network: u16, key: String, @@ -167,6 +168,7 @@ impl Account { Self::execute_for_network(network, |net| func::(key, message, seed, raw)) } + // Abstract Network Execution Logic fn execute_for_network(network: u16, func: F) -> Result where F: FnOnce(&dyn Network) -> Result,