Skip to main content

richat_filter/protobuf/
encode.rs

1use {
2    crate::protobuf::{bytes_encode, bytes_encoded_len},
3    prost::{
4        DecodeError, Message,
5        bytes::{Buf, BufMut},
6        encoding::{
7            self, DecodeContext, WireType, encode_key, encode_varint, encoded_len_varint, key_len,
8            message,
9        },
10    },
11    prost_types::Timestamp,
12    richat_proto::{geyser::subscribe_update::UpdateOneof, solana::storage::confirmed_block},
13    solana_clock::Slot,
14    solana_pubkey::Pubkey,
15    std::borrow::Cow,
16};
17
18#[derive(Debug)]
19pub struct SubscribeUpdateMessageLimited<'a> {
20    pub filters: &'a [&'a str],
21    pub update: UpdateOneofLimitedEncode<'a>,
22    pub created_at: Timestamp,
23}
24
25impl Message for SubscribeUpdateMessageLimited<'_> {
26    fn encode_raw(&self, buf: &mut impl BufMut)
27    where
28        Self: Sized,
29    {
30        for filter in self.filters {
31            bytes_encode(1, filter.as_bytes(), buf);
32        }
33        self.update.encode(buf);
34        message::encode(11, &self.created_at, buf);
35    }
36
37    fn encoded_len(&self) -> usize {
38        self.filters
39            .iter()
40            .map(|filter| bytes_encoded_len(1, filter.as_bytes()))
41            .sum::<usize>()
42            + self.update.encoded_len()
43            + message::encoded_len(11, &self.created_at)
44    }
45
46    fn clear(&mut self) {
47        unimplemented!()
48    }
49
50    fn merge_field(
51        &mut self,
52        _tag: u32,
53        _wire_type: WireType,
54        _buf: &mut impl Buf,
55        _ctx: DecodeContext,
56    ) -> Result<(), DecodeError>
57    where
58        Self: Sized,
59    {
60        unimplemented!()
61    }
62}
63
64#[derive(Debug)]
65pub enum UpdateOneofLimitedEncode<'a> {
66    Account(UpdateOneofLimitedEncodeAccount<'a>),
67    Slot(&'a [u8]),
68    Transaction(&'a [u8]),
69    TransactionStatus(UpdateOneofLimitedEncodeTransactionStatus<'a>),
70    Block(UpdateOneofLimitedEncodeBlock<'a>),
71    BlockMeta(&'a [u8]),
72    Entry(&'a [u8]),
73}
74
75impl UpdateOneofLimitedEncode<'_> {
76    const fn tag(&self) -> u32 {
77        match self {
78            Self::Account(_) => 2u32,
79            Self::Slot(_) => 3u32,
80            Self::Transaction(_) => 4u32,
81            Self::TransactionStatus(_) => 10u32,
82            Self::Block(_) => 5u32,
83            Self::BlockMeta(_) => 7u32,
84            Self::Entry(_) => 8u32,
85        }
86    }
87
88    fn len(&self) -> usize {
89        match self {
90            Self::Account(account) => account.encoded_len(),
91            Self::Slot(slice) => slice.len(),
92            Self::Transaction(slice) => slice.len(),
93            Self::TransactionStatus(tx_status) => tx_status.encoded_len(),
94            Self::Block(block) => block.encoded_len(),
95            Self::BlockMeta(slice) => slice.len(),
96            Self::Entry(slice) => slice.len(),
97        }
98    }
99
100    pub fn encode(&self, buf: &mut impl BufMut) {
101        encode_key(self.tag(), WireType::LengthDelimited, buf);
102        encode_varint(self.len() as u64, buf);
103        match self {
104            Self::Account(account) => account.encode_raw(buf),
105            Self::Slot(slice) => buf.put_slice(slice),
106            Self::Transaction(slice) => buf.put_slice(slice),
107            Self::TransactionStatus(tx_status) => tx_status.encode_raw(buf),
108            Self::Block(block) => block.encode_raw(buf),
109            Self::BlockMeta(slice) => buf.put_slice(slice),
110            Self::Entry(slice) => buf.put_slice(slice),
111        }
112    }
113
114    pub fn encoded_len(&self) -> usize {
115        let len = self.len();
116        key_len(self.tag()) + encoded_len_varint(len as u64) + len
117    }
118}
119
120#[derive(Debug)]
121pub enum UpdateOneofLimitedEncodeAccount<'a> {
122    Slice(&'a [u8]),
123    Fields {
124        account: UpdateOneofLimitedEncodeAccountInner<'a>,
125        slot: Slot,
126        is_startup: bool,
127    },
128}
129
130impl Message for UpdateOneofLimitedEncodeAccount<'_> {
131    fn encode_raw(&self, buf: &mut impl BufMut) {
132        match self {
133            Self::Slice(slice) => buf.put_slice(slice),
134            Self::Fields {
135                account,
136                slot,
137                is_startup,
138            } => {
139                encode_key(1u32, WireType::LengthDelimited, buf);
140                encode_varint(account.encoded_len() as u64, buf);
141                account.encode_raw(buf);
142                if *slot != 0u64 {
143                    encoding::uint64::encode(2u32, slot, buf);
144                }
145                if !is_startup {
146                    encoding::bool::encode(3u32, is_startup, buf);
147                }
148            }
149        }
150    }
151
152    fn encoded_len(&self) -> usize {
153        match self {
154            Self::Slice(slice) => slice.len(),
155            Self::Fields {
156                account,
157                slot,
158                is_startup,
159            } => {
160                let account_len = account.encoded_len();
161                key_len(1u32)
162                    + encoded_len_varint(account_len as u64)
163                    + account_len
164                    + if *slot != 0u64 {
165                        encoding::uint64::encoded_len(2u32, slot)
166                    } else {
167                        0
168                    }
169                    + if !is_startup {
170                        encoding::bool::encoded_len(3u32, is_startup)
171                    } else {
172                        0
173                    }
174            }
175        }
176    }
177
178    fn merge_field(
179        &mut self,
180        _tag: u32,
181        _wire_type: WireType,
182        _buf: &mut impl Buf,
183        _ctx: DecodeContext,
184    ) -> Result<(), DecodeError>
185    where
186        Self: Sized,
187    {
188        unimplemented!()
189    }
190
191    fn clear(&mut self) {
192        unimplemented!()
193    }
194}
195
196#[derive(Debug)]
197pub struct UpdateOneofLimitedEncodeAccountInner<'a> {
198    pub pubkey: &'a Pubkey,
199    pub lamports: u64,
200    pub owner: &'a Pubkey,
201    pub executable: bool,
202    pub rent_epoch: u64,
203    pub data: Cow<'a, [u8]>,
204    pub write_version: u64,
205    pub txn_signature: Option<&'a [u8]>,
206}
207
208impl Message for UpdateOneofLimitedEncodeAccountInner<'_> {
209    fn encode_raw(&self, buf: &mut impl BufMut) {
210        bytes_encode(1u32, self.pubkey.as_ref(), buf);
211        if self.lamports != 0u64 {
212            encoding::uint64::encode(2u32, &self.lamports, buf);
213        }
214        bytes_encode(3u32, self.owner.as_ref(), buf);
215        if self.executable {
216            encoding::bool::encode(4u32, &self.executable, buf);
217        }
218        if self.rent_epoch != 0u64 {
219            encoding::uint64::encode(5u32, &self.rent_epoch, buf);
220        }
221        if !self.data.is_empty() {
222            bytes_encode(6u32, self.data.as_ref(), buf);
223        }
224        if self.write_version != 0u64 {
225            encoding::uint64::encode(7u32, &self.write_version, buf);
226        }
227        if let Some(value) = self.txn_signature {
228            bytes_encode(8u32, value, buf);
229        }
230    }
231
232    fn encoded_len(&self) -> usize {
233        bytes_encoded_len(1u32, self.pubkey.as_ref())
234            + if self.lamports != 0u64 {
235                encoding::uint64::encoded_len(2u32, &self.lamports)
236            } else {
237                0
238            }
239            + bytes_encoded_len(3u32, self.owner.as_ref())
240            + if self.executable {
241                encoding::bool::encoded_len(4u32, &self.executable)
242            } else {
243                0
244            }
245            + if self.rent_epoch != 0u64 {
246                encoding::uint64::encoded_len(5u32, &self.rent_epoch)
247            } else {
248                0
249            }
250            + if !self.data.is_empty() {
251                bytes_encoded_len(6u32, self.data.as_ref())
252            } else {
253                0
254            }
255            + if self.write_version != 0u64 {
256                encoding::uint64::encoded_len(7u32, &self.write_version)
257            } else {
258                0
259            }
260            + self
261                .txn_signature
262                .as_ref()
263                .map_or(0, |value| bytes_encoded_len(8u32, value))
264    }
265
266    fn merge_field(
267        &mut self,
268        _tag: u32,
269        _wire_type: WireType,
270        _buf: &mut impl Buf,
271        _ctx: DecodeContext,
272    ) -> Result<(), DecodeError>
273    where
274        Self: Sized,
275    {
276        unimplemented!()
277    }
278
279    fn clear(&mut self) {
280        unimplemented!()
281    }
282}
283
284#[derive(Debug)]
285pub struct UpdateOneofLimitedEncodeTransactionStatus<'a> {
286    pub slot: u64,
287    pub signature: &'a [u8],
288    pub is_vote: bool,
289    pub index: u64,
290    pub err: Option<confirmed_block::TransactionError>,
291}
292
293impl Message for UpdateOneofLimitedEncodeTransactionStatus<'_> {
294    fn encode_raw(&self, buf: &mut impl BufMut) {
295        if self.slot != 0u64 {
296            encoding::uint64::encode(1u32, &self.slot, buf);
297        }
298        if !self.signature.is_empty() {
299            bytes_encode(2u32, self.signature, buf);
300        }
301        if self.is_vote {
302            encoding::bool::encode(3u32, &self.is_vote, buf);
303        }
304        if self.index != 0u64 {
305            encoding::uint64::encode(4u32, &self.index, buf);
306        }
307        if let Some(msg) = &self.err {
308            encoding::message::encode(5u32, msg, buf);
309        }
310    }
311
312    fn encoded_len(&self) -> usize {
313        (if self.slot != 0u64 {
314            encoding::uint64::encoded_len(1u32, &self.slot)
315        } else {
316            0
317        }) + if !self.signature.is_empty() {
318            bytes_encoded_len(2u32, self.signature)
319        } else {
320            0
321        } + if self.is_vote {
322            encoding::bool::encoded_len(3u32, &self.is_vote)
323        } else {
324            0
325        } + if self.index != 0u64 {
326            encoding::uint64::encoded_len(4u32, &self.index)
327        } else {
328            0
329        } + self
330            .err
331            .as_ref()
332            .map_or(0, |msg| encoding::message::encoded_len(5u32, msg))
333    }
334
335    fn merge_field(
336        &mut self,
337        _tag: u32,
338        _wire_type: WireType,
339        _buf: &mut impl Buf,
340        _ctx: DecodeContext,
341    ) -> Result<(), DecodeError>
342    where
343        Self: Sized,
344    {
345        unimplemented!()
346    }
347
348    fn clear(&mut self) {
349        unimplemented!()
350    }
351}
352
353#[derive(Debug)]
354pub struct UpdateOneofLimitedEncodeBlock<'a> {
355    pub slot: u64,
356    pub blockhash: &'a str,
357    pub rewards: Option<confirmed_block::Rewards>,
358    pub block_time: Option<confirmed_block::UnixTimestamp>,
359    pub block_height: Option<confirmed_block::BlockHeight>,
360    pub parent_slot: u64,
361    pub parent_blockhash: &'a str,
362    pub executed_transaction_count: u64,
363    pub transactions: Vec<&'a [u8]>,
364    pub updated_account_count: u64,
365    pub accounts: Vec<UpdateOneofLimitedEncodeAccountInner<'a>>,
366    pub entries_count: u64,
367    pub entries: Vec<&'a [u8]>,
368}
369
370impl Message for UpdateOneofLimitedEncodeBlock<'_> {
371    fn encode_raw(&self, buf: &mut impl BufMut) {
372        if self.slot != 0u64 {
373            encoding::uint64::encode(1u32, &self.slot, buf);
374        }
375        if !self.blockhash.is_empty() {
376            bytes_encode(2u32, self.blockhash.as_ref(), buf);
377        }
378        if let Some(msg) = &self.rewards {
379            encoding::message::encode(3u32, msg, buf);
380        }
381        if let Some(msg) = &self.block_time {
382            encoding::message::encode(4u32, msg, buf);
383        }
384        if let Some(msg) = &self.block_height {
385            encoding::message::encode(5u32, msg, buf);
386        }
387        for slice in &self.transactions {
388            encode_key(6u32, WireType::LengthDelimited, buf);
389            encode_varint(slice.len() as u64, buf);
390            buf.put_slice(slice);
391        }
392        if self.parent_slot != 0u64 {
393            encoding::uint64::encode(7u32, &self.parent_slot, buf);
394        }
395        if !self.parent_blockhash.is_empty() {
396            bytes_encode(8u32, self.parent_blockhash.as_ref(), buf);
397        }
398        if self.executed_transaction_count != 0u64 {
399            encoding::uint64::encode(9u32, &self.executed_transaction_count, buf);
400        }
401        if self.updated_account_count != 0u64 {
402            encoding::uint64::encode(10u32, &self.updated_account_count, buf);
403        }
404        for msg in &self.accounts {
405            encoding::message::encode(11u32, msg, buf);
406        }
407        if self.entries_count != 0u64 {
408            encoding::uint64::encode(12u32, &self.entries_count, buf);
409        }
410        for slice in &self.entries {
411            encode_key(13u32, WireType::LengthDelimited, buf);
412            encode_varint(slice.len() as u64, buf);
413            buf.put_slice(slice);
414        }
415    }
416
417    fn encoded_len(&self) -> usize {
418        (if self.slot != 0u64 {
419            encoding::uint64::encoded_len(1u32, &self.slot)
420        } else {
421            0
422        }) + if !self.blockhash.is_empty() {
423            bytes_encoded_len(2u32, self.blockhash.as_ref())
424        } else {
425            0
426        } + self
427            .rewards
428            .as_ref()
429            .map_or(0, |msg| encoding::message::encoded_len(3u32, msg))
430            + self
431                .block_time
432                .as_ref()
433                .map_or(0, |msg| encoding::message::encoded_len(4u32, msg))
434            + self
435                .block_height
436                .as_ref()
437                .map_or(0, |msg| encoding::message::encoded_len(5u32, msg))
438            + (key_len(6u32) * self.transactions.len()
439                + self
440                    .transactions
441                    .iter()
442                    .map(|slice| slice.len())
443                    .map(|len| len + encoded_len_varint(len as u64))
444                    .sum::<usize>())
445            + if self.parent_slot != 0u64 {
446                encoding::uint64::encoded_len(7u32, &self.parent_slot)
447            } else {
448                0
449            }
450            + if !self.parent_blockhash.is_empty() {
451                bytes_encoded_len(8u32, self.parent_blockhash.as_ref())
452            } else {
453                0
454            }
455            + if self.executed_transaction_count != 0u64 {
456                encoding::uint64::encoded_len(9u32, &self.executed_transaction_count)
457            } else {
458                0
459            }
460            + if self.updated_account_count != 0u64 {
461                encoding::uint64::encoded_len(10u32, &self.updated_account_count)
462            } else {
463                0
464            }
465            + encoding::message::encoded_len_repeated(11u32, &self.accounts)
466            + if self.entries_count != 0u64 {
467                encoding::uint64::encoded_len(12u32, &self.entries_count)
468            } else {
469                0
470            }
471            + (key_len(13u32) * self.entries.len()
472                + self
473                    .entries
474                    .iter()
475                    .map(|slice| slice.len())
476                    .map(|len| len + encoded_len_varint(len as u64))
477                    .sum::<usize>())
478    }
479
480    fn merge_field(
481        &mut self,
482        _tag: u32,
483        _wire_type: WireType,
484        _buf: &mut impl Buf,
485        _ctx: DecodeContext,
486    ) -> Result<(), DecodeError>
487    where
488        Self: Sized,
489    {
490        unimplemented!()
491    }
492
493    fn clear(&mut self) {
494        unimplemented!()
495    }
496}
497
498#[derive(Debug)]
499pub struct SubscribeUpdateMessageProst<'a> {
500    pub filters: &'a [&'a str],
501    pub update: UpdateOneof,
502    pub created_at: Timestamp,
503}
504
505impl Message for SubscribeUpdateMessageProst<'_> {
506    fn encode_raw(&self, buf: &mut impl BufMut)
507    where
508        Self: Sized,
509    {
510        for filter in self.filters {
511            bytes_encode(1, filter.as_bytes(), buf);
512        }
513        self.update.encode(buf);
514        message::encode(11, &self.created_at, buf);
515    }
516
517    fn encoded_len(&self) -> usize {
518        self.filters
519            .iter()
520            .map(|filter| bytes_encoded_len(1, filter.as_bytes()))
521            .sum::<usize>()
522            + self.update.encoded_len()
523            + message::encoded_len(11, &self.created_at)
524    }
525
526    fn clear(&mut self) {
527        unimplemented!()
528    }
529
530    fn merge_field(
531        &mut self,
532        _tag: u32,
533        _wire_type: WireType,
534        _buf: &mut impl Buf,
535        _ctx: DecodeContext,
536    ) -> Result<(), DecodeError>
537    where
538        Self: Sized,
539    {
540        unimplemented!()
541    }
542}