-
Notifications
You must be signed in to change notification settings - Fork 16
/
router.rs
73 lines (63 loc) · 2.12 KB
/
router.rs
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
use crate::modules::bank::BankBalanceKeeper;
use crate::modules::ibc::transfer::IbcTransferModule;
use basecoin_store::context::Store;
use ibc::apps::transfer::types::MODULE_ID_STR as IBC_TRANSFER_MODULE_ID;
use ibc::core::router::module::Module as IbcModule;
use ibc::core::router::router::Router;
use ibc::core::router::types::error::RouterError;
use ibc::core::{host::types::identifiers::PortId, router::types::module::ModuleId};
use std::{borrow::Borrow, collections::BTreeMap, fmt::Debug};
#[derive(Clone, Debug)]
pub struct IbcRouter<S>
where
S: Store + Debug,
{
transfer: IbcTransferModule<BankBalanceKeeper<S>>,
/// Mapping of which IBC modules own which port
port_to_module_map: BTreeMap<PortId, ModuleId>,
}
impl<S> IbcRouter<S>
where
S: Store + Debug,
{
pub fn new(transfer: IbcTransferModule<BankBalanceKeeper<S>>) -> Self {
let mut port_to_module_map = BTreeMap::default();
let transfer_module_id: ModuleId = ModuleId::new(IBC_TRANSFER_MODULE_ID.to_string());
port_to_module_map.insert(PortId::transfer(), transfer_module_id);
IbcRouter {
transfer,
port_to_module_map,
}
}
pub fn transfer(self) -> IbcTransferModule<BankBalanceKeeper<S>> {
self.transfer
}
}
impl<S> Router for IbcRouter<S>
where
S: Store + Debug,
{
fn get_route(&self, module_id: &ModuleId) -> Option<&dyn IbcModule> {
if <ModuleId as Borrow<str>>::borrow(module_id) == IBC_TRANSFER_MODULE_ID {
Some(&self.transfer as &dyn IbcModule)
} else {
None
}
}
fn get_route_mut(&mut self, module_id: &ModuleId) -> Option<&mut dyn IbcModule> {
if <ModuleId as Borrow<str>>::borrow(module_id) == IBC_TRANSFER_MODULE_ID {
Some(&mut self.transfer as &mut dyn IbcModule)
} else {
None
}
}
fn lookup_module(&self, port_id: &PortId) -> Option<ModuleId> {
self.port_to_module_map
.get(port_id)
.ok_or(RouterError::UnknownPort {
port_id: port_id.clone(),
})
.cloned()
.ok()
}
}