1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>.
16
use {
17
    cumulus_primitives_core::ParaId,
18
    jsonrpsee::{core::RpcResult, proc_macros::rpc},
19
};
20

            
21
#[rpc(server)]
22
#[jsonrpsee::core::async_trait]
23
pub trait ManualContainerChainsExclusionApi {
24
    /// Preventing container chains from producing blocks
25
    #[method(name = "mock_excludeContainerChains")]
26
    async fn exclude_container_chains(&self, para_ids: Vec<ParaId>) -> RpcResult<()>;
27
}
28

            
29
pub struct ManualContainerChainsExclusion {
30
    pub container_chain_exclusion_message_channel: flume::Sender<Vec<ParaId>>,
31
}
32

            
33
#[jsonrpsee::core::async_trait]
34
impl ManualContainerChainsExclusionApiServer for ManualContainerChainsExclusion {
35
16
    async fn exclude_container_chains(&self, para_ids: Vec<ParaId>) -> RpcResult<()> {
36
16
        let container_chain_exclusion_message_channel =
37
16
            self.container_chain_exclusion_message_channel.clone();
38
16

            
39
16
        // Push the message to the shared channel where it will be queued up
40
16
        // to be injected in to an upcoming block.
41
16
        container_chain_exclusion_message_channel
42
16
            .send_async(para_ids)
43
16
            .await
44
16
            .map_err(|err| internal_err(err.to_string()))?;
45

            
46
16
        Ok(())
47
32
    }
48
}
49

            
50
// This bit cribbed from frontier.
51
pub fn internal_err<T: AsRef<str>>(message: T) -> jsonrpsee::types::ErrorObjectOwned {
52
    jsonrpsee::types::error::ErrorObject::borrowed(
53
        jsonrpsee::types::error::INTERNAL_ERROR_CODE,
54
        message.as_ref(),
55
        None,
56
    )
57
    .into_owned()
58
}