Skip to content

Commit

Permalink
Update sv2-tp-crates
Browse files Browse the repository at this point in the history
  • Loading branch information
UnidenifiedUser committed Jul 12, 2022
1 parent c41b561 commit 5b132bd
Show file tree
Hide file tree
Showing 29 changed files with 236 additions and 207 deletions.
30 changes: 3 additions & 27 deletions protocols/v2/binary-sv2/binary-sv2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ pub fn clone_message<T: Serialize>(_: T) -> T {

#[cfg(not(feature = "with_serde"))]
pub fn u256_from_int<V: Into<u64>>(value: V) -> U256<'static> {
// initialize u256 as a bytes vec of len 24
let mut u256 = vec![0_u8; 24];
let val: u64 = value.into();
for v in &(val.to_le_bytes()) {
// add 8 bytes to u256
u256.push(*v)
}
// Always safe cause u256 is 24 + 8 (32) bytes
let u256: U256 = u256.try_into().unwrap();
u256
}
Expand All @@ -31,33 +34,6 @@ pub fn u256_from_int<V: Into<u64>>(value: V) -> U256<'static> {
mod test {
use super::*;

mod test_bytes {
use super::*;
use core::convert::TryInto;

#[derive(Deserialize, Serialize, PartialEq, Debug, Clone)]
struct Test<'decoder> {
#[cfg_attr(feature = "with_serde", serde(borrow))]
a: Bytes<'decoder>,
}

#[test]
fn test_struct() {
let mut bytes = [98; 890];
let a: Bytes = (&mut bytes[..]).try_into().unwrap();
let expected = Test { a };

#[cfg(not(feature = "with_serde"))]
let mut bytes = to_bytes(expected.clone()).unwrap();
#[cfg(feature = "with_serde")]
let mut bytes = to_bytes(&expected.clone()).unwrap();

let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();

assert_eq!(deserialized, expected);
}
}

mod test_struct {
use super::*;
use core::convert::TryInto;
Expand Down
40 changes: 26 additions & 14 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/decodable.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::{
codec::{GetSize, SizeHint},
datatypes::{Bytes, Signature, Sv2DataType, B016M, B0255, B032, B064K, U24, U256},
datatypes::{Signature, Sv2DataType, U32AsRef, B016M, B0255, B032, B064K, U24, U256},
Error,
};
use alloc::vec::Vec;
use std::convert::TryFrom;
#[cfg(not(feature = "no_std"))]
use std::io::{Cursor, Read};

Expand Down Expand Up @@ -55,13 +56,13 @@ pub enum PrimitiveMarker {
U256,
Signature,
U32,
U32AsRef,
F32,
U64,
B032,
B0255,
B064K,
B016M,
Bytes,
}

/// Passed to a decoder to define the structure of the data to be decoded
Expand All @@ -84,13 +85,13 @@ pub enum DecodablePrimitive<'a> {
U256(U256<'a>),
Signature(Signature<'a>),
U32(u32),
U32AsRef(U32AsRef<'a>),
F32(f32),
U64(u64),
B032(B032<'a>),
B0255(B0255<'a>),
B064K(B064K<'a>),
B016M(B016M<'a>),
Bytes(Bytes<'a>),
}

/// Used to contrustuct messages is returned by the decoder
Expand All @@ -101,6 +102,7 @@ pub enum DecodableField<'a> {
}

impl SizeHint for PrimitiveMarker {
// PrimitiveMarker need introspection to return a size hint. This method is not implementeable
fn size_hint(_data: &[u8], _offset: usize) -> Result<usize, Error> {
unimplemented!()
}
Expand All @@ -114,18 +116,19 @@ impl SizeHint for PrimitiveMarker {
Self::U256 => U256::size_hint(data, offset),
Self::Signature => Signature::size_hint(data, offset),
Self::U32 => u32::size_hint(data, offset),
Self::U32AsRef => U32AsRef::size_hint(data, offset),
Self::F32 => f32::size_hint(data, offset),
Self::U64 => u64::size_hint(data, offset),
Self::B032 => B032::size_hint(data, offset),
Self::B0255 => B0255::size_hint(data, offset),
Self::B064K => B064K::size_hint(data, offset),
Self::B016M => B016M::size_hint(data, offset),
Self::Bytes => Bytes::size_hint(data, offset),
}
}
}

impl SizeHint for FieldMarker {
// FieldMarker need introspection to return a size hint. This method is not implementeable
fn size_hint(_data: &[u8], _offset: usize) -> Result<usize, Error> {
unimplemented!()
}
Expand All @@ -145,6 +148,7 @@ impl SizeHint for FieldMarker {
}

impl SizeHint for Vec<FieldMarker> {
// FieldMarker need introspection to return a size hint. This method is not implementeable
fn size_hint(_data: &[u8], _offset: usize) -> Result<usize, Error> {
unimplemented!()
}
Expand All @@ -165,12 +169,18 @@ impl From<PrimitiveMarker> for FieldMarker {
}
}

impl From<Vec<FieldMarker>> for FieldMarker {
fn from(mut v: Vec<FieldMarker>) -> Self {
impl TryFrom<Vec<FieldMarker>> for FieldMarker {
type Error = crate::Error;

fn try_from(mut v: Vec<FieldMarker>) -> Result<Self, crate::Error> {
match v.len() {
0 => panic!("TODO"),
1 => v.pop().unwrap(),
_ => FieldMarker::Struct(v),
// It shouldn't be possible to call this function with a void Vec but for safety
// reasons it is implemented with TryFrom and not From if needed should be possible
// to use From and just panic
0 => Err(crate::Error::VoidFieldMarker),
// This is always safe: if v.len is 1 pop can not fail
1 => Ok(v.pop().unwrap()),
_ => Ok(FieldMarker::Struct(v)),
}
}
}
Expand All @@ -196,6 +206,9 @@ impl PrimitiveMarker {
DecodablePrimitive::Signature(Signature::from_bytes_unchecked(&mut data[offset..]))
}
Self::U32 => DecodablePrimitive::U32(u32::from_bytes_unchecked(&mut data[offset..])),
Self::U32AsRef => {
DecodablePrimitive::U32AsRef(U32AsRef::from_bytes_unchecked(&mut data[offset..]))
}
Self::F32 => DecodablePrimitive::F32(f32::from_bytes_unchecked(&mut data[offset..])),
Self::U64 => DecodablePrimitive::U64(u64::from_bytes_unchecked(&mut data[offset..])),
Self::B032 => DecodablePrimitive::B032(B032::from_bytes_unchecked(&mut data[offset..])),
Expand All @@ -208,9 +221,6 @@ impl PrimitiveMarker {
Self::B016M => {
DecodablePrimitive::B016M(B016M::from_bytes_unchecked(&mut data[offset..]))
}
Self::Bytes => {
DecodablePrimitive::Bytes(Bytes::from_bytes_unchecked(&mut data[offset..]))
}
}
}

Expand All @@ -226,13 +236,15 @@ impl PrimitiveMarker {
reader,
)?)),
Self::U32 => Ok(DecodablePrimitive::U32(u32::from_reader_(reader)?)),
Self::U32AsRef => Ok(DecodablePrimitive::U32AsRef(U32AsRef::from_reader_(
reader,
)?)),
Self::F32 => Ok(DecodablePrimitive::F32(f32::from_reader_(reader)?)),
Self::U64 => Ok(DecodablePrimitive::U64(u64::from_reader_(reader)?)),
Self::B032 => Ok(DecodablePrimitive::B032(B032::from_reader_(reader)?)),
Self::B0255 => Ok(DecodablePrimitive::B0255(B0255::from_reader_(reader)?)),
Self::B064K => Ok(DecodablePrimitive::B064K(B064K::from_reader_(reader)?)),
Self::B016M => Ok(DecodablePrimitive::B016M(B016M::from_reader_(reader)?)),
Self::Bytes => Ok(DecodablePrimitive::Bytes(Bytes::from_reader_(reader)?)),
}
}
}
Expand All @@ -247,13 +259,13 @@ impl<'a> GetSize for DecodablePrimitive<'a> {
DecodablePrimitive::U256(v) => v.get_size(),
DecodablePrimitive::Signature(v) => v.get_size(),
DecodablePrimitive::U32(v) => v.get_size(),
DecodablePrimitive::U32AsRef(v) => v.get_size(),
DecodablePrimitive::F32(v) => v.get_size(),
DecodablePrimitive::U64(v) => v.get_size(),
DecodablePrimitive::B032(v) => v.get_size(),
DecodablePrimitive::B0255(v) => v.get_size(),
DecodablePrimitive::B064K(v) => v.get_size(),
DecodablePrimitive::B016M(v) => v.get_size(),
DecodablePrimitive::Bytes(v) => v.get_size(),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
codec::GetSize,
datatypes::{Bytes, Signature, Sv2DataType, B016M, B0255, B032, B064K, U24, U256},
datatypes::{Signature, Sv2DataType, U32AsRef, B016M, B0255, B032, B064K, U24, U256},
Error,
};
use alloc::vec::Vec;
Expand Down Expand Up @@ -42,13 +42,13 @@ pub enum EncodablePrimitive<'a> {
U256(U256<'a>),
Signature(Signature<'a>),
U32(u32),
U32AsRef(U32AsRef<'a>),
F32(f32),
U64(u64),
B032(B032<'a>),
B0255(B0255<'a>),
B064K(B064K<'a>),
B016M(B016M<'a>),
Bytes(Bytes<'a>),
}

impl<'a> EncodablePrimitive<'a> {
Expand All @@ -62,13 +62,13 @@ impl<'a> EncodablePrimitive<'a> {
Self::U256(v) => v.to_slice(dst),
Self::Signature(v) => v.to_slice(dst),
Self::U32(v) => v.to_slice(dst),
Self::U32AsRef(v) => v.to_slice(dst),
Self::F32(v) => v.to_slice(dst),
Self::U64(v) => v.to_slice(dst),
Self::B032(v) => v.to_slice(dst),
Self::B0255(v) => v.to_slice(dst),
Self::B064K(v) => v.to_slice(dst),
Self::B016M(v) => v.to_slice(dst),
Self::Bytes(v) => v.to_slice(dst),
}
}

Expand All @@ -83,13 +83,13 @@ impl<'a> EncodablePrimitive<'a> {
Self::U256(v) => v.to_writer_(writer),
Self::Signature(v) => v.to_writer_(writer),
Self::U32(v) => v.to_writer_(writer),
Self::U32AsRef(v) => v.to_writer_(writer),
Self::F32(v) => v.to_writer_(writer),
Self::U64(v) => v.to_writer_(writer),
Self::B032(v) => v.to_writer_(writer),
Self::B0255(v) => v.to_writer_(writer),
Self::B064K(v) => v.to_writer_(writer),
Self::B016M(v) => v.to_writer_(writer),
Self::Bytes(v) => v.to_writer_(writer),
}
}
}
Expand All @@ -105,13 +105,13 @@ impl<'a> GetSize for EncodablePrimitive<'a> {
Self::U256(v) => v.get_size(),
Self::Signature(v) => v.get_size(),
Self::U32(v) => v.get_size(),
Self::U32AsRef(v) => v.get_size(),
Self::F32(v) => v.get_size(),
Self::U64(v) => v.get_size(),
Self::B032(v) => v.get_size(),
Self::B0255(v) => v.get_size(),
Self::B064K(v) => v.get_size(),
Self::B016M(v) => v.get_size(),
Self::Bytes(v) => v.get_size(),
}
}
}
Expand Down
Loading

0 comments on commit 5b132bd

Please sign in to comment.