      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
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // This file is part of Parity.

    // Parity is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.

    // Parity is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.

    // You should have received a copy of the GNU General Public License
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    //! Tendermint message handling.

    use util::*;
    use super::{Height, View, BlockHash, Step};
    use error::Error;
    use header::Header;
    use rlp::{Rlp, UntrustedRlp, RlpStream, Stream, Encodable, Decodable, Decoder, DecoderError, View as RlpView};
    use ethkey::{recover, public_to_address};
    use super::super::vote_collector::Message;

    /// Message transmitted between consensus participants.
    #[derive(Debug, PartialEq, Eq, Clone, Hash, Default)]
    pub struct ConsensusMessage {
    	pub vote_step: VoteStep,
    	pub block_hash: Option<BlockHash>,
    	pub signature: H520,
    }

    /// Complete step of the consensus process.
    #[derive(Debug, PartialEq, Eq, Clone, Hash)]
    pub struct VoteStep {
    	pub height: Height,
    	pub view: View,
    	pub step: Step,
    }

    impl VoteStep {
    	pub fn new(height: Height, view: View, step: Step) -> Self {
    		VoteStep { height: height, view: view, step: step }
    	}

    	pub fn is_height(&self, height: Height) -> bool {
    		self.height == height
    	}

    	pub fn is_view(&self, height: Height, view: View) -> bool {
    		self.height == height && self.view == view
    	}
    }

    /// Header consensus view.
    pub fn consensus_view(header: &Header) -> Result<View, ::rlp::DecoderError> {
    	let view_rlp = header.seal().get(0).expect("seal passed basic verification; seal has 3 fields; qed");
    	UntrustedRlp::new(view_rlp.as_slice()).as_val()
    }

    impl Message for ConsensusMessage {
    	type Round = VoteStep;

    	fn signature(&self) -> H520 { self.signature }

    	fn block_hash(&self) -> Option<H256> { self.block_hash }

    	fn round(&self) -> &VoteStep { &self.vote_step }

    	fn is_broadcastable(&self) -> bool { self.vote_step.step.is_pre() }
    }

    impl ConsensusMessage {
    	pub fn new(signature: H520, height: Height, view: View, step: Step, block_hash: Option<BlockHash>) -> Self {
    		ConsensusMessage {
    			signature: signature,
    			block_hash: block_hash,
    			vote_step: VoteStep::new(height, view, step),
    		}
    	}

    	pub fn new_proposal(header: &Header) -> Result<Self, ::rlp::DecoderError> {
    		Ok(ConsensusMessage {
    			vote_step: VoteStep::new(header.number() as Height, consensus_view(header)?, Step::Propose),
    			signature: UntrustedRlp::new(header.seal().get(1).expect("seal passed basic verification; seal has 3 fields; qed").as_slice()).as_val()?,
    			block_hash: Some(header.bare_hash()),
    		})
    	}

    	pub fn new_commit(proposal: &ConsensusMessage, signature: H520) -> Self {
    		let mut vote_step = proposal.vote_step.clone();
    		vote_step.step = Step::Precommit;
    		ConsensusMessage {
    			vote_step: vote_step,
    			block_hash: proposal.block_hash,
    			signature: signature,
    		}
    	}

    	pub fn verify(&self) -> Result<Address, Error> {
    		let full_rlp = ::rlp::encode(self);
    		let block_info = Rlp::new(&full_rlp).at(1);
    		let public_key = recover(&self.signature.into(), &block_info.as_raw().sha3())?;
    		Ok(public_to_address(&public_key))
    	}

    	pub fn precommit_hash(&self) -> H256 {
    		let mut vote_step = self.vote_step.clone();
    		vote_step.step = Step::Precommit;
    		message_info_rlp(&vote_step, self.block_hash).sha3()
    	}
    }

    impl Default for VoteStep {
    	fn default() -> Self {
    		VoteStep::new(0, 0, Step::Propose)
    	}
    }

    impl PartialOrd for VoteStep {
    	fn partial_cmp(&self, m: &VoteStep) -> Option<Ordering> {
    		Some(self.cmp(m))
    	}
    }

    impl Ord for VoteStep {
    	fn cmp(&self, m: &VoteStep) -> Ordering {
    		if self.height != m.height {
    			self.height.cmp(&m.height)
    		} else if self.view != m.view {
    			self.view.cmp(&m.view)
    		} else {
    			self.step.number().cmp(&m.step.number())
    		}
    	}
    }

    impl Step {
    	fn number(&self) -> u8 {
    		match *self {
    			Step::Propose => 0,
    			Step::Prevote => 1,
    			Step::Precommit => 2,
    			Step::Commit => 3,
    		}
    	}
    }

    impl Decodable for Step {
    	fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
    		match decoder.as_rlp().as_val()? {
    			0u8 => Ok(Step::Propose),
    			1 => Ok(Step::Prevote),
    			2 => Ok(Step::Precommit),
    			_ => Err(DecoderError::Custom("Invalid step.")),
    		}
    	}
    }

    impl Encodable for Step {
    	fn rlp_append(&self, s: &mut RlpStream) {
    		s.append(&self.number());
    	}
    }

    /// (signature, (height, view, step, block_hash))
    impl Decodable for ConsensusMessage {
    	fn decode<D>(decoder: &D) -> Result<Self, DecoderError> where D: Decoder {
    		let rlp = decoder.as_rlp();
    		let m = rlp.at(1)?;
    		let block_message: H256 = m.val_at(3)?;
    		Ok(ConsensusMessage {
    			vote_step: VoteStep::new(m.val_at(0)?, m.val_at(1)?, m.val_at(2)?),
    			block_hash: match block_message.is_zero() {
    				true => None,
    				false => Some(block_message),
    			},
    			signature: rlp.val_at(0)?,
    		})
      }
    }

    impl Encodable for ConsensusMessage {
    	fn rlp_append(&self, s: &mut RlpStream) {
    		let info = message_info_rlp(&self.vote_step, self.block_hash);
    		s.begin_list(2)
    			.append(&self.signature)
    			.append_raw(&info, 1);
    	}
    }

    pub fn message_info_rlp(vote_step: &VoteStep, block_hash: Option<BlockHash>) -> Bytes {
    	// TODO: figure out whats wrong with nested list encoding
    	let mut s = RlpStream::new_list(5);
    	s.append(&vote_step.height).append(&vote_step.view).append(&vote_step.step).append(&block_hash.unwrap_or_else(H256::zero));
    	s.out()
    }

    pub fn message_full_rlp(signature: &H520, vote_info: &Bytes) -> Bytes {
    	let mut s = RlpStream::new_list(2);
    	s.append(signature).append_raw(vote_info, 1);
    	s.out()
    }

    #[cfg(test)]
    mod tests {
    	use util::*;
    	use rlp::*;
    	use ethkey::Secret;
    	use account_provider::AccountProvider;
    	use header::Header;
    	use super::super::Step;
    	use super::*;

    	#[test]
    	fn encode_decode() {
    		let message = ConsensusMessage {
    			signature: H520::default(),
    			vote_step: VoteStep {
    				height: 10,
    				view: 123,
    				step: Step::Precommit,
    			},
    			block_hash: Some("1".sha3())
    		};
    		let raw_rlp = ::rlp::encode(&message).to_vec();
    		let rlp = Rlp::new(&raw_rlp);
    		assert_eq!(message, rlp.as_val());

    		let message = ConsensusMessage {
    			signature: H520::default(),
    			vote_step: VoteStep {
    				height: 1314,
    				view: 0,
    				step: Step::Prevote,
    			},
    			block_hash: None
    		};
    		let raw_rlp = ::rlp::encode(&message);
    		let rlp = Rlp::new(&raw_rlp);
    		assert_eq!(message, rlp.as_val());
    	}

    	#[test]
    	fn generate_and_verify() {
    		let tap = Arc::new(AccountProvider::transient_provider());
    		let addr = tap.insert_account(Secret::from_slice(&"0".sha3()).unwrap(), "0").unwrap();
    		tap.unlock_account_permanently(addr, "0".into()).unwrap();

    		let mi = message_info_rlp(&VoteStep::new(123, 2, Step::Precommit), Some(H256::default()));

    		let raw_rlp = message_full_rlp(&tap.sign(addr, None, mi.sha3()).unwrap().into(), &mi);

    		let rlp = UntrustedRlp::new(&raw_rlp);
    		let message: ConsensusMessage = rlp.as_val().unwrap();
    		match message.verify() { Ok(a) if a == addr => {}, _ => panic!(), };
    	}

    	#[test]
    	fn proposal_message() {
    		let mut header = Header::default();
    		let seal = vec![
    			::rlp::encode(&0u8).to_vec(),
    			::rlp::encode(&H520::default()).to_vec(),
    			Vec::new()
    		];
    		header.set_seal(seal);
    		let message = ConsensusMessage::new_proposal(&header).unwrap();
    		assert_eq!(
    			message,
    			ConsensusMessage {
    				signature: Default::default(),
    				vote_step: VoteStep {
    					height: 0,
    					view: 0,
    					step: Step::Propose,
    				},
    				block_hash: Some(header.bare_hash())
    			}
    		);
    	}

    	#[test]
    	fn message_info_from_header() {
    		let header = Header::default();
    		let pro = ConsensusMessage {
    			signature: Default::default(),
    			vote_step: VoteStep::new(0, 0, Step::Propose),
    			block_hash: Some(header.bare_hash())
    		};
    		let pre = message_info_rlp(&VoteStep::new(0, 0, Step::Precommit), Some(header.bare_hash()));

    		assert_eq!(pro.precommit_hash(), pre.sha3());
    	}

    	#[test]
    	fn step_ordering() {
    			assert!(VoteStep::new(10, 123, Step::Precommit) < VoteStep::new(11, 123, Step::Precommit));
    			assert!(VoteStep::new(10, 123, Step::Propose) < VoteStep::new(11, 123, Step::Precommit));
    			assert!(VoteStep::new(10, 122, Step::Propose) < VoteStep::new(11, 123, Step::Propose));
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)