ant_protocol/storage/chunks.rs
1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use super::ChunkAddress;
10use crate::NetworkAddress;
11use bytes::Bytes;
12use serde::{Deserialize, Deserializer, Serialize, Serializer};
13
14use xor_name::XorName;
15
16/// This is the max manually observed overhead when compressing random 4MB of data using Brotli.
17/// It might be possible there could be edge-cases where the overhead is even higher.
18const BROTLI_MAX_OVERHEAD_BYTES: usize = 16;
19
20/// When encrypting chunks, a bit of padding is added to make the size a multiple of 16.
21/// When the chunk size is already a multiple of 16, a full block padding will be added.
22const PKCS7_MAX_PADDING_BYTES: usize = 16;
23
24/// Chunk, an immutable chunk of data
25#[derive(Hash, Eq, PartialEq, PartialOrd, Ord, Clone, custom_debug::Debug)]
26pub struct Chunk {
27 /// Network address. Omitted when serialising and
28 /// calculated from the `value` when deserialising.
29 pub address: ChunkAddress,
30 /// Contained data.
31 #[debug(skip)]
32 pub value: Bytes,
33}
34
35impl Chunk {
36 /// The maximum size of an unencrypted/raw chunk is 4MB.
37 pub const MAX_RAW_SIZE: usize = 4 * 1024 * 1024;
38
39 /// The maximum size of an encrypted chunk is 4MB + 32 bytes.
40 /// + 16 bytes Brotli compression overhead for random data.
41 /// + 16 bytes due to Pkcs7 encryption padding.
42 pub const MAX_SIZE: usize =
43 Self::MAX_RAW_SIZE + BROTLI_MAX_OVERHEAD_BYTES + PKCS7_MAX_PADDING_BYTES;
44
45 /// Creates a new instance of `Chunk`.
46 pub fn new(value: Bytes) -> Self {
47 Self {
48 address: ChunkAddress::new(XorName::from_content(value.as_ref())),
49 value,
50 }
51 }
52
53 /// Returns the value.
54 pub fn value(&self) -> &Bytes {
55 &self.value
56 }
57
58 /// Returns the address.
59 pub fn address(&self) -> &ChunkAddress {
60 &self.address
61 }
62
63 /// Returns the NetworkAddress
64 pub fn network_address(&self) -> NetworkAddress {
65 NetworkAddress::ChunkAddress(self.address)
66 }
67
68 /// Returns the name.
69 pub fn name(&self) -> &XorName {
70 self.address.xorname()
71 }
72
73 /// Returns size of this chunk after serialisation.
74 pub fn size(&self) -> usize {
75 self.value.len()
76 }
77
78 /// Returns true if the chunk is too big
79 pub fn is_too_big(&self) -> bool {
80 self.size() > Self::MAX_SIZE
81 }
82}
83
84impl Serialize for Chunk {
85 fn serialize<S: Serializer>(&self, serialiser: S) -> Result<S::Ok, S::Error> {
86 // Address is omitted since it's derived from value
87 self.value.serialize(serialiser)
88 }
89}
90
91impl<'de> Deserialize<'de> for Chunk {
92 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
93 let value = Deserialize::deserialize(deserializer)?;
94 Ok(Self::new(value))
95 }
96}