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
use sc_client_api::{Backend, Finalizer as SubstrateFinalizer, HeaderBackend, LockImportRun};
use sp_blockchain::Error as ClientError;
use sp_runtime::traits::Header as SubstrateHeader;

use crate::{
    phron_primitives::Block,
    finalization::{PhronFinalizer, BlockFinalizer},
    sync::{
        substrate::{InnerJustification, Justification},
        Finalizer,
    },
};

impl<BE, C> Finalizer<Justification> for PhronFinalizer<Block, BE, C>
where
    BE: Backend<Block>,
    C: HeaderBackend<Block> + LockImportRun<Block, BE> + SubstrateFinalizer<Block, BE>,
{
    type Error = ClientError;

    fn finalize(&self, justification: Justification) -> Result<(), Self::Error> {
        match justification.inner_justification {
            InnerJustification::PhronJustification(phron_justification) => self.finalize_block(
                (justification.header.hash(), *justification.header.number()).into(),
                phron_justification.into(),
            ),
            _ => Err(Self::Error::BadJustification(
                "Trying fo finalize the genesis block using virtual sync justification."
                    .to_string(),
            )),
        }
    }
}