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
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of Substrate.

// Substrate 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.

// Substrate 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 Substrate.  If not, see <http://www.gnu.org/licenses/>.

//! Private implementation details of BABE digests.

#[cfg(feature = "std")]
use super::{BABE_ENGINE_ID, AuthoritySignature};
#[cfg(not(feature = "std"))]
use super::{VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH};
use super::{AuthorityId, AuthorityIndex, SlotNumber, BabeAuthorityWeight};
#[cfg(feature = "std")]
use sp_runtime::{DigestItem, generic::OpaqueDigestItemId};
#[cfg(feature = "std")]
use std::fmt::Debug;
use codec::{Decode, Encode};
#[cfg(feature = "std")]
use codec::{Codec, Input, Error};
#[cfg(feature = "std")]
use schnorrkel::{
	SignatureError, errors::MultiSignatureStage,
	vrf::{VRFProof, VRFOutput, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH}
};
use sp_std::vec::Vec;


/// A BABE pre-runtime digest. This contains all data required to validate a
/// block and for the BABE runtime module. Slots can be assigned to a primary
/// (VRF based) and to a secondary (slot number based).
#[cfg(feature = "std")]
#[derive(Clone, Debug)]
pub enum PreDigest {
	/// A primary VRF-based slot assignment.
	Primary {
		/// VRF output
		vrf_output: VRFOutput,
		/// VRF proof
		vrf_proof: VRFProof,
		/// Authority index
		authority_index: super::AuthorityIndex,
		/// Slot number
		slot_number: SlotNumber,
	},
	/// A secondary deterministic slot assignment.
	Secondary {
		/// Authority index
		authority_index: super::AuthorityIndex,
		/// Slot number
		slot_number: SlotNumber,
	},
}

#[cfg(feature = "std")]
impl PreDigest {
	/// Returns the slot number of the pre digest.
	pub fn authority_index(&self) -> AuthorityIndex {
		match self {
			PreDigest::Primary { authority_index, .. } => *authority_index,
			PreDigest::Secondary { authority_index, .. } => *authority_index,
		}
	}

	/// Returns the slot number of the pre digest.
	pub fn slot_number(&self) -> SlotNumber {
		match self {
			PreDigest::Primary { slot_number, .. } => *slot_number,
			PreDigest::Secondary { slot_number, .. } => *slot_number,
		}
	}

	/// Returns the weight _added_ by this digest, not the cumulative weight
	/// of the chain.
	pub fn added_weight(&self) -> crate::BabeBlockWeight {
		match self {
			PreDigest::Primary { .. } => 1,
			PreDigest::Secondary { .. } => 0,
		}
	}
}

/// A raw version of `BabePreDigest`, usable on `no_std`.
#[derive(Copy, Clone, Encode, Decode)]
pub enum RawPreDigest {
	/// A primary VRF-based slot assignment.
	#[codec(index = "1")]
	Primary {
		/// Authority index
		authority_index: AuthorityIndex,
		/// Slot number
		slot_number: SlotNumber,
		/// VRF output
		vrf_output: [u8; VRF_OUTPUT_LENGTH],
		/// VRF proof
		vrf_proof: [u8; VRF_PROOF_LENGTH],
	},
	/// A secondary deterministic slot assignment.
	#[codec(index = "2")]
	Secondary {
		/// Authority index
		///
		/// This is not strictly-speaking necessary, since the secondary slots
		/// are assigned based on slot number and epoch randomness. But including
		/// it makes things easier for higher-level users of the chain data to
		/// be aware of the author of a secondary-slot block.
		authority_index: AuthorityIndex,
		/// Slot number
		slot_number: SlotNumber,
	},
}

impl RawPreDigest {
	/// Returns the slot number of the pre digest.
	pub fn slot_number(&self) -> SlotNumber {
		match self {
			RawPreDigest::Primary { slot_number, .. } => *slot_number,
			RawPreDigest::Secondary { slot_number, .. } => *slot_number,
		}
	}
}

#[cfg(feature = "std")]
impl Encode for PreDigest {
	fn encode(&self) -> Vec<u8> {
		let raw = match self {
			PreDigest::Primary {
				vrf_output,
				vrf_proof,
				authority_index,
				slot_number,
			} => {
				RawPreDigest::Primary {
					vrf_output: *vrf_output.as_bytes(),
					vrf_proof: vrf_proof.to_bytes(),
					authority_index: *authority_index,
					slot_number: *slot_number,
				}
			},
			PreDigest::Secondary {
				authority_index,
				slot_number,
			} => {
				RawPreDigest::Secondary {
					authority_index: *authority_index,
					slot_number: *slot_number,
				}
			},
		};

		codec::Encode::encode(&raw)
	}
}

#[cfg(feature = "std")]
impl codec::EncodeLike for PreDigest {}

#[cfg(feature = "std")]
impl Decode for PreDigest {
	fn decode<R: Input>(i: &mut R) -> Result<Self, Error> {
		let pre_digest = match Decode::decode(i)? {
			RawPreDigest::Primary { vrf_output, vrf_proof, authority_index, slot_number } => {
				// Verify (at compile time) that the sizes in babe_primitives are correct
				let _: [u8; super::VRF_OUTPUT_LENGTH] = vrf_output;
				let _: [u8; super::VRF_PROOF_LENGTH] = vrf_proof;

				PreDigest::Primary {
					vrf_proof: VRFProof::from_bytes(&vrf_proof).map_err(convert_error)?,
					vrf_output: VRFOutput::from_bytes(&vrf_output).map_err(convert_error)?,
					authority_index,
					slot_number,
				}
			},
			RawPreDigest::Secondary { authority_index, slot_number } => {
				PreDigest::Secondary { authority_index, slot_number }
			},
		};

		Ok(pre_digest)
	}
}

/// Information about the next epoch. This is broadcast in the first block
/// of the epoch.
#[derive(Decode, Encode, Default, PartialEq, Eq, Clone, sp_runtime::RuntimeDebug)]
pub struct NextEpochDescriptor {
	/// The authorities.
	pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,

	/// The value of randomness to use for the slot-assignment.
	pub randomness: [u8; VRF_OUTPUT_LENGTH],
}

/// A digest item which is usable with BABE consensus.
#[cfg(feature = "std")]
pub trait CompatibleDigestItem: Sized {
	/// Construct a digest item which contains a BABE pre-digest.
	fn babe_pre_digest(seal: PreDigest) -> Self;

	/// If this item is an BABE pre-digest, return it.
	fn as_babe_pre_digest(&self) -> Option<PreDigest>;

	/// Construct a digest item which contains a BABE seal.
	fn babe_seal(signature: AuthoritySignature) -> Self;

	/// If this item is a BABE signature, return the signature.
	fn as_babe_seal(&self) -> Option<AuthoritySignature>;

	/// If this item is a BABE epoch, return it.
	fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor>;
}

#[cfg(feature = "std")]
impl<Hash> CompatibleDigestItem for DigestItem<Hash> where
	Hash: Debug + Send + Sync + Eq + Clone + Codec + 'static
{
	fn babe_pre_digest(digest: PreDigest) -> Self {
		DigestItem::PreRuntime(BABE_ENGINE_ID, digest.encode())
	}

	fn as_babe_pre_digest(&self) -> Option<PreDigest> {
		self.try_to(OpaqueDigestItemId::PreRuntime(&BABE_ENGINE_ID))
	}

	fn babe_seal(signature: AuthoritySignature) -> Self {
		DigestItem::Seal(BABE_ENGINE_ID, signature.encode())
	}

	fn as_babe_seal(&self) -> Option<AuthoritySignature> {
		self.try_to(OpaqueDigestItemId::Seal(&BABE_ENGINE_ID))
	}

	fn as_next_epoch_descriptor(&self) -> Option<NextEpochDescriptor> {
		self.try_to(OpaqueDigestItemId::Consensus(&BABE_ENGINE_ID))
			.and_then(|x: super::ConsensusLog| match x {
				super::ConsensusLog::NextEpochData(n) => Some(n),
				_ => None,
			})
	}
}

#[cfg(feature = "std")]
fn convert_error(e: SignatureError) -> codec::Error {
	use SignatureError::*;
	use MultiSignatureStage::*;
	match e {
		EquationFalse => "Signature error: `EquationFalse`".into(),
		PointDecompressionError => "Signature error: `PointDecompressionError`".into(),
		ScalarFormatError => "Signature error: `ScalarFormatError`".into(),
		NotMarkedSchnorrkel => "Signature error: `NotMarkedSchnorrkel`".into(),
		BytesLengthError { .. } => "Signature error: `BytesLengthError`".into(),
		MuSigAbsent { musig_stage: Commitment } =>
			"Signature error: `MuSigAbsent` at stage `Commitment`".into(),
		MuSigAbsent { musig_stage: Reveal } =>
			"Signature error: `MuSigAbsent` at stage `Reveal`".into(),
		MuSigAbsent { musig_stage: Cosignature } =>
			"Signature error: `MuSigAbsent` at stage `Commitment`".into(),
		MuSigInconsistent { musig_stage: Commitment, duplicate: true } =>
			"Signature error: `MuSigInconsistent` at stage `Commitment` on duplicate".into(),
		MuSigInconsistent { musig_stage: Commitment, duplicate: false } =>
			"Signature error: `MuSigInconsistent` at stage `Commitment` on not duplicate".into(),
		MuSigInconsistent { musig_stage: Reveal, duplicate: true } =>
			"Signature error: `MuSigInconsistent` at stage `Reveal` on duplicate".into(),
		MuSigInconsistent { musig_stage: Reveal, duplicate: false } =>
			"Signature error: `MuSigInconsistent` at stage `Reveal` on not duplicate".into(),
		MuSigInconsistent { musig_stage: Cosignature, duplicate: true } =>
			"Signature error: `MuSigInconsistent` at stage `Cosignature` on duplicate".into(),
		MuSigInconsistent { musig_stage: Cosignature, duplicate: false } =>
			"Signature error: `MuSigInconsistent` at stage `Cosignature` on not duplicate".into(),
	}
}