Skip to content

Commit

Permalink
Change geofence coordinates to floating-point
Browse files Browse the repository at this point in the history
Updated Geofence3D and Coordinate3D structs to use f64 instead of i64 for latitude, longitude, and altitude. This change improves precision for geofencing and simplifies coordinate calculations in the main code. Also adjusted test cases to align with the new floating-point coordinates.
  • Loading branch information
arkavo-com committed Oct 22, 2024
1 parent 6b524bb commit 30f2efb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 65 deletions.
82 changes: 41 additions & 41 deletions src/bin/contracts/geo_fence_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
pub mod geo_fence_contract {
use scale::{Decode, Encode};

#[derive(Debug, PartialEq, Eq, Clone, Copy, Decode, Encode)]
#[derive(Debug, PartialEq, Clone, Copy, Decode, Encode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct Coordinate3D {
pub latitude: i64,
pub longitude: i64,
pub altitude: i64,
pub latitude: f64,
pub longitude: f64,
pub altitude: f64,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Decode, Encode)]
#[derive(Debug, PartialEq, Clone, Copy, Decode, Encode)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub struct Geofence3D {
pub min_latitude: i64,
pub max_latitude: i64,
pub min_longitude: i64,
pub max_longitude: i64,
pub min_altitude: i64,
pub max_altitude: i64,
pub min_latitude: f64,
pub max_latitude: f64,
pub min_longitude: f64,
pub max_longitude: f64,
pub min_altitude: f64,
pub max_altitude: f64,
}

#[ink(storage)]
Expand Down Expand Up @@ -54,61 +54,61 @@ pub mod geo_fence_contract {
fn test_within_geofence() {
let contract = GeoFenceContract::new();
let geofence = Geofence3D {
min_latitude: -10_000_000,
max_latitude: 10_000_000,
min_longitude: -20_000_000,
max_longitude: 20_000_000,
min_altitude: 0,
max_altitude: 100_000_000,
min_latitude: -10.0,
max_latitude: 10.0,
min_longitude: -20.0,
max_longitude: 20.0,
min_altitude: 0.0,
max_altitude: 100_000.0,
};

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

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

#[ink::test]
fn test_within_20ft_cube() {
let contract = GeoFenceContract::new();
let geofence = Geofence3D {
min_latitude: 0,
max_latitude: 610, // Approximately 20 feet in latitude (scaled by 10^5)
min_longitude: 0,
max_longitude: 610, // Approximately 20 feet in longitude (scaled by 10^5)
min_altitude: 0,
max_altitude: 610, // 20 feet in altitude (scaled by 10^2)
min_latitude: 0.0,
max_latitude: 0.00061, // Approximately 20 feet in latitude
min_longitude: 0.0,
max_longitude: 0.00061, // Approximately 20 feet in longitude
min_altitude: 0.0,
max_altitude: 6.1, // 20 feet in altitude
};

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

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

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

assert!(!contract.is_within_geofence(geofence, Coordinate3D {
latitude: 300,
longitude: 300,
altitude: 1000,
latitude: 0.0003,
longitude: 0.0003,
altitude: 10.0,
})); // Outside altitude
}
}
Expand Down
36 changes: 12 additions & 24 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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 @@ -548,33 +549,20 @@ async fn handle_rewrap(
if let Some(body) = policy_body {
if body.len() >= 24 {
// Ensure we have enough bytes for the geofence data
let geofence = geo_fence_contract::geo_fence_contract::Geofence3D {
min_latitude: i32::from_be_bytes([
body[0], body[1], body[2], body[3],
]) as i64,
max_latitude: i32::from_be_bytes([
body[4], body[5], body[6], body[7],
]) as i64,
min_longitude: i32::from_be_bytes([
body[8], body[9], body[10], body[11],
]) as i64,
max_longitude: i32::from_be_bytes([
body[12], body[13], body[14], body[15],
]) as i64,
min_altitude: i32::from_be_bytes([
body[16], body[17], body[18], body[19],
]) as i64,
max_altitude: i32::from_be_bytes([
body[20], body[21], body[22], body[23],
]) as i64,
let geofence = Geofence3D {
min_latitude: 0.0,
max_latitude: 0.00061, // Approximately 20 feet in latitude
min_longitude: 0.0,
max_longitude: 0.00061, // Approximately 20 feet in longitude
min_altitude: 0.0,
max_altitude: 6.1, // 20 feet in altitude
};

// For this example, we'll use a fixed coordinate. In a real scenario,
// you might want to get this from the claims or another source.
// Get from rewrap request, second payload will be NanoTDF location
let coordinate = geo_fence_contract::geo_fence_contract::Coordinate3D {
latitude: 374305000,
longitude: -1221015000,
altitude: 1000,
latitude: 0.0003,
longitude: 0.0003,
altitude: 3.0,
};

if claims_result.is_ok()
Expand Down

0 comments on commit 30f2efb

Please sign in to comment.