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

Minor improvements regarding Strings in cln-plugin #135

Merged
merged 2 commits into from
Dec 11, 2022
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
19 changes: 10 additions & 9 deletions watchtower-plugin/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ impl RegisterParams {
fn from_id(tower_id: &str) -> Result<Self, RegisterError> {
Ok(Self {
tower_id: TowerId::from_str(tower_id)
.map_err(|_| RegisterError::InvalidId("Invalid tower id".into()))?,
.map_err(|_| RegisterError::InvalidId("Invalid tower id".to_owned()))?,
host: None,
port: None,
})
}

fn with_host(self, host: &str) -> Result<Self, RegisterError> {
if host.is_empty() {
Err(RegisterError::InvalidHost("hostname is empty".into()))
Err(RegisterError::InvalidHost("hostname is empty".to_owned()))
} else if host.contains(' ') {
Err(RegisterError::InvalidHost(
"hostname contains white spaces".into(),
"hostname contains white spaces".to_owned(),
))
} else {
Ok(Self {
Expand Down Expand Up @@ -193,21 +193,22 @@ impl TryFrom<serde_json::Value> for GetAppointmentParams {
)))
} else {
let tower_id = if let Some(s) = a.get(0).unwrap().as_str() {
TowerId::from_str(s)
.map_err(|_| GetAppointmentError::InvalidId("Invalid tower id".into()))
TowerId::from_str(s).map_err(|_| {
GetAppointmentError::InvalidId("Invalid tower id".to_owned())
})
} else {
Err(GetAppointmentError::InvalidId(
"tower_id must be a hex encoded string".into(),
"tower_id must be a hex encoded string".to_owned(),
))
}?;

let locator = if let Some(s) = a.get(1).unwrap().as_str() {
Locator::from_hex(s).map_err(|_| {
GetAppointmentError::InvalidLocator("Invalid locator".into())
GetAppointmentError::InvalidLocator("Invalid locator".to_owned())
})
} else {
Err(GetAppointmentError::InvalidLocator(
"locator must be a hex encoded string".into(),
"locator must be a hex encoded string".to_owned(),
))
}?;

Expand Down Expand Up @@ -262,7 +263,7 @@ mod tests {
// Any properly formatted host should work
let params = RegisterParams::from_id(VALID_ID).unwrap();
let host = "myhost";
assert_eq!(params.with_host(host).unwrap().host, Some(host.into()));
assert_eq!(params.with_host(host).unwrap().host, Some(host.to_owned()));

// Host must not be empty not have spaces
assert!(matches!(
Expand Down
38 changes: 19 additions & 19 deletions watchtower-plugin/src/dbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let tower_info = TowerInfo::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand Down Expand Up @@ -821,7 +821,7 @@ mod tests {
towers.insert(
tower_id,
TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand Down Expand Up @@ -872,7 +872,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let mut tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand All @@ -886,9 +886,9 @@ mod tests {
let appointment = generate_random_appointment(None);
let user_signature = "user_signature";
let appointment_receipt = AppointmentReceipt::with_signature(
user_signature.into(),
user_signature.to_owned(),
42,
"tower_signature".into(),
"tower_signature".to_owned(),
);

tower_summary.available_slots -= 1;
Expand Down Expand Up @@ -935,15 +935,15 @@ mod tests {

// Add both
let tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
);
let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".into(),
"user_signature".to_owned(),
42,
"tower_signature".into(),
"tower_signature".to_owned(),
);
dbm.store_appointment_receipt(
tower_id,
Expand Down Expand Up @@ -971,7 +971,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand All @@ -987,9 +987,9 @@ mod tests {
for _ in 0..5 {
let appointment = generate_random_appointment(None);
let appointment_receipt = AppointmentReceipt::with_signature(
user_signature.into(),
user_signature.to_owned(),
42,
"tower_signature".into(),
"tower_signature".to_owned(),
);
let pending_appointment = generate_random_appointment(None);
let invalid_appointment = generate_random_appointment(None);
Expand Down Expand Up @@ -1058,7 +1058,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let mut tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand Down Expand Up @@ -1188,7 +1188,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let mut tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand Down Expand Up @@ -1251,7 +1251,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand All @@ -1266,9 +1266,9 @@ mod tests {
// Store a misbehaving proof and load it back
let appointment = generate_random_appointment(None);
let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".into(),
"user_signature".to_owned(),
42,
"tower_signature".into(),
"tower_signature".to_owned(),
);

let proof = MisbehaviorProof::new(
Expand Down Expand Up @@ -1300,7 +1300,7 @@ mod tests {

let receipt = get_random_registration_receipt();
let tower_summary = TowerSummary::new(
net_addr.into(),
net_addr.to_owned(),
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
Expand All @@ -1315,9 +1315,9 @@ mod tests {
// // Store a misbehaving proof check
let appointment = generate_random_appointment(None);
let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".into(),
"user_signature".to_owned(),
42,
"tower_signature".into(),
"tower_signature".to_owned(),
);

let proof = MisbehaviorProof::new(
Expand Down
16 changes: 8 additions & 8 deletions watchtower-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ mod tests {

#[test]
fn test_new() {
let net_addr: String = "addr".into();
let net_addr: String = "addr".to_owned();

let tower_summary = TowerSummary::new(
net_addr.clone(),
Expand All @@ -336,7 +336,7 @@ mod tests {

#[test]
fn test_with_appointments() {
let net_addr: String = "addr".into();
let net_addr: String = "addr".to_owned();

let pending_appointments =
HashSet::from_iter([generate_random_appointment(None).locator]);
Expand Down Expand Up @@ -368,7 +368,7 @@ mod tests {
#[test]
fn test_with_status() {
let mut tower_summary = TowerSummary::new(
"addr".into(),
"addr".to_owned(),
AVAILABLE_SLOTS,
SUBSCRIPTION_START,
SUBSCRIPTION_EXPIRY,
Expand Down Expand Up @@ -407,7 +407,7 @@ mod tests {
#[test]
fn test_new() {
let tower_info = TowerInfo::new(
"addr".into(),
"addr".to_owned(),
AVAILABLE_SLOTS,
SUBSCRIPTION_START,
SUBSCRIPTION_EXPIRY,
Expand All @@ -423,7 +423,7 @@ mod tests {
#[test]
fn test_with_status() {
let mut tower_info = TowerInfo::empty(
"addr".into(),
"addr".to_owned(),
AVAILABLE_SLOTS,
SUBSCRIPTION_START,
SUBSCRIPTION_EXPIRY,
Expand All @@ -437,17 +437,17 @@ mod tests {
#[test]
fn test_set_misbehaving_proof() {
let mut tower_info = TowerInfo::empty(
"addr".into(),
"addr".to_owned(),
AVAILABLE_SLOTS,
SUBSCRIPTION_START,
SUBSCRIPTION_EXPIRY,
);
assert_eq!(tower_info.misbehaving_proof, None);

let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".into(),
"user_signature".to_owned(),
SUBSCRIPTION_START + 1,
"tower_signature".into(),
"tower_signature".to_owned(),
);
let proof = MisbehaviorProof::new(
generate_random_appointment(None).locator,
Expand Down
4 changes: 2 additions & 2 deletions watchtower-plugin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async fn register(
v: serde_json::Value,
) -> Result<serde_json::Value, Error> {
let params = RegisterParams::try_from(v).map_err(|x| anyhow!(x))?;
let host = params.host.unwrap_or_else(|| "localhost".into());
let host = params.host.unwrap_or_else(|| "localhost".to_owned());
let tower_id = params.tower_id;
let user_id = plugin.state().lock().unwrap().user_id;

Expand Down Expand Up @@ -115,7 +115,7 @@ async fn register(
.state()
.lock()
.unwrap()
.add_update_tower(tower_id, tower_net_addr, &receipt).map_err(|e| {
.add_update_tower(tower_id, &tower_net_addr, &receipt).map_err(|e| {
if e.is_expiry() {
anyhow!("Registration receipt contains a subscription expiry that is not higher than the one we are currently registered for")
} else {
Expand Down
22 changes: 12 additions & 10 deletions watchtower-plugin/src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub async fn send_appointment(
) -> Result<(common_msgs::AddAppointmentResponse, AppointmentReceipt), AddAppointmentError> {
let request_data = common_msgs::AddAppointmentRequest {
appointment: Some(appointment.clone().into()),
signature: signature.into(),
signature: signature.to_owned(),
};

match process_post_response(
Expand All @@ -99,7 +99,7 @@ pub async fn send_appointment(
{
ApiResponse::Response::<common_msgs::AddAppointmentResponse>(r) => {
let receipt = AppointmentReceipt::with_signature(
signature.into(),
signature.to_owned(),
r.start_block,
r.signature.clone(),
);
Expand Down Expand Up @@ -139,7 +139,7 @@ pub async fn post_request<S: Serialize>(
.map_err(|e| RequestError::ConnectionError(format!("{}", e)))?
} else {
return Err(RequestError::ConnectionError(
"Cannot connect to an onion address without a proxy".into(),
"Cannot connect to an onion address without a proxy".to_owned(),
));
}
} else {
Expand All @@ -149,9 +149,11 @@ pub async fn post_request<S: Serialize>(
client.post(endpoint).json(&data).send().await.map_err(|e| {
log::debug!("POST request failed: {:?}", e);
if e.is_connect() | e.is_timeout() {
RequestError::ConnectionError("Cannot connect to the tower. Connection refused".into())
RequestError::ConnectionError(
"Cannot connect to the tower. Connection refused".to_owned(),
)
} else {
RequestError::Unexpected("Unexpected error ocurred (see logs for more info)".into())
RequestError::Unexpected("Unexpected error ocurred (see logs for more info)".to_owned())
}
})
}
Expand Down Expand Up @@ -187,11 +189,11 @@ mod tests {
fn test_is_connection() {
let error_message = "error_msg";
for error in [
RequestError::ConnectionError(error_message.into()),
RequestError::DeserializeError(error_message.into()),
RequestError::Unexpected(error_message.into()),
RequestError::ConnectionError(error_message.to_owned()),
RequestError::DeserializeError(error_message.to_owned()),
RequestError::Unexpected(error_message.to_owned()),
] {
if error == RequestError::ConnectionError(error_message.into()) {
if error == RequestError::ConnectionError(error_message.to_owned()) {
assert!(error.is_connection())
} else {
assert!(!error.is_connection())
Expand Down Expand Up @@ -359,7 +361,7 @@ mod tests {
#[tokio::test]
async fn test_send_appointment_api_error() {
let api_error = ApiError {
error: "error_msg".into(),
error: "error_msg".to_owned(),
error_code: 1,
};

Expand Down
Loading