Skip to content

Commit 8db8ae7

Browse files
committed
add consts
1 parent ca5ca2a commit 8db8ae7

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

rust/catalyst-voting/src/txs/v1/decoding.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,41 @@ use anyhow::{anyhow, bail, ensure};
77
use super::{EncryptedVote, PublicKey, Tx, Vote, VoterProof};
88
use crate::utils::{read_array, read_be_u32, read_be_u64, read_be_u8};
99

10+
/// Jörmungandr tx fragment tag.
11+
const FRAGMENT_TAG: u8 = 11;
12+
/// Jörmungandr tx input tag.
13+
const INPUT_TAG: u8 = 0xFF;
14+
/// Jörmungandr tx number of inputs.
15+
const NUMBER_OF_INPUTS: u8 = 1;
16+
/// Jörmungandr tx number of outputs.
17+
const NUMBER_OF_OUTPUTS: u8 = 0;
18+
/// Jörmungandr tx padding tag.
19+
const PADDING_TAG: u8 = 0;
20+
/// Jörmungandr tx private vote tag.
21+
const PRIVATE_VOTE_TAG: u8 = 2;
22+
/// Jörmungandr tx public vote tag.
23+
const PUBLIC_VOTE_TAG: u8 = 1;
24+
1025
impl Tx {
1126
/// Convert this `Tx` to its underlying sequence of bytes.
1227
#[must_use]
1328
#[allow(clippy::cast_possible_truncation)]
1429
pub fn to_bytes(&self) -> Vec<u8> {
1530
// Initialize already with the padding tag `0` and fragment tag `11`.
16-
let mut tx_body = vec![0, 11];
31+
let mut tx_body = vec![PADDING_TAG, FRAGMENT_TAG];
1732

1833
tx_body.extend_from_slice(&self.vote_plan_id);
1934
tx_body.push(self.proposal_index);
2035

2136
match &self.vote {
2237
Vote::Public(vote) => {
2338
// Public vote tag
24-
tx_body.push(1);
39+
tx_body.push(PUBLIC_VOTE_TAG);
2540
tx_body.push(*vote);
2641
},
2742
Vote::Private(vote, proof) => {
2843
// Private vote tag
29-
tx_body.push(2);
44+
tx_body.push(PRIVATE_VOTE_TAG);
3045
tx_body.push(vote.size() as u8);
3146
tx_body.extend_from_slice(&vote.to_bytes());
3247

@@ -38,11 +53,11 @@ impl Tx {
3853
// Zeros block date
3954
tx_body.extend_from_slice(&[0u8; 8]);
4055
// Number of inputs
41-
tx_body.push(1);
56+
tx_body.push(NUMBER_OF_INPUTS);
4257
// Number of outputs
43-
tx_body.push(0);
58+
tx_body.push(NUMBER_OF_OUTPUTS);
4459
// Input tag
45-
tx_body.push(0xFF);
60+
tx_body.push(INPUT_TAG);
4661
// Zero value
4762
tx_body.extend_from_slice(&[0u8; 8]);
4863

@@ -70,14 +85,14 @@ impl Tx {
7085

7186
let padding_tag = read_be_u8(reader)?;
7287
ensure!(
73-
padding_tag == 0,
74-
"Invalid padding tag field value, must be equals to `0`, provided: {padding_tag}.",
88+
padding_tag == PADDING_TAG,
89+
"Invalid padding tag field value, must be equals to {PADDING_TAG}, provided: {padding_tag}.",
7590
);
7691

7792
let fragment_tag = read_be_u8(reader)?;
7893
ensure!(
79-
fragment_tag == 11,
80-
"Invalid fragment tag field value, must be equals to `11`, provided: {fragment_tag}.",
94+
fragment_tag == FRAGMENT_TAG,
95+
"Invalid fragment tag field value, must be equals to {FRAGMENT_TAG}, provided: {fragment_tag}.",
8196
);
8297

8398
let vote_plan_id = read_array(reader)?;
@@ -86,11 +101,11 @@ impl Tx {
86101

87102
let vote_tag = read_be_u8(reader)?;
88103
let vote = match vote_tag {
89-
1 => {
104+
PUBLIC_VOTE_TAG => {
90105
let vote = read_be_u8(reader)?;
91106
Vote::Public(vote)
92107
},
93-
2 => {
108+
PRIVATE_VOTE_TAG => {
94109
let size = read_be_u8(reader)?;
95110
let vote = EncryptedVote::from_bytes(reader, size.into())
96111
.map_err(|e| anyhow!("Invalid encrypted vote, error: {e}."))?;
@@ -101,28 +116,32 @@ impl Tx {
101116

102117
Vote::Private(vote, proof)
103118
},
104-
tag => bail!("Invalid vote tag value, must be equals to `0` or `1`, provided: {tag}"),
119+
tag => {
120+
bail!(
121+
"Invalid vote tag value, must be equals to {PUBLIC_VOTE_TAG} or {PRIVATE_VOTE_TAG}, provided: {tag}"
122+
)
123+
},
105124
};
106125

107126
// skip block date (epoch and slot)
108127
read_be_u64(reader)?;
109128

110129
let inputs_amount = read_be_u8(reader)?;
111130
ensure!(
112-
inputs_amount == 1,
113-
"Invalid number of inputs, expected: `1`, provided: {inputs_amount}",
131+
inputs_amount == NUMBER_OF_INPUTS,
132+
"Invalid number of inputs, expected: {NUMBER_OF_INPUTS}, provided: {inputs_amount}",
114133
);
115134

116135
let outputs_amount = read_be_u8(reader)?;
117136
ensure!(
118-
outputs_amount == 0,
119-
"Invalid number of outputs, expected: `0`, provided: {outputs_amount}",
137+
outputs_amount == NUMBER_OF_OUTPUTS,
138+
"Invalid number of outputs, expected: {NUMBER_OF_OUTPUTS}, provided: {outputs_amount}",
120139
);
121140

122141
let input_tag = read_be_u8(reader)?;
123142
ensure!(
124-
input_tag == 0xFF,
125-
"Invalid input tag, expected: `255`, provided: {input_tag}",
143+
input_tag == INPUT_TAG,
144+
"Invalid input tag, expected: {INPUT_TAG}, provided: {input_tag}",
126145
);
127146

128147
// skip value

0 commit comments

Comments
 (0)