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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use async_trait::async_trait;
use creep::Context;
use crate::types::{Address, Epoch, Hash, MerkleRoot, Proof, SignedTransaction, Validator};
use crate::{traits::mempool::MixedTxHashes, ProtocolResult};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MessageTarget {
Broadcast,
Specified(Address),
}
#[derive(Debug, Clone)]
pub struct NodeInfo {
pub chain_id: Hash,
pub self_address: Address,
}
#[async_trait]
pub trait Consensus: Send + Sync {
async fn set_proposal(&self, ctx: Context, proposal: Vec<u8>) -> ProtocolResult<()>;
async fn set_vote(&self, ctx: Context, vote: Vec<u8>) -> ProtocolResult<()>;
async fn set_qc(&self, ctx: Context, qc: Vec<u8>) -> ProtocolResult<()>;
async fn update_epoch(&self, ctx: Context, msg: Vec<u8>) -> ProtocolResult<()>;
}
#[async_trait]
pub trait ConsensusAdapter: Send + Sync {
async fn get_txs_from_mempool(
&self,
ctx: Context,
epoch_id: u64,
cycle_limit: u64,
) -> ProtocolResult<MixedTxHashes>;
async fn check_txs(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
async fn sync_txs(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
async fn get_full_txs(
&self,
ctx: Context,
txs: Vec<Hash>,
) -> ProtocolResult<Vec<SignedTransaction>>;
async fn transmit(
&self,
ctx: Context,
msg: Vec<u8>,
end: &str,
target: MessageTarget,
) -> ProtocolResult<()>;
async fn execute(
&self,
node_info: NodeInfo,
order_root: MerkleRoot,
epoch_id: u64,
cycles_price: u64,
coinbase: Address,
signed_txs: Vec<SignedTransaction>,
cycles_limit: u64,
timestamp: u64,
) -> ProtocolResult<()>;
async fn flush_mempool(&self, ctx: Context, txs: Vec<Hash>) -> ProtocolResult<()>;
async fn save_epoch(&self, ctx: Context, epoch: Epoch) -> ProtocolResult<()>;
async fn save_proof(&self, ctx: Context, proof: Proof) -> ProtocolResult<()>;
async fn save_signed_txs(
&self,
ctx: Context,
signed_txs: Vec<SignedTransaction>,
) -> ProtocolResult<()>;
async fn get_last_validators(
&self,
ctx: Context,
epoch_id: u64,
) -> ProtocolResult<Vec<Validator>>;
async fn get_current_epoch_id(&self, ctx: Context) -> ProtocolResult<u64>;
async fn pull_epoch(&self, ctx: Context, epoch_id: u64, end: &str) -> ProtocolResult<Epoch>;
async fn pull_txs(
&self,
ctx: Context,
hashes: Vec<Hash>,
end: &str,
) -> ProtocolResult<Vec<SignedTransaction>>;
async fn get_epoch_by_id(&self, ctx: Context, epoch_id: u64) -> ProtocolResult<Epoch>;
}