This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathverification.rs
More file actions
109 lines (92 loc) · 3.83 KB
/
verification.rs
File metadata and controls
109 lines (92 loc) · 3.83 KB
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
// This file is part of Substrate.
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program 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.
// This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
//! Verification for BABE headers.
use super::{find_pre_digest, sassafras_err, BlockT, Epoch, Error};
use sc_consensus_slots::CheckedHeader;
use sp_consensus_sassafras::{
digests::{CompatibleDigestItem, PreDigest},
make_transcript, AuthorityId, AuthorityPair, AuthoritySignature,
};
use sp_consensus_slots::Slot;
use sp_runtime::{traits::Header, DigestItem};
/// Sassafras verification parameters
pub struct VerificationParams<'a, B: 'a + BlockT> {
/// The header being verified.
pub header: B::Header,
/// The pre-digest of the header being verified. This is optional - if prior verification
/// code had to read it, it can be included here to avoid duplicate work.
pub pre_digest: Option<PreDigest>,
/// The slot number of the current time.
pub slot_now: Slot,
/// Epoch descriptor of the epoch this block _should_ be under, if it's valid.
pub epoch: &'a Epoch,
}
pub struct VerifiedHeaderInfo {
pub pre_digest: DigestItem,
pub seal: DigestItem,
pub author: AuthorityId,
}
/// Check a header has been signed by the right key. If the slot is too far in
/// the future, an error will be returned. If successful, returns the pre-header
/// and the digest item containing the seal.
///
/// The seal must be the last digest. Otherwise, the whole header is considered
/// unsigned. This is required for security and must not be changed.
///
/// This digest item will always return `Some` when used with `as_babe_pre_digest`.
///
/// The given header can either be from a primary or secondary slot assignment,
/// with each having different validation logic.
pub(super) fn check_header<B: BlockT + Sized>(
params: VerificationParams<B>,
) -> Result<CheckedHeader<B::Header, VerifiedHeaderInfo>, Error<B>> {
let VerificationParams { mut header, pre_digest, slot_now, epoch } = params;
let pre_digest = pre_digest.map(Ok).unwrap_or_else(|| find_pre_digest::<B>(&header))?;
if pre_digest.slot > slot_now {
return Ok(CheckedHeader::Deferred(header, pre_digest.slot))
}
let seal = header
.digest_mut()
.pop()
.ok_or_else(|| sassafras_err(Error::HeaderUnsealed(header.hash())))?;
let _sig = seal
.as_sassafras_seal()
.ok_or_else(|| sassafras_err(Error::HeaderBadSeal(header.hash())))?;
// The pre-hash of the header doesn't include the seal and that's what we sign
let _pre_hash = header.hash();
//let authorities = &epoch.authorities;
let author = match epoch.authorities.get(pre_digest.authority_index as usize) {
Some(author) => author.0.clone(),
None => return Err(sassafras_err(Error::SlotAuthorNotFound)),
};
match pre_digest.ticket_vrf_proof {
Some(_) => {
// TODO-SASS: check primary
// 1. the ticket proof is valid
// 2. the block vrf proof is valid
log::debug!(target: "sassafras", "🌳 checking primary (TODO)");
},
None => {
// TODO-SASS: check secondary
// 1. the expected author index
log::debug!(target: "sassafras", "🌳 checking secondary (TODO)");
},
}
let info = VerifiedHeaderInfo {
pre_digest: CompatibleDigestItem::sassafras_pre_digest(pre_digest),
seal,
author,
};
Ok(CheckedHeader::Checked(header, info))
}