|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! State of a reconfiguration coordinator inside a [`crate::Node`] |
| 6 | +
|
| 7 | +use crate::crypto::{LrtqShare, Sha3_256Digest, ShareDigestLrtq}; |
| 8 | +use crate::messages::{PeerMsg, PrepareMsg}; |
| 9 | +use crate::validators::{ReconfigurationError, ValidatedReconfigureMsg}; |
| 10 | +use crate::{Configuration, Envelope, Epoch, PlatformId}; |
| 11 | +use gfss::shamir::{SecretShares, Share}; |
| 12 | +use secrecy::ExposeSecret; |
| 13 | +use std::collections::{BTreeMap, BTreeSet}; |
| 14 | +use std::time::Instant; |
| 15 | + |
| 16 | +/// The state of a reconfiguration coordinator. |
| 17 | +/// |
| 18 | +/// A coordinator can be any trust quorum node that is a member of both the old |
| 19 | +/// and new group. The coordinator is chosen by Nexus for a given epoch when a |
| 20 | +/// trust quorum reconfiguration is triggered. Reconfiguration is only performed |
| 21 | +/// when the control plane is up, as we use Nexus to persist prepares and ensure |
| 22 | +/// commitment happens, even if the system crashes while committing. If a |
| 23 | +/// rack crash (such as a power outage) occurs before nexus is informed of the |
| 24 | +/// prepares, nexus will skip the epoch and start a new reconfiguration. This |
| 25 | +/// allows progress to always be made with a full linearization of epochs. |
| 26 | +/// |
| 27 | +/// We allow some unused fields before we complete the coordination code |
| 28 | +#[allow(unused)] |
| 29 | +pub struct CoordinatorState { |
| 30 | + /// A copy of the platform_id from [`Node`] purely for ergonomics |
| 31 | + platform_id: PlatformId, |
| 32 | + |
| 33 | + /// When the reconfiguration started |
| 34 | + pub start_time: Instant, |
| 35 | + |
| 36 | + /// A copy of the message used to start this reconfiguration |
| 37 | + pub reconfigure_msg: ValidatedReconfigureMsg, |
| 38 | + |
| 39 | + /// Configuration that will get persisted inside a `Prepare` message in a |
| 40 | + /// `Node`s `PersistentState`, once it is possible to create the Prepare. |
| 41 | + pub configuration: Configuration, |
| 42 | + |
| 43 | + /// What is the coordinator currently doing |
| 44 | + pub op: CoordinatorOperation, |
| 45 | + |
| 46 | + /// When to resend prepare messages next |
| 47 | + pub retry_deadline: Instant, |
| 48 | +} |
| 49 | + |
| 50 | +impl CoordinatorState { |
| 51 | + /// Start coordinating a reconfiguration for a brand new trust quorum |
| 52 | + /// |
| 53 | + /// Return the newly constructed `CoordinatorState` along with this node's |
| 54 | + /// `PrepareMsg` so that it can be persisted. |
| 55 | + /// |
| 56 | + /// Precondition: This node must be a member of the new configuration |
| 57 | + /// or this method will panic. This is ensured as part of passing in a |
| 58 | + /// `ValidatedReconfigureMsg`. |
| 59 | + pub fn new_uninitialized( |
| 60 | + my_platform_id: PlatformId, |
| 61 | + now: Instant, |
| 62 | + msg: ValidatedReconfigureMsg, |
| 63 | + ) -> Result<(CoordinatorState, PrepareMsg), ReconfigurationError> { |
| 64 | + // Create a configuration for this epoch |
| 65 | + let (config, shares) = |
| 66 | + Configuration::new(my_platform_id.clone(), &msg)?; |
| 67 | + |
| 68 | + let shares_by_member: BTreeMap<PlatformId, Share> = config |
| 69 | + .members |
| 70 | + .keys() |
| 71 | + .cloned() |
| 72 | + .zip(shares.shares.expose_secret().iter().cloned()) |
| 73 | + .collect(); |
| 74 | + |
| 75 | + let mut prepares = BTreeMap::new(); |
| 76 | + let mut my_prepare_msg: Option<PrepareMsg> = None; |
| 77 | + for (platform_id, share) in shares_by_member.into_iter() { |
| 78 | + let prepare_msg = PrepareMsg { config: config.clone(), share }; |
| 79 | + if platform_id == my_platform_id { |
| 80 | + // The prepare message to add to our `PersistentState` |
| 81 | + my_prepare_msg = Some(prepare_msg); |
| 82 | + } else { |
| 83 | + // Create a message that requires sending |
| 84 | + prepares.insert(platform_id, prepare_msg); |
| 85 | + } |
| 86 | + } |
| 87 | + let op = CoordinatorOperation::Prepare { |
| 88 | + prepares, |
| 89 | + prepare_acks: BTreeSet::new(), |
| 90 | + }; |
| 91 | + |
| 92 | + let state = CoordinatorState::new(my_platform_id, now, msg, config, op); |
| 93 | + Ok((state, my_prepare_msg.unwrap())) |
| 94 | + } |
| 95 | + |
| 96 | + /// A reconfiguration from one group to another |
| 97 | + pub fn new_reconfiguration( |
| 98 | + my_platform_id: PlatformId, |
| 99 | + now: Instant, |
| 100 | + msg: ValidatedReconfigureMsg, |
| 101 | + last_committed_config: &Configuration, |
| 102 | + ) -> Result<CoordinatorState, ReconfigurationError> { |
| 103 | + let (config, new_shares) = |
| 104 | + Configuration::new(my_platform_id.clone(), &msg)?; |
| 105 | + |
| 106 | + // We must collect shares from the last configuration |
| 107 | + // so we can recompute the old rack secret. |
| 108 | + let op = CoordinatorOperation::CollectShares { |
| 109 | + epoch: last_committed_config.epoch, |
| 110 | + members: last_committed_config.members.clone(), |
| 111 | + collected_shares: BTreeMap::new(), |
| 112 | + new_shares, |
| 113 | + }; |
| 114 | + |
| 115 | + Ok(CoordinatorState::new(my_platform_id, now, msg, config, op)) |
| 116 | + } |
| 117 | + |
| 118 | + // Intentionallly private! |
| 119 | + fn new( |
| 120 | + platform_id: PlatformId, |
| 121 | + now: Instant, |
| 122 | + reconfigure_msg: ValidatedReconfigureMsg, |
| 123 | + configuration: Configuration, |
| 124 | + op: CoordinatorOperation, |
| 125 | + ) -> CoordinatorState { |
| 126 | + // We always set the retry deadline to `now` so that we will send |
| 127 | + // messages upon new construction. This field gets updated after |
| 128 | + // prepares are sent. |
| 129 | + let retry_deadline = now; |
| 130 | + CoordinatorState { |
| 131 | + platform_id, |
| 132 | + start_time: now, |
| 133 | + reconfigure_msg, |
| 134 | + configuration, |
| 135 | + op, |
| 136 | + retry_deadline, |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Send any required messages as a reconfiguration coordinator |
| 141 | + // |
| 142 | + // This varies depending upon the current `CoordinatorState`. |
| 143 | + // |
| 144 | + // In some cases a `PrepareMsg` will be added locally to the |
| 145 | + // `PersistentState`, requiring persistence from the caller. In this case we |
| 146 | + // will return a copy of it. |
| 147 | + // |
| 148 | + // This method is "in progress" - allow unused parameters for now |
| 149 | + #[allow(unused)] |
| 150 | + pub fn send_msgs(&mut self, now: Instant, outbox: &mut Vec<Envelope>) { |
| 151 | + match &self.op { |
| 152 | + CoordinatorOperation::CollectShares { |
| 153 | + epoch, |
| 154 | + members, |
| 155 | + collected_shares, |
| 156 | + .. |
| 157 | + } => {} |
| 158 | + CoordinatorOperation::CollectLrtqShares { members, shares } => {} |
| 159 | + CoordinatorOperation::Prepare { prepares, prepare_acks } => { |
| 160 | + for (platform_id, prepare) in prepares.clone().into_iter() { |
| 161 | + outbox.push(Envelope { |
| 162 | + to: platform_id, |
| 163 | + from: self.platform_id.clone(), |
| 164 | + msg: PeerMsg::Prepare(prepare), |
| 165 | + }); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +/// What should the coordinator be doing? |
| 173 | +/// |
| 174 | +/// We haven't started implementing upgrade from LRTQ yet |
| 175 | +#[allow(unused)] |
| 176 | +pub enum CoordinatorOperation { |
| 177 | + CollectShares { |
| 178 | + epoch: Epoch, |
| 179 | + members: BTreeMap<PlatformId, Sha3_256Digest>, |
| 180 | + collected_shares: BTreeMap<PlatformId, Share>, |
| 181 | + new_shares: SecretShares, |
| 182 | + }, |
| 183 | + // Epoch is always 0 |
| 184 | + CollectLrtqShares { |
| 185 | + members: BTreeMap<PlatformId, ShareDigestLrtq>, |
| 186 | + shares: BTreeMap<PlatformId, LrtqShare>, |
| 187 | + }, |
| 188 | + Prepare { |
| 189 | + /// The set of Prepares to send to each node |
| 190 | + prepares: BTreeMap<PlatformId, PrepareMsg>, |
| 191 | + |
| 192 | + /// Acknowledgements that the prepare has been received |
| 193 | + prepare_acks: BTreeSet<PlatformId>, |
| 194 | + }, |
| 195 | +} |
0 commit comments