otx_pool_plugin_protocol/
lib.rs

1use otx_format::jsonrpc_types::OpenTransaction;
2
3use ckb_types::core::service::Request;
4use ckb_types::H256;
5use crossbeam_channel::Sender;
6use serde_derive::{Deserialize, Serialize};
7
8use std::path::PathBuf;
9
10pub trait Plugin: Send + Sync {
11    fn get_name(&self) -> String;
12    fn get_meta(&self) -> PluginMeta;
13    fn get_info(&self) -> PluginInfo;
14    fn on_new_otx(&self, _otx: OpenTransaction) {
15        // This is a default implementation that does nothing.
16    }
17    fn on_new_intervel(&self, _interval: u64) {
18        // This is a default implementation that does nothing.
19    }
20    fn on_commit_otx(&self, _otxs: Vec<H256>) {
21        // This is a default implementation that does nothing.
22    }
23}
24
25#[derive(Clone, Debug)]
26pub struct PluginMeta {
27    /// The installation path of the plug-in, the built-in plugin binary_path is default value.
28    pub binary_path: PathBuf,
29    /// Activation falg.
30    pub is_active: bool,
31    /// Built-in flag.
32    pub is_built_in: bool,
33}
34
35impl PluginMeta {
36    pub fn new(binary_path: PathBuf, is_active: bool, is_built_in: bool) -> PluginMeta {
37        PluginMeta {
38            binary_path,
39            is_active,
40            is_built_in,
41        }
42    }
43}
44
45pub type HostServiceHandler = Sender<Request<MessageFromPlugin, MessageFromHost>>;
46
47pub enum MessageType {
48    Request,
49    Response,
50    Notify,
51}
52
53#[derive(Clone, Debug, Serialize, Deserialize)]
54pub enum MessageFromHost {
55    // Notify
56    NewOtx(OpenTransaction),
57    NewInterval(u64),
58    OtxPoolStart,
59    OtxPoolStop,
60    CommitOtx(Vec<H256>),
61
62    // Request
63    GetPluginInfo,
64
65    // Response
66    Ok,
67    Error(String),
68}
69
70impl MessageFromHost {
71    pub fn get_message_type(&self) -> MessageType {
72        match self {
73            Self::NewOtx(_)
74            | Self::NewInterval(_)
75            | Self::OtxPoolStart
76            | Self::OtxPoolStop
77            | Self::CommitOtx(_) => MessageType::Notify,
78            Self::GetPluginInfo | Self::Ok | Self::Error(_) => MessageType::Request,
79        }
80    }
81}
82
83#[derive(Clone, Debug, Serialize, Deserialize)]
84pub enum MessageFromPlugin {
85    // Response
86    Ok,
87    Error(String),
88    PluginInfo(PluginInfo),
89
90    // Request
91    NewMergedOtx((OpenTransaction, Vec<H256>)),
92    DiscardOtx((H256, OpenTransaction)),
93    ModifyOtx((H256, OpenTransaction)),
94    SentToCkb(H256),
95    MergeOtxsAndSentToCkb((Vec<H256>, H256)),
96}
97
98impl MessageFromPlugin {
99    pub fn get_message_type(&self) -> MessageType {
100        match self {
101            Self::Ok | Self::Error(_) | Self::PluginInfo(_) => MessageType::Response,
102            Self::NewMergedOtx(_)
103            | Self::DiscardOtx(_)
104            | Self::ModifyOtx(_)
105            | Self::SentToCkb(_)
106            | Self::MergeOtxsAndSentToCkb(_) => MessageType::Request,
107        }
108    }
109}
110
111#[derive(Clone, Debug, Serialize, Deserialize)]
112pub struct PluginInfo {
113    pub name: String,
114    pub description: String,
115    pub version: String,
116}
117
118impl PluginInfo {
119    pub fn new(name: &str, description: &str, version: &str) -> Self {
120        PluginInfo {
121            name: name.into(),
122            description: description.into(),
123            version: version.into(),
124        }
125    }
126}