casper_types/system/auction/
bridge.rs

1use alloc::vec::Vec;
2
3use crate::{
4    bytesrepr::{self, FromBytes, ToBytes},
5    CLType, CLTyped, EraId, PublicKey,
6};
7#[cfg(feature = "datasize")]
8use datasize::DataSize;
9#[cfg(feature = "json-schema")]
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12
13/// A bridge record pointing to a new `ValidatorBid` after the public key was changed.
14#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
15#[cfg_attr(feature = "datasize", derive(DataSize))]
16#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
17#[serde(deny_unknown_fields)]
18pub struct Bridge {
19    /// Previous validator public key associated with the bid.
20    old_validator_public_key: PublicKey,
21    /// New validator public key associated with the bid.
22    new_validator_public_key: PublicKey,
23    /// Era when bridge record was created.
24    era_id: EraId,
25}
26
27impl Bridge {
28    /// Creates new instance of a bridge record.
29    pub fn new(
30        old_validator_public_key: PublicKey,
31        new_validator_public_key: PublicKey,
32        era_id: EraId,
33    ) -> Self {
34        Self {
35            old_validator_public_key,
36            new_validator_public_key,
37            era_id,
38        }
39    }
40
41    /// Gets the old validator public key
42    pub fn old_validator_public_key(&self) -> &PublicKey {
43        &self.old_validator_public_key
44    }
45
46    /// Gets the new validator public key
47    pub fn new_validator_public_key(&self) -> &PublicKey {
48        &self.new_validator_public_key
49    }
50
51    /// Gets the era when key change happened
52    pub fn era_id(&self) -> &EraId {
53        &self.era_id
54    }
55}
56
57impl CLTyped for Bridge {
58    fn cl_type() -> CLType {
59        CLType::Any
60    }
61}
62
63impl ToBytes for Bridge {
64    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
65        let mut result = bytesrepr::allocate_buffer(self)?;
66        self.write_bytes(&mut result)?;
67        Ok(result)
68    }
69
70    fn serialized_length(&self) -> usize {
71        self.old_validator_public_key.serialized_length()
72            + self.new_validator_public_key.serialized_length()
73            + self.era_id.serialized_length()
74    }
75
76    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
77        self.old_validator_public_key.write_bytes(writer)?;
78        self.new_validator_public_key.write_bytes(writer)?;
79        self.era_id.write_bytes(writer)?;
80        Ok(())
81    }
82}
83
84impl FromBytes for Bridge {
85    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
86        let (old_validator_public_key, bytes) = FromBytes::from_bytes(bytes)?;
87        let (new_validator_public_key, bytes) = FromBytes::from_bytes(bytes)?;
88        let (era_id, bytes) = FromBytes::from_bytes(bytes)?;
89        Ok((
90            Bridge {
91                old_validator_public_key,
92                new_validator_public_key,
93                era_id,
94            },
95            bytes,
96        ))
97    }
98}