Skip to main content

muta_protocol/traits/
consensus.rs

1use async_trait::async_trait;
2use creep::Context;
3
4use crate::types::{Address, Epoch, Hash, MerkleRoot, Proof, SignedTransaction, Validator};
5use crate::{traits::mempool::MixedTxHashes, ProtocolResult};
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum MessageTarget {
9    Broadcast,
10    Specified(Address),
11}
12
13#[derive(Debug, Clone)]
14pub struct NodeInfo {
15    pub chain_id:     Hash,
16    pub self_address: Address,
17}
18
19#[async_trait]
20pub trait Consensus: Send + Sync {
21    /// Network set a received signed proposal to consensus.
22    async fn set_proposal(&self, ctx: Context, proposal: Vec<u8>) -> ProtocolResult<()>;
23
24    /// Network set a received signed vote to consensus.
25    async fn set_vote(&self, ctx: Context, vote: Vec<u8>) -> ProtocolResult<()>;
26
27    /// Network set a received quorum certificate to consensus.
28    async fn set_qc(&self, ctx: Context, qc: Vec<u8>) -> ProtocolResult<()>;
29
30    /// Update an epoch to consensus. This may be either a rich status from the
31    /// executor or a synchronous epoch that need to be insert to the database.
32    async fn update_epoch(&self, ctx: Context, msg: Vec<u8>) -> ProtocolResult<()>;
33}
34
35#[async_trait]
36pub trait ConsensusAdapter: Send + Sync {
37    /// Get some transaction hashes of the given epoch ID. The amount of the
38    /// transactions is limited by the given cycle limit and return a
39    /// `MixedTxHashes` struct.
40    async fn get_txs_from_mempool(
41        &self,
42        ctx: Context,
43        epoch_id: u64,
44        cycle_limit: u64,
45    ) -> ProtocolResult<MixedTxHashes>;
46
47    /// Check the correctness of the given transactions.
48    async fn check_txs(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
49
50    /// Synchronous signed transactions.
51    async fn sync_txs(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
52
53    /// Get the signed transactions corresponding to the given hashes.
54    async fn get_full_txs(
55        &self,
56        ctx: Context,
57        txs: Vec<Hash>,
58    ) -> ProtocolResult<Vec<SignedTransaction>>;
59
60    /// Consensus transmit a message to the given target.
61    async fn transmit(
62        &self,
63        ctx: Context,
64        msg: Vec<u8>,
65        end: &str,
66        target: MessageTarget,
67    ) -> ProtocolResult<()>;
68
69    /// Execute some transactions.
70    async fn execute(
71        &self,
72        node_info: NodeInfo,
73        order_root: MerkleRoot,
74        epoch_id: u64,
75        cycles_price: u64,
76        coinbase: Address,
77        signed_txs: Vec<SignedTransaction>,
78        cycles_limit: u64,
79        timestamp: u64,
80    ) -> ProtocolResult<()>;
81
82    /// Flush the given transactions in the mempool.
83    async fn flush_mempool(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
84
85    /// Save an epoch to the database.
86    async fn save_epoch(&self, ctx: Context, epoch: Epoch) -> ProtocolResult<()>;
87
88    ///
89    async fn save_proof(&self, ctx: Context, proof: Proof) -> ProtocolResult<()>;
90
91    /// Save some signed transactions to the database.
92    async fn save_signed_txs(
93        &self,
94        ctx: Context,
95        signed_txs: Vec<SignedTransaction>,
96    ) -> ProtocolResult<()>;
97
98    /// Get the validator list of the given last epoch.
99    async fn get_last_validators(
100        &self,
101        ctx: Context,
102        epoch_id: u64,
103    ) -> ProtocolResult<Vec<Validator>>;
104
105    /// Get the current epoch ID from storage.
106    async fn get_current_epoch_id(&self, ctx: Context) -> ProtocolResult<u64>;
107
108    /// Pull some epochs from other nodes from `begin` to `end`.
109    async fn pull_epoch(&self, ctx: Context, epoch_id: u64, end: &str) -> ProtocolResult<Epoch>;
110
111    /// Pull signed transactions corresponding to the given hashes from other
112    /// nodes.
113    async fn pull_txs(
114        &self,
115        ctx: Context,
116        hashes: Vec<Hash>,
117        end: &str,
118    ) -> ProtocolResult<Vec<SignedTransaction>>;
119
120    /// Get an epoch corresponding to the given epoch ID.
121    async fn get_epoch_by_id(&self, ctx: Context, epoch_id: u64) -> ProtocolResult<Epoch>;
122}