Skip to content
Merged
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
24 changes: 20 additions & 4 deletions crates/aleph-types/src/message/store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::cid::Cid;
use crate::item_hash::ItemHash;
use crate::item_hash::{AlephItemHash, ItemHash};
use crate::storage_size::Bytes;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
Expand All @@ -8,13 +8,15 @@ use std::collections::HashMap;
#[serde(tag = "item_type", rename_all = "lowercase")]
pub enum StorageBackend {
Ipfs { item_hash: Cid },
Storage { item_hash: ItemHash },
Storage { item_hash: AlephItemHash },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct StoreContent {
#[serde(flatten)]
pub item_type: StorageBackend,
/// A combination of the `item_hash` and `item_type` fields, deserialized together to detect
/// inconsistencies. Use the `file_hash()` method to access the file hash.
file_hash: StorageBackend,
#[serde(default)]
/// Size of the file. Generated by CCNs upon processing.
pub size: Option<Bytes>,
Expand All @@ -26,11 +28,21 @@ pub struct StoreContent {
pub metadata: Option<HashMap<String, serde_json::Value>>,
}

impl StoreContent {
pub fn file_hash(&self) -> ItemHash {
match &self.file_hash {
StorageBackend::Ipfs { item_hash: cid } => ItemHash::Ipfs(cid.clone()),
StorageBackend::Storage { item_hash } => ItemHash::Native(*item_hash),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::chain::{Address, Chain, Signature};
use crate::channel::Channel;
use crate::item_hash;
use crate::message::base_message::{Message, MessageContentEnum};
use crate::message::{ContentSource, MessageType};
use crate::timestamp::Timestamp;
Expand Down Expand Up @@ -79,12 +91,16 @@ mod tests {
match message.content() {
MessageContentEnum::Store(store) => {
assert_eq!(
store.item_type,
store.file_hash,
StorageBackend::Ipfs {
item_hash: Cid::try_from("QmYULJoNGPDmoRq4WNWTDTUvJGJv1hosox8H6vVd1kCsY8")
.unwrap()
}
);
assert_eq!(
store.file_hash(),
item_hash!("QmYULJoNGPDmoRq4WNWTDTUvJGJv1hosox8H6vVd1kCsY8")
);

assert!(store.size.is_none());
assert!(store.content_type.is_none());
Expand Down