bp_messages/
storage_keys.rs1pub const OPERATING_MODE_VALUE_NAME: &str = "PalletOperatingMode";
21pub const OUTBOUND_MESSAGES_MAP_NAME: &str = "OutboundMessages";
23pub const OUTBOUND_LANES_MAP_NAME: &str = "OutboundLanes";
25pub const INBOUND_LANES_MAP_NAME: &str = "InboundLanes";
27
28use crate::{MessageKey, MessageNonce};
29
30use codec::Encode;
31use frame_support::Blake2_128Concat;
32use sp_core::storage::StorageKey;
33
34pub fn operating_mode_key(pallet_prefix: &str) -> StorageKey {
36 StorageKey(
37 bp_runtime::storage_value_final_key(
38 pallet_prefix.as_bytes(),
39 OPERATING_MODE_VALUE_NAME.as_bytes(),
40 )
41 .to_vec(),
42 )
43}
44
45pub fn message_key<LaneId: Encode>(
47 pallet_prefix: &str,
48 lane: LaneId,
49 nonce: MessageNonce,
50) -> StorageKey {
51 bp_runtime::storage_map_final_key::<Blake2_128Concat>(
52 pallet_prefix,
53 OUTBOUND_MESSAGES_MAP_NAME,
54 &MessageKey { lane_id: lane, nonce }.encode(),
55 )
56}
57
58pub fn outbound_lane_data_key<LaneId: Encode>(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
60 bp_runtime::storage_map_final_key::<Blake2_128Concat>(
61 pallet_prefix,
62 OUTBOUND_LANES_MAP_NAME,
63 &lane.encode(),
64 )
65}
66
67pub fn inbound_lane_data_key<LaneId: Encode>(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
69 bp_runtime::storage_map_final_key::<Blake2_128Concat>(
70 pallet_prefix,
71 INBOUND_LANES_MAP_NAME,
72 &lane.encode(),
73 )
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use crate::{
80 lane::{HashedLaneId, LegacyLaneId},
81 LaneIdType,
82 };
83 use hex_literal::hex;
84
85 #[test]
86 fn operating_mode_key_computed_properly() {
87 let storage_key = operating_mode_key("BridgeMessages").0;
90 assert_eq!(
91 storage_key,
92 hex!("dd16c784ebd3390a9bc0357c7511ed010f4cf0917788d791142ff6c1f216e7b3").to_vec(),
93 "Unexpected storage key: {}",
94 hex::encode(&storage_key),
95 );
96 }
97
98 #[test]
99 fn storage_message_key_computed_properly() {
100 let storage_key =
103 message_key("BridgeMessages", &HashedLaneId::try_new(1, 2).unwrap(), 42).0;
104 assert_eq!(
105 storage_key,
106 hex!("dd16c784ebd3390a9bc0357c7511ed018a395e6242c6813b196ca31ed0547ea70e9bdb8f50c68d12f06eabb57759ee5eb1d3dccd8b3c3a012afe265f3e3c4432129b8aee50c9dcf87f9793be208e5ea02a00000000000000").to_vec(),
107 "Unexpected storage key: {}",
108 hex::encode(&storage_key),
109 );
110
111 let storage_key = message_key("BridgeMessages", &LegacyLaneId(*b"test"), 42).0;
113 assert_eq!(
114 storage_key,
115 hex!("dd16c784ebd3390a9bc0357c7511ed018a395e6242c6813b196ca31ed0547ea79446af0e09063bd4a7874aef8a997cec746573742a00000000000000").to_vec(),
116 "Unexpected storage key: {}",
117 hex::encode(&storage_key),
118 );
119 }
120
121 #[test]
122 fn outbound_lane_data_key_computed_properly() {
123 let storage_key =
126 outbound_lane_data_key("BridgeMessages", &HashedLaneId::try_new(1, 2).unwrap()).0;
127 assert_eq!(
128 storage_key,
129 hex!("dd16c784ebd3390a9bc0357c7511ed0196c246acb9b55077390e3ca723a0ca1fd3bef8b00df8ca7b01813b5e2741950db1d3dccd8b3c3a012afe265f3e3c4432129b8aee50c9dcf87f9793be208e5ea0").to_vec(),
130 "Unexpected storage key: {}",
131 hex::encode(&storage_key),
132 );
133
134 let storage_key = outbound_lane_data_key("BridgeMessages", &LegacyLaneId(*b"test")).0;
136 assert_eq!(
137 storage_key,
138 hex!("dd16c784ebd3390a9bc0357c7511ed0196c246acb9b55077390e3ca723a0ca1f44a8995dd50b6657a037a7839304535b74657374").to_vec(),
139 "Unexpected storage key: {}",
140 hex::encode(&storage_key),
141 );
142 }
143
144 #[test]
145 fn inbound_lane_data_key_computed_properly() {
146 let storage_key =
149 inbound_lane_data_key("BridgeMessages", &HashedLaneId::try_new(1, 2).unwrap()).0;
150 assert_eq!(
151 storage_key,
152 hex!("dd16c784ebd3390a9bc0357c7511ed01e5f83cf83f2127eb47afdc35d6e43fabd3bef8b00df8ca7b01813b5e2741950db1d3dccd8b3c3a012afe265f3e3c4432129b8aee50c9dcf87f9793be208e5ea0").to_vec(),
153 "Unexpected storage key: {}",
154 hex::encode(&storage_key),
155 );
156
157 let storage_key = inbound_lane_data_key("BridgeMessages", &LegacyLaneId(*b"test")).0;
159 assert_eq!(
160 storage_key,
161 hex!("dd16c784ebd3390a9bc0357c7511ed01e5f83cf83f2127eb47afdc35d6e43fab44a8995dd50b6657a037a7839304535b74657374").to_vec(),
162 "Unexpected storage key: {}",
163 hex::encode(&storage_key),
164 );
165 }
166}