Skip to content

Commit 60c58c3

Browse files
committed
chore(common): enhance readability of 'RegisterSignatureMessageDmq' bytes decoder
1 parent cd38598 commit 60c58c3

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

mithril-common/src/messages/register_signature.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,31 +116,33 @@ impl RegisterSignatureMessageDmq {
116116

117117
/// Extract a `RegisterSignatureMessageDmq` from bytes.
118118
pub fn try_from_bytes_vec(bytes: &[u8]) -> StdResult<Self> {
119+
const SIGNED_ENTITY_TYPE_LENGTH_BYTES_SIZE: usize = 2;
120+
const SIGNATURE_LENGTH_BYTES_SIZE: usize = 4;
119121
let mut bytes_index = 0;
120122

121-
let mut u16_bytes = [0u8; 2];
123+
let mut u16_bytes = [0u8; SIGNED_ENTITY_TYPE_LENGTH_BYTES_SIZE];
122124
u16_bytes.copy_from_slice(
123125
bytes
124-
.get(bytes_index..bytes_index + 2)
126+
.get(bytes_index..bytes_index + SIGNED_ENTITY_TYPE_LENGTH_BYTES_SIZE)
125127
.ok_or(anyhow!("Failed to read `Signed entity type length` bytes"))?,
126128
);
127129
let signed_entity_bytes_length = u16::from_be_bytes(u16_bytes) as usize;
128-
bytes_index += 2;
130+
bytes_index += SIGNED_ENTITY_TYPE_LENGTH_BYTES_SIZE;
129131

130132
let signed_entity_bytes = bytes
131133
.get(bytes_index..bytes_index + signed_entity_bytes_length)
132134
.ok_or(anyhow!("Failed to read `Signed entity type` bytes"))?;
133135
let signed_entity_type = SignedEntityType::try_from_bytes(signed_entity_bytes)?;
134136
bytes_index += signed_entity_bytes_length;
135137

136-
let mut u32_bytes = [0u8; 4];
138+
let mut u32_bytes = [0u8; SIGNATURE_LENGTH_BYTES_SIZE];
137139
u32_bytes.copy_from_slice(
138140
bytes
139-
.get(bytes_index..bytes_index + 4)
141+
.get(bytes_index..bytes_index + SIGNATURE_LENGTH_BYTES_SIZE)
140142
.ok_or(anyhow!("Failed to read `Signature length` bytes"))?,
141143
);
142144
let signature_bytes_length = u32::from_be_bytes(u32_bytes) as usize;
143-
bytes_index += 4;
145+
bytes_index += SIGNATURE_LENGTH_BYTES_SIZE;
144146

145147
let signature_bytes = bytes
146148
.get(bytes_index..bytes_index + signature_bytes_length)

0 commit comments

Comments
 (0)