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

pub mod address;
pub mod constant;
pub mod network_type;
pub mod rpc_client;
pub mod types;

use async_trait::async_trait;
use ckb_types::H256;
use serde::{Deserialize, Serialize};
use types::{HeaderViewWithExtension, IndexerTip, Order, Pagination, SearchKey, Tx};

use ckb_jsonrpc_types::{
    BlockNumber, BlockView, CellInfo, HeaderView, JsonBytes, OutPoint, TransactionView, Uint32,
};
// Cell changes on a single block
#[derive(Serialize, Deserialize)]
pub struct Submit {
    pub header: HeaderView,
    pub inputs: Vec<OutPoint>,
    pub outputs: Vec<(OutPoint, CellInfo)>,
}

pub trait TipState {
    fn load(&self) -> &IndexerTip;
    fn update(&mut self, current: IndexerTip);
}

#[async_trait]
pub trait SubmitProcess {
    fn is_closed(&self) -> bool;
    // if false return, it means this cell process should be shutdown
    async fn submit_cells(&mut self, cells: Vec<Submit>) -> bool;
    async fn submit_headers(&mut self, headers: Vec<HeaderViewWithExtension>) -> bool;
}

#[async_trait]
pub trait Rpc {
    // ckb indexer `get_transactions`
    async fn get_transactions(
        &self,
        search_key: SearchKey,
        order: Order,
        limit: Uint32,
        after: Option<JsonBytes>,
    ) -> Result<Pagination<Tx>, std::io::Error>;

    // ckb rpc `get_transaction`
    async fn get_transaction(&self, hash: &H256)
        -> Result<Option<TransactionView>, std::io::Error>;

    // ckb rpc `get_header_by_number`
    async fn get_header_by_number(&self, number: BlockNumber)
        -> Result<HeaderView, std::io::Error>;
    // ckb indexer `get_indexer_tip`
    async fn get_indexer_tip(&self) -> Result<IndexerTip, std::io::Error>;

    // ckb rpc `get_block_by_number`
    async fn get_block_by_number(&self, number: BlockNumber) -> Result<BlockView, std::io::Error>;
}

impl TipState for IndexerTip {
    fn load(&self) -> &IndexerTip {
        self
    }

    fn update(&mut self, current: IndexerTip) {
        *self = current
    }
}