Skip to content

Commit 6eff0c1

Browse files
committed
store field frames
1 parent 13ed4ce commit 6eff0c1

File tree

4 files changed

+62
-109
lines changed

4 files changed

+62
-109
lines changed

vote/src/vote_state_view.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,29 +189,29 @@ impl VoteStateFrame {
189189

190190
fn votes_frame(&self) -> VotesFrame {
191191
match &self {
192-
Self::V1_14_11(frame) => VotesFrame::Lockout(frame.votes_frame()),
193-
Self::V3(frame) => VotesFrame::Landed(frame.votes_frame()),
192+
Self::V1_14_11(frame) => VotesFrame::Lockout(frame.votes_frame),
193+
Self::V3(frame) => VotesFrame::Landed(frame.votes_frame),
194194
}
195195
}
196196

197197
fn root_slot_frame(&self) -> RootSlotFrame {
198198
match &self {
199-
Self::V1_14_11(vote_frame) => vote_frame.root_slot_frame(),
200-
Self::V3(vote_frame) => vote_frame.root_slot_frame(),
199+
Self::V1_14_11(vote_frame) => vote_frame.root_slot_frame,
200+
Self::V3(vote_frame) => vote_frame.root_slot_frame,
201201
}
202202
}
203203

204204
fn authorized_voters_frame(&self) -> AuthorizedVotersListFrame {
205205
match &self {
206-
Self::V1_14_11(frame) => frame.authorized_voters_frame(),
207-
Self::V3(frame) => frame.authorized_voters_frame(),
206+
Self::V1_14_11(frame) => frame.authorized_voters_frame,
207+
Self::V3(frame) => frame.authorized_voters_frame,
208208
}
209209
}
210210

211211
fn epoch_credits_frame(&self) -> EpochCreditsListFrame {
212212
match &self {
213-
Self::V1_14_11(frame) => frame.epoch_credits_frame(),
214-
Self::V3(frame) => frame.epoch_credits_frame(),
213+
Self::V1_14_11(frame) => frame.epoch_credits_frame,
214+
Self::V3(frame) => frame.epoch_credits_frame,
215215
}
216216
}
217217
}

vote/src/vote_state_view/field_frames.rs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use {
22
super::{list_view::ListView, Result, VoteStateViewError},
33
solana_clock::{Epoch, Slot},
44
solana_pubkey::Pubkey,
5-
solana_vote_interface::state::Lockout,
65
std::io::BufRead,
76
};
87

@@ -71,18 +70,17 @@ impl LockoutItem {
7170
}
7271
}
7372

73+
#[derive(Debug, Clone, Copy)]
74+
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
7475
pub(super) struct LockoutListFrame {
75-
len: usize,
76+
len: u8,
7677
}
7778

7879
impl LockoutListFrame {
79-
pub(super) const fn new(len: usize) -> Self {
80-
Self { len }
81-
}
82-
8380
pub(super) fn read(cursor: &mut std::io::Cursor<&[u8]>) -> Result<Self> {
8481
let len = solana_serialize_utils::cursor::read_u64(cursor)
8582
.map_err(|_err| VoteStateViewError::ParseError)? as usize;
83+
let len = u8::try_from(len).map_err(|_| VoteStateViewError::ParseError)?;
8684
let frame = Self { len };
8785
cursor.consume(frame.total_item_size());
8886
Ok(frame)
@@ -93,22 +91,21 @@ impl ListFrame<'_> for LockoutListFrame {
9391
type Item = LockoutItem;
9492

9593
fn len(&self) -> usize {
96-
self.len
94+
self.len as usize
9795
}
9896
}
9997

98+
#[derive(Debug, Clone, Copy)]
99+
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
100100
pub(super) struct LandedVotesListFrame {
101-
len: usize,
101+
len: u8,
102102
}
103103

104104
impl LandedVotesListFrame {
105-
pub(super) const fn new(len: usize) -> Self {
106-
Self { len }
107-
}
108-
109105
pub(super) fn read(cursor: &mut std::io::Cursor<&[u8]>) -> Result<Self> {
110106
let len = solana_serialize_utils::cursor::read_u64(cursor)
111107
.map_err(|_err| VoteStateViewError::ParseError)? as usize;
108+
let len = u8::try_from(len).map_err(|_| VoteStateViewError::ParseError)?;
112109
let frame = Self { len };
113110
cursor.consume(frame.total_item_size());
114111
Ok(frame)
@@ -126,7 +123,7 @@ impl ListFrame<'_> for LandedVotesListFrame {
126123
type Item = LockoutItem;
127124

128125
fn len(&self) -> usize {
129-
self.len
126+
self.len as usize
130127
}
131128

132129
fn item_size(&self) -> usize {
@@ -138,18 +135,17 @@ impl ListFrame<'_> for LandedVotesListFrame {
138135
}
139136
}
140137

138+
#[derive(Debug, Clone, Copy)]
139+
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
141140
pub(super) struct AuthorizedVotersListFrame {
142-
len: usize,
141+
len: u8,
143142
}
144143

145144
impl AuthorizedVotersListFrame {
146-
pub(super) const fn new(len: usize) -> Self {
147-
Self { len }
148-
}
149-
150145
pub(super) fn read(cursor: &mut std::io::Cursor<&[u8]>) -> Result<Self> {
151146
let len = solana_serialize_utils::cursor::read_u64(cursor)
152147
.map_err(|_err| VoteStateViewError::ParseError)? as usize;
148+
let len = u8::try_from(len).map_err(|_| VoteStateViewError::ParseError)?;
153149
let frame = Self { len };
154150
cursor.consume(frame.total_item_size());
155151
Ok(frame)
@@ -166,7 +162,7 @@ impl ListFrame<'_> for AuthorizedVotersListFrame {
166162
type Item = AuthorizedVoterItem;
167163

168164
fn len(&self) -> usize {
169-
self.len
165+
self.len as usize
170166
}
171167
}
172168

@@ -190,18 +186,17 @@ pub struct EpochCreditsItem {
190186
prev_credits: [u8; 8],
191187
}
192188

189+
#[derive(Debug, Clone, Copy)]
190+
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
193191
pub(super) struct EpochCreditsListFrame {
194-
len: usize,
192+
len: u8,
195193
}
196194

197195
impl EpochCreditsListFrame {
198-
pub(super) const fn new(len: usize) -> Self {
199-
Self { len }
200-
}
201-
202196
pub(super) fn read(cursor: &mut std::io::Cursor<&[u8]>) -> Result<Self> {
203197
let len = solana_serialize_utils::cursor::read_u64(cursor)
204198
.map_err(|_err| VoteStateViewError::ParseError)? as usize;
199+
let len = u8::try_from(len).map_err(|_| VoteStateViewError::ParseError)?;
205200
let frame = Self { len };
206201
cursor.consume(frame.total_item_size());
207202
Ok(frame)
@@ -212,7 +207,7 @@ impl ListFrame<'_> for EpochCreditsListFrame {
212207
type Item = EpochCreditsItem;
213208

214209
fn len(&self) -> usize {
215-
self.len
210+
self.len as usize
216211
}
217212
}
218213

@@ -263,19 +258,13 @@ impl RootSlotView<'_> {
263258
}
264259
}
265260

261+
#[derive(Debug, Clone, Copy)]
262+
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
266263
pub(super) struct RootSlotFrame {
267264
has_root_slot: bool,
268265
}
269266

270267
impl RootSlotFrame {
271-
pub(super) const fn new(has_root_slot: bool) -> Self {
272-
Self { has_root_slot }
273-
}
274-
275-
pub(super) fn has_root_slot(&self) -> bool {
276-
self.has_root_slot
277-
}
278-
279268
pub(super) fn total_size(&self) -> usize {
280269
1 + self.size()
281270
}

vote/src/vote_state_view/frame_v1_14_11.rs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use {
1313
#[derive(Debug, Clone)]
1414
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
1515
pub(super) struct VoteStateFrameV1_14_11 {
16-
pub(super) num_votes: u8,
17-
pub(super) has_root_slot: bool,
18-
pub(super) num_authorized_voters: u8,
19-
pub(super) num_epoch_credits: u8,
16+
pub(super) votes_frame: LockoutListFrame,
17+
pub(super) root_slot_frame: RootSlotFrame,
18+
pub(super) authorized_voters_frame: AuthorizedVotersListFrame,
19+
pub(super) epoch_credits_frame: EpochCreditsListFrame,
2020
}
2121

2222
impl VoteStateFrameV1_14_11 {
@@ -25,44 +25,26 @@ impl VoteStateFrameV1_14_11 {
2525
let mut cursor = std::io::Cursor::new(bytes);
2626
cursor.set_position(votes_offset as u64);
2727

28-
let votes = LockoutListFrame::read(&mut cursor)?;
29-
let root_slot = RootSlotFrame::read(&mut cursor)?;
30-
let authorized_voters = AuthorizedVotersListFrame::read(&mut cursor)?;
28+
let votes_frame = LockoutListFrame::read(&mut cursor)?;
29+
let root_slot_frame = RootSlotFrame::read(&mut cursor)?;
30+
let authorized_voters_frame = AuthorizedVotersListFrame::read(&mut cursor)?;
3131
PriorVotersFrame::read(&mut cursor);
32-
let epoch_credits = EpochCreditsListFrame::read(&mut cursor)?;
32+
let epoch_credits_frame = EpochCreditsListFrame::read(&mut cursor)?;
3333
cursor.consume(core::mem::size_of::<BlockTimestamp>());
3434
// trailing bytes are allowed. consistent with default behavior of
3535
// function bincode::deserialize
3636
if cursor.position() as usize <= bytes.len() {
3737
Ok(Self {
38-
num_votes: u8::try_from(votes.len()).map_err(|_| VoteStateViewError::ParseError)?,
39-
has_root_slot: root_slot.has_root_slot(),
40-
num_authorized_voters: u8::try_from(authorized_voters.len())
41-
.map_err(|_| VoteStateViewError::ParseError)?,
42-
num_epoch_credits: u8::try_from(epoch_credits.len())
43-
.map_err(|_| VoteStateViewError::ParseError)?,
38+
votes_frame,
39+
root_slot_frame,
40+
authorized_voters_frame,
41+
epoch_credits_frame,
4442
})
4543
} else {
4644
Err(VoteStateViewError::ParseError)
4745
}
4846
}
4947

50-
pub(super) fn votes_frame(&self) -> LockoutListFrame {
51-
LockoutListFrame::new(self.num_votes as usize)
52-
}
53-
54-
pub(super) fn root_slot_frame(&self) -> RootSlotFrame {
55-
RootSlotFrame::new(self.has_root_slot)
56-
}
57-
58-
pub(super) fn authorized_voters_frame(&self) -> AuthorizedVotersListFrame {
59-
AuthorizedVotersListFrame::new(self.num_authorized_voters as usize)
60-
}
61-
62-
pub(super) const fn epoch_credits_frame(&self) -> EpochCreditsListFrame {
63-
EpochCreditsListFrame::new(self.num_epoch_credits as usize)
64-
}
65-
6648
pub(super) fn get_field_offset(&self, field: Field) -> usize {
6749
match field {
6850
Field::NodePubkey => Self::node_pubkey_offset(),
@@ -92,22 +74,22 @@ impl VoteStateFrameV1_14_11 {
9274
}
9375

9476
fn root_slot_offset(&self) -> usize {
95-
Self::votes_offset() + self.votes_frame().total_size()
77+
Self::votes_offset() + self.votes_frame.total_size()
9678
}
9779

9880
fn authorized_voters_offset(&self) -> usize {
99-
self.root_slot_offset() + self.root_slot_frame().total_size()
81+
self.root_slot_offset() + self.root_slot_frame.total_size()
10082
}
10183

10284
fn prior_voters_offset(&self) -> usize {
103-
self.authorized_voters_offset() + self.authorized_voters_frame().total_size()
85+
self.authorized_voters_offset() + self.authorized_voters_frame.total_size()
10486
}
10587

10688
fn epoch_credits_offset(&self) -> usize {
10789
self.prior_voters_offset() + PriorVotersFrame.total_size()
10890
}
10991

11092
fn last_timestamp_offset(&self) -> usize {
111-
self.epoch_credits_offset() + self.epoch_credits_frame().total_size()
93+
self.epoch_credits_offset() + self.epoch_credits_frame.total_size()
11294
}
11395
}

vote/src/vote_state_view/frame_v3.rs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ use {
1414
#[derive(Debug, Clone)]
1515
#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
1616
pub(super) struct VoteStateFrameV3 {
17-
pub(super) num_votes: u8,
18-
pub(super) has_root_slot: bool,
19-
pub(super) num_authorized_voters: u8,
20-
pub(super) num_epoch_credits: u8,
17+
pub(super) votes_frame: LandedVotesListFrame,
18+
pub(super) root_slot_frame: RootSlotFrame,
19+
pub(super) authorized_voters_frame: AuthorizedVotersListFrame,
20+
pub(super) epoch_credits_frame: EpochCreditsListFrame,
2121
}
2222

2323
impl VoteStateFrameV3 {
@@ -26,44 +26,26 @@ impl VoteStateFrameV3 {
2626
let mut cursor = std::io::Cursor::new(bytes);
2727
cursor.set_position(votes_offset as u64);
2828

29-
let votes = LandedVotesListFrame::read(&mut cursor)?;
30-
let root_slot = RootSlotFrame::read(&mut cursor)?;
31-
let authorized_voters = AuthorizedVotersListFrame::read(&mut cursor)?;
29+
let votes_frame = LandedVotesListFrame::read(&mut cursor)?;
30+
let root_slot_frame = RootSlotFrame::read(&mut cursor)?;
31+
let authorized_voters_frame = AuthorizedVotersListFrame::read(&mut cursor)?;
3232
PriorVotersFrame::read(&mut cursor);
33-
let epoch_credits = EpochCreditsListFrame::read(&mut cursor)?;
33+
let epoch_credits_frame = EpochCreditsListFrame::read(&mut cursor)?;
3434
cursor.consume(core::mem::size_of::<BlockTimestamp>());
3535
// trailing bytes are allowed. consistent with default behavior of
3636
// function bincode::deserialize
3737
if cursor.position() as usize <= bytes.len() {
3838
Ok(Self {
39-
num_votes: u8::try_from(votes.len()).map_err(|_| VoteStateViewError::ParseError)?,
40-
has_root_slot: root_slot.has_root_slot(),
41-
num_authorized_voters: u8::try_from(authorized_voters.len())
42-
.map_err(|_| VoteStateViewError::ParseError)?,
43-
num_epoch_credits: u8::try_from(epoch_credits.len())
44-
.map_err(|_| VoteStateViewError::ParseError)?,
39+
votes_frame,
40+
root_slot_frame,
41+
authorized_voters_frame,
42+
epoch_credits_frame,
4543
})
4644
} else {
4745
Err(VoteStateViewError::ParseError)
4846
}
4947
}
5048

51-
pub(super) fn votes_frame(&self) -> LandedVotesListFrame {
52-
LandedVotesListFrame::new(self.num_votes as usize)
53-
}
54-
55-
pub(super) fn root_slot_frame(&self) -> RootSlotFrame {
56-
RootSlotFrame::new(self.has_root_slot)
57-
}
58-
59-
pub(super) fn authorized_voters_frame(&self) -> AuthorizedVotersListFrame {
60-
AuthorizedVotersListFrame::new(self.num_authorized_voters as usize)
61-
}
62-
63-
pub(super) const fn epoch_credits_frame(&self) -> EpochCreditsListFrame {
64-
EpochCreditsListFrame::new(self.num_epoch_credits as usize)
65-
}
66-
6749
pub(super) fn get_field_offset(&self, field: Field) -> usize {
6850
match field {
6951
Field::NodePubkey => Self::node_pubkey_offset(),
@@ -93,22 +75,22 @@ impl VoteStateFrameV3 {
9375
}
9476

9577
fn root_slot_offset(&self) -> usize {
96-
Self::votes_offset() + self.votes_frame().total_size()
78+
Self::votes_offset() + self.votes_frame.total_size()
9779
}
9880

9981
fn authorized_voters_offset(&self) -> usize {
100-
self.root_slot_offset() + self.root_slot_frame().total_size()
82+
self.root_slot_offset() + self.root_slot_frame.total_size()
10183
}
10284

10385
fn prior_voters_offset(&self) -> usize {
104-
self.authorized_voters_offset() + self.authorized_voters_frame().total_size()
86+
self.authorized_voters_offset() + self.authorized_voters_frame.total_size()
10587
}
10688

10789
fn epoch_credits_offset(&self) -> usize {
10890
self.prior_voters_offset() + PriorVotersFrame.total_size()
10991
}
11092

11193
fn last_timestamp_offset(&self) -> usize {
112-
self.epoch_credits_offset() + self.epoch_credits_frame().total_size()
94+
self.epoch_credits_offset() + self.epoch_credits_frame.total_size()
11395
}
11496
}

0 commit comments

Comments
 (0)