Skip to content

Commit 315e79d

Browse files
committed
extract const
Signed-off-by: bkioshn <bkioshn@gmail.com>
1 parent 6682c0a commit 315e79d

File tree

5 files changed

+45
-31
lines changed

5 files changed

+45
-31
lines changed

rust/hermes-ipfs/src/constant.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Define constant needed for IPFS and Document Sync
2+
3+
/// Current document synchronization protocol version.
4+
#[allow(dead_code)]
5+
pub(crate) const PROTOCOL_VERSION: u8 = 1;
6+
7+
/// `CID` version that Doc Sync supports.
8+
#[allow(dead_code)]
9+
pub(crate) const CID_VERSION: u8 = 1;
10+
11+
/// `CID` multihash digest size that Doc Sync supports.
12+
#[allow(dead_code)]
13+
pub(crate) const CID_DIGEST_SIZE: u8 = 32;
14+
15+
/// Multihash SHA256.
16+
#[allow(dead_code)]
17+
pub(crate) const MULTIHASH_SHA256: u8 = 0x12;
18+
19+
/// Codec CBOR.
20+
pub(crate) const CODEC_CBOR: u8 = 0x51;

rust/hermes-ipfs/src/doc_sync/envelope.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use catalyst_types::uuid::{self, CborContext, UuidV7};
1111
use ed25519_dalek::VerifyingKey;
1212
use minicbor::{Decode, Encode, Encoder, data::Type, encode::Write};
1313

14-
use crate::doc_sync::{PROTOCOL_VERSION, PublicKey, Signature};
14+
use crate::{
15+
constant::PROTOCOL_VERSION,
16+
doc_sync::{PublicKey, Signature},
17+
};
1518

1619
/// The unsigned portion of the message envelope.
1720
/// This structure corresponds to the **signature input** array:
@@ -47,7 +50,7 @@ impl EnvelopePayload {
4750
Ok(Self {
4851
peer: PublicKey(peer),
4952
seq: UuidV7::new(),
50-
ver: PROTOCOL_VERSION,
53+
ver: PROTOCOL_VERSION.into(),
5154
payload,
5255
})
5356
}
@@ -99,7 +102,7 @@ impl EnvelopePayload {
99102
let seq: UuidV7 = decoder.decode_with(&mut CborContext::Tagged)?;
100103
let ver = decoder.u64()?;
101104

102-
if ver != PROTOCOL_VERSION {
105+
if ver != PROTOCOL_VERSION.into() {
103106
return Err(minicbor::decode::Error::message(format!(
104107
"unsupported protocol version: {ver}"
105108
)));
@@ -169,7 +172,7 @@ impl<'b, C> Decode<'b, C> for EnvelopePayload {
169172
let seq: UuidV7 = d.decode_with(&mut CborContext::Tagged)?;
170173
let ver = d.u64()?;
171174

172-
if ver != PROTOCOL_VERSION {
175+
if ver != PROTOCOL_VERSION.into() {
173176
return Err(minicbor::decode::Error::message(format!(
174177
"unsupported protocol version: {ver}"
175178
)));
@@ -428,7 +431,7 @@ mod tests {
428431
signed
429432
.encode_with(payload.seq, &mut CborContext::Tagged)
430433
.unwrap();
431-
signed.u64(PROTOCOL_VERSION + 1).unwrap();
434+
signed.u64(PROTOCOL_VERSION.into() + 1).unwrap();
432435
<Vec<u8> as Write>::write_all(signed.writer_mut(), &payload.payload).unwrap();
433436
signed.encode(Signature(signature)).unwrap();
434437

@@ -512,7 +515,7 @@ mod tests {
512515
bad_array
513516
.encode_with(payload.seq, &mut CborContext::Tagged)
514517
.unwrap();
515-
bad_array.u64(PROTOCOL_VERSION).unwrap();
518+
bad_array.u64(PROTOCOL_VERSION.into()).unwrap();
516519
// Skip payload & signature to force length error or skip signature
517520

518521
let mut envelope = Encoder::new(Vec::new());

rust/hermes-ipfs/src/doc_sync/mod.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,13 @@ pub use envelope::{Envelope, EnvelopePayload};
1212
use minicbor::{Decode, Encode, Encoder, decode, encode::Write};
1313
pub use state_machine::{StateMachine, StateSnapshot, SyncState};
1414

15-
/// Current document synchronization protocol version.
16-
const PROTOCOL_VERSION: u64 = 1;
17-
18-
/// `CID` version that Doc Sync supports.
19-
const CID_VERSION: u8 = 1;
20-
21-
/// `CID` codec that Doc Sync supports (CBOR).
22-
const CID_CODEC: u8 = 0x51;
23-
24-
/// `CID` multihash code that Doc Sync supports (SHA256).
25-
const CID_MULTIHASH_CODE: u8 = 0x12;
26-
27-
/// `CID` multihash digest size that Doc Sync supports.
28-
const CID_DIGEST_SIZE: u8 = 32;
15+
use crate::constant::{CID_DIGEST_SIZE, CID_VERSION, CODEC_CBOR, MULTIHASH_SHA256};
2916

3017
/// Validates CID according to Doc Sync specification constraints.
3118
fn validate_cid(cid: &crate::Cid) -> bool {
3219
cid.version() as u8 == CID_VERSION
33-
&& cid.codec() == u64::from(CID_CODEC)
34-
&& cid.hash().code() == u64::from(CID_MULTIHASH_CODE)
20+
&& cid.codec() == u64::from(CODEC_CBOR)
21+
&& cid.hash().code() == u64::from(MULTIHASH_SHA256)
3522
&& cid.hash().digest().len() == usize::from(CID_DIGEST_SIZE)
3623
}
3724

rust/hermes-ipfs/src/doc_sync/payload.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,16 @@ mod tests {
544544
mod body {
545545
use minicbor::data::{Tag, Token};
546546

547+
use crate::constant::{CID_DIGEST_SIZE, CODEC_CBOR, MULTIHASH_SHA256, PROTOCOL_VERSION};
548+
547549
// Generates a valid Doc Sync `CID` (according to the spec).
548550
const fn generate_cid(seed: u8) -> [u8; 36] {
549-
const VERSION: u8 = 1;
550-
const CODEC_CBOR: u8 = 0x51;
551-
const CODE_SHA_256: u8 = 0x12;
552-
const DIGEST_32: u8 = 0x20;
553-
let prefix = [VERSION, CODEC_CBOR, CODE_SHA_256, DIGEST_32];
551+
let prefix = [
552+
PROTOCOL_VERSION,
553+
CODEC_CBOR,
554+
MULTIHASH_SHA256,
555+
CID_DIGEST_SIZE,
556+
];
554557
let mut ret = [seed; 36];
555558
ret.split_at_mut(prefix.len()).0.copy_from_slice(&prefix);
556559
ret

rust/hermes-ipfs/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//!
33
//! Provides support for storage, and `PubSub` functionality.
44
5+
pub(crate) mod constant;
6+
57
#[cfg(feature = "doc-sync")]
68
pub mod doc_sync;
79

@@ -36,6 +38,8 @@ use rust_ipfs::{
3638
dag::ResolveError, dummy, gossipsub::IntoGossipsubTopic,
3739
};
3840

41+
use crate::constant::CODEC_CBOR;
42+
3943
#[derive(Debug, Display, From, Into)]
4044
/// `PubSub` Message ID.
4145
pub struct MessageId(pub PubsubMessageId);
@@ -177,12 +181,9 @@ impl HermesIpfs {
177181
&self,
178182
data: Vec<u8>,
179183
) -> anyhow::Result<IpfsPath> {
180-
/// Codec for Hermes CID.
181-
const CODEC_CBOR: u64 = 0x51;
182-
183184
let cbor_data = minicbor::to_vec(data)
184185
.map_err(|e| anyhow::anyhow!("Failed to encode data to CBOR: {e:?}"))?;
185-
let cid = Cid::new_v1(CODEC_CBOR, Code::Sha2_256.digest(&cbor_data));
186+
let cid = Cid::new_v1(CODEC_CBOR.into(), Code::Sha2_256.digest(&cbor_data));
186187
let block = Block::new(cid, cbor_data)
187188
.map_err(|e| anyhow::anyhow!("Failed to create IPFS block: {e:?}"))?;
188189
let ipfs_path: IpfsPath = self.node.put_block(&block).await?.into();

0 commit comments

Comments
 (0)