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
use async_trait::async_trait;
use creep::Context;

use crate::types::{Hash, SignedTransaction};
use crate::ProtocolResult;

#[allow(dead_code)]
pub struct MixedTxHashes {
    pub order_tx_hashes:   Vec<Hash>,
    pub propose_tx_hashes: Vec<Hash>,
}

impl MixedTxHashes {
    pub fn clap(self) -> (Vec<Hash>, Vec<Hash>) {
        (self.order_tx_hashes, self.propose_tx_hashes)
    }
}

#[async_trait]
pub trait MemPool: Send + Sync {
    async fn insert(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

    async fn package(&self, ctx: Context, cycle_limit: u64) -> ProtocolResult<MixedTxHashes>;

    async fn flush(&self, ctx: Context, tx_hashes: Vec<Hash>) -> ProtocolResult<()>;

    async fn get_full_txs(
        &self,
        ctx: Context,
        tx_hashes: Vec<Hash>,
    ) -> ProtocolResult<Vec<SignedTransaction>>;

    async fn ensure_order_txs(
        &self,
        ctx: Context,
        order_tx_hashes: Vec<Hash>,
    ) -> ProtocolResult<()>;

    async fn sync_propose_txs(
        &self,
        ctx: Context,
        propose_tx_hashes: Vec<Hash>,
    ) -> ProtocolResult<()>;
}

#[async_trait]
pub trait MemPoolAdapter: Send + Sync {
    async fn pull_txs(
        &self,
        ctx: Context,
        tx_hashes: Vec<Hash>,
    ) -> ProtocolResult<Vec<SignedTransaction>>;

    async fn broadcast_tx(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

    async fn check_signature(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

    async fn check_transaction(&self, ctx: Context, tx: SignedTransaction) -> ProtocolResult<()>;

    async fn check_storage_exist(&self, ctx: Context, tx_hash: Hash) -> ProtocolResult<()>;

    async fn get_latest_epoch_id(&self, ctx: Context) -> ProtocolResult<u64>;
}