muta_protocol/traits/
consensus.rs1use 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 async fn set_proposal(&self, ctx: Context, proposal: Vec<u8>) -> ProtocolResult<()>;
23
24 async fn set_vote(&self, ctx: Context, vote: Vec<u8>) -> ProtocolResult<()>;
26
27 async fn set_qc(&self, ctx: Context, qc: Vec<u8>) -> ProtocolResult<()>;
29
30 async fn update_epoch(&self, ctx: Context, msg: Vec<u8>) -> ProtocolResult<()>;
33}
34
35#[async_trait]
36pub trait ConsensusAdapter: Send + Sync {
37 async fn get_txs_from_mempool(
41 &self,
42 ctx: Context,
43 epoch_id: u64,
44 cycle_limit: u64,
45 ) -> ProtocolResult<MixedTxHashes>;
46
47 async fn check_txs(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
49
50 async fn sync_txs(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
52
53 async fn get_full_txs(
55 &self,
56 ctx: Context,
57 txs: Vec<Hash>,
58 ) -> ProtocolResult<Vec<SignedTransaction>>;
59
60 async fn transmit(
62 &self,
63 ctx: Context,
64 msg: Vec<u8>,
65 end: &str,
66 target: MessageTarget,
67 ) -> ProtocolResult<()>;
68
69 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 async fn flush_mempool(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
84
85 async fn save_epoch(&self, ctx: Context, epoch: Epoch) -> ProtocolResult<()>;
87
88 async fn save_proof(&self, ctx: Context, proof: Proof) -> ProtocolResult<()>;
90
91 async fn save_signed_txs(
93 &self,
94 ctx: Context,
95 signed_txs: Vec<SignedTransaction>,
96 ) -> ProtocolResult<()>;
97
98 async fn get_last_validators(
100 &self,
101 ctx: Context,
102 epoch_id: u64,
103 ) -> ProtocolResult<Vec<Validator>>;
104
105 async fn get_current_epoch_id(&self, ctx: Context) -> ProtocolResult<u64>;
107
108 async fn pull_epoch(&self, ctx: Context, epoch_id: u64, end: &str) -> ProtocolResult<Epoch>;
110
111 async fn pull_txs(
114 &self,
115 ctx: Context,
116 hashes: Vec<Hash>,
117 end: &str,
118 ) -> ProtocolResult<Vec<SignedTransaction>>;
119
120 async fn get_epoch_by_id(&self, ctx: Context, epoch_id: u64) -> ProtocolResult<Epoch>;
122}