diff --git a/substrate/tendermint/client/src/validators.rs b/substrate/tendermint/client/src/validators.rs index 409733c7..08454638 100644 --- a/substrate/tendermint/client/src/validators.rs +++ b/substrate/tendermint/client/src/validators.rs @@ -123,17 +123,14 @@ impl Signer for TendermintSigner { type ValidatorId = u16; type Signature = Signature; - async fn validator_id(&self) -> u16 { + async fn validator_id(&self) -> Option { let key = self.get_public_key().await; for (i, k) in (*self.1 .0).read().unwrap().lookup.iter().enumerate() { if k == &key { - return u16::try_from(i).unwrap(); + return Some(u16::try_from(i).unwrap()); } } - // TODO: Enable switching between being a validator and not being one, likely be returning - // Option here. Non-validators should be able to simply not broadcast when they think - // they have messages. - panic!("not a validator"); + None } async fn sign(&self, msg: &[u8]) -> Signature { diff --git a/substrate/tendermint/machine/src/ext.rs b/substrate/tendermint/machine/src/ext.rs index 1eb4827b..b9ffe5cc 100644 --- a/substrate/tendermint/machine/src/ext.rs +++ b/substrate/tendermint/machine/src/ext.rs @@ -41,8 +41,8 @@ pub trait Signer: Send + Sync { /// Signature type. type Signature: Signature; - /// Returns the validator's current ID. - async fn validator_id(&self) -> Self::ValidatorId; + /// Returns the validator's current ID. Returns None if they aren't a current validator. + async fn validator_id(&self) -> Option; /// Sign a signature with the current validator's private key. async fn sign(&self, msg: &[u8]) -> Self::Signature; } @@ -52,7 +52,7 @@ impl Signer for Arc { type ValidatorId = S::ValidatorId; type Signature = S::Signature; - async fn validator_id(&self) -> Self::ValidatorId { + async fn validator_id(&self) -> Option { self.as_ref().validator_id().await } diff --git a/substrate/tendermint/machine/src/lib.rs b/substrate/tendermint/machine/src/lib.rs index 9e457a5e..506321f3 100644 --- a/substrate/tendermint/machine/src/lib.rs +++ b/substrate/tendermint/machine/src/lib.rs @@ -107,7 +107,7 @@ pub struct TendermintMachine { validators: N::SignatureScheme, weights: Arc, - validator_id: N::ValidatorId, + validator_id: Option, number: BlockNumber, canonical_start_time: u64, @@ -179,20 +179,21 @@ impl TendermintMachine { &mut self, data: Data::Signature>, ) { - let step = data.step(); - // 27, 33, 41, 46, 60, 64 - self.step = step; - self.queue.push_back(Message { - sender: self.validator_id, - number: self.number, - round: self.round, - data, - }); + if let Some(validator_id) = &self.validator_id { + // 27, 33, 41, 46, 60, 64 + self.step = data.step(); + self.queue.push_back(Message { + sender: *validator_id, + number: self.number, + round: self.round, + data, + }); + } } // 14-21 fn round_propose(&mut self) -> bool { - if self.weights.proposer(self.number, self.round) == self.validator_id { + if Some(self.weights.proposer(self.number, self.round)) == self.validator_id { let (round, block) = self .valid .clone() diff --git a/substrate/tendermint/machine/tests/ext.rs b/substrate/tendermint/machine/tests/ext.rs index 79a04496..3e3e52ce 100644 --- a/substrate/tendermint/machine/tests/ext.rs +++ b/substrate/tendermint/machine/tests/ext.rs @@ -21,8 +21,8 @@ impl Signer for TestSigner { type ValidatorId = TestValidatorId; type Signature = [u8; 32]; - async fn validator_id(&self) -> TestValidatorId { - self.0 + async fn validator_id(&self) -> Option { + Some(self.0) } async fn sign(&self, msg: &[u8]) -> [u8; 32] {