sui_types/
messages_grpc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::base_types::{ObjectID, SequenceNumber, TransactionDigest};
use crate::crypto::{AuthoritySignInfo, AuthorityStrongQuorumSignInfo};
use crate::effects::{
    SignedTransactionEffects, TransactionEvents, VerifiedSignedTransactionEffects,
};
use crate::error::SuiError;
use crate::object::Object;
use crate::transaction::{CertifiedTransaction, SenderSignedData, SignedTransaction, Transaction};

use bytes::Bytes;
use move_core_types::annotated_value::MoveStructLayout;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub enum ObjectInfoRequestKind {
    /// Request the latest object state.
    LatestObjectInfo,
    /// Request a specific version of the object.
    /// This is used only for debugging purpose and will not work as a generic solution
    /// since we don't keep around all historic object versions.
    /// No production code should depend on this kind.
    PastObjectInfoDebug(SequenceNumber),
}

/// Layout generation options -- you can either generate or not generate the layout.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub enum LayoutGenerationOption {
    Generate,
    None,
}

/// A request for information about an object and optionally its
/// parent certificate at a specific version.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize, Deserialize)]
pub struct ObjectInfoRequest {
    /// The id of the object to retrieve, at the latest version.
    pub object_id: ObjectID,
    /// if true return the layout of the object.
    pub generate_layout: LayoutGenerationOption,
    /// The type of request, either latest object info or the past.
    pub request_kind: ObjectInfoRequestKind,
}

impl ObjectInfoRequest {
    pub fn past_object_info_debug_request(
        object_id: ObjectID,
        version: SequenceNumber,
        generate_layout: LayoutGenerationOption,
    ) -> Self {
        ObjectInfoRequest {
            object_id,
            generate_layout,
            request_kind: ObjectInfoRequestKind::PastObjectInfoDebug(version),
        }
    }

    pub fn latest_object_info_request(
        object_id: ObjectID,
        generate_layout: LayoutGenerationOption,
    ) -> Self {
        ObjectInfoRequest {
            object_id,
            generate_layout,
            request_kind: ObjectInfoRequestKind::LatestObjectInfo,
        }
    }
}

/// This message provides information about the latest object and its lock.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectInfoResponse {
    /// Value of the requested object in this authority
    pub object: Object,
    /// Schema of the Move value inside this object.
    /// None if the object is a Move package, or the request did not ask for the layout
    pub layout: Option<MoveStructLayout>,
    /// Transaction the object is locked on in this authority.
    /// None if the object is not currently locked by this authority.
    /// This should be only used for debugging purpose, such as from sui-tool. No prod clients should
    /// rely on it.
    pub lock_for_debugging: Option<SignedTransaction>,
}

/// Verified version of `ObjectInfoResponse`. `layout` and `lock_for_debugging` are skipped because they
/// are not needed and we don't want to verify them.
#[derive(Debug, Clone)]
pub struct VerifiedObjectInfoResponse {
    /// Value of the requested object in this authority
    pub object: Object,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TransactionInfoRequest {
    pub transaction_digest: TransactionDigest,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TransactionStatus {
    /// Signature over the transaction.
    Signed(AuthoritySignInfo),
    /// For executed transaction, we could return an optional certificate signature on the transaction
    /// (i.e. the signature part of the CertifiedTransaction), as well as the signed effects.
    /// The certificate signature is optional because for transactions executed in previous
    /// epochs, we won't keep around the certificate signatures.
    Executed(
        Option<AuthorityStrongQuorumSignInfo>,
        SignedTransactionEffects,
        TransactionEvents,
    ),
}

impl TransactionStatus {
    pub fn into_signed_for_testing(self) -> AuthoritySignInfo {
        match self {
            Self::Signed(s) => s,
            _ => unreachable!("Incorrect response type"),
        }
    }

    pub fn into_effects_for_testing(self) -> SignedTransactionEffects {
        match self {
            Self::Executed(_, e, _) => e,
            _ => unreachable!("Incorrect response type"),
        }
    }
}

impl PartialEq for TransactionStatus {
    fn eq(&self, other: &Self) -> bool {
        match self {
            Self::Signed(s1) => match other {
                Self::Signed(s2) => s1.epoch == s2.epoch,
                _ => false,
            },
            Self::Executed(c1, e1, ev1) => match other {
                Self::Executed(c2, e2, ev2) => {
                    c1.as_ref().map(|a| a.epoch) == c2.as_ref().map(|a| a.epoch)
                        && e1.epoch() == e2.epoch()
                        && e1.digest() == e2.digest()
                        && ev1.digest() == ev2.digest()
                }
                _ => false,
            },
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct HandleTransactionResponse {
    pub status: TransactionStatus,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TransactionInfoResponse {
    pub transaction: SenderSignedData,
    pub status: TransactionStatus,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HandleCertificateResponseV2 {
    pub signed_effects: SignedTransactionEffects,
    pub events: TransactionEvents,
    /// Not used. Full node local execution fast path was deprecated.
    pub fastpath_input_objects: Vec<Object>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SubmitCertificateResponse {
    /// If transaction is already executed, return same result as handle_certificate
    pub executed: Option<HandleCertificateResponseV2>,
}

#[derive(Clone, Debug)]
pub struct VerifiedHandleCertificateResponse {
    pub signed_effects: VerifiedSignedTransactionEffects,
    pub events: TransactionEvents,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SystemStateRequest {
    // This is needed to make gRPC happy.
    pub _unused: bool,
}

/// Response type for version 3 of the handle certificate validator API.
///
/// The corresponding version 3 request type allows for a client to request events as well as
/// input/output objects from a transaction's execution. Given Validators operate with very
/// aggressive object pruning, the return of input/output objects is only done immediately after
/// the transaction has been executed locally on the validator and will not be returned for
/// requests to previously executed transactions.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HandleCertificateResponseV3 {
    pub effects: SignedTransactionEffects,
    pub events: Option<TransactionEvents>,

    /// If requested, will included all initial versions of objects modified in this transaction.
    /// This includes owned objects included as input into the transaction as well as the assigned
    /// versions of shared objects.
    //
    // TODO: In the future we may want to include shared objects or child objects which were read
    // but not modified during execution.
    pub input_objects: Option<Vec<Object>>,

    /// If requested, will included all changed objects, including mutated, created and unwrapped
    /// objects. In other words, all objects that still exist in the object state after this
    /// transaction.
    pub output_objects: Option<Vec<Object>>,
    pub auxiliary_data: Option<Vec<u8>>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HandleCertificateRequestV3 {
    pub certificate: CertifiedTransaction,

    pub include_events: bool,
    pub include_input_objects: bool,
    pub include_output_objects: bool,
    pub include_auxiliary_data: bool,
}

impl From<HandleCertificateResponseV3> for HandleCertificateResponseV2 {
    fn from(value: HandleCertificateResponseV3) -> Self {
        Self {
            signed_effects: value.effects,
            events: value.events.unwrap_or_default(),
            fastpath_input_objects: Vec::new(),
        }
    }
}

/// Response type for the handle Soft Bundle certificates validator API.
/// If `wait_for_effects` is true, it is guaranteed that:
///  - Number of responses will be equal to the number of input transactions.
///  - The order of the responses matches the order of the input transactions.
///
/// Otherwise, `responses` will be empty.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HandleSoftBundleCertificatesResponseV3 {
    pub responses: Vec<HandleCertificateResponseV3>,
}

/// Soft Bundle request.  See [SIP-19](https://github.com/sui-foundation/sips/blob/main/sips/sip-19.md).
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HandleSoftBundleCertificatesRequestV3 {
    pub certificates: Vec<CertifiedTransaction>,

    pub wait_for_effects: bool,
    pub include_events: bool,
    pub include_input_objects: bool,
    pub include_output_objects: bool,
    pub include_auxiliary_data: bool,
}

// =========== ExecutedData ===========

#[derive(Default, Clone)]
pub struct ExecutedData {
    pub effects: crate::effects::TransactionEffects,
    pub events: Option<crate::effects::TransactionEvents>,
    pub input_objects: Vec<crate::object::Object>,
    pub output_objects: Vec<crate::object::Object>,
}

#[derive(Clone, prost::Message)]
pub struct RawExecutedData {
    #[prost(bytes = "bytes", tag = "1")]
    pub effects: Bytes,
    #[prost(bytes = "bytes", optional, tag = "2")]
    pub events: Option<Bytes>,
    #[prost(bytes = "bytes", repeated, tag = "3")]
    pub input_objects: Vec<Bytes>,
    #[prost(bytes = "bytes", repeated, tag = "4")]
    pub output_objects: Vec<Bytes>,
}

// =========== SubmitTx types ===========

#[derive(Clone, Debug)]
pub struct SubmitTxRequest {
    pub transaction: Transaction,
}

impl SubmitTxRequest {
    pub fn new_transaction(transaction: Transaction) -> Self {
        Self { transaction }
    }
}

impl SubmitTxRequest {
    pub fn into_raw(&self) -> Result<RawSubmitTxRequest, SuiError> {
        let transactions = vec![bcs::to_bytes(&self.transaction)
            .map_err(|e| SuiError::TransactionSerializationError {
                error: e.to_string(),
            })?
            .into()];
        Ok(RawSubmitTxRequest {
            transactions,
            ..Default::default()
        })
    }
}

#[derive(Clone)]
pub enum SubmitTxResult {
    Submitted {
        consensus_position: crate::messages_consensus::ConsensusPosition,
    },
    Executed {
        effects_digest: crate::digests::TransactionEffectsDigest,
        // Response should always include details for executed transactions.
        // TODO(fastpath): validate this field is always present and return an error during deserialization.
        details: Option<Box<ExecutedData>>,
        // Whether the transaction was executed using fast path.
        fast_path: bool,
    },
    Rejected {
        error: crate::error::SuiError,
    },
}

impl std::fmt::Debug for SubmitTxResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Submitted { consensus_position } => f
                .debug_struct("Submitted")
                .field("consensus_position", consensus_position)
                .finish(),
            Self::Executed {
                effects_digest,
                fast_path,
                ..
            } => f
                .debug_struct("Executed")
                .field("effects_digest", &format_args!("{}", effects_digest))
                .field("fast_path", fast_path)
                .finish(),
            Self::Rejected { error } => f.debug_struct("Rejected").field("error", &error).finish(),
        }
    }
}

#[derive(Clone, Debug)]
pub struct SubmitTxResponse {
    pub results: Vec<SubmitTxResult>,
}

#[derive(Clone, prost::Message)]
pub struct RawSubmitTxRequest {
    /// The transactions to be submitted. When the vector is empty, then this is treated as a ping request.
    #[prost(bytes = "bytes", repeated, tag = "1")]
    pub transactions: Vec<Bytes>,

    /// When submitting multiple transactions, attempt to include them in
    /// the same block with the same order (soft bundle), if true.
    /// Otherwise, allow the transactions to be included separately and
    /// out of order in blocks (batch).
    #[prost(bool, tag = "2")]
    pub soft_bundle: bool,
}

#[derive(Clone, prost::Message)]
pub struct RawSubmitTxResponse {
    // Results corresponding to each transaction in the request.
    #[prost(message, repeated, tag = "1")]
    pub results: Vec<RawSubmitTxResult>,
}

#[derive(Clone, prost::Message)]
pub struct RawSubmitTxResult {
    #[prost(oneof = "RawValidatorSubmitStatus", tags = "1, 2, 3")]
    pub inner: Option<RawValidatorSubmitStatus>,
}

#[derive(Clone, prost::Oneof)]
pub enum RawValidatorSubmitStatus {
    // Serialized Consensus Position.
    #[prost(bytes = "bytes", tag = "1")]
    Submitted(Bytes),

    // Transaction has already been executed (finalized).
    #[prost(message, tag = "2")]
    Executed(RawExecutedStatus),

    // Transaction is rejected from consensus submission.
    #[prost(message, tag = "3")]
    Rejected(RawRejectedStatus),
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, prost::Enumeration)]
#[repr(i32)]
pub enum RawPingType {
    FastPath = 0,
    Consensus = 1,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PingType {
    // Testing the time that it takes for the block of the ping transaction to appear as certified via the FastPath.
    FastPath,
    // Testing the time that it takes for the block of the ping transaction to appear as certified via the Consensus.
    // This is useful when want to test the end to end latency from when a block is proposed up to when it comes out of consensus as certified.
    Consensus,
}

impl PingType {
    pub fn as_str(&self) -> &str {
        match self {
            PingType::FastPath => "fastpath",
            PingType::Consensus => "consensus",
        }
    }
}

// =========== WaitForEffects types ===========

pub struct WaitForEffectsRequest {
    pub transaction_digest: Option<crate::digests::TransactionDigest>,
    /// If consensus position is provided, waits in the server handler for the transaction in it to execute,
    /// either in fastpath outputs or finalized.
    /// If it is not provided, only waits for finalized effects of the transaction in the server handler,
    /// but not for fastpath outputs.
    pub consensus_position: Option<crate::messages_consensus::ConsensusPosition>,
    /// Whether to include details of the effects,
    /// including the effects content, events, input objects, and output objects.
    pub include_details: bool,
    /// If this is a ping request, then this is the type of ping.
    pub ping: Option<PingType>,
}

#[derive(Clone)]
pub enum WaitForEffectsResponse {
    Executed {
        effects_digest: crate::digests::TransactionEffectsDigest,
        details: Option<Box<ExecutedData>>,
        fast_path: bool,
    },
    // The transaction was rejected by consensus.
    Rejected {
        // The reason of the reject vote casted by the validator.
        // If None, the validator did not cast a reject vote.
        error: Option<crate::error::SuiError>,
    },
    // The transaction position is expired, with the local epoch and committed round.
    // When round is None, the expiration is due to lagging epoch in the request.
    Expired {
        epoch: u64,
        round: Option<u32>,
    },
}

impl std::fmt::Debug for WaitForEffectsResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Executed {
                effects_digest,
                fast_path,
                ..
            } => f
                .debug_struct("Executed")
                .field("effects_digest", effects_digest)
                .field("fast_path", fast_path)
                .finish(),
            Self::Rejected { error } => f.debug_struct("Rejected").field("error", error).finish(),
            Self::Expired { epoch, round } => f
                .debug_struct("Expired")
                .field("epoch", epoch)
                .field("round", round)
                .finish(),
        }
    }
}

#[derive(Clone, prost::Message)]
pub struct RawWaitForEffectsRequest {
    /// The transaction's digest. If it's a ping request, then this will practically be ignored.
    #[prost(bytes = "bytes", optional, tag = "1")]
    pub transaction_digest: Option<Bytes>,

    /// If provided, wait for the consensus position to execute and wait for fastpath outputs of the transaction,
    /// in addition to waiting for finalized effects.
    /// If not provided, only wait for finalized effects.
    #[prost(bytes = "bytes", optional, tag = "2")]
    pub consensus_position: Option<Bytes>,

    /// Whether to include details of the effects,
    /// including the effects content, events, input objects, and output objects.
    #[prost(bool, tag = "3")]
    pub include_details: bool,

    /// Set when this is a ping request, to differentiate between fastpath and consensus pings.
    #[prost(enumeration = "RawPingType", optional, tag = "4")]
    pub ping_type: Option<i32>,
}

#[derive(Clone, prost::Message)]
pub struct RawWaitForEffectsResponse {
    // In order to represent an enum in protobuf, we need to use oneof.
    // However, oneof also allows the value to be unset, which corresponds to None value.
    // Hence, we need to use Option type for `inner`.
    // We expect the value to be set in a valid response.
    #[prost(oneof = "RawValidatorTransactionStatus", tags = "1, 2, 3")]
    pub inner: Option<RawValidatorTransactionStatus>,
}

#[derive(Clone, prost::Oneof)]
pub enum RawValidatorTransactionStatus {
    #[prost(message, tag = "1")]
    Executed(RawExecutedStatus),
    #[prost(message, tag = "2")]
    Rejected(RawRejectedStatus),
    #[prost(message, tag = "3")]
    Expired(RawExpiredStatus),
}

#[derive(Clone, prost::Message)]
pub struct RawExecutedStatus {
    #[prost(bytes = "bytes", tag = "1")]
    pub effects_digest: Bytes,
    #[prost(message, optional, tag = "2")]
    pub details: Option<RawExecutedData>,
    #[prost(bool, tag = "3")]
    pub fast_path: bool,
}

#[derive(Clone, prost::Message)]
pub struct RawRejectedStatus {
    #[prost(bytes = "bytes", optional, tag = "1")]
    pub error: Option<Bytes>,
}

#[derive(Clone, prost::Message)]
pub struct RawExpiredStatus {
    // Validator's current epoch.
    #[prost(uint64, tag = "1")]
    pub epoch: u64,
    // Validator's current round. 0 if it is not yet checked.
    #[prost(uint32, optional, tag = "2")]
    pub round: Option<u32>,
}

// =========== ValidatorHealth types ===========

/// Request for validator health information (used for latency measurement)
#[derive(Clone, Debug, Default)]
pub struct ValidatorHealthRequest {}

/// Response with validator health metrics (data collected but not used for scoring yet)
#[derive(Clone, Debug, Default)]
pub struct ValidatorHealthResponse {
    /// Number of in-flight execution transactions from execution scheduler
    pub num_inflight_execution_transactions: u64,
    /// Number of in-flight consensus transactions
    pub num_inflight_consensus_transactions: u64,
    /// Last committed leader round from Mysticeti consensus
    pub last_committed_leader_round: u32,
    /// Last locally built checkpoint sequence number
    pub last_locally_built_checkpoint: u64,
}

/// Raw protobuf request for validator health information (evolvable)
#[derive(Clone, prost::Message)]
pub struct RawValidatorHealthRequest {}

/// Raw protobuf response with validator health metrics (evolvable)
#[derive(Clone, prost::Message)]
pub struct RawValidatorHealthResponse {
    /// Number of pending certificates
    #[prost(uint64, optional, tag = "1")]
    pub pending_certificates: Option<u64>,
    /// Number of in-flight consensus messages
    #[prost(uint64, optional, tag = "2")]
    pub inflight_consensus_messages: Option<u64>,
    /// Current consensus round
    #[prost(uint64, optional, tag = "3")]
    pub consensus_round: Option<u64>,
    /// Current checkpoint sequence number
    #[prost(uint64, optional, tag = "4")]
    pub checkpoint_sequence: Option<u64>,
}

// =========== Parse helpers ===========

impl From<RawPingType> for PingType {
    fn from(value: RawPingType) -> Self {
        match value {
            RawPingType::FastPath => PingType::FastPath,
            RawPingType::Consensus => PingType::Consensus,
        }
    }
}

impl From<PingType> for RawPingType {
    fn from(value: PingType) -> Self {
        match value {
            PingType::FastPath => RawPingType::FastPath,
            PingType::Consensus => RawPingType::Consensus,
        }
    }
}

impl TryFrom<ExecutedData> for RawExecutedData {
    type Error = crate::error::SuiError;

    fn try_from(value: ExecutedData) -> Result<Self, Self::Error> {
        let effects = bcs::to_bytes(&value.effects)
            .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
                type_info: "ExecutedData.effects".to_string(),
                error: err.to_string(),
            })?
            .into();
        let events = if let Some(events) = &value.events {
            Some(
                bcs::to_bytes(events)
                    .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
                        type_info: "ExecutedData.events".to_string(),
                        error: err.to_string(),
                    })?
                    .into(),
            )
        } else {
            None
        };
        let mut input_objects = Vec::with_capacity(value.input_objects.len());
        for object in value.input_objects {
            input_objects.push(
                bcs::to_bytes(&object)
                    .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
                        type_info: "ExecutedData.input_objects".to_string(),
                        error: err.to_string(),
                    })?
                    .into(),
            );
        }
        let mut output_objects = Vec::with_capacity(value.output_objects.len());
        for object in value.output_objects {
            output_objects.push(
                bcs::to_bytes(&object)
                    .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
                        type_info: "ExecutedData.output_objects".to_string(),
                        error: err.to_string(),
                    })?
                    .into(),
            );
        }
        Ok(RawExecutedData {
            effects,
            events,
            input_objects,
            output_objects,
        })
    }
}

impl TryFrom<RawExecutedData> for ExecutedData {
    type Error = crate::error::SuiError;

    fn try_from(value: RawExecutedData) -> Result<Self, Self::Error> {
        let effects = bcs::from_bytes(&value.effects).map_err(|err| {
            crate::error::SuiError::GrpcMessageDeserializeError {
                type_info: "RawExecutedData.effects".to_string(),
                error: err.to_string(),
            }
        })?;
        let events = if let Some(events) = value.events {
            Some(bcs::from_bytes(&events).map_err(|err| {
                crate::error::SuiError::GrpcMessageDeserializeError {
                    type_info: "RawExecutedData.events".to_string(),
                    error: err.to_string(),
                }
            })?)
        } else {
            None
        };
        let mut input_objects = Vec::with_capacity(value.input_objects.len());
        for object in value.input_objects {
            input_objects.push(bcs::from_bytes(&object).map_err(|err| {
                crate::error::SuiError::GrpcMessageDeserializeError {
                    type_info: "RawExecutedData.input_objects".to_string(),
                    error: err.to_string(),
                }
            })?);
        }
        let mut output_objects = Vec::with_capacity(value.output_objects.len());
        for object in value.output_objects {
            output_objects.push(bcs::from_bytes(&object).map_err(|err| {
                crate::error::SuiError::GrpcMessageDeserializeError {
                    type_info: "RawExecutedData.output_objects".to_string(),
                    error: err.to_string(),
                }
            })?);
        }
        Ok(ExecutedData {
            effects,
            events,
            input_objects,
            output_objects,
        })
    }
}

impl TryFrom<SubmitTxResult> for RawSubmitTxResult {
    type Error = crate::error::SuiError;

    fn try_from(value: SubmitTxResult) -> Result<Self, Self::Error> {
        let inner = match value {
            SubmitTxResult::Submitted { consensus_position } => {
                let consensus_position = consensus_position.into_raw()?;
                RawValidatorSubmitStatus::Submitted(consensus_position)
            }
            SubmitTxResult::Executed {
                effects_digest,
                details,
                fast_path,
            } => {
                let raw_executed = try_from_response_executed(effects_digest, details, fast_path)?;
                RawValidatorSubmitStatus::Executed(raw_executed)
            }
            SubmitTxResult::Rejected { error } => {
                RawValidatorSubmitStatus::Rejected(try_from_response_rejected(Some(error))?)
            }
        };
        Ok(RawSubmitTxResult { inner: Some(inner) })
    }
}

impl TryFrom<RawSubmitTxResult> for SubmitTxResult {
    type Error = crate::error::SuiError;

    fn try_from(value: RawSubmitTxResult) -> Result<Self, Self::Error> {
        match value.inner {
            Some(RawValidatorSubmitStatus::Submitted(consensus_position)) => {
                Ok(SubmitTxResult::Submitted {
                    consensus_position: consensus_position.as_ref().try_into()?,
                })
            }
            Some(RawValidatorSubmitStatus::Executed(executed)) => {
                let (effects_digest, details, fast_path) = try_from_raw_executed_status(executed)?;
                Ok(SubmitTxResult::Executed {
                    effects_digest,
                    details,
                    fast_path,
                })
            }
            Some(RawValidatorSubmitStatus::Rejected(error)) => {
                let error = try_from_raw_rejected_status(error)?.unwrap_or(
                    crate::error::SuiError::GrpcMessageDeserializeError {
                        type_info: "RawSubmitTxResult.inner.Error".to_string(),
                        error: "RawSubmitTxResult.inner.Error is None".to_string(),
                    },
                );
                Ok(SubmitTxResult::Rejected { error })
            }
            None => Err(crate::error::SuiError::GrpcMessageDeserializeError {
                type_info: "RawSubmitTxResult.inner".to_string(),
                error: "RawSubmitTxResult.inner is None".to_string(),
            }),
        }
    }
}

impl TryFrom<RawSubmitTxResponse> for SubmitTxResponse {
    type Error = crate::error::SuiError;

    fn try_from(value: RawSubmitTxResponse) -> Result<Self, Self::Error> {
        // TODO(fastpath): handle multiple transactions.
        if value.results.len() != 1 {
            return Err(crate::error::SuiError::GrpcMessageDeserializeError {
                type_info: "RawSubmitTxResponse.results".to_string(),
                error: format!("Expected exactly 1 result, got {}", value.results.len()),
            });
        }

        let results = value
            .results
            .into_iter()
            .map(|result| result.try_into())
            .collect::<Result<Vec<SubmitTxResult>, crate::error::SuiError>>()?;

        Ok(Self { results })
    }
}

fn try_from_raw_executed_status(
    executed: RawExecutedStatus,
) -> Result<
    (
        crate::digests::TransactionEffectsDigest,
        Option<Box<ExecutedData>>,
        bool,
    ),
    crate::error::SuiError,
> {
    let effects_digest = bcs::from_bytes(&executed.effects_digest).map_err(|err| {
        crate::error::SuiError::GrpcMessageDeserializeError {
            type_info: "RawWaitForEffectsResponse.effects_digest".to_string(),
            error: err.to_string(),
        }
    })?;
    let executed_data = if let Some(details) = executed.details {
        Some(Box::new(details.try_into()?))
    } else {
        None
    };
    Ok((effects_digest, executed_data, executed.fast_path))
}

fn try_from_raw_rejected_status(
    rejected: RawRejectedStatus,
) -> Result<Option<crate::error::SuiError>, crate::error::SuiError> {
    match rejected.error {
        Some(error_bytes) => {
            let error = bcs::from_bytes(&error_bytes).map_err(|err| {
                crate::error::SuiError::GrpcMessageDeserializeError {
                    type_info: "RawWaitForEffectsResponse.rejected.reason".to_string(),
                    error: err.to_string(),
                }
            })?;
            Ok(Some(error))
        }
        None => Ok(None),
    }
}

fn try_from_response_rejected(
    error: Option<crate::error::SuiError>,
) -> Result<RawRejectedStatus, crate::error::SuiError> {
    let error = match error {
        Some(e) => Some(
            bcs::to_bytes(&e)
                .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
                    type_info: "RawRejectedStatus.error".to_string(),
                    error: err.to_string(),
                })?
                .into(),
        ),
        None => None,
    };
    Ok(RawRejectedStatus { error })
}

fn try_from_response_executed(
    effects_digest: crate::digests::TransactionEffectsDigest,
    details: Option<Box<ExecutedData>>,
    fast_path: bool,
) -> Result<RawExecutedStatus, crate::error::SuiError> {
    let effects_digest = bcs::to_bytes(&effects_digest)
        .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
            type_info: "RawWaitForEffectsResponse.effects_digest".to_string(),
            error: err.to_string(),
        })?
        .into();
    let details = if let Some(details) = details {
        Some((*details).try_into()?)
    } else {
        None
    };
    Ok(RawExecutedStatus {
        effects_digest,
        details,
        fast_path,
    })
}

impl TryFrom<RawWaitForEffectsRequest> for WaitForEffectsRequest {
    type Error = crate::error::SuiError;

    fn try_from(value: RawWaitForEffectsRequest) -> Result<Self, Self::Error> {
        let transaction_digest = match value.transaction_digest {
            Some(digest) => Some(bcs::from_bytes(&digest).map_err(|err| {
                crate::error::SuiError::GrpcMessageDeserializeError {
                    type_info: "RawWaitForEffectsRequest.transaction_digest".to_string(),
                    error: err.to_string(),
                }
            })?),
            None => None,
        };
        let consensus_position = match value.consensus_position {
            Some(cp) => Some(cp.as_ref().try_into()?),
            None => None,
        };
        let ping = value
            .ping_type
            .map(|p| {
                RawPingType::try_from(p).map(PingType::from).map_err(|e| {
                    SuiError::GrpcMessageDeserializeError {
                        type_info: "RawWaitForEffectsRequest.ping".to_string(),
                        error: e.to_string(),
                    }
                })
            })
            .transpose()?;
        Ok(Self {
            consensus_position,
            transaction_digest,
            include_details: value.include_details,
            ping,
        })
    }
}

impl TryFrom<WaitForEffectsRequest> for RawWaitForEffectsRequest {
    type Error = crate::error::SuiError;

    fn try_from(value: WaitForEffectsRequest) -> Result<Self, Self::Error> {
        let transaction_digest = match value.transaction_digest {
            Some(digest) => Some(
                bcs::to_bytes(&digest)
                    .map_err(|err| crate::error::SuiError::GrpcMessageSerializeError {
                        type_info: "RawWaitForEffectsRequest.transaction_digest".to_string(),
                        error: err.to_string(),
                    })?
                    .into(),
            ),
            None => None,
        };
        let consensus_position = match value.consensus_position {
            Some(cp) => Some(cp.into_raw()?),
            None => None,
        };
        let ping_type = value.ping.map(|p| {
            let raw: RawPingType = p.into();
            raw.into()
        });
        Ok(Self {
            consensus_position,
            transaction_digest,
            include_details: value.include_details,
            ping_type,
        })
    }
}

impl TryFrom<RawWaitForEffectsResponse> for WaitForEffectsResponse {
    type Error = crate::error::SuiError;

    fn try_from(value: RawWaitForEffectsResponse) -> Result<Self, Self::Error> {
        match value.inner {
            Some(RawValidatorTransactionStatus::Executed(executed)) => {
                let (effects_digest, details, fast_path) = try_from_raw_executed_status(executed)?;
                Ok(Self::Executed {
                    effects_digest,
                    details,
                    fast_path,
                })
            }
            Some(RawValidatorTransactionStatus::Rejected(rejected)) => {
                let error = try_from_raw_rejected_status(rejected)?;
                Ok(Self::Rejected { error })
            }
            Some(RawValidatorTransactionStatus::Expired(expired)) => Ok(Self::Expired {
                epoch: expired.epoch,
                round: expired.round,
            }),
            None => Err(crate::error::SuiError::GrpcMessageDeserializeError {
                type_info: "RawWaitForEffectsResponse.inner".to_string(),
                error: "RawWaitForEffectsResponse.inner is None".to_string(),
            }),
        }
    }
}

impl TryFrom<WaitForEffectsResponse> for RawWaitForEffectsResponse {
    type Error = crate::error::SuiError;

    fn try_from(value: WaitForEffectsResponse) -> Result<Self, Self::Error> {
        let inner = match value {
            WaitForEffectsResponse::Executed {
                effects_digest,
                details,
                fast_path,
            } => {
                let raw_executed = try_from_response_executed(effects_digest, details, fast_path)?;
                RawValidatorTransactionStatus::Executed(raw_executed)
            }
            WaitForEffectsResponse::Rejected { error } => {
                let raw_rejected = try_from_response_rejected(error)?;
                RawValidatorTransactionStatus::Rejected(raw_rejected)
            }
            WaitForEffectsResponse::Expired { epoch, round } => {
                RawValidatorTransactionStatus::Expired(RawExpiredStatus { epoch, round })
            }
        };
        Ok(RawWaitForEffectsResponse { inner: Some(inner) })
    }
}

impl TryFrom<ValidatorHealthRequest> for RawValidatorHealthRequest {
    type Error = crate::error::SuiError;

    fn try_from(_value: ValidatorHealthRequest) -> Result<Self, Self::Error> {
        Ok(Self {})
    }
}

impl TryFrom<RawValidatorHealthRequest> for ValidatorHealthRequest {
    type Error = crate::error::SuiError;

    fn try_from(_value: RawValidatorHealthRequest) -> Result<Self, Self::Error> {
        // Empty request - ignore reserved field for now
        Ok(Self {})
    }
}

impl TryFrom<ValidatorHealthResponse> for RawValidatorHealthResponse {
    type Error = crate::error::SuiError;

    fn try_from(value: ValidatorHealthResponse) -> Result<Self, Self::Error> {
        Ok(Self {
            pending_certificates: Some(value.num_inflight_execution_transactions),
            inflight_consensus_messages: Some(value.num_inflight_consensus_transactions),
            consensus_round: Some(value.last_committed_leader_round as u64),
            checkpoint_sequence: Some(value.last_locally_built_checkpoint),
        })
    }
}

impl TryFrom<RawValidatorHealthResponse> for ValidatorHealthResponse {
    type Error = crate::error::SuiError;

    fn try_from(value: RawValidatorHealthResponse) -> Result<Self, Self::Error> {
        Ok(Self {
            num_inflight_consensus_transactions: value.inflight_consensus_messages.unwrap_or(0),
            num_inflight_execution_transactions: value.pending_certificates.unwrap_or(0),
            last_locally_built_checkpoint: value.checkpoint_sequence.unwrap_or(0),
            last_committed_leader_round: value.consensus_round.unwrap_or(0) as u32,
        })
    }
}