|
1 | 1 | //! Helpers for decoding Taiko-specific block `extraData` fields. |
2 | 2 |
|
3 | | -use std::{error::Error, fmt::Display}; |
4 | | - |
5 | | -/// Index of the end-of-proposal flag in Shasta extra data. |
6 | | -pub const SHASTA_EXTRA_DATA_END_OF_PROPOSAL_INDEX: usize = 7; |
7 | | - |
8 | | -/// Exact number of bytes required for Shasta extra data. |
9 | | -pub const SHASTA_EXTRA_DATA_LEN: usize = 8; |
10 | | - |
11 | | -/// Error indicating that the Shasta extra data has an invalid length. |
12 | | -#[derive(Debug, Clone, PartialEq, Eq)] |
13 | | -pub struct ShastaExtraDataError { |
14 | | - /// The actual length of the provided extra data. |
15 | | - pub got: usize, |
16 | | - /// The expected length of the Shasta extra data. |
17 | | - pub expected: usize, |
18 | | -} |
19 | | - |
20 | | -impl Display for ShastaExtraDataError { |
21 | | - /// Formats the error message indicating the invalid length of Shasta extra data. |
22 | | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
23 | | - write!(f, "invalid Shasta extra data length: {} != {}", self.got, self.expected) |
24 | | - } |
25 | | -} |
26 | | - |
27 | | -/// Implements the standard Error trait for ShastaExtraDataError. |
28 | | -impl Error for ShastaExtraDataError {} |
| 3 | +/// Minimum number of bytes required for Shasta extra data. |
| 4 | +pub const SHASTA_EXTRA_DATA_LEN: usize = 7; |
29 | 5 |
|
30 | 6 | /// Returns the base fee sharing percentage encoded in Shasta extra data. |
31 | 7 | pub fn decode_shasta_basefee_sharing_pctg(extra: &[u8]) -> u8 { |
32 | 8 | extra.first().copied().unwrap_or_default() |
33 | 9 | } |
34 | 10 |
|
35 | | -/// Returns the proposal ID and end-of-proposal flag encoded in Shasta extra data. |
36 | | -pub fn decode_shasta_proposal_id(extra: &[u8]) -> Result<(u64, bool), ShastaExtraDataError> { |
37 | | - if extra.len() != SHASTA_EXTRA_DATA_LEN { |
38 | | - return Err(ShastaExtraDataError { got: extra.len(), expected: SHASTA_EXTRA_DATA_LEN }); |
| 11 | +/// Returns the proposal ID encoded in Shasta extra data (bytes 1..6, big-endian). |
| 12 | +pub fn decode_shasta_proposal_id(extra: &[u8]) -> Option<u64> { |
| 13 | + if extra.len() < SHASTA_EXTRA_DATA_LEN { |
| 14 | + return None; |
39 | 15 | } |
40 | 16 |
|
41 | 17 | let mut buf = [0u8; 8]; |
42 | 18 | buf[2..].copy_from_slice(&extra[1..7]); |
43 | | - let proposal_id = u64::from_be_bytes(buf); |
44 | | - let end_of_proposal = extra[SHASTA_EXTRA_DATA_END_OF_PROPOSAL_INDEX] != 0; |
45 | | - Ok((proposal_id, end_of_proposal)) |
46 | | -} |
47 | | - |
48 | | -#[cfg(test)] |
49 | | -mod tests { |
50 | | - use super::*; |
51 | | - |
52 | | - #[test] |
53 | | - fn decodes_shasta_proposal_id_and_end_of_proposal() { |
54 | | - let extra = [0x2a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x01]; |
55 | | - let (proposal_id, end_of_proposal) = decode_shasta_proposal_id(&extra).unwrap(); |
56 | | - assert_eq!(proposal_id, 0x010203040506); |
57 | | - assert!(end_of_proposal); |
58 | | - } |
59 | | - |
60 | | - #[test] |
61 | | - fn rejects_invalid_shasta_extra_data_len() { |
62 | | - let err = decode_shasta_proposal_id(&[0x01, 0x02, 0x03]).unwrap_err(); |
63 | | - assert_eq!(err.expected, SHASTA_EXTRA_DATA_LEN); |
64 | | - assert_eq!(err.got, 3); |
65 | | - } |
| 19 | + Some(u64::from_be_bytes(buf)) |
66 | 20 | } |
0 commit comments