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
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use std::{
    fmt::{Debug, Display, Formatter},
    sync::Arc,
};

use crate::{
    experimental::pipeline_phase::{ResponseWithInstruction, StatelessPipeline},
    state_replication::{StateComputer, StateComputerCommitCallBackType},
};
use async_trait::async_trait;
use consensus_types::executed_block::ExecutedBlock;
use diem_types::ledger_info::LedgerInfoWithSignatures;
use executor_types::Error;

/// [ This class is used when consensus.decoupled = true ]
/// PersistingPhase is a singleton that receives aggregated blocks from
/// the buffer manager and persists them. Upon success, it returns
/// a response.

pub struct PersistingRequest {
    pub blocks: Vec<Arc<ExecutedBlock>>,
    pub commit_ledger_info: LedgerInfoWithSignatures,
    pub callback: StateComputerCommitCallBackType,
}

impl Debug for PersistingRequest {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}

impl Display for PersistingRequest {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        write!(
            f,
            "PersistingRequest({:?}, {})",
            self.blocks, self.commit_ledger_info,
        )
    }
}

pub type PersistingResponse = Result<(), Error>;

pub struct PersistingPhase {
    persisting_handle: Arc<dyn StateComputer>,
}

impl PersistingPhase {
    pub fn new(persisting_handle: Arc<dyn StateComputer>) -> Self {
        Self { persisting_handle }
    }
}

#[async_trait]
impl StatelessPipeline for PersistingPhase {
    type Request = PersistingRequest;
    type Response = PersistingResponse;
    async fn process(&self, req: PersistingRequest) -> ResponseWithInstruction<PersistingResponse> {
        let PersistingRequest {
            blocks,
            commit_ledger_info,
            callback,
        } = req;

        ResponseWithInstruction::from(
            self.persisting_handle
                .commit(&blocks, commit_ledger_info, callback)
                .await,
        )
    }
}