Skip to content

Commit

Permalink
feat(su): boot loader fix test, remove dependency #730
Browse files Browse the repository at this point in the history
  • Loading branch information
VinceJuliano committed Aug 21, 2024
1 parent ad0b644 commit 7027209
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 875 deletions.
895 changes: 28 additions & 867 deletions servers/su/Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion servers/su/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ default-run = "su"
[dependencies]
actix-web = "4"
async-trait = "0.1.74"
bundlr-sdk = "0.5.0"
reqwest = "0.11.22"
serde = "1.0.188"
serde_json = "1.0.107"
Expand All @@ -34,6 +33,8 @@ futures = "0.3.30"
rocksdb = "0.22.0"
actix-web-prom = { version = "0.8.0", features = ["process"] }
prometheus = { version = "0.13.4", features = ["process"] }
lazy_static = "1.5.0"
avro-rs = "0.13.0"

[[bin]]
name = "su"
Expand Down
2 changes: 1 addition & 1 deletion servers/su/src/domain/core/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use bundlr_sdk::tags::Tag;
use super::tags::Tag;

use super::bytes::{ByteErrorType, DataBundle, DataItem};
use super::dal::{Gateway, Log, ScheduleProvider, Signer, TxStatus};
Expand Down
8 changes: 4 additions & 4 deletions servers/su/src/domain/core/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::clone::Clone;

use bytes::{BufMut, Bytes};

use bundlr_sdk::{error::BundlrError, tags::*};
use super::tags::*;

use base64_url;
use sha2::{Digest, Sha256, Sha384};
Expand All @@ -14,9 +14,9 @@ pub enum ByteErrorType {
ByteError(String),
}

impl From<BundlrError> for ByteErrorType {
fn from(error: BundlrError) -> Self {
ByteErrorType::ByteError(format!("Byte error: {}", error))
impl From<TagError> for ByteErrorType {
fn from(error: TagError) -> Self {
ByteErrorType::ByteError(format!("Byte error: {:?}", error))
}
}

Expand Down
9 changes: 7 additions & 2 deletions servers/su/src/domain/core/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use super::bytes::{ByteErrorType, DataBundle, DataItem};
use bundlr_sdk::tags::*;
use super::tags::*;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum JsonErrorType {
Expand Down Expand Up @@ -803,15 +803,20 @@ mod tests {
#[test]
fn test_from_bundle() {
let d_item_string = PROCESS_ITEM_STR.to_string();
let a_d_item_string = ASSIGNMENT_ITEM_STR.to_string();
let item_bytes = base64_url::decode(&d_item_string).expect("failed to encode data item");
let assignment_item_bytes =
base64_url::decode(&a_d_item_string).expect("failed to encode data item");
let data_item = DataItem::from_bytes(item_bytes).expect("failed to build data item");
let assignment_data_item =
DataItem::from_bytes(assignment_item_bytes).expect("failed to build data item");
let tags = vec![
Tag::new(&"Bundle-Format".to_string(), &"binary".to_string()),
Tag::new(&"Bundle-Version".to_string(), &"2.0.0".to_string()),
Tag::new(&"Block-Height".to_string(), &"100".to_string()),
Tag::new(&"Timestamp".to_string(), &"100".to_string()),
];
let mut data_bundle = DataBundle::new(tags);
data_bundle.add_item(assignment_data_item);
data_bundle.add_item(data_item);
let process = Process::from_bundle(&data_bundle).expect("failed to create process");
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions servers/su/src/domain/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod bytes;
mod builder;
// build json from raw data
mod json;
mod tags;

// traits for injecting dependencies
pub mod dal;
Expand Down
77 changes: 77 additions & 0 deletions servers/su/src/domain/core/tags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use avro_rs::{from_avro_datum, to_avro_datum, Schema};
use bytes::Bytes;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};

#[derive(Debug)]
pub enum TagError {
NoBytesLeft,
InvalidTagEncoding,
}


#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Tag {
pub name: String,
pub value: String,
}

impl Tag {
pub fn new(name: &str, value: &str) -> Self {
Tag {
name: name.to_string(),
value: value.to_string(),
}
}
}

const SCHEMA_STR: &str = r#"{
"type": "array",
"items": {
"type": "record",
"name": "Tag",
"fields": [
{ "name": "name", "type": "string" },
{ "name": "value", "type": "string" }
]
}
}"#;

lazy_static! {
pub static ref TAGS_SCHEMA: Schema = Schema::parse_str(SCHEMA_STR).unwrap();
}

// const TAGS_READER: Reader<'static, Vec<Tag>> = Reader::with_schema(&TAGS_SCHEMA, Vec::<Tag>::new());
// const TAGS_WRITER: Writer<'static, Vec<Tag>> = Writer::new(&TAGS_SCHEMA, Vec::new());

pub trait AvroEncode {
fn encode(&self) -> Result<Bytes, TagError>;
}

pub trait AvroDecode {
fn decode(&mut self) -> Result<Vec<Tag>, TagError>;
}

impl AvroEncode for Vec<Tag> {
fn encode(&self) -> Result<Bytes, TagError> {
let v = avro_rs::to_value(self)?;
to_avro_datum(&TAGS_SCHEMA, v)
.map(|v| v.into())
.map_err(|_| TagError::NoBytesLeft)
}
}

impl AvroDecode for &mut [u8] {
fn decode(&mut self) -> Result<Vec<Tag>, TagError> {
let x = self.to_vec();
let v = from_avro_datum(&TAGS_SCHEMA, &mut x.as_slice(), Some(&TAGS_SCHEMA))
.map_err(|_| TagError::InvalidTagEncoding)?;
avro_rs::from_value(&v).map_err(|_| TagError::InvalidTagEncoding)
}
}

impl From<avro_rs::DeError> for TagError {
fn from(_: avro_rs::DeError) -> Self {
TagError::InvalidTagEncoding
}
}
Binary file modified servers/su/su
Binary file not shown.

0 comments on commit 7027209

Please sign in to comment.