Missing decode calls

This commit is contained in:
Daniel Sanchez Quiros 2023-02-16 11:55:09 +01:00
parent a8b9bf22f5
commit cbfa4121df
3 changed files with 16 additions and 127 deletions

View File

@ -2,11 +2,11 @@
// std
use multiaddr::Multiaddr;
use std::ffi::{CStr, CString};
use std::ffi::CString;
// crates
// internal
use super::config::WakuNodeConfig;
use crate::general::{JsonResponse, PeerId, Result};
use crate::general::{PeerId, Result};
use crate::utils::decode_and_free_response;
/// Instantiates a Waku node
@ -27,72 +27,28 @@ pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<bool> {
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let json_response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(result_ptr);
}
json_response.into()
decode_and_free_response(result_ptr)
}
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation.
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start)
pub fn waku_start() -> Result<bool> {
let response_ptr = unsafe { waku_sys::waku_start() };
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let json_response: JsonResponse<bool> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
json_response.into()
decode_and_free_response(response_ptr)
}
/// Stops a Waku node
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
pub fn waku_stop() -> Result<bool> {
let response_ptr = unsafe { waku_sys::waku_stop() };
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let json_response: JsonResponse<bool> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
json_response.into()
decode_and_free_response(response_ptr)
}
/// If the execution is successful, the result is the peer ID as a string (base58 encoded)
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
pub fn waku_peer_id() -> Result<PeerId> {
let response_ptr = unsafe { waku_sys::waku_peerid() };
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let json_response: JsonResponse<String> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
json_response.into()
decode_and_free_response(response_ptr)
}
/// Get the multiaddresses the Waku node is listening to

View File

@ -1,13 +1,13 @@
//! Waku [peer handling and connection](https://rfc.vac.dev/spec/36/#connecting-to-peers) methods
// std
use std::ffi::{CStr, CString};
use std::ffi::CString;
use std::time::Duration;
// crates
use multiaddr::Multiaddr;
use serde::Deserialize;
// internal
use crate::general::{JsonResponse, PeerId, ProtocolId, Result};
use crate::general::{PeerId, ProtocolId, Result};
use crate::utils::decode_and_free_response;
/// Add a node multiaddress and protocol to the waku nodes peerstore.
@ -27,18 +27,7 @@ pub fn waku_add_peers(address: &Multiaddr, protocol_id: ProtocolId) -> Result<Pe
res
};
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("&str should build properly from the returning response");
let result: JsonResponse<PeerId> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
result.into()
decode_and_free_response(response_ptr)
}
/// Dial peer using a multiaddress
@ -64,18 +53,7 @@ pub fn waku_connect_peer_with_address(
res
};
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("&str should build properly from the returning response");
let result: JsonResponse<bool> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
Result::from(result).map(|_| ())
decode_and_free_response::<bool>(response_ptr).map(|_| ())
}
/// Dial peer using a peer id
@ -87,7 +65,7 @@ pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option<Duration>) ->
let peer_id_ptr = CString::new(peer_id.as_bytes())
.expect("CString should build properly from peer id")
.into_raw();
let response_ptr = unsafe {
let result_ptr = unsafe {
let res = waku_sys::waku_connect_peerid(
peer_id_ptr,
timeout
@ -98,18 +76,7 @@ pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option<Duration>) ->
res
};
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("&str should build properly from the returning response");
let result: JsonResponse<bool> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
Result::from(result).map(|_| ())
decode_and_free_response::<bool>(result_ptr).map(|_| ())
}
/// Disconnect a peer using its peer id
@ -124,37 +91,14 @@ pub fn waku_disconnect_peer_with_id(peer_id: &PeerId) -> Result<()> {
drop(CString::from_raw(peer_id_ptr));
res
};
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("&str should build properly from the returning response");
let result: JsonResponse<bool> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
Result::from(result).map(|_| ())
decode_and_free_response::<bool>(response_ptr).map(|_| ())
}
/// Get number of connected peers
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peer_count)
pub fn waku_peer_count() -> Result<usize> {
let response_ptr = unsafe { waku_sys::waku_peer_cnt() };
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("&str should build properly from the returning response");
let result: JsonResponse<usize> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
result.into()
decode_and_free_response(response_ptr)
}
/// Waku peer supported protocol

View File

@ -7,9 +7,7 @@ use std::time::Duration;
use aes_gcm::{Aes256Gcm, Key};
use secp256k1::{PublicKey, SecretKey};
// internal
use crate::general::{
Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic,
};
use crate::general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic};
use crate::utils::decode_and_free_response;
/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/)
@ -272,16 +270,7 @@ pub fn waku_enough_peers(pubsub_topic: Option<WakuPubSubTopic>) -> Result<bool>
res
};
let result = unsafe { CStr::from_ptr(result_ptr) }
.to_str()
.expect("&str from result should always be extracted");
let enough_peers: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
unsafe { waku_sys::waku_utils_free(result_ptr) };
enough_peers.into()
decode_and_free_response(result_ptr)
}
pub fn waku_relay_subscribe(pubsub_topic: Option<WakuPubSubTopic>) -> Result<()> {