tendermint/abci/types.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
//! ABCI-specific data types used in requests and responses.
//!
//! These types have changes from the core data structures to better accommodate
//! ABCI applications.
//!
//! [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#data-types)
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use super::{Code, Event};
use crate::{
block::{self, BlockIdFlag},
prelude::*,
serializers, vote, Signature, Time,
};
/// A validator address with voting power.
///
/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#validator)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Validator {
/// The validator's address (the first 20 bytes of `SHA256(public_key)`).
pub address: [u8; 20],
/// The voting power of the validator.
pub power: vote::Power,
}
/// Information about a whether a validator signed the last block.
///
/// [ABCI documentation](https://github.com/cometbft/cometbft/blob/v0.38.x/spec/abci/abci%2B%2B_methods.md#voteinfo)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct VoteInfo {
/// Identifies the validator.
pub validator: Validator,
/// Whether or not the validator signed the last block.
pub sig_info: BlockSignatureInfo,
}
/// Information about a whether a validator signed the last block,
/// together with vote extensions.
///
/// [ABCI documentation](https://github.com/cometbft/cometbft/blob/v0.38.x/spec/abci/abci%2B%2B_methods.md#extendedvoteinfo)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ExtendedVoteInfo {
/// Identifies the validator.
pub validator: Validator,
/// Whether or not the validator signed the last block.
pub sig_info: BlockSignatureInfo,
/// Non-deterministic extension provided by the sending validator's application.
///
/// This field is only used since CometBFT 0.38. It will be ignored when
/// encoding into earlier protocol versions.
pub vote_extension: Bytes,
/// Signature for the vote extension.
///
/// This field has been added in CometBFT 0.38 and will be ignored when
/// encoding into earlier protocol versions.
pub extension_signature: Option<Signature>,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// Information on how the validator voted for a block.
pub enum BlockSignatureInfo {
/// Full information available, as determined by the [`BlockIdFlag`] value.
///
/// In CometBFT versions before 0.38, both [`Commit`] and [`Nil`] values
/// are encoded as the true value of the `signed_last_block` field,
/// losing information on whether the vote was for the block or nil.
///
/// [`Commit`]: BlockIdFlag::Commit
/// [`Nil`]: BlockIdFlag::Nil
Flag(BlockIdFlag),
/// In the message encoded in a CometBFT version before 0.38,
/// the `signed_last_block` field has the value of true.
///
/// This variant should not be used in CometBFT 0.38 or later
/// and will result in the "undefined" encoding in protobuf.
LegacySigned,
}
impl BlockSignatureInfo {
/// Whether the validator has signed the block accordingly to this information.
pub fn is_signed(&self) -> bool {
use BlockIdFlag::*;
match self {
BlockSignatureInfo::Flag(Commit) | BlockSignatureInfo::Flag(Nil) => true,
BlockSignatureInfo::Flag(Absent) => false,
BlockSignatureInfo::LegacySigned => true,
}
}
}
/// The possible kinds of [`Misbehavior`].
///
/// Note: the
/// [ABCI documentation](https://github.com/tendermint/tendermint/blob/main/spec/abci/abci++_methods.md#misbehaviortype)
/// calls this `MisbehaviorType`, but we follow the Rust convention and name it `MisbehaviorKind`
/// to avoid confusion with Rust types.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum MisbehaviorKind {
/// Unknown evidence type (proto default value).
Unknown = 0,
/// Evidence that the validator voted for two different blocks in the same
/// round of the same height.
DuplicateVote = 1,
/// Evidence that a validator attacked a light client.
LightClientAttack = 2,
}
/// Evidence of validator misbehavior.
///
/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#evidence)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Misbehavior {
/// The kind of evidence.
///
/// Note: this field is called `type` in the protobuf, but we call it `kind`
/// to avoid the Rust keyword.
pub kind: MisbehaviorKind,
/// The offending validator.
pub validator: Validator,
/// The height when the offense occurred.
pub height: block::Height,
/// The corresponding time when the offense occurred.
pub time: Time,
/// Total voting power of the validator set at `height`.
///
/// This is included in case the ABCI application does not store historical
/// validators, cf.
/// [#4581](https://github.com/tendermint/tendermint/issues/4581)
pub total_voting_power: vote::Power,
}
/// Information on a block commit.
///
/// [ABCI documentation](https://github.com/cometbft/cometbft/blob/v0.38.x/spec/abci/abci%2B%2B_methods.md#commitinfo)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct CommitInfo {
/// The commit round.
///
/// Reflects the total number of rounds it took to come to consensus for the
/// current block.
pub round: block::Round,
/// The list of validator addresses in the last validator set, with their
/// voting power and whether or not they signed a vote.
pub votes: Vec<VoteInfo>,
}
/// Information on a block commit with provided vote extensions.
///
/// [ABCI documentation](https://github.com/cometbft/cometbft/blob/v0.38.x/spec/abci/abci%2B%2B_methods.md#extendedcommitinfo)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ExtendedCommitInfo {
/// The commit round.
///
/// Reflects the total number of rounds it took to come to consensus for the
/// current block.
pub round: block::Round,
/// The list of validator addresses in the last validator set, with their
/// voting power and whether or not they signed a vote.
pub votes: Vec<ExtendedVoteInfo>,
}
/// Used for state sync snapshots.
///
/// When sent across the network, a `Snapshot` can be at most 4 MB.
///
/// [ABCI documentation](https://docs.tendermint.com/master/spec/abci/abci.html#snapshot)
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Snapshot {
/// The height at which the snapshot was taken
pub height: block::Height,
/// The application-specific snapshot format identifier.
///
/// This allows applications to version their snapshot data format and make
/// backwards-incompatible changes. Tendermint does not interpret this field.
pub format: u32,
/// The number of chunks in the snapshot. Must be at least 1.
pub chunks: u32,
/// An arbitrary snapshot hash.
///
/// This hash must be equal only for identical snapshots across nodes.
/// Tendermint does not interpret the hash, only compares it with other
/// hashes.
pub hash: Bytes,
/// Arbitrary application metadata, e.g., chunk hashes or other verification data.
pub metadata: Bytes,
}
/// Results of executing one individual transaction.
///
/// This structure is equivalent to [`response::DeliverTx`] which will be
/// deprecated and removed.
///
/// [`response::DeliverTx`]: super::response::DeliverTx
#[derive(Clone, PartialEq, Eq, Debug, Default, Serialize, Deserialize)]
pub struct ExecTxResult {
/// The response code.
///
/// This code should be `Ok` only if the transaction is fully valid. However,
/// invalid transactions included in a block will still be executed against
/// the application state.
pub code: Code,
/// Result bytes, if any.
#[serde(with = "serializers::nullable")]
pub data: Bytes,
/// The output of the application's logger.
///
/// **May be non-deterministic**.
pub log: String,
/// Additional information.
///
/// **May be non-deterministic**.
pub info: String,
/// Amount of gas requested for the transaction.
#[serde(with = "serializers::from_str")]
pub gas_wanted: i64,
/// Amount of gas consumed by the transaction.
#[serde(with = "serializers::from_str")]
pub gas_used: i64,
/// Events that occurred while executing the transaction.
pub events: Vec<Event>,
/// The namespace for the `code`.
pub codespace: String,
}
// =============================================================================
// Protobuf conversions
// =============================================================================
mod v0_34 {
use super::{
BlockSignatureInfo, CommitInfo, Misbehavior, MisbehaviorKind, Snapshot, Validator, VoteInfo,
};
use crate::{block::BlockIdFlag, prelude::*, Error};
use tendermint_proto::v0_34::abci as pb;
use tendermint_proto::Protobuf;
use bytes::Bytes;
impl From<Validator> for pb::Validator {
fn from(v: Validator) -> Self {
Self {
address: Bytes::copy_from_slice(&v.address[..]),
power: v.power.into(),
}
}
}
impl TryFrom<pb::Validator> for Validator {
type Error = Error;
fn try_from(vu: pb::Validator) -> Result<Self, Self::Error> {
let address = if vu.address.len() == 20 {
let mut bytes = [0u8; 20];
bytes.copy_from_slice(&vu.address);
bytes
} else {
return Err(Error::invalid_account_id_length());
};
Ok(Self {
address,
power: vu.power.try_into()?,
})
}
}
impl Protobuf<pb::Validator> for Validator {}
impl From<VoteInfo> for pb::VoteInfo {
fn from(vi: VoteInfo) -> Self {
Self {
validator: Some(vi.validator.into()),
signed_last_block: vi.sig_info.is_signed(),
}
}
}
impl TryFrom<pb::VoteInfo> for VoteInfo {
type Error = Error;
fn try_from(vi: pb::VoteInfo) -> Result<Self, Self::Error> {
let sig_info = if vi.signed_last_block {
BlockSignatureInfo::LegacySigned
} else {
BlockSignatureInfo::Flag(BlockIdFlag::Absent)
};
Ok(Self {
validator: vi
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
sig_info,
})
}
}
impl Protobuf<pb::VoteInfo> for VoteInfo {}
impl From<Misbehavior> for pb::Evidence {
fn from(evidence: Misbehavior) -> Self {
Self {
r#type: evidence.kind as i32,
validator: Some(evidence.validator.into()),
height: evidence.height.into(),
time: Some(evidence.time.into()),
total_voting_power: evidence.total_voting_power.into(),
}
}
}
impl TryFrom<pb::Evidence> for Misbehavior {
type Error = Error;
fn try_from(evidence: pb::Evidence) -> Result<Self, Self::Error> {
let kind = match evidence.r#type {
0 => MisbehaviorKind::Unknown,
1 => MisbehaviorKind::DuplicateVote,
2 => MisbehaviorKind::LightClientAttack,
_ => return Err(Error::invalid_evidence()),
};
Ok(Self {
kind,
validator: evidence
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
height: evidence.height.try_into()?,
time: evidence
.time
.ok_or_else(Error::missing_timestamp)?
.try_into()?,
total_voting_power: evidence.total_voting_power.try_into()?,
})
}
}
impl Protobuf<pb::Evidence> for Misbehavior {}
impl From<CommitInfo> for pb::LastCommitInfo {
fn from(lci: CommitInfo) -> Self {
Self {
round: lci.round.into(),
votes: lci.votes.into_iter().map(Into::into).collect(),
}
}
}
impl TryFrom<pb::LastCommitInfo> for CommitInfo {
type Error = Error;
fn try_from(lci: pb::LastCommitInfo) -> Result<Self, Self::Error> {
Ok(Self {
round: lci.round.try_into()?,
votes: lci
.votes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}
impl Protobuf<pb::LastCommitInfo> for CommitInfo {}
impl From<Snapshot> for pb::Snapshot {
fn from(snapshot: Snapshot) -> Self {
Self {
height: snapshot.height.into(),
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
metadata: snapshot.metadata,
}
}
}
impl TryFrom<pb::Snapshot> for Snapshot {
type Error = Error;
fn try_from(snapshot: pb::Snapshot) -> Result<Self, Self::Error> {
Ok(Self {
height: snapshot.height.try_into()?,
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
metadata: snapshot.metadata,
})
}
}
impl Protobuf<pb::Snapshot> for Snapshot {}
}
mod v0_37 {
use super::{
BlockSignatureInfo, CommitInfo, ExtendedCommitInfo, ExtendedVoteInfo, Misbehavior,
MisbehaviorKind, Snapshot, Validator, VoteInfo,
};
use crate::{block::BlockIdFlag, prelude::*, Error};
use tendermint_proto::v0_37::abci as pb;
use tendermint_proto::Protobuf;
use bytes::Bytes;
impl From<Validator> for pb::Validator {
fn from(v: Validator) -> Self {
Self {
address: Bytes::copy_from_slice(&v.address[..]),
power: v.power.into(),
}
}
}
impl TryFrom<pb::Validator> for Validator {
type Error = Error;
fn try_from(vu: pb::Validator) -> Result<Self, Self::Error> {
let address = if vu.address.len() == 20 {
let mut bytes = [0u8; 20];
bytes.copy_from_slice(&vu.address);
bytes
} else {
return Err(Error::invalid_account_id_length());
};
Ok(Self {
address,
power: vu.power.try_into()?,
})
}
}
impl Protobuf<pb::Validator> for Validator {}
impl From<VoteInfo> for pb::VoteInfo {
fn from(vi: VoteInfo) -> Self {
Self {
validator: Some(vi.validator.into()),
signed_last_block: vi.sig_info.is_signed(),
}
}
}
impl TryFrom<pb::VoteInfo> for VoteInfo {
type Error = Error;
fn try_from(vi: pb::VoteInfo) -> Result<Self, Self::Error> {
let sig_info = if vi.signed_last_block {
BlockSignatureInfo::LegacySigned
} else {
BlockSignatureInfo::Flag(BlockIdFlag::Absent)
};
Ok(Self {
validator: vi
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
sig_info,
})
}
}
impl Protobuf<pb::VoteInfo> for VoteInfo {}
// ExtendedVoteInfo is defined in 0.37, but the vote_extension field
// should be always nil and is ignored.
impl From<ExtendedVoteInfo> for pb::ExtendedVoteInfo {
fn from(vi: ExtendedVoteInfo) -> Self {
Self {
validator: Some(vi.validator.into()),
signed_last_block: vi.sig_info.is_signed(),
vote_extension: Default::default(),
}
}
}
impl TryFrom<pb::ExtendedVoteInfo> for ExtendedVoteInfo {
type Error = Error;
fn try_from(vi: pb::ExtendedVoteInfo) -> Result<Self, Self::Error> {
let sig_info = if vi.signed_last_block {
BlockSignatureInfo::LegacySigned
} else {
BlockSignatureInfo::Flag(BlockIdFlag::Absent)
};
Ok(Self {
validator: vi
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
sig_info,
vote_extension: Default::default(),
extension_signature: None,
})
}
}
impl Protobuf<pb::ExtendedVoteInfo> for ExtendedVoteInfo {}
impl From<Misbehavior> for pb::Misbehavior {
fn from(evidence: Misbehavior) -> Self {
Self {
r#type: evidence.kind as i32,
validator: Some(evidence.validator.into()),
height: evidence.height.into(),
time: Some(evidence.time.into()),
total_voting_power: evidence.total_voting_power.into(),
}
}
}
impl TryFrom<pb::Misbehavior> for Misbehavior {
type Error = Error;
fn try_from(evidence: pb::Misbehavior) -> Result<Self, Self::Error> {
let kind = match evidence.r#type {
0 => MisbehaviorKind::Unknown,
1 => MisbehaviorKind::DuplicateVote,
2 => MisbehaviorKind::LightClientAttack,
_ => return Err(Error::invalid_evidence()),
};
Ok(Self {
kind,
validator: evidence
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
height: evidence.height.try_into()?,
time: evidence
.time
.ok_or_else(Error::missing_timestamp)?
.try_into()?,
total_voting_power: evidence.total_voting_power.try_into()?,
})
}
}
impl Protobuf<pb::Misbehavior> for Misbehavior {}
impl From<CommitInfo> for pb::CommitInfo {
fn from(lci: CommitInfo) -> Self {
Self {
round: lci.round.into(),
votes: lci.votes.into_iter().map(Into::into).collect(),
}
}
}
impl TryFrom<pb::CommitInfo> for CommitInfo {
type Error = Error;
fn try_from(lci: pb::CommitInfo) -> Result<Self, Self::Error> {
Ok(Self {
round: lci.round.try_into()?,
votes: lci
.votes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}
impl Protobuf<pb::CommitInfo> for CommitInfo {}
impl From<ExtendedCommitInfo> for pb::ExtendedCommitInfo {
fn from(lci: ExtendedCommitInfo) -> Self {
Self {
round: lci.round.into(),
votes: lci.votes.into_iter().map(Into::into).collect(),
}
}
}
impl TryFrom<pb::ExtendedCommitInfo> for ExtendedCommitInfo {
type Error = Error;
fn try_from(lci: pb::ExtendedCommitInfo) -> Result<Self, Self::Error> {
Ok(Self {
round: lci.round.try_into()?,
votes: lci
.votes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}
impl Protobuf<pb::ExtendedCommitInfo> for ExtendedCommitInfo {}
impl From<Snapshot> for pb::Snapshot {
fn from(snapshot: Snapshot) -> Self {
Self {
height: snapshot.height.into(),
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
metadata: snapshot.metadata,
}
}
}
impl TryFrom<pb::Snapshot> for Snapshot {
type Error = Error;
fn try_from(snapshot: pb::Snapshot) -> Result<Self, Self::Error> {
Ok(Self {
height: snapshot.height.try_into()?,
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
metadata: snapshot.metadata,
})
}
}
impl Protobuf<pb::Snapshot> for Snapshot {}
}
mod v0_38 {
use super::{
BlockSignatureInfo, CommitInfo, ExecTxResult, ExtendedCommitInfo, ExtendedVoteInfo,
Misbehavior, MisbehaviorKind, Snapshot, Validator, VoteInfo,
};
use crate::{prelude::*, Error, Signature};
use tendermint_proto::v0_38::abci as pb;
use tendermint_proto::v0_38::types::BlockIdFlag as RawBlockIdFlag;
use tendermint_proto::Protobuf;
use bytes::Bytes;
impl From<Validator> for pb::Validator {
fn from(v: Validator) -> Self {
Self {
address: Bytes::copy_from_slice(&v.address[..]),
power: v.power.into(),
}
}
}
impl TryFrom<pb::Validator> for Validator {
type Error = Error;
fn try_from(vu: pb::Validator) -> Result<Self, Self::Error> {
let address = if vu.address.len() == 20 {
let mut bytes = [0u8; 20];
bytes.copy_from_slice(&vu.address);
bytes
} else {
return Err(Error::invalid_account_id_length());
};
Ok(Self {
address,
power: vu.power.try_into()?,
})
}
}
impl Protobuf<pb::Validator> for Validator {}
impl From<BlockSignatureInfo> for RawBlockIdFlag {
fn from(value: BlockSignatureInfo) -> Self {
// The API user should not use the LegacySigned flag in
// values for 0.38-based chains. As this conversion is infallible,
// silently convert it to the undefined value.
match value {
BlockSignatureInfo::Flag(flag) => flag.into(),
BlockSignatureInfo::LegacySigned => RawBlockIdFlag::Unknown,
}
}
}
impl From<VoteInfo> for pb::VoteInfo {
fn from(vi: VoteInfo) -> Self {
let block_id_flag: RawBlockIdFlag = vi.sig_info.into();
Self {
validator: Some(vi.validator.into()),
block_id_flag: block_id_flag as i32,
}
}
}
impl TryFrom<pb::VoteInfo> for VoteInfo {
type Error = Error;
fn try_from(vi: pb::VoteInfo) -> Result<Self, Self::Error> {
let block_id_flag: RawBlockIdFlag = vi
.block_id_flag
.try_into()
.map_err(|_| Error::block_id_flag())?;
Ok(Self {
validator: vi
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
sig_info: BlockSignatureInfo::Flag(block_id_flag.try_into()?),
})
}
}
impl Protobuf<pb::VoteInfo> for VoteInfo {}
impl From<ExtendedVoteInfo> for pb::ExtendedVoteInfo {
fn from(vi: ExtendedVoteInfo) -> Self {
let block_id_flag: RawBlockIdFlag = vi.sig_info.into();
Self {
validator: Some(vi.validator.into()),
vote_extension: vi.vote_extension,
extension_signature: vi.extension_signature.map(Into::into).unwrap_or_default(),
block_id_flag: block_id_flag as i32,
}
}
}
impl TryFrom<pb::ExtendedVoteInfo> for ExtendedVoteInfo {
type Error = Error;
fn try_from(vi: pb::ExtendedVoteInfo) -> Result<Self, Self::Error> {
let block_id_flag: RawBlockIdFlag = vi
.block_id_flag
.try_into()
.map_err(|_| Error::block_id_flag())?;
Ok(Self {
validator: vi
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
sig_info: BlockSignatureInfo::Flag(block_id_flag.try_into()?),
vote_extension: vi.vote_extension,
extension_signature: Signature::new(vi.extension_signature)?,
})
}
}
impl Protobuf<pb::ExtendedVoteInfo> for ExtendedVoteInfo {}
impl From<Misbehavior> for pb::Misbehavior {
fn from(evidence: Misbehavior) -> Self {
Self {
r#type: evidence.kind as i32,
validator: Some(evidence.validator.into()),
height: evidence.height.into(),
time: Some(evidence.time.into()),
total_voting_power: evidence.total_voting_power.into(),
}
}
}
impl TryFrom<pb::Misbehavior> for Misbehavior {
type Error = Error;
fn try_from(evidence: pb::Misbehavior) -> Result<Self, Self::Error> {
let kind = match evidence.r#type {
0 => MisbehaviorKind::Unknown,
1 => MisbehaviorKind::DuplicateVote,
2 => MisbehaviorKind::LightClientAttack,
_ => return Err(Error::invalid_evidence()),
};
Ok(Self {
kind,
validator: evidence
.validator
.ok_or_else(Error::missing_validator)?
.try_into()?,
height: evidence.height.try_into()?,
time: evidence
.time
.ok_or_else(Error::missing_timestamp)?
.try_into()?,
total_voting_power: evidence.total_voting_power.try_into()?,
})
}
}
impl Protobuf<pb::Misbehavior> for Misbehavior {}
impl From<CommitInfo> for pb::CommitInfo {
fn from(lci: CommitInfo) -> Self {
Self {
round: lci.round.into(),
votes: lci.votes.into_iter().map(Into::into).collect(),
}
}
}
impl TryFrom<pb::CommitInfo> for CommitInfo {
type Error = Error;
fn try_from(lci: pb::CommitInfo) -> Result<Self, Self::Error> {
Ok(Self {
round: lci.round.try_into()?,
votes: lci
.votes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}
impl Protobuf<pb::CommitInfo> for CommitInfo {}
impl From<ExtendedCommitInfo> for pb::ExtendedCommitInfo {
fn from(lci: ExtendedCommitInfo) -> Self {
Self {
round: lci.round.into(),
votes: lci.votes.into_iter().map(Into::into).collect(),
}
}
}
impl TryFrom<pb::ExtendedCommitInfo> for ExtendedCommitInfo {
type Error = Error;
fn try_from(lci: pb::ExtendedCommitInfo) -> Result<Self, Self::Error> {
Ok(Self {
round: lci.round.try_into()?,
votes: lci
.votes
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
})
}
}
impl Protobuf<pb::ExtendedCommitInfo> for ExtendedCommitInfo {}
impl From<Snapshot> for pb::Snapshot {
fn from(snapshot: Snapshot) -> Self {
Self {
height: snapshot.height.into(),
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
metadata: snapshot.metadata,
}
}
}
impl TryFrom<pb::Snapshot> for Snapshot {
type Error = Error;
fn try_from(snapshot: pb::Snapshot) -> Result<Self, Self::Error> {
Ok(Self {
height: snapshot.height.try_into()?,
format: snapshot.format,
chunks: snapshot.chunks,
hash: snapshot.hash,
metadata: snapshot.metadata,
})
}
}
impl Protobuf<pb::Snapshot> for Snapshot {}
impl From<ExecTxResult> for pb::ExecTxResult {
fn from(deliver_tx: ExecTxResult) -> Self {
Self {
code: deliver_tx.code.into(),
data: deliver_tx.data,
log: deliver_tx.log,
info: deliver_tx.info,
gas_wanted: deliver_tx.gas_wanted,
gas_used: deliver_tx.gas_used,
events: deliver_tx.events.into_iter().map(Into::into).collect(),
codespace: deliver_tx.codespace,
}
}
}
impl TryFrom<pb::ExecTxResult> for ExecTxResult {
type Error = Error;
fn try_from(deliver_tx: pb::ExecTxResult) -> Result<Self, Self::Error> {
Ok(Self {
code: deliver_tx.code.into(),
data: deliver_tx.data,
log: deliver_tx.log,
info: deliver_tx.info,
gas_wanted: deliver_tx.gas_wanted,
gas_used: deliver_tx.gas_used,
events: deliver_tx
.events
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
codespace: deliver_tx.codespace,
})
}
}
impl Protobuf<pb::ExecTxResult> for ExecTxResult {}
}