Skip to content
This repository has been archived by the owner on Jun 25, 2019. It is now read-only.

Commit

Permalink
Start adding the networking (abstracted from the transport)
Browse files Browse the repository at this point in the history
  • Loading branch information
chritchens committed Feb 20, 2018
1 parent b54fdf3 commit 9a9ce6e
Show file tree
Hide file tree
Showing 31 changed files with 2,993 additions and 61 deletions.
3 changes: 3 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ pub const DEFAULT_BASE_FEE: f32 = 0.0;
/// The default base difficulty per connection.
pub const DEFAULT_BASE_DIFFICULTY: u32 = 0;

/// The maximum number of bytes per chunk in a network stream.
pub const MAX_CHUNK_SIZE: u32 = 1<<9;

/// The mainnet witness.
pub const MAINWITNESS: &str = "1e9f288451e2beb8b5c7ae598c4ca0cfe88722a8d0c44b5ff1d42c6fde17b7f6";

Expand Down
16 changes: 14 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,22 @@ pub enum ErrorKind {
UnknownMode,
#[fail(display="Invalid mode")]
InvalidMode,
#[fail(display="Unknown network")]
UnknownNetwork,
#[fail(display="Invalid session")]
InvalidSession,
#[fail(display="Invalid message")]
InvalidMessage,
#[fail(display="Invalid resource")]
InvalidResource,
#[fail(display="Unknown resource")]
UnknownResource,
#[fail(display="Invalid method")]
InvalidMethod,
#[fail(display="Unknown method")]
UnknownMethod,
#[fail(display="Invalid network")]
InvalidNetwork,
#[fail(display="Unknown network")]
UnknownNetwork,
#[fail(display="Invalid store")]
InvalidStore,
#[fail(display="Not enough space")]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ pub mod utils;
pub mod models;
pub mod store;
pub mod node;
//pub mod network;
pub mod network;
Empty file added src/network/client.rs
Empty file.
22 changes: 22 additions & 0 deletions src/network/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2018 Yobicash Ltd.
//
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>
// and the Apache 2.0 license <LICENSE-APACHE or https://opensource.org/licenses/Apache-2.0>.
// This file may not be copied, modified, or distributed except according to those
// terms.

//! The `handlers` module provides the Yobicash network handlers types and methods.

// pub mod ping;
// pub mod list;
// pub mod sample;
// pub mod get;
// pub mod lookup;
// pub mod put;

// pub use self::ping::*;
// pub use self::list::*;
// pub use self::sample::*;
// pub use self::get::*;
// pub use self::lookup::*;
// pub use self::put::*;
14 changes: 14 additions & 0 deletions src/network/message/get/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2018 Yobicash Ltd.
//
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>
// and the Apache 2.0 license <LICENSE-APACHE or https://opensource.org/licenses/Apache-2.0>.
// This file may not be copied, modified, or distributed except according to those
// terms.

//! The `get` module provides the Yobicash network get message types and methods.

pub mod request;
pub mod response;

use self::request::*;
use self::response::*;
159 changes: 159 additions & 0 deletions src/network/message/get/request.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2018 Yobicash Ltd.
//
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>
// and the Apache 2.0 license <LICENSE-APACHE or https://opensource.org/licenses/Apache-2.0>.
// This file may not be copied, modified, or distributed except according to those
// terms.

//! The `request` module provides the Yobicash network get request message type and methods.

use serde_json as json;
use rmp_serde as messagepack;
use hex;

use error::ErrorKind;
use result::Result;
use traits::{Validate, HexSerialize, Serialize};
use utils::{Version, NetworkType};
use crypto::Digest;
use crypto::HexSerialize as CryptoHexSerialize;
use network::session::Session;
use network::resource_type::ResourceType;

/// The request for getting a resource.
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
pub struct GetRequest {
/// Id of the session.
pub id: u32,
/// The version of the protocol.
pub version: Version,
/// The network type of the protocol.
pub network_type: NetworkType,
/// The maximum size of the `GetRequest` message.
pub max_size: u32,
/// The resource type.
pub resource_type: ResourceType,
/// The resoure id.
pub resource_id: Digest,
}

impl GetRequest {
/// Creates a new `GetRequest`.
pub fn new(session: &Session,
resource_type: ResourceType,
resource_id: Digest) -> Result<GetRequest> {
session.validate()?;

if session.max_size.is_none() {
return Err(ErrorKind::InvalidSession.into());
}

let max_size = session.max_size.unwrap();

let id = session.id;
let version = session.version.clone();
let network_type = session.network_type;

let get_request = GetRequest {
id: id,
version: version,
network_type: network_type,
max_size: max_size,
resource_type: resource_type,
resource_id: resource_id,
};

if get_request.to_bytes()?.len() as u32 > max_size {
return Err(ErrorKind::InvalidLength.into());
}

Ok(get_request)
}
}

impl Validate for GetRequest {
fn validate(&self) -> Result<()> {
self.version.validate()?;

if self.to_bytes()?.len() as u32 > self.max_size {
return Err(ErrorKind::InvalidLength.into());
}

Ok(())
}
}

impl<'a> Serialize<'a> for GetRequest {
fn to_json(&self) -> Result<String> {

let obj = json!({
"id": self.id,
"version": self.version.to_string(),
"network_type": self.network_type.to_hex()?,
"max_size": self.max_size,
"resource_type": self.resource_type.to_hex()?,
"resource_id": self.resource_id.to_hex()?,
});

let s = obj.to_string();

Ok(s)
}

fn from_json(s: &str) -> Result<Self> {
let obj: json::Value = json::from_str(s)?;

let id_value = obj["id"].clone();
let id: u32 = json::from_value(id_value)?;

let version_value = obj["version"].clone();
let version_str: String = json::from_value(version_value)?;
let version = Version::from_string(&version_str)?;

let network_type_value = obj["network_type"].clone();
let network_type_hex: String = json::from_value(network_type_value)?;
let network_type = NetworkType::from_hex(&network_type_hex)?;

let max_size_value = obj["max_size"].clone();
let max_size: u32 = json::from_value(max_size_value)?;

let resource_type_value = obj["resource_type"].clone();
let resource_type_hex: String = json::from_value(resource_type_value)?;
let resource_type = ResourceType::from_hex(&resource_type_hex)?;

let resource_id_value = obj["resource_id"].clone();
let resource_id_hex: String = json::from_value(resource_id_value)?;
let resource_id = Digest::from_hex(&resource_id_hex)?;

let get_request = GetRequest {
id: id,
version: version,
network_type: network_type,
max_size: max_size,
resource_type: resource_type,
resource_id: resource_id,
};

Ok(get_request)
}

fn to_bytes(&self) -> Result<Vec<u8>> {
let buf = messagepack::to_vec(self)?;

Ok(buf)
}

fn from_bytes(b: &[u8]) -> Result<Self> {
let get_request = messagepack::from_slice(b)?;

Ok(get_request)
}

fn to_hex(&self) -> Result<String> {
Ok(hex::encode(self.to_bytes()?))
}

fn from_hex(s: &str) -> Result<Self> {
Self::from_bytes(&hex::decode(s)?)
}
}
Loading

0 comments on commit 9a9ce6e

Please sign in to comment.