Trait ibc_core::primitives::proto::Protobuf

source ·
pub trait Protobuf<T>: Sized + Clone + TryFrom<T>
where T: Message + From<Self> + Default, Self::Error: Display,
{ // Provided methods fn encode<B>(self, buf: &mut B) -> Result<(), Error> where B: BufMut { ... } fn encode_length_delimited<B>(self, buf: &mut B) -> Result<(), Error> where B: BufMut { ... } fn decode<B>(buf: B) -> Result<Self, Error> where B: Buf { ... } fn decode_length_delimited<B>(buf: B) -> Result<Self, Error> where B: Buf { ... } fn encoded_len(self) -> usize { ... } fn encode_vec(self) -> Vec<u8> { ... } fn decode_vec(v: &[u8]) -> Result<Self, Error> { ... } fn encode_length_delimited_vec(self) -> Vec<u8> { ... } fn decode_length_delimited_vec(v: &[u8]) -> Result<Self, Error> { ... } }
Expand description

Allows for easy Google Protocol Buffers encoding and decoding of domain types with validation.

§Examples

use bytes::BufMut;
use prost::Message;
use core::convert::TryFrom;
use tendermint_proto::Protobuf;

// This struct would ordinarily be automatically generated by prost.
#[derive(Clone, PartialEq, Message)]
pub struct MyRawType {
    #[prost(uint64, tag="1")]
    pub a: u64,
    #[prost(string, tag="2")]
    pub b: String,
}

#[derive(Clone)]
pub struct MyDomainType {
    a: u64,
    b: String,
}

impl MyDomainType {
    /// Trivial constructor with basic validation logic.
    pub fn new(a: u64, b: String) -> Result<Self, String> {
        if a < 1 {
            return Err("a must be greater than 0".to_owned());
        }
        Ok(Self { a, b })
    }
}

impl TryFrom<MyRawType> for MyDomainType {
    type Error = String;

    fn try_from(value: MyRawType) -> Result<Self, Self::Error> {
        Self::new(value.a, value.b)
    }
}

impl From<MyDomainType> for MyRawType {
    fn from(value: MyDomainType) -> Self {
        Self { a: value.a, b: value.b }
    }
}

impl Protobuf<MyRawType> for MyDomainType {}


// Simulate an incoming valid raw message
let valid_raw = MyRawType { a: 1, b: "Hello!".to_owned() };
let mut valid_raw_bytes: Vec<u8> = Vec::new();
valid_raw.encode(&mut valid_raw_bytes).unwrap();
assert!(!valid_raw_bytes.is_empty());

// Try to decode the simulated incoming message
let valid_domain = MyDomainType::decode(valid_raw_bytes.clone().as_ref()).unwrap();
assert_eq!(1, valid_domain.a);
assert_eq!("Hello!".to_owned(), valid_domain.b);

// Encode it to compare the serialized form to what we received
let mut valid_domain_bytes: Vec<u8> = Vec::new();
valid_domain.encode(&mut valid_domain_bytes).unwrap();
assert_eq!(valid_raw_bytes, valid_domain_bytes);

// Simulate an incoming invalid raw message
let invalid_raw = MyRawType { a: 0, b: "Hello!".to_owned() };
let mut invalid_raw_bytes: Vec<u8> = Vec::new();
invalid_raw.encode(&mut invalid_raw_bytes).unwrap();

// We expect a validation error here
assert!(MyDomainType::decode(invalid_raw_bytes.as_ref()).is_err());

Provided Methods§

source

fn encode<B>(self, buf: &mut B) -> Result<(), Error>
where B: BufMut,

Encode into a buffer in Protobuf format.

Uses prost::Message::encode after converting into its counterpart Protobuf data structure.

source

fn encode_length_delimited<B>(self, buf: &mut B) -> Result<(), Error>
where B: BufMut,

Encode with a length-delimiter to a buffer in Protobuf format.

An error will be returned if the buffer does not have sufficient capacity.

Uses prost::Message::encode_length_delimited after converting into its counterpart Protobuf data structure.

source

fn decode<B>(buf: B) -> Result<Self, Error>
where B: Buf,

Constructor that attempts to decode an instance from a buffer.

The entire buffer will be consumed.

Similar to prost::Message::decode but with additional validation prior to constructing the destination type.

source

fn decode_length_delimited<B>(buf: B) -> Result<Self, Error>
where B: Buf,

Constructor that attempts to decode a length-delimited instance from the buffer.

The entire buffer will be consumed.

Similar to prost::Message::decode_length_delimited but with additional validation prior to constructing the destination type.

source

fn encoded_len(self) -> usize

Returns the encoded length of the message without a length delimiter.

Uses prost::Message::encoded_len after converting to its counterpart Protobuf data structure.

source

fn encode_vec(self) -> Vec<u8>

Encodes into a Protobuf-encoded Vec<u8>.

source

fn decode_vec(v: &[u8]) -> Result<Self, Error>

Constructor that attempts to decode a Protobuf-encoded instance from a Vec<u8> (or equivalent).

source

fn encode_length_delimited_vec(self) -> Vec<u8>

Encode with a length-delimiter to a Vec<u8> Protobuf-encoded message.

source

fn decode_length_delimited_vec(v: &[u8]) -> Result<Self, Error>

Constructor that attempts to decode a Protobuf-encoded instance with a length-delimiter from a Vec<u8> or equivalent.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Protobuf<i32> for Type

source§

impl Protobuf<i32> for Type

source§

impl Protobuf<i64> for Height

source§

impl Protobuf<String> for Id

source§

impl Protobuf<Vec<u8>> for Hash

source§

impl Protobuf<Vec<u8>> for Id

source§

impl Protobuf<Vec<u8>> for AppHash

source§

impl Protobuf<Vec<u8>> for Signature

source§

impl Protobuf<Duration> for Duration

source§

impl Protobuf<Timestamp> for Time

source§

impl Protobuf<BlockParams> for Size

source§

impl Protobuf<ConsensusParams> for Params

source§

impl Protobuf<Event> for Event

source§

impl Protobuf<EventAttribute> for EventAttribute

source§

impl Protobuf<Evidence> for Misbehavior

source§

impl Protobuf<LastCommitInfo> for CommitInfo

source§

impl Protobuf<Request> for Request

source§

impl Protobuf<RequestApplySnapshotChunk> for ApplySnapshotChunk

source§

impl Protobuf<RequestBeginBlock> for BeginBlock

source§

impl Protobuf<RequestCheckTx> for CheckTx

source§

impl Protobuf<RequestDeliverTx> for DeliverTx

source§

impl Protobuf<RequestEcho> for Echo

source§

impl Protobuf<RequestEndBlock> for EndBlock

source§

impl Protobuf<RequestInfo> for Info

source§

impl Protobuf<RequestInitChain> for InitChain

source§

impl Protobuf<RequestLoadSnapshotChunk> for LoadSnapshotChunk

source§

impl Protobuf<RequestOfferSnapshot> for OfferSnapshot

source§

impl Protobuf<RequestQuery> for Query

source§

impl Protobuf<RequestSetOption> for SetOption

source§

impl Protobuf<Response> for Response

source§

impl Protobuf<ResponseApplySnapshotChunk> for ApplySnapshotChunk

source§

impl Protobuf<ResponseBeginBlock> for BeginBlock

source§

impl Protobuf<ResponseCheckTx> for CheckTx

source§

impl Protobuf<ResponseCommit> for Commit

source§

impl Protobuf<ResponseDeliverTx> for DeliverTx

source§

impl Protobuf<ResponseEcho> for Echo

source§

impl Protobuf<ResponseEndBlock> for EndBlock

source§

impl Protobuf<ResponseException> for Exception

source§

impl Protobuf<ResponseInfo> for Info

source§

impl Protobuf<ResponseInitChain> for InitChain

source§

impl Protobuf<ResponseListSnapshots> for ListSnapshots

source§

impl Protobuf<ResponseLoadSnapshotChunk> for LoadSnapshotChunk

source§

impl Protobuf<ResponseOfferSnapshot> for OfferSnapshot

source§

impl Protobuf<ResponseQuery> for Query

source§

impl Protobuf<ResponseSetOption> for SetOption

source§

impl Protobuf<Snapshot> for Snapshot

source§

impl Protobuf<Validator> for Validator

source§

impl Protobuf<ValidatorUpdate> for Update

source§

impl Protobuf<VoteInfo> for VoteInfo

source§

impl Protobuf<Proof> for Proof

source§

impl Protobuf<ProofOp> for ProofOp

source§

impl Protobuf<ProofOps> for ProofOps

source§

impl Protobuf<PublicKey> for PublicKey

source§

impl Protobuf<PubKeyRequest> for PubKeyRequest

source§

impl Protobuf<PubKeyResponse> for PubKeyResponse

source§

impl Protobuf<SignProposalRequest> for SignProposalRequest

source§

impl Protobuf<SignVoteRequest> for SignVoteRequest

source§

impl Protobuf<SignedProposalResponse> for SignedProposalResponse

source§

impl Protobuf<SignedVoteResponse> for SignedVoteResponse

source§

impl Protobuf<Block> for Block

source§

impl Protobuf<BlockId> for Id

source§

impl Protobuf<BlockParams> for Size

source§

impl Protobuf<CanonicalProposal> for CanonicalProposal

source§

impl Protobuf<CanonicalVote> for CanonicalVote

source§

impl Protobuf<ConsensusParams> for Params

source§

impl Protobuf<DuplicateVoteEvidence> for DuplicateVoteEvidence

source§

impl Protobuf<Evidence> for Evidence

source§

impl Protobuf<EvidenceList> for List

source§

impl Protobuf<EvidenceParams> for Params

source§

impl Protobuf<Header> for Header

source§

impl Protobuf<LightBlock> for ConflictingBlock

source§

impl Protobuf<LightClientAttackEvidence> for LightClientAttackEvidence

source§

impl Protobuf<PartSetHeader> for Header

source§

impl Protobuf<Proposal> for Proposal

source§

impl Protobuf<SignedHeader> for SignedHeader

source§

impl Protobuf<SimpleValidator> for SimpleValidator

source§

impl Protobuf<ValidatorParams> for ValidatorParams

source§

impl Protobuf<ValidatorSet> for Set

source§

impl Protobuf<VersionParams> for VersionParams

source§

impl Protobuf<Vote> for Vote

source§

impl Protobuf<Consensus> for Version

source§

impl Protobuf<CommitInfo> for CommitInfo

source§

impl Protobuf<Event> for Event

source§

impl Protobuf<EventAttribute> for EventAttribute

source§

impl Protobuf<ExtendedCommitInfo> for ExtendedCommitInfo

source§

impl Protobuf<ExtendedVoteInfo> for ExtendedVoteInfo

source§

impl Protobuf<Misbehavior> for Misbehavior

source§

impl Protobuf<Request> for Request

source§

impl Protobuf<RequestApplySnapshotChunk> for ApplySnapshotChunk

source§

impl Protobuf<RequestBeginBlock> for BeginBlock

source§

impl Protobuf<RequestCheckTx> for CheckTx

source§

impl Protobuf<RequestDeliverTx> for DeliverTx

source§

impl Protobuf<RequestEcho> for Echo

source§

impl Protobuf<RequestEndBlock> for EndBlock

source§

impl Protobuf<RequestInfo> for Info

source§

impl Protobuf<RequestInitChain> for InitChain

source§

impl Protobuf<RequestLoadSnapshotChunk> for LoadSnapshotChunk

source§

impl Protobuf<RequestOfferSnapshot> for OfferSnapshot

source§

impl Protobuf<RequestPrepareProposal> for PrepareProposal

source§

impl Protobuf<RequestProcessProposal> for ProcessProposal

source§

impl Protobuf<RequestQuery> for Query

source§

impl Protobuf<Response> for Response

source§

impl Protobuf<ResponseApplySnapshotChunk> for ApplySnapshotChunk

source§

impl Protobuf<ResponseBeginBlock> for BeginBlock

source§

impl Protobuf<ResponseCheckTx> for CheckTx

source§

impl Protobuf<ResponseCommit> for Commit

source§

impl Protobuf<ResponseDeliverTx> for DeliverTx

source§

impl Protobuf<ResponseEcho> for Echo

source§

impl Protobuf<ResponseEndBlock> for EndBlock

source§

impl Protobuf<ResponseException> for Exception

source§

impl Protobuf<ResponseInfo> for Info

source§

impl Protobuf<ResponseInitChain> for InitChain

source§

impl Protobuf<ResponseListSnapshots> for ListSnapshots

source§

impl Protobuf<ResponseLoadSnapshotChunk> for LoadSnapshotChunk

source§

impl Protobuf<ResponseOfferSnapshot> for OfferSnapshot

source§

impl Protobuf<ResponsePrepareProposal> for PrepareProposal

source§

impl Protobuf<ResponseProcessProposal> for ProcessProposal

source§

impl Protobuf<ResponseQuery> for Query

source§

impl Protobuf<Snapshot> for Snapshot

source§

impl Protobuf<Validator> for Validator

source§

impl Protobuf<ValidatorUpdate> for Update

source§

impl Protobuf<VoteInfo> for VoteInfo

source§

impl Protobuf<Proof> for Proof

source§

impl Protobuf<ProofOp> for ProofOp

source§

impl Protobuf<ProofOps> for ProofOps

source§

impl Protobuf<PublicKey> for PublicKey

source§

impl Protobuf<PubKeyRequest> for PubKeyRequest

source§

impl Protobuf<PubKeyResponse> for PubKeyResponse

source§

impl Protobuf<SignProposalRequest> for SignProposalRequest

source§

impl Protobuf<SignVoteRequest> for SignVoteRequest

source§

impl Protobuf<SignedProposalResponse> for SignedProposalResponse

source§

impl Protobuf<SignedVoteResponse> for SignedVoteResponse

source§

impl Protobuf<Block> for Block

source§

impl Protobuf<BlockId> for Id

source§

impl Protobuf<BlockParams> for Size

source§

impl Protobuf<CanonicalProposal> for CanonicalProposal

source§

impl Protobuf<CanonicalVote> for CanonicalVote

source§

impl Protobuf<ConsensusParams> for Params

source§

impl Protobuf<DuplicateVoteEvidence> for DuplicateVoteEvidence

source§

impl Protobuf<Evidence> for Evidence

source§

impl Protobuf<EvidenceList> for List

source§

impl Protobuf<EvidenceParams> for Params

source§

impl Protobuf<Header> for Header

source§

impl Protobuf<LightBlock> for ConflictingBlock

source§

impl Protobuf<LightClientAttackEvidence> for LightClientAttackEvidence

source§

impl Protobuf<PartSetHeader> for Header

source§

impl Protobuf<Proposal> for Proposal

source§

impl Protobuf<SignedHeader> for SignedHeader

source§

impl Protobuf<SimpleValidator> for SimpleValidator

source§

impl Protobuf<TxProof> for Proof

source§

impl Protobuf<ValidatorParams> for ValidatorParams

source§

impl Protobuf<ValidatorSet> for Set

source§

impl Protobuf<VersionParams> for VersionParams

source§

impl Protobuf<Vote> for Vote

source§

impl Protobuf<Consensus> for Version

source§

impl Protobuf<CommitInfo> for CommitInfo

source§

impl Protobuf<Event> for Event

source§

impl Protobuf<EventAttribute> for EventAttribute

source§

impl Protobuf<ExecTxResult> for ExecTxResult

source§

impl Protobuf<ExtendedCommitInfo> for ExtendedCommitInfo

source§

impl Protobuf<ExtendedVoteInfo> for ExtendedVoteInfo

source§

impl Protobuf<Misbehavior> for Misbehavior

source§

impl Protobuf<Request> for Request

source§

impl Protobuf<RequestApplySnapshotChunk> for ApplySnapshotChunk

source§

impl Protobuf<RequestCheckTx> for CheckTx

source§

impl Protobuf<RequestEcho> for Echo

source§

impl Protobuf<RequestExtendVote> for ExtendVote

source§

impl Protobuf<RequestFinalizeBlock> for FinalizeBlock

source§

impl Protobuf<RequestInfo> for Info

source§

impl Protobuf<RequestInitChain> for InitChain

source§

impl Protobuf<RequestLoadSnapshotChunk> for LoadSnapshotChunk

source§

impl Protobuf<RequestOfferSnapshot> for OfferSnapshot

source§

impl Protobuf<RequestPrepareProposal> for PrepareProposal

source§

impl Protobuf<RequestProcessProposal> for ProcessProposal

source§

impl Protobuf<RequestQuery> for Query

source§

impl Protobuf<RequestVerifyVoteExtension> for VerifyVoteExtension

source§

impl Protobuf<Response> for Response

source§

impl Protobuf<ResponseApplySnapshotChunk> for ApplySnapshotChunk

source§

impl Protobuf<ResponseCheckTx> for CheckTx

source§

impl Protobuf<ResponseCommit> for Commit

source§

impl Protobuf<ResponseEcho> for Echo

source§

impl Protobuf<ResponseException> for Exception

source§

impl Protobuf<ResponseExtendVote> for ExtendVote

source§

impl Protobuf<ResponseFinalizeBlock> for FinalizeBlock

source§

impl Protobuf<ResponseInfo> for Info

source§

impl Protobuf<ResponseInitChain> for InitChain

source§

impl Protobuf<ResponseListSnapshots> for ListSnapshots

source§

impl Protobuf<ResponseLoadSnapshotChunk> for LoadSnapshotChunk

source§

impl Protobuf<ResponseOfferSnapshot> for OfferSnapshot

source§

impl Protobuf<ResponsePrepareProposal> for PrepareProposal

source§

impl Protobuf<ResponseProcessProposal> for ProcessProposal

source§

impl Protobuf<ResponseQuery> for Query

source§

impl Protobuf<ResponseVerifyVoteExtension> for VerifyVoteExtension

source§

impl Protobuf<Snapshot> for Snapshot

source§

impl Protobuf<Validator> for Validator

source§

impl Protobuf<ValidatorUpdate> for Update

source§

impl Protobuf<VoteInfo> for VoteInfo

source§

impl Protobuf<Proof> for Proof

source§

impl Protobuf<ProofOp> for ProofOp

source§

impl Protobuf<ProofOps> for ProofOps

source§

impl Protobuf<PublicKey> for PublicKey

source§

impl Protobuf<PubKeyRequest> for PubKeyRequest

source§

impl Protobuf<PubKeyResponse> for PubKeyResponse

source§

impl Protobuf<SignProposalRequest> for SignProposalRequest

source§

impl Protobuf<SignVoteRequest> for SignVoteRequest

source§

impl Protobuf<SignedProposalResponse> for SignedProposalResponse

source§

impl Protobuf<SignedVoteResponse> for SignedVoteResponse

source§

impl Protobuf<AbciParams> for AbciParams

source§

impl Protobuf<Block> for Block

source§

impl Protobuf<BlockId> for Id

source§

impl Protobuf<BlockParams> for Size

source§

impl Protobuf<CanonicalProposal> for CanonicalProposal

source§

impl Protobuf<CanonicalVote> for CanonicalVote

source§

impl Protobuf<ConsensusParams> for Params

source§

impl Protobuf<DuplicateVoteEvidence> for DuplicateVoteEvidence

source§

impl Protobuf<Evidence> for Evidence

source§

impl Protobuf<EvidenceList> for List

source§

impl Protobuf<EvidenceParams> for Params

source§

impl Protobuf<Header> for Header

source§

impl Protobuf<LightBlock> for ConflictingBlock

source§

impl Protobuf<LightClientAttackEvidence> for LightClientAttackEvidence

source§

impl Protobuf<PartSetHeader> for Header

source§

impl Protobuf<Proposal> for Proposal

source§

impl Protobuf<SignedHeader> for SignedHeader

source§

impl Protobuf<SimpleValidator> for SimpleValidator

source§

impl Protobuf<ValidatorParams> for ValidatorParams

source§

impl Protobuf<ValidatorSet> for Set

source§

impl Protobuf<VersionParams> for VersionParams

source§

impl Protobuf<Vote> for Vote

source§

impl Protobuf<Consensus> for Version

Implementors§

source§

impl Protobuf<Channel> for ChannelEnd

source§

impl Protobuf<Counterparty> for ibc_core::channel::types::channel::Counterparty

source§

impl Protobuf<IdentifiedChannel> for IdentifiedChannelEnd

source§

impl Protobuf<MsgAcknowledgement> for MsgAcknowledgement

source§

impl Protobuf<MsgChannelCloseConfirm> for MsgChannelCloseConfirm

source§

impl Protobuf<MsgChannelCloseInit> for MsgChannelCloseInit

source§

impl Protobuf<MsgChannelOpenAck> for MsgChannelOpenAck

source§

impl Protobuf<MsgChannelOpenConfirm> for MsgChannelOpenConfirm

source§

impl Protobuf<MsgChannelOpenInit> for MsgChannelOpenInit

source§

impl Protobuf<MsgChannelOpenTry> for MsgChannelOpenTry

source§

impl Protobuf<MsgRecvPacket> for MsgRecvPacket

source§

impl Protobuf<MsgTimeout> for MsgTimeout

source§

impl Protobuf<MsgTimeoutOnClose> for MsgTimeoutOnClose

source§

impl Protobuf<Height> for ibc_core::client::types::Height

source§

impl Protobuf<MsgCreateClient> for MsgCreateClient

source§

impl Protobuf<MsgRecoverClient> for MsgRecoverClient

source§

impl Protobuf<MsgSubmitMisbehaviour> for MsgSubmitMisbehaviour

source§

impl Protobuf<MsgUpdateClient> for MsgUpdateClient

source§

impl Protobuf<MsgUpgradeClient> for MsgUpgradeClient

source§

impl Protobuf<MerkleProof> for MerkleProof

source§

impl Protobuf<ConnectionEnd> for ConnectionEnd

source§

impl Protobuf<Counterparty> for ibc_core::connection::types::Counterparty

source§

impl Protobuf<IdentifiedConnection> for IdentifiedConnectionEnd

source§

impl Protobuf<MsgConnectionOpenAck> for MsgConnectionOpenAck

source§

impl Protobuf<MsgConnectionOpenConfirm> for MsgConnectionOpenConfirm

source§

impl Protobuf<MsgConnectionOpenInit> for MsgConnectionOpenInit

source§

impl Protobuf<MsgConnectionOpenTry> for MsgConnectionOpenTry

source§

impl Protobuf<Version> for ibc_core::connection::types::version::Version