Skip to content

Commit

Permalink
Make dns_sec optional in ffi
Browse files Browse the repository at this point in the history
make address option in set _address
  • Loading branch information
SWvheerden committed Mar 4, 2024
1 parent 1235f11 commit 03192e1
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ async fn set_base_node_peer(
) -> Result<(CommsPublicKey, Multiaddr), CommandError> {
println!("Setting base node peer...");
println!("{}::{}", public_key, address);
wallet.set_base_node_peer(public_key.clone(), address.clone()).await?;
wallet
.set_base_node_peer(public_key.clone(), Some(address.clone()))
.await?;
Ok((public_key, address))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl wallet_server::Wallet for WalletGrpcServer {
println!("{}::{}", public_key, net_address);
let mut wallet = self.wallet.clone();
wallet
.set_base_node_peer(public_key.clone(), net_address.clone())
.set_base_node_peer(public_key.clone(), Some(net_address.clone()))
.await
.map_err(|e| Status::internal(format!("{:?}", e)))?;

Expand Down
2 changes: 1 addition & 1 deletion applications/minotari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ pub async fn start_wallet(
.ok_or_else(|| ExitError::new(ExitCode::ConfigError, "Configured base node has no address!"))?;

wallet
.set_base_node_peer(base_node.public_key.clone(), net_address.address().clone())
.set_base_node_peer(base_node.public_key.clone(), Some(net_address.address().clone()))
.await
.map_err(|e| {
ExitError::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ impl AppStateInner {
self.wallet
.set_base_node_peer(
peer.public_key.clone(),
peer.addresses.best().ok_or(UiError::NoAddress)?.address().clone(),
Some(peer.addresses.best().ok_or(UiError::NoAddress)?.address().clone()),
)
.await?;

Expand All @@ -1058,7 +1058,7 @@ impl AppStateInner {
self.wallet
.set_base_node_peer(
peer.public_key.clone(),
peer.addresses.best().ok_or(UiError::NoAddress)?.address().clone(),
Some(peer.addresses.best().ok_or(UiError::NoAddress)?.address().clone()),
)
.await?;

Expand Down Expand Up @@ -1096,7 +1096,7 @@ impl AppStateInner {
self.wallet
.set_base_node_peer(
previous.public_key.clone(),
previous.addresses.best().ok_or(UiError::NoAddress)?.address().clone(),
Some(previous.addresses.best().ok_or(UiError::NoAddress)?.address().clone()),
)
.await?;

Expand Down
40 changes: 27 additions & 13 deletions base_layer/wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,10 @@ where
pub async fn set_base_node_peer(
&mut self,
public_key: CommsPublicKey,
address: Multiaddr,
address: Option<Multiaddr>,
) -> Result<(), WalletError> {
info!(
"Wallet setting base node peer, public key: {}, net address: {}.",
"Wallet setting base node peer, public key: {}, net address: {:?}.",
public_key, address
);

Expand All @@ -387,27 +387,41 @@ where
let mut connectivity = self.comms.connectivity();
if let Some(mut current_peer) = peer_manager.find_by_public_key(&public_key).await? {
// Only invalidate the identity signature if addresses are different
if current_peer.addresses.contains(&address) {
info!(
target: LOG_TARGET,
"Address for base node differs from storage. Was {}, setting to {}",
current_peer.addresses,
address
);

current_peer.addresses.add_address(&address, &PeerAddressSource::Config);
peer_manager.add_peer(current_peer.clone()).await?;
if address.is_some() {
let add = address.unwrap();
if !current_peer.addresses.contains(&add) {
info!(
target: LOG_TARGET,
"Address for base node differs from storage. Was {}, setting to {}",
current_peer.addresses,
add
);

current_peer.addresses.add_address(&add, &PeerAddressSource::Config);
peer_manager.add_peer(current_peer.clone()).await?;
}
}
connectivity
.add_peer_to_allow_list(current_peer.node_id.clone())
.await?;
self.wallet_connectivity.set_base_node(current_peer);
} else {
let node_id = NodeId::from_key(&public_key);
if address.is_none() {
debug!(
target: LOG_TARGET,
"Trying to add new peer without an address",
);
return Err(WalletError::ArgumentError {
argument: "set_base_node_peer, address".to_string(),
value: "{Missing}".to_string(),
message: "New peers need the address filled in".to_string(),
});
}
let peer = Peer::new(
public_key,
node_id,
MultiaddressesWithStats::from_addresses_with_source(vec![address], &PeerAddressSource::Config),
MultiaddressesWithStats::from_addresses_with_source(vec![address.unwrap()], &PeerAddressSource::Config),
PeerFlags::empty(),
PeerFeatures::COMMUNICATION_NODE,
Default::default(),
Expand Down
43 changes: 26 additions & 17 deletions base_layer/wallet_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5260,6 +5260,7 @@ pub unsafe extern "C" fn wallet_create(
seed_words: *const TariSeedWords,
network_str: *const c_char,
peer_seed_str: *const c_char,
dns_sec: bool,

callback_received_transaction: unsafe extern "C" fn(*mut TariPendingInboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(*mut TariCompletedTransaction),
Expand Down Expand Up @@ -5485,7 +5486,7 @@ pub unsafe extern "C" fn wallet_create(

let peer_seeds = PeerSeedsConfig {
dns_seeds_name_server: DEFAULT_DNS_NAME_SERVER.parse().unwrap(),
dns_seeds_use_dnssec: true,
dns_seeds_use_dnssec: dns_sec,
dns_seeds: StringList::from(vec![peer_seed.to_string()]),
..Default::default()
};
Expand Down Expand Up @@ -6343,31 +6344,26 @@ pub unsafe extern "C" fn wallet_set_base_node_peer(
return false;
}

let parsed_addr;
if address.is_null() {
error = LibWalletError::from(InterfaceError::NullError("address".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return false;
let parsed_addr = if address.is_null() {
None
} else {
match CStr::from_ptr(address).to_str() {
Ok(v) => {
parsed_addr = match Multiaddr::from_str(v) {
Ok(v) => v,
Err(_) => {
error = LibWalletError::from(InterfaceError::InvalidArgument("address is invalid".to_string()))
.code;
ptr::swap(error_out, &mut error as *mut c_int);
return false;
},
}
Ok(v) => match Multiaddr::from_str(v) {
Ok(v) => Some(v),
Err(_) => {
error =
LibWalletError::from(InterfaceError::InvalidArgument("address is invalid".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return false;
},
},
_ => {
error = LibWalletError::from(InterfaceError::PointerError("address".to_string())).code;
ptr::swap(error_out, &mut error as *mut c_int);
return false;
},
}
}
};

if let Err(e) = (*wallet)
.runtime
Expand Down Expand Up @@ -9572,6 +9568,7 @@ mod test {
ptr::null(),
alice_network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9617,6 +9614,7 @@ mod test {
ptr::null(),
alice_network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9731,6 +9729,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -9956,6 +9955,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10021,6 +10021,7 @@ mod test {
seed_words,
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10099,6 +10100,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10249,6 +10251,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10383,6 +10386,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10597,6 +10601,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -10819,6 +10824,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -11072,6 +11078,7 @@ mod test {
ptr::null(),
network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -11312,6 +11319,7 @@ mod test {
ptr::null(),
alice_network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down Expand Up @@ -11374,6 +11382,7 @@ mod test {
ptr::null(),
bob_network_str,
dns_string,
false,
received_tx_callback,
received_tx_reply_callback,
received_tx_finalized_callback,
Expand Down
1 change: 1 addition & 0 deletions base_layer/wallet_ffi/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,7 @@ struct TariWallet *wallet_create(TariCommsConfig *config,
const struct TariSeedWords *seed_words,
const char *network_str,
const char *peer_seed_str,
bool dns_sec,
void (*callback_received_transaction)(TariPendingInboundTransaction*),
void (*callback_received_transaction_reply)(TariCompletedTransaction*),
void (*callback_received_finalized_transaction)(TariCompletedTransaction*),
Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/ffi/ffi_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ extern "C" {
seed_words: *const TariSeedWords,
network_str: *const c_char,
peer_seed_str: *const c_char,
dns_sec: bool,
callback_received_transaction: unsafe extern "C" fn(*mut TariPendingInboundTransaction),
callback_received_transaction_reply: unsafe extern "C" fn(*mut TariCompletedTransaction),
callback_received_finalized_transaction: unsafe extern "C" fn(*mut TariCompletedTransaction),
Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/ffi/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ impl Wallet {
seed_words_ptr,
CString::new("localnet").unwrap().into_raw(),
CString::new("").unwrap().into_raw(),
false,
callback_received_transaction,
callback_received_transaction_reply,
callback_received_finalized_transaction,
Expand Down

0 comments on commit 03192e1

Please sign in to comment.