hopr_db_entity/conversions/
tickets.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
use sea_orm::Set;

use hopr_crypto_types::prelude::*;
use hopr_internal_types::prelude::*;
use hopr_primitive_types::prelude::*;

use crate::errors::DbEntityError;
use crate::ticket;

impl TryFrom<&ticket::Model> for AcknowledgedTicket {
    type Error = DbEntityError;

    fn try_from(value: &ticket::Model) -> Result<Self, Self::Error> {
        let response = Response::try_from(value.response.as_ref())?;

        let mut ticket = TicketBuilder::default()
            .channel_id(Hash::from_hex(&value.channel_id)?)
            .amount(U256::from_be_bytes(&value.amount))
            .index(U256::from_be_bytes(&value.index).as_u64())
            .index_offset(value.index_offset as u32)
            .win_prob_encoded(
                value
                    .winning_probability
                    .as_slice()
                    .try_into()
                    .map_err(|_| DbEntityError::ConversionError("invalid winning probability".into()))?,
            )
            .channel_epoch(U256::from_be_bytes(&value.channel_epoch).as_u32())
            .challenge(response.to_challenge().to_ethereum_challenge())
            .signature(
                value
                    .signature
                    .as_slice()
                    .try_into()
                    .map_err(|_| DbEntityError::ConversionError("invalid signature format".into()))?,
            )
            .build_verified(
                value
                    .hash
                    .as_slice()
                    .try_into()
                    .map_err(|_| DbEntityError::ConversionError("invalid ticket hash".into()))?,
            )
            .map_err(|e| DbEntityError::ConversionError(format!("invalid ticket in the db: {e}")))?
            .into_acknowledged(response);

        ticket.status = AcknowledgedTicketStatus::try_from(value.state as u8)
            .map_err(|_| DbEntityError::ConversionError("invalid ticket state".into()))?;

        Ok(ticket)
    }
}

impl TryFrom<ticket::Model> for AcknowledgedTicket {
    type Error = DbEntityError;

    fn try_from(value: ticket::Model) -> Result<Self, Self::Error> {
        Self::try_from(&value)
    }
}

impl From<AcknowledgedTicket> for ticket::ActiveModel {
    fn from(value: AcknowledgedTicket) -> Self {
        ticket::ActiveModel {
            channel_id: Set(value.verified_ticket().channel_id.to_hex()),
            amount: Set(value.verified_ticket().amount.amount().to_be_bytes().to_vec()),
            index: Set(value.verified_ticket().index.to_be_bytes().to_vec()),
            index_offset: Set(value.verified_ticket().index_offset as i32),
            winning_probability: Set(value.verified_ticket().encoded_win_prob.to_vec()),
            channel_epoch: Set(U256::from(value.verified_ticket().channel_epoch).to_be_bytes().to_vec()),
            signature: Set(value.verified_ticket().signature.unwrap().as_ref().to_vec()),
            response: Set(value.response.as_ref().to_vec()),
            state: Set(value.status as i8),
            hash: Set(value.ticket.verified_hash().as_ref().to_vec()),
            ..Default::default()
        }
    }
}