Skip to content

Commit

Permalink
fix(bugs): fixed bugs identified when testing
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamj authored and kgmyatthu committed Sep 10, 2023
1 parent 96701dc commit fb0f035
Show file tree
Hide file tree
Showing 16 changed files with 381 additions and 229 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ npm i gitlab:shardus/shardus-net
### Javascript

```js
const port = 1234;
const address = 'localhost';
const port = 1234
const address = 'localhost'

const sn = require('shardus-net')({ port, address });
const sn = require('shardus-net')({ port, address })
```

### Typescript

```ts
import * as ShardusNet from 'shardus-net';
import * as ShardusNet from 'shardus-net'

const port = 1234;
const address = 'localhost';
const sn = ShardusNet.createNetwork({ port, address });
const port = 1234
const address = 'localhost'
const sn = ShardusNet.createNetwork({ port, address })
```

### sn.send
Expand Down Expand Up @@ -94,7 +94,7 @@ const server = await sn.listen((data, remote, protocol, respond) => {
// When you want to spin down your listener, simply call stopListening and pass in
// the server object you were given when you started listening.

await sn.stopListening(servers);
await sn.stopListening(servers)
```

## Developing
Expand Down
21 changes: 14 additions & 7 deletions scripts/copy-rename.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
const fs = require('fs');
const path = require('path');
const fs = require('fs')
const path = require('path')

const sourceFile = path.join(__dirname, '../', 'target', 'x86_64-apple-darwin', 'debug', 'libshardus_net.dylib');
const targetFile = path.join(__dirname, '../', 'shardus-net.node');
const sourceFile = path.join(
__dirname,
'../',
'target',
'x86_64-apple-darwin',
'debug',
'libshardus_net.dylib'
)
const targetFile = path.join(__dirname, '../', 'shardus-net.node')

fs.copyFile(sourceFile, targetFile, (err) => {
if (err) {
console.error('Error copying file:', err);
console.error('Error copying file:', err)
} else {
console.log('File copied and renamed successfully');
console.log('File copied and renamed successfully')
}
});
})
55 changes: 5 additions & 50 deletions shardus_net/src/header_factory.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::Cursor;

use crate::headers::header_types::Header;
use crate::headers::header_v1::HeaderV1;

Expand All @@ -22,10 +24,10 @@ pub fn unwrap_serialized_header(mut serialized_header: Vec<u8>) -> Option<(u8, V
Some((version, serialized_header))
}

pub fn header_deserialize_factory(version: u8, serialized_header: Vec<u8>) -> Option<Header> {
pub fn header_deserialize_factory(version: u8, serialized_header_cursor: &mut Cursor<Vec<u8>>) -> Option<Header> {
match version {
1 => {
let deserialized = HeaderV1::deserialize(serialized_header)?;
let deserialized = HeaderV1::deserialize(serialized_header_cursor)?;
Some(Header::V1(deserialized))
}
_ => None,
Expand All @@ -43,10 +45,7 @@ pub fn header_serialize_factory(header_version: u8, header: Header) -> Option<Ve

#[allow(dead_code)]
pub fn vec_to_hex_string(data: Vec<u8>) -> String {
data.iter()
.map(|byte| format!("{:02x}", byte))
.collect::<Vec<String>>()
.join("")
data.iter().map(|byte| format!("{:02x}", byte)).collect::<Vec<String>>().join("")
}

pub fn header_from_json_string(json_str: &str, version: &u8) -> Option<Header> {
Expand All @@ -55,47 +54,3 @@ pub fn header_from_json_string(json_str: &str, version: &u8) -> Option<Header> {
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::headers::header_v1::HeaderV1;
use uuid::Uuid;

#[test]
fn test_wrap_unwrap() {
let serialized_header = vec![1, 2, 3];
let version = 1;
let wrapped = wrap_serialized_header(version, serialized_header.clone());
let unwrapped = unwrap_serialized_header(wrapped).unwrap();

assert_eq!(unwrapped.0, version);
assert_eq!(unwrapped.1, serialized_header);
}

#[test]
fn test_header_deserialize_factory() {
let header = HeaderV1 {
sender_address: [1; 32],
uuid: Uuid::new_v4(),
message_type: 42,
message_length: 100,
authorization_data: vec![1, 2, 3],
};

let serialized = header.serialize();
let wrapped = wrap_serialized_header(1, serialized.clone());
let unwrapped = unwrap_serialized_header(wrapped).unwrap();
let deserialized = header_deserialize_factory(unwrapped.0, unwrapped.1).unwrap();

match deserialized {
Header::V1(deserialized_header) => {
assert_eq!(header.sender_address, deserialized_header.sender_address);
assert_eq!(header.uuid, deserialized_header.uuid);
assert_eq!(header.message_type, deserialized_header.message_type);
assert_eq!(header.message_length, deserialized_header.message_length);
assert_eq!(header.authorization_data, deserialized_header.authorization_data);
}
}
}
}
8 changes: 1 addition & 7 deletions shardus_net/src/headers/header_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ pub enum Header {
}

impl Header {
pub fn validate(&self, message: Vec<u8>) -> bool {
match self {
Header::V1(header_v1) => header_v1.validate(message),
}
}

pub fn to_json_string(&self) -> Option<String> {
match self {
Header::V1(header_v1) => header_v1.to_json_string(),
Expand All @@ -21,4 +15,4 @@ impl Header {
pub struct WrappedHeader {
pub version: u8,
pub header_json_string: String,
}
}
54 changes: 6 additions & 48 deletions shardus_net/src/headers/header_v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@ use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct HeaderV1 {
pub sender_address: [u8; 32],
pub uuid: Uuid,
pub message_type: u32,
pub message_length: u32,
pub authorization_data: Vec<u8>,
}

impl HeaderV1 {
// Serialize the struct into a Vec<u8>
pub fn serialize(&self) -> Vec<u8> {
let mut buffer = Vec::new();

// Serialize sender_address (32 bytes)
buffer.write_all(&self.sender_address).unwrap();

// Serialize uuid (16 bytes)
buffer.write_all(self.uuid.as_bytes()).unwrap();

Expand All @@ -31,23 +26,11 @@ impl HeaderV1 {
// Serialize message_length (4 bytes)
buffer.write_all(&self.message_length.to_le_bytes()).unwrap();

// Serialize the length of authorization_data (4 bytes)
buffer.write_all(&(self.authorization_data.len() as u32).to_le_bytes()).unwrap();

// Serialize authorization_data
buffer.write_all(&self.authorization_data).unwrap();

buffer
}

// Deserialize a Vec<u8> into a HeaderV1 struct
pub fn deserialize(data: Vec<u8>) -> Option<Self> {
let mut cursor = Cursor::new(data);

// Deserialize sender_address
let mut sender_address = [0u8; 32];
cursor.read_exact(&mut sender_address).ok()?;

// Deserialize a Vec<u8> cursor into a HeaderV1 struct
pub fn deserialize(cursor: &mut Cursor<Vec<u8>>) -> Option<Self> {
// Deserialize uuid
let mut uuid_bytes = [0u8; 16];
cursor.read_exact(&mut uuid_bytes).ok()?;
Expand All @@ -63,29 +46,7 @@ impl HeaderV1 {
cursor.read_exact(&mut message_length_bytes).ok()?;
let message_length = u32::from_le_bytes(message_length_bytes);

// Deserialize the length of authorization_data
let mut auth_data_length_bytes = [0u8; 4];
cursor.read_exact(&mut auth_data_length_bytes).ok()?;
let auth_data_length = u32::from_le_bytes(auth_data_length_bytes) as usize;

// Deserialize authorization_data
let mut authorization_data = vec![0u8; auth_data_length];
cursor.read_exact(&mut authorization_data).ok()?;

Some(Self {
sender_address,
uuid,
message_type,
message_length,
authorization_data,
})
}

pub fn validate(&self, message: Vec<u8>) -> bool {
if message.len() != self.message_length as usize {
return false;
}
true
Some(Self { uuid, message_type, message_length })
}

pub fn from_json_string(json_str: &str) -> Option<Self> {
Expand All @@ -105,20 +66,17 @@ mod tests {
#[test]
fn test_serialize_deserialize_header_v1() {
let header = HeaderV1 {
sender_address: [1; 32],
uuid: Uuid::new_v4(),
message_type: 42,
message_length: 100,
authorization_data: vec![1, 2, 3],
};

let serialized = header.serialize();
let deserialized = HeaderV1::deserialize(serialized).unwrap();
let serialized = header.serialize(); // make sure your serialize function now works with the new HeaderV1
let mut cursor = Cursor::new(serialized);
let deserialized = HeaderV1::deserialize(&mut cursor).unwrap(); // make sure your deserialize function now works with the new HeaderV1

assert_eq!(header.sender_address, deserialized.sender_address);
assert_eq!(header.uuid, deserialized.uuid);
assert_eq!(header.message_type, deserialized.message_type);
assert_eq!(header.message_length, deserialized.message_length);
assert_eq!(header.authorization_data, deserialized.authorization_data);
}
}
8 changes: 5 additions & 3 deletions shardus_net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ fn send(mut cx: FunctionContext) -> JsResult<JsUndefined> {

pub fn send_with_headers(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let cx = &mut cx;
let host: String = cx.argument::<JsString>(0)?.value(cx) as String;
let port: u16 = cx.argument::<JsNumber>(1)?.value(cx) as u16;
let port: u16 = cx.argument::<JsNumber>(0)?.value(cx) as u16;
let host: String = cx.argument::<JsString>(1)?.value(cx) as String;
let header_version: u8 = cx.argument::<JsNumber>(2)?.value(cx) as u8;
let header_js_string: String = cx.argument::<JsString>(3)?.value(cx) as String;
let data_js_string: String = cx.argument::<JsString>(4)?.value(cx) as String;
let complete_cb = cx.argument::<JsFunction>(6)?.root(cx);
let complete_cb = cx.argument::<JsFunction>(5)?.root(cx);

let shardus_net_sender = cx.this().get(cx, "_sender")?.downcast_or_throw::<JsBox<Arc<ShardusNetSender>>, _>(cx)?;
let stats_incrementers = cx.this().get(cx, "_stats_incrementers")?.downcast_or_throw::<JsBox<Incrementers>, _>(cx)?;
Expand All @@ -207,6 +207,8 @@ pub fn send_with_headers(mut cx: FunctionContext) -> JsResult<JsUndefined> {

stats_incrementers.increment_outstanding_sends();

println!("header_js_string: {}", header_js_string);

let header = match header_from_json_string(&header_js_string, &header_version) {
Some(header) => header,
None => {
Expand Down
12 changes: 5 additions & 7 deletions shardus_net/src/shardus_net_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,12 @@ impl ShardusNetListener {
let header_version = buffer[1];
let msg_bytes = &buffer[2..];

let cursor = Cursor::new(msg_bytes);
let header = header_deserialize_factory(header_version, cursor.get_ref().to_vec()).expect("Failed to deserialize header");
let mut cursor = Cursor::new(msg_bytes.to_vec());
let header = header_deserialize_factory(header_version, &mut cursor).expect("Failed to deserialize header");
let header_length = cursor.position() as usize;
println!("header: {}", header.to_json_string().expect("Failed to serialize header"));

let remaining_msg_bytes = &msg_bytes[header_length..];
if header.validate(remaining_msg_bytes.to_vec()) == false {
error!("Failed to validate header");
continue;
}
let remaining_msg_bytes = &msg_bytes[( header_length)..];

let wrapped_header = WrappedHeader {
version: header_version,
Expand All @@ -129,6 +126,7 @@ impl ShardusNetListener {

// deserialize remaining bytes as your message
let msg = String::from_utf8(remaining_msg_bytes.to_vec())?;
println!("msg: {}", msg);
received_msg_tx.send((msg, remote_addr, Some(wrapped_header))).map_err(|_| SendError(()))?;
} else {
// No header present
Expand Down
7 changes: 3 additions & 4 deletions src/headers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
export interface Headers {
[key: string]: string;
[key: string]: any;
}

export interface HeaderV1 {
senderAddress: Uint8Array; // Using Uint8Array for [u8; 32]
uuid: string; // Assuming we will convert Uuid to a string representation
uuid: string;
messageType: number;
messageLength: number;
authorizationData: Uint8Array; // Using Uint8Array for Vec<u8>
}
Loading

0 comments on commit fb0f035

Please sign in to comment.