Skip to content

Commit

Permalink
Refactor to improve code formatting
Browse files Browse the repository at this point in the history
Reorganized import statements in `main.rs` and improved code readability by reformatting lines for better alignment. Enhanced the clarity of deprecation warnings and structured method definitions more consistently across `event_generated.rs`.
  • Loading branch information
arkavo-com committed Oct 24, 2024
1 parent 8c8db9e commit 7aee5c2
Show file tree
Hide file tree
Showing 3 changed files with 510 additions and 173 deletions.
88 changes: 51 additions & 37 deletions src/bin/contracts/geo_fence_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,15 @@ pub mod geo_fence_contract {
Self::new()
}
}

impl GeoFenceContract {
#[ink(constructor)]
pub fn new() -> Self {
Self {}
}

#[ink(message)]
pub fn is_within_geofence(
&self,
geofence: Geofence3D,
coordinate: Coordinate3D,
) -> bool {
pub fn is_within_geofence(&self, geofence: Geofence3D, coordinate: Coordinate3D) -> bool {
coordinate.latitude >= geofence.min_latitude
&& coordinate.latitude <= geofence.max_latitude
&& coordinate.longitude >= geofence.min_longitude
Expand All @@ -68,17 +64,23 @@ pub mod geo_fence_contract {
max_altitude: 100_000.0,
};

assert!(contract.is_within_geofence(geofence, Coordinate3D {
latitude: 0.0,
longitude: 0.0,
altitude: 50_000.0,
}));
assert!(contract.is_within_geofence(
geofence,
Coordinate3D {
latitude: 0.0,
longitude: 0.0,
altitude: 50_000.0,
}
));

assert!(!contract.is_within_geofence(geofence, Coordinate3D {
latitude: -15.0,
longitude: 0.0,
altitude: 50_000.0,
}));
assert!(!contract.is_within_geofence(
geofence,
Coordinate3D {
latitude: -15.0,
longitude: 0.0,
altitude: 50_000.0,
}
));
}

#[ink::test]
Expand All @@ -93,29 +95,41 @@ pub mod geo_fence_contract {
max_altitude: 6.1, // 20 feet in altitude
};

assert!(contract.is_within_geofence(geofence, Coordinate3D {
latitude: 0.0003,
longitude: 0.0003,
altitude: 3.0,
})); // Inside the cube
assert!(contract.is_within_geofence(
geofence,
Coordinate3D {
latitude: 0.0003,
longitude: 0.0003,
altitude: 3.0,
}
)); // Inside the cube

assert!(!contract.is_within_geofence(geofence, Coordinate3D {
latitude: 0.001,
longitude: 0.0003,
altitude: 3.0,
})); // Outside latitude
assert!(!contract.is_within_geofence(
geofence,
Coordinate3D {
latitude: 0.001,
longitude: 0.0003,
altitude: 3.0,
}
)); // Outside latitude

assert!(!contract.is_within_geofence(geofence, Coordinate3D {
latitude: 0.0003,
longitude: 0.001,
altitude: 3.0,
})); // Outside longitude
assert!(!contract.is_within_geofence(
geofence,
Coordinate3D {
latitude: 0.0003,
longitude: 0.001,
altitude: 3.0,
}
)); // Outside longitude

assert!(!contract.is_within_geofence(geofence, Coordinate3D {
latitude: 0.0003,
longitude: 0.0003,
altitude: 10.0,
})); // Outside altitude
assert!(!contract.is_within_geofence(
geofence,
Coordinate3D {
latitude: 0.0003,
longitude: 0.0003,
altitude: 10.0,
}
)); // Outside altitude
}
}
}
}
10 changes: 7 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod schemas;

use crate::contracts::contract_simple_abac;
use crate::contracts::geo_fence_contract;
use crate::contracts::geo_fence_contract::geo_fence_contract::Geofence3D;
use crate::schemas::event_generated::arkavo::{Event, EventData};
use aes_gcm::aead::generic_array::GenericArray;
use aes_gcm::aead::KeyInit;
Expand Down Expand Up @@ -38,7 +39,6 @@ use tokio::net::TcpListener;
use tokio::sync::{mpsc, Mutex};
use tokio_native_tls::TlsAcceptor;
use tokio_tungstenite::tungstenite::Message;
use crate::contracts::geo_fence_contract::geo_fence_contract::Geofence3D;

#[derive(Serialize, Deserialize, Debug)]
struct PublicKeyMessage {
Expand Down Expand Up @@ -438,7 +438,7 @@ async fn handle_binary_message(
)
.await
} // internal
Some(MessageType::Event) => handle_event(server_state, payload, nats_connection).await, // embedded
Some(MessageType::Event) => handle_event(server_state, payload, nats_connection).await, // embedded
None => {
// println!("Unknown message type: {:?}", message_type);
None
Expand Down Expand Up @@ -809,7 +809,11 @@ async fn handle_nats_event(
connection_state.outgoing_tx.send(ws_message)?;
Ok(())
}
async fn handle_event(server_state: &Arc<ServerState>, payload: &[u8], nats_connection: Arc<NatsConnection>,) -> Option<Message> {
async fn handle_event(
server_state: &Arc<ServerState>,
payload: &[u8],
nats_connection: Arc<NatsConnection>,
) -> Option<Message> {
let start_time = Instant::now();
let mut event_data: Option<Vec<u8>> = None;
if let Ok(event) = root::<Event>(payload) {
Expand Down
Loading

0 comments on commit 7aee5c2

Please sign in to comment.