celestia-types 1.0.0

Core types, traits and constants for working with the Celestia ecosystem
Documentation
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
//! Types relaying malfeasance evidence

#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
pub use wbg::*;

#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
mod wbg {
    use tendermint::evidence::{ConflictingBlock, Evidence};
    use tendermint::vote::{Type as VoteType, Vote};
    use wasm_bindgen::prelude::*;

    use crate::block::{JsBlockId, JsSignedHeader};
    use crate::signature::JsSignature;
    use crate::validator_set::{JsValidatorInfo, JsValidatorSet};

    /// Votes are signed messages from validators for a particular block which include
    /// information about the validator signing it.
    #[derive(Clone, Debug)]
    #[wasm_bindgen(getter_with_clone, js_name = "Vote")]
    pub struct JsVote {
        /// Type of vote (prevote or precommit)
        pub vote_type: JsVoteType,
        /// Block height
        pub height: u64,
        /// Round
        pub round: u32,
        /// Block ID
        pub block_id: Option<JsBlockId>,
        /// Timestamp
        pub timestamp: Option<String>,
        /// Validator address
        pub validator_address: String,
        /// Validator index
        pub validator_index: u32,
        /// Signature
        pub signature: Option<JsSignature>,
        /// Vote extension provided by the application. Only valid for precommit messages.
        pub extension: Vec<u8>,
        /// Vote extension signature by the validator Only valid for precommit messages.
        pub extension_signature: Option<JsSignature>,
    }

    impl From<Vote> for JsVote {
        fn from(value: Vote) -> Self {
            JsVote {
                vote_type: value.vote_type.into(),
                height: value.height.value(),
                round: value.round.into(),
                block_id: value.block_id.map(Into::into),
                timestamp: value.timestamp.map(|ts| ts.to_rfc3339()),
                validator_address: value.validator_address.to_string(),
                validator_index: value.validator_index.into(),
                signature: value.signature.map(Into::into),
                extension: value.extension,
                extension_signature: value.extension_signature.map(Into::into),
            }
        }
    }

    /// Types of votes
    #[derive(Clone, Copy, Debug)]
    #[wasm_bindgen(js_name = "VoteType")]
    pub enum JsVoteType {
        /// Prevote
        Prevote,
        /// Precommit
        Precommit,
    }

    impl From<VoteType> for JsVoteType {
        fn from(value: VoteType) -> Self {
            match value {
                VoteType::Prevote => JsVoteType::Prevote,
                VoteType::Precommit => JsVoteType::Precommit,
            }
        }
    }

    /// Duplicate vote evidence
    #[derive(Clone, Debug)]
    #[wasm_bindgen(getter_with_clone, js_name = "DuplicateVoteEvidence")]
    pub struct JsDuplicateVoteEvidence {
        /// Vote A
        pub vote_a: JsVote,
        /// Vote B
        pub vote_b: JsVote,
        /// Total voting power
        pub total_voting_power: u64,
        /// Validator power
        pub validator_power: u64,
        /// Timestamp
        pub timestamp: String,
    }
    /// Conflicting block detected in light client attack
    #[derive(Clone, Debug)]
    #[wasm_bindgen(getter_with_clone, js_name = "ConflictingBlock")]
    pub struct JsConflictingBlock {
        /// Signed header
        pub signed_header: JsSignedHeader,
        /// Validator set
        pub validator_set: JsValidatorSet,
    }

    impl From<ConflictingBlock> for JsConflictingBlock {
        fn from(value: ConflictingBlock) -> Self {
            JsConflictingBlock {
                signed_header: value.signed_header.into(),
                validator_set: value.validator_set.into(),
            }
        }
    }

    /// LightClient attack evidence
    #[derive(Clone, Debug)]
    #[wasm_bindgen(getter_with_clone, js_name = "LightClientAttackEvidence")]
    pub struct JsLightClientAttackEvidence {
        /// Conflicting block
        pub conflicting_block: JsConflictingBlock,
        /// Common height
        pub common_height: u64,
        /// Byzantine validators
        pub byzantine_validators: Vec<JsValidatorInfo>,
        /// Total voting power
        pub total_voting_power: u64,
        /// Timestamp
        pub timestamp: String,
    }

    /// Attack type for the associated evidence
    #[wasm_bindgen(js_name = "AttackType")]
    pub enum JsAttackType {
        /// Duplicate vote
        DuplicateVote,
        /// LightClient attack
        LightClient,
    }

    /// Evidence of malfeasance by validators (i.e. signing conflicting votes or light client attack).
    #[wasm_bindgen(js_name = Evidence)]
    pub struct JsEvidence(Evidence);

    #[wasm_bindgen]
    impl JsEvidence {
        /// type of the attack for the provided evidence
        pub fn attack_type(&self) -> JsAttackType {
            match &self.0 {
                Evidence::DuplicateVote(_) => JsAttackType::DuplicateVote,
                Evidence::LightClientAttack(_) => JsAttackType::LightClient,
            }
        }

        /// get duplicate vote evidence, if appropriate type
        #[wasm_bindgen(getter)]
        pub fn duplicate_vote_evidence(&self) -> Option<JsDuplicateVoteEvidence> {
            let Evidence::DuplicateVote(e) = &self.0 else {
                return None;
            };
            Some(JsDuplicateVoteEvidence {
                vote_a: e.vote_a.clone().into(),
                vote_b: e.vote_b.clone().into(),
                total_voting_power: e.total_voting_power.into(),
                validator_power: e.validator_power.into(),
                timestamp: e.timestamp.to_rfc3339(),
            })
        }

        /// get light client attack evidence, if appropriate type
        #[wasm_bindgen(getter)]
        pub fn light_client_attack_evidence(&self) -> Option<JsLightClientAttackEvidence> {
            let Evidence::LightClientAttack(e) = &self.0 else {
                return None;
            };
            Some(JsLightClientAttackEvidence {
                conflicting_block: e.conflicting_block.clone().into(),
                common_height: e.common_height.value(),
                byzantine_validators: e
                    .byzantine_validators
                    .iter()
                    .cloned()
                    .map(Into::into)
                    .collect(),
                total_voting_power: e.total_voting_power.into(),
                timestamp: e.timestamp.to_rfc3339(),
            })
        }
    }

    impl From<Evidence> for JsEvidence {
        fn from(value: Evidence) -> Self {
            JsEvidence(value)
        }
    }
}

/// uniffi conversion types
#[cfg(feature = "uniffi")]
pub mod uniffi_types {
    use tendermint::evidence::{
        ConflictingBlock as TendermintConfliclingBlock, DuplicateVoteEvidence,
        Evidence as TendermintEvidence, LightClientAttackEvidence, List as TendermintEvidenceList,
    };
    use tendermint::vote::{Type as VoteType, Vote as TendermintVote};
    use uniffi::{Enum, Record};

    use crate::block::header::uniffi_types::SignedHeader;
    use crate::block::uniffi_types::{BlockHeight, BlockId};
    use crate::error::UniffiConversionError;
    use crate::signature::uniffi_types::Signature;
    use crate::state::UniffiAccountId;
    use crate::uniffi_types::Time;
    use crate::validator_set::uniffi_types::{ValidatorInfo, ValidatorSet};

    /// Conflicting block detected in light client attack
    #[derive(Record)]
    pub struct ConflictingBlock {
        /// Signed header
        pub signed_header: SignedHeader,
        /// Validator set
        pub validator_set: ValidatorSet,
    }

    impl TryFrom<TendermintConfliclingBlock> for ConflictingBlock {
        type Error = UniffiConversionError;

        fn try_from(value: TendermintConfliclingBlock) -> Result<Self, Self::Error> {
            Ok(ConflictingBlock {
                signed_header: value.signed_header.try_into()?,
                validator_set: value.validator_set.into(),
            })
        }
    }

    impl TryFrom<ConflictingBlock> for TendermintConfliclingBlock {
        type Error = UniffiConversionError;

        fn try_from(value: ConflictingBlock) -> Result<Self, Self::Error> {
            Ok(TendermintConfliclingBlock {
                signed_header: value.signed_header.try_into()?,
                validator_set: value.validator_set.try_into()?,
            })
        }
    }

    uniffi::custom_type!(TendermintEvidenceList, Vec<Evidence>, {
        remote,
        try_lift: |value| {
            let evidence : Vec<_> = value.into_iter().map(|e| e.try_into()).collect::<Result<_, _>>()?;
            Ok(TendermintEvidenceList::new(evidence))
        },
        lower: |value| value.into_vec().into_iter().map(|e| e.try_into()).collect::<Result<_,_>>().expect("valid timestamps")
    });

    /// Evidence of malfeasance by validators (i.e. signing conflicting votes or light client attack).
    #[allow(clippy::large_enum_variant)]
    #[derive(Enum)]
    pub enum Evidence {
        /// Duplicate vote evidence
        DuplicateVote {
            /// Vote A
            vote_a: Vote,
            /// Vote B
            vote_b: Vote,
            /// Total voting power
            total_voting_power: u64,
            /// Validator power
            validator_power: u64,
            /// Timestamp
            timestamp: Time,
        },
        /// LightClient attack evidence
        LightClientAttack {
            /// Conflicting block
            conflicting_block: ConflictingBlock,
            /// Common height
            common_height: u64,
            /// Byzantine validators
            byzantine_validators: Vec<ValidatorInfo>,
            /// Total voting power
            total_voting_power: u64,
            /// Timestamp
            timestamp: Time,
        },
    }

    impl TryFrom<TendermintEvidence> for Evidence {
        type Error = UniffiConversionError;

        fn try_from(value: TendermintEvidence) -> Result<Self, Self::Error> {
            Ok(match value {
                TendermintEvidence::DuplicateVote(e) => Evidence::DuplicateVote {
                    vote_a: e.vote_a.try_into()?,
                    vote_b: e.vote_b.try_into()?,
                    total_voting_power: e.total_voting_power.value(),
                    validator_power: e.validator_power.value(),
                    timestamp: e.timestamp.try_into()?,
                },
                TendermintEvidence::LightClientAttack(e) => Evidence::LightClientAttack {
                    conflicting_block: e.conflicting_block.try_into()?,
                    common_height: e.common_height.into(),
                    byzantine_validators: e
                        .byzantine_validators
                        .into_iter()
                        .map(|v| v.into())
                        .collect(),
                    total_voting_power: e.total_voting_power.value(),
                    timestamp: e.timestamp.try_into()?,
                },
            })
        }
    }

    impl TryFrom<Evidence> for TendermintEvidence {
        type Error = UniffiConversionError;
        fn try_from(value: Evidence) -> Result<Self, Self::Error> {
            match value {
                Evidence::DuplicateVote {
                    vote_a,
                    vote_b,
                    total_voting_power,
                    validator_power,
                    timestamp,
                } => {
                    let evidence = DuplicateVoteEvidence {
                        vote_a: vote_a.try_into()?,
                        vote_b: vote_b.try_into()?,
                        total_voting_power: total_voting_power
                            .try_into()
                            .map_err(|_| UniffiConversionError::InvalidVotingPower)?,
                        validator_power: validator_power
                            .try_into()
                            .map_err(|_| UniffiConversionError::InvalidVotingPower)?,
                        timestamp: timestamp.try_into()?,
                    };
                    Ok(TendermintEvidence::DuplicateVote(Box::new(evidence)))
                }
                Evidence::LightClientAttack {
                    conflicting_block,
                    common_height,
                    byzantine_validators,
                    total_voting_power,
                    timestamp,
                } => {
                    let evidence = LightClientAttackEvidence {
                        conflicting_block: conflicting_block.try_into()?,
                        common_height: common_height
                            .try_into()
                            .map_err(|_| UniffiConversionError::HeaderHeightOutOfRange)?,
                        byzantine_validators: byzantine_validators
                            .into_iter()
                            .map(|v| v.try_into())
                            .collect::<Result<_, _>>()?,
                        total_voting_power: total_voting_power
                            .try_into()
                            .map_err(|_| UniffiConversionError::InvalidVotingPower)?,
                        timestamp: timestamp.try_into()?,
                    };

                    Ok(TendermintEvidence::LightClientAttack(Box::new(evidence)))
                }
            }
        }
    }

    /// Types of votes
    #[uniffi::remote(Enum)]
    #[repr(u8)]
    pub enum VoteType {
        /// Prevote
        Prevote = 1,
        /// Precommit
        Precommit = 2,
    }

    /// Votes are signed messages from validators for a particular block which include information
    /// about the validator signing it.
    #[derive(Record)]
    pub struct Vote {
        /// Type of vote (prevote or precommit)
        pub vote_type: VoteType,
        /// Block height
        pub height: BlockHeight,
        /// Round
        pub round: u32,
        /// Block ID
        pub block_id: Option<BlockId>,
        /// Timestamp
        pub timestamp: Option<Time>,
        /// Validator address
        pub validator_address: UniffiAccountId,
        /// Validator index
        pub validator_index: u32,
        /// Signature
        pub signature: Option<Signature>,
        /// Vote extension provided by the application. Only valid for precommit messages.
        pub extension: Vec<u8>,
        /// Vote extension signature by the validator Only valid for precommit messages.
        pub extension_signature: Option<Signature>,
    }

    impl TryFrom<Vote> for TendermintVote {
        type Error = UniffiConversionError;

        fn try_from(value: Vote) -> Result<Self, Self::Error> {
            Ok(TendermintVote {
                vote_type: value.vote_type,
                height: value.height.try_into()?,
                round: value
                    .round
                    .try_into()
                    .map_err(|_| UniffiConversionError::InvalidRoundIndex)?,
                block_id: value.block_id.map(TryInto::try_into).transpose()?,
                timestamp: value.timestamp.map(TryInto::try_into).transpose()?,
                validator_address: value.validator_address.try_into()?,
                validator_index: value
                    .validator_index
                    .try_into()
                    .map_err(|_| UniffiConversionError::InvalidValidatorIndex)?,
                signature: value.signature.map(TryInto::try_into).transpose()?,
                extension: value.extension,
                extension_signature: value
                    .extension_signature
                    .map(TryInto::try_into)
                    .transpose()?,
            })
        }
    }

    impl TryFrom<TendermintVote> for Vote {
        type Error = UniffiConversionError;

        fn try_from(value: TendermintVote) -> Result<Self, Self::Error> {
            Ok(Vote {
                vote_type: value.vote_type,
                height: value.height.into(),
                round: value.round.value(),
                block_id: value.block_id.map(Into::into),
                timestamp: value.timestamp.map(TryInto::try_into).transpose()?,
                validator_address: value.validator_address.into(),
                validator_index: value.validator_index.value(),
                signature: value.signature.map(Into::into),
                extension: value.extension,
                extension_signature: value.extension_signature.map(Into::into),
            })
        }
    }

    uniffi::custom_type!(TendermintVote, Vote, {
        remote,
        try_lift: |value| Ok(value.try_into()?),
        lower: |value| value.try_into().expect("valid tendermint time")
    });
}