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
use crate::config::date_format;
use crate::Result;
use jormungandr_automation::{jormungandr::JormungandrRest, testing::time::wait_for_epoch};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
pub const FORMAT: &str = "%Y-%m-%d %H:%M:%S";

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum VoteTime {
    Blockchain(VoteBlockchainTime),
    Real {
        #[serde(with = "time::serde::rfc3339")]
        vote_start_timestamp: OffsetDateTime,
        #[serde(with = "time::serde::rfc3339")]
        tally_start_timestamp: OffsetDateTime,
        #[serde(with = "time::serde::rfc3339")]
        tally_end_timestamp: OffsetDateTime,
        find_best_match: bool,
    },
}

impl VoteTime {
    pub fn real_from_str<S: Into<String>>(
        vote_start_timestamp: S,
        tally_start_timestamp: S,
        tally_end_timestamp: S,
    ) -> Result<Self> {
        Ok(Self::Real {
            vote_start_timestamp: OffsetDateTime::parse(
                &vote_start_timestamp.into(),
                &date_format(),
            )?,
            tally_start_timestamp: OffsetDateTime::parse(
                &tally_start_timestamp.into(),
                &date_format(),
            )?,
            tally_end_timestamp: OffsetDateTime::parse(
                &tally_end_timestamp.into(),
                &date_format(),
            )?,
            find_best_match: false,
        })
    }

    pub fn real(
        vote_start_timestamp: OffsetDateTime,
        tally_start_timestamp: OffsetDateTime,
        tally_end_timestamp: OffsetDateTime,
    ) -> Self {
        Self::Real {
            vote_start_timestamp,
            tally_start_timestamp,
            tally_end_timestamp,
            find_best_match: false,
        }
    }

    pub fn blockchain(
        vote_start: u32,
        tally_start: u32,
        tally_end: u32,
        slots_per_epoch: u32,
    ) -> Self {
        Self::Blockchain(VoteBlockchainTime {
            vote_start,
            tally_start,
            tally_end,
            slots_per_epoch,
        })
    }
}

impl Default for VoteTime {
    fn default() -> Self {
        Self::Blockchain(VoteBlockchainTime::default())
    }
}

impl Default for VoteBlockchainTime {
    fn default() -> Self {
        Self {
            vote_start: 1,
            tally_start: 2,
            tally_end: 3,
            slots_per_epoch: 30,
        }
    }
}

#[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub struct VoteBlockchainTime {
    pub vote_start: u32,
    pub tally_start: u32,
    pub tally_end: u32,
    pub slots_per_epoch: u32,
}

impl VoteBlockchainTime {
    pub fn wait_for_vote_start(self, rest: JormungandrRest) {
        wait_for_epoch(self.vote_start, rest);
    }

    pub fn wait_for_tally_start(self, rest: JormungandrRest) {
        wait_for_epoch(self.tally_start, rest);
    }

    pub fn wait_for_tally_end(self, rest: JormungandrRest) {
        wait_for_epoch(self.tally_end, rest);
    }
}

impl From<VoteBlockchainTime> for VoteTime {
    fn from(vote_blockchain_time: VoteBlockchainTime) -> Self {
        VoteTime::Blockchain(vote_blockchain_time)
    }
}