Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add priority canonical block #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ base64 = "0.13.0"


[features]
priority = ["common"]
client = ["attohttpc", "tungstenite", "anyhow", "common", "url"]
sms = ["smaz", "common"]
news = ["smaz", "common", "uuid"]
common = ["serde_bytes", "serde_cbor"]
default = ["sms", "client", "location", "cli", "news", "common"]
default = ["sms", "client", "location", "cli", "news", "common", "priority"]
location = ["derive-try-from-primitive", "common", "bitflags"]
cli = ["clap", "humantime", "client", "anyhow", "url"]

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ pub mod news;
#[cfg(feature = "location")]
pub mod location;

#[cfg(feature = "priority")]
pub mod priority;

pub mod serde;
81 changes: 81 additions & 0 deletions src/priority/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use bp7::canonical::{new_canonical_block, CanonicalBlockType, CanonicalData};
use bp7::{CanonicalBlock, EndpointID};
use derive_try_from_primitive::TryFromPrimitive;
use serde::de::{SeqAccess, Visitor};
use serde::ser::{SerializeSeq, Serializer};
use serde::{de, Deserialize, Deserializer, Serialize};
use std::convert::TryFrom;
use std::fmt;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum PriorityError {
#[error("serde cbor error: {0}")]
Cbor(#[from] serde_cbor::Error),
#[error("failed to create endpoint: {0}")]
EndpointIdInvalid(#[from] bp7::eid::EndpointIdError),
#[error("invalid endpoint supplied")]
InvalidEndpoint,
#[error("payload missing")]
PayloadMissing,
#[error("invalid priority block")]
InvalidPriorityBlock,
}

// HOP_COUNT_BLOCK is a BlockType for a Hop Count block as defined in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a comment describing the block a bit better?
don't follow my bad example :)

// section 4.3.3.
pub const PRIORITY_BLOCK: CanonicalBlockType = 224;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PriorityBlockData(u16);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a doc string describing the priority system would be nice. is lower better or higher?
it might make sense to have const's or enums for HIGHEST, LOWEST and MEDIUM priorities.
an even more convenient way for the API would be helpers to return a complete block, e.g., new_highest_priority_block, new_lowest_priority_block, etc.


pub fn new_priority_block(block_number: u64, data: PriorityBlockData) -> CanonicalBlock {
new_canonical_block(
PRIORITY_BLOCK,
block_number,
0,
CanonicalData::Unknown(serde_cbor::to_vec(&data).unwrap_or_default()),
)
}

pub fn get_priority_data(cblock: &CanonicalBlock) -> Result<PriorityBlockData, PriorityError> {
if cblock.block_type == PRIORITY_BLOCK {
if let CanonicalData::Unknown(data) = cblock.data() {
serde_cbor::from_slice(data).map_err(|_err| PriorityError::InvalidPriorityBlock)
} else {
Err(PriorityError::InvalidPriorityBlock)
}
} else {
Err(PriorityError::InvalidPriorityBlock)
}
}

#[cfg(test)]
mod tests {
use crate::priority::{
get_priority_data, new_priority_block, PriorityBlockData,
};
use bp7::bundle::Block;
use bp7::EndpointID;
use std::convert::TryFrom;

#[test]
fn test_priority_roundtrip() {
let data = PriorityBlockData(23);
let buf = serde_cbor::to_vec(&data).unwrap();
let data2 = serde_cbor::from_slice(&buf).unwrap();
assert_eq!(data, data2);
}

#[test]
fn test_cblock_priority_roundtrip() {
let data = PriorityBlockData(42);

let cblock = new_priority_block(1, data.clone());
let buf = cblock.to_cbor();
let cblock2 = serde_cbor::from_slice(&buf).unwrap();
assert_eq!(cblock, cblock2);
let data2 = get_priority_data(&cblock2).unwrap();
assert_eq!(data, data2);
}
}