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
use penumbra_proto::penumbra::core::component::sct::v1 as pb;
use penumbra_proto::DomainType;
use serde::{Deserialize, Serialize};

#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Copy, Clone, Serialize, Deserialize)]
#[serde(try_from = "pb::Epoch", into = "pb::Epoch")]
pub struct Epoch {
    pub index: u64,
    pub start_height: u64,
}

impl DomainType for Epoch {
    type Proto = pb::Epoch;
}

impl From<pb::Epoch> for Epoch {
    fn from(msg: pb::Epoch) -> Self {
        Epoch {
            index: msg.index,
            start_height: msg.start_height,
        }
    }
}

impl From<Epoch> for pb::Epoch {
    fn from(epoch: Epoch) -> Self {
        pb::Epoch {
            index: epoch.index,
            start_height: epoch.start_height,
        }
    }
}

impl Epoch {
    // Returns true if current_height is the scheduled last block of the epoch
    pub fn is_scheduled_epoch_end(&self, current_height: u64, epoch_duration: u64) -> bool {
        current_height - self.start_height >= epoch_duration - 1
    }
}