|
| 1 | +// Copyright 2019-2020 Parity Technologies (UK) Ltd. |
| 2 | +// This file is part of Parity Bridges Common. |
| 3 | + |
| 4 | +// Parity Bridges Common is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// Parity Bridges Common is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use bridge_node_runtime::{BlockNumber, Hash, Header as RuntimeHeader}; |
| 18 | +use codec::Decode; |
| 19 | +use sp_blockchain::Error as ClientError; |
| 20 | + |
| 21 | +/// Builtin errors. |
| 22 | +#[derive(Debug)] |
| 23 | +pub enum Error { |
| 24 | + /// Failed to decode Substrate header. |
| 25 | + HeaderDecode(codec::Error), |
| 26 | + /// Failed to decode best voters set. |
| 27 | + BestVotersDecode(codec::Error), |
| 28 | + /// Failed to decode finality proof. |
| 29 | + FinalityProofDecode(codec::Error), |
| 30 | + /// Failed to verify justification. |
| 31 | + JustificationVerify(ClientError), |
| 32 | +} |
| 33 | + |
| 34 | +/// Substrate header. |
| 35 | +#[derive(Debug)] |
| 36 | +pub struct Header { |
| 37 | + /// Header hash. |
| 38 | + pub hash: Hash, |
| 39 | + /// Parent header hash. |
| 40 | + pub parent_hash: Hash, |
| 41 | + /// Header number. |
| 42 | + pub number: BlockNumber, |
| 43 | + /// GRANDPA validators change signal. |
| 44 | + pub signal: Option<ValidatorsSetSignal>, |
| 45 | +} |
| 46 | + |
| 47 | +/// GRANDPA validators set change signal. |
| 48 | +#[derive(Debug)] |
| 49 | +pub struct ValidatorsSetSignal { |
| 50 | + /// Signal delay. |
| 51 | + pub delay: BlockNumber, |
| 52 | + /// New validators set. |
| 53 | + pub validators: Vec<u8>, |
| 54 | +} |
| 55 | + |
| 56 | +/// Parse Substrate header. |
| 57 | +pub fn parse_substrate_header(raw_header: &[u8]) -> Result<Header, Error> { |
| 58 | + RuntimeHeader::decode(&mut &raw_header[..]) |
| 59 | + .map(|header| Header { |
| 60 | + hash: header.hash(), |
| 61 | + parent_hash: header.parent_hash, |
| 62 | + number: header.number, |
| 63 | + signal: None, // TODO: parse me |
| 64 | + }) |
| 65 | + .map_err(Error::HeaderDecode) |
| 66 | +} |
| 67 | + |
| 68 | +/// Verify GRANDPA finality proof. |
| 69 | +pub fn verify_substrate_finality_proof( |
| 70 | + _best_set_id: u64, |
| 71 | + _raw_best_voters: &[u8], |
| 72 | + _raw_best_header: &[u8], |
| 73 | + _raw_headers: &[&[u8]], |
| 74 | + _raw_finality_proof: &[u8], |
| 75 | +) -> Result<(usize, usize), Error> { |
| 76 | + Err(Error::JustificationVerify(ClientError::Msg( |
| 77 | + "Not yet implemented".into(), |
| 78 | + ))) // TODO: implement me |
| 79 | +} |
0 commit comments