Skip to content

Commit

Permalink
Merge pull request #2996 from autonomys/dsn-map-compact
Browse files Browse the repository at this point in the history
Serialize DSN mappings as a compact tuple
  • Loading branch information
teor2345 authored Sep 2, 2024
2 parents adc748d + dd777f2 commit 35e8b37
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
25 changes: 25 additions & 0 deletions crates/subspace-core-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ pub const BLAKE3_HASH_SIZE: usize = 32;
/// BLAKE3 hash output
pub type Blake3Hash = [u8; BLAKE3_HASH_SIZE];

/// BLAKE3 hash output wrapper, which serializes it as a hex string
// TODO: rename this type to Blake3Hash into a newtype, after checking for any breaking changes
#[derive(
Debug,
Default,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Hash,
From,
Into,
Deref,
DerefMut,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Blake3HashHex(#[cfg_attr(feature = "serde", serde(with = "hex"))] Blake3Hash);

/// Type of randomness.
#[derive(
Debug,
Expand Down
30 changes: 26 additions & 4 deletions crates/subspace-core-primitives/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

use crate::{Blake3Hash, PieceIndex};
use crate::{Blake3Hash, Blake3HashHex, PieceIndex};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::default::Default;
Expand Down Expand Up @@ -130,18 +130,36 @@ pub struct PieceObjectMapping {
/// Object stored in the history of the blockchain
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "serde",
serde(from = "CompactGlobalObject", into = "CompactGlobalObject")
)]
pub struct GlobalObject {
/// Object hash.
/// We order by hash, so object hash lookups can be performed efficiently.
#[cfg_attr(feature = "serde", serde(with = "hex"))]
pub hash: Blake3Hash,
/// Piece index where object is contained (at least its beginning, might not fit fully)
pub piece_index: PieceIndex,
/// Raw record offset of the object in that piece, for use with `Record::to_raw_record_bytes`
pub offset: u32,
}

impl From<CompactGlobalObject> for GlobalObject {
fn from(object: CompactGlobalObject) -> Self {
Self {
hash: *object.0,
piece_index: object.1,
offset: object.2,
}
}
}

impl From<GlobalObject> for CompactGlobalObject {
fn from(object: GlobalObject) -> CompactGlobalObject {
Self(object.hash.into(), object.piece_index, object.offset)
}
}

impl GlobalObject {
/// Returns a newly created GlobalObject from a piece index and object.
pub fn new(piece_index: PieceIndex, piece_object: &PieceObject) -> Self {
Expand All @@ -153,6 +171,11 @@ impl GlobalObject {
}
}

/// Space-saving serialization of an object stored in the history of the blockchain
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompactGlobalObject(Blake3HashHex, PieceIndex, u32);

/// Mapping of objects stored in the history of the blockchain
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand All @@ -163,7 +186,6 @@ pub enum GlobalObjectMapping {
#[codec(index = 0)]
V0 {
/// Objects stored in the history of the blockchain.
/// Mappings are ordered by the piece index and offset of the first GlobalObject in objects.
objects: Vec<GlobalObject>,
},
}
Expand Down

0 comments on commit 35e8b37

Please sign in to comment.