Skip to content

Commit

Permalink
Payload
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 1, 2023
1 parent 2e18691 commit cc4c633
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions sdk/src/types/block/payload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,42 @@ use crate::types::block::{protocol::ProtocolParameters, Error};
/// A generic payload that can represent different types defining block payloads.
#[derive(Clone, Eq, PartialEq, From)]
pub enum Payload {
/// A signed transaction payload.
SignedTransaction(Box<SignedTransactionPayload>),
/// A tagged data payload.
TaggedData(Box<TaggedDataPayload>),
/// A signed transaction payload.
SignedTransaction(Box<SignedTransactionPayload>),
/// A candidacy announcement payload.
CandidacyAnnouncement(CandidacyAnnouncementPayload),
}

impl core::fmt::Debug for Payload {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::SignedTransaction(payload) => payload.fmt(f),
Self::TaggedData(payload) => payload.fmt(f),
Self::SignedTransaction(payload) => payload.fmt(f),
Self::CandidacyAnnouncement(payload) => payload.fmt(f),
}
}
}

impl From<SignedTransactionPayload> for Payload {
fn from(payload: SignedTransactionPayload) -> Self {
Self::SignedTransaction(Box::new(payload))
}
}

impl From<TaggedDataPayload> for Payload {
fn from(payload: TaggedDataPayload) -> Self {
Self::TaggedData(Box::new(payload))
}
}

impl From<SignedTransactionPayload> for Payload {
fn from(payload: SignedTransactionPayload) -> Self {
Self::SignedTransaction(Box::new(payload))
}
}

impl Payload {
/// Returns the payload kind of a `Payload`.
pub fn kind(&self) -> u8 {
match self {
Self::SignedTransaction(_) => SignedTransactionPayload::KIND,
Self::TaggedData(_) => TaggedDataPayload::KIND,
Self::SignedTransaction(_) => SignedTransactionPayload::KIND,
Self::CandidacyAnnouncement(_) => CandidacyAnnouncementPayload::KIND,
}
}
Expand All @@ -80,14 +80,14 @@ impl Packable for Payload {

fn pack<P: Packer>(&self, packer: &mut P) -> Result<(), P::Error> {
match self {
Self::SignedTransaction(transaction) => {
SignedTransactionPayload::KIND.pack(packer)?;
transaction.pack(packer)
}
Self::TaggedData(tagged_data) => {
TaggedDataPayload::KIND.pack(packer)?;
tagged_data.pack(packer)
}
Self::SignedTransaction(transaction) => {
SignedTransactionPayload::KIND.pack(packer)?;
transaction.pack(packer)
}
Self::CandidacyAnnouncement(_) => CandidacyAnnouncementPayload::KIND.pack(packer),
}?;

Expand All @@ -99,10 +99,10 @@ impl Packable for Payload {
visitor: &Self::UnpackVisitor,
) -> Result<Self, UnpackError<Self::UnpackError, U::Error>> {
Ok(match u8::unpack::<_, VERIFY>(unpacker, &()).coerce()? {
TaggedDataPayload::KIND => Self::from(TaggedDataPayload::unpack::<_, VERIFY>(unpacker, &()).coerce()?),
SignedTransactionPayload::KIND => {
Self::from(SignedTransactionPayload::unpack::<_, VERIFY>(unpacker, visitor).coerce()?)
}
TaggedDataPayload::KIND => Self::from(TaggedDataPayload::unpack::<_, VERIFY>(unpacker, &()).coerce()?),
CandidacyAnnouncementPayload::KIND => Self::from(CandidacyAnnouncementPayload),
k => return Err(UnpackError::Packable(Error::InvalidPayloadKind(k))),
})
Expand Down Expand Up @@ -197,30 +197,30 @@ pub mod dto {
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PayloadDto {
SignedTransaction(Box<SignedTransactionPayloadDto>),
TaggedData(Box<TaggedDataPayload>),
SignedTransaction(Box<SignedTransactionPayloadDto>),
CandidacyAnnouncement,
}

impl From<SignedTransactionPayloadDto> for PayloadDto {
fn from(payload: SignedTransactionPayloadDto) -> Self {
Self::SignedTransaction(Box::new(payload))
}
}

impl From<TaggedDataPayload> for PayloadDto {
fn from(payload: TaggedDataPayload) -> Self {
Self::TaggedData(Box::new(payload))
}
}

impl From<SignedTransactionPayloadDto> for PayloadDto {
fn from(payload: SignedTransactionPayloadDto) -> Self {
Self::SignedTransaction(Box::new(payload))
}
}

impl From<&Payload> for PayloadDto {
fn from(value: &Payload) -> Self {
match value {
Payload::TaggedData(p) => Self::TaggedData(p.clone()),
Payload::SignedTransaction(p) => {
Self::SignedTransaction(Box::new(SignedTransactionPayloadDto::from(p.as_ref())))
}
Payload::TaggedData(p) => Self::TaggedData(p.clone()),
Payload::CandidacyAnnouncement(_) => Self::CandidacyAnnouncement,
}
}
Expand All @@ -232,10 +232,10 @@ pub mod dto {

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
Ok(match dto {
PayloadDto::TaggedData(p) => Self::from(*p),
PayloadDto::SignedTransaction(p) => {
Self::from(SignedTransactionPayload::try_from_dto_with_params_inner(*p, params)?)
}
PayloadDto::TaggedData(p) => Self::from(*p),
PayloadDto::CandidacyAnnouncement => Self::from(CandidacyAnnouncementPayload),
})
}
Expand Down

0 comments on commit cc4c633

Please sign in to comment.