referrerpolicy=no-referrer-when-downgrade

bp_messages/
storage_keys.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Parity Bridges Common.
3
4// Parity Bridges Common 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// Parity Bridges Common 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 Parity Bridges Common.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Storage keys of bridge messages pallet.
18
19/// Name of the `OPERATING_MODE_VALUE_NAME` storage value.
20pub const OPERATING_MODE_VALUE_NAME: &str = "PalletOperatingMode";
21/// Name of the `OutboundMessages` storage map.
22pub const OUTBOUND_MESSAGES_MAP_NAME: &str = "OutboundMessages";
23/// Name of the `OutboundLanes` storage map.
24pub const OUTBOUND_LANES_MAP_NAME: &str = "OutboundLanes";
25/// Name of the `InboundLanes` storage map.
26pub 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
34/// Storage key of the `PalletOperatingMode` value in the runtime storage.
35pub 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
45/// Storage key of the outbound message in the runtime storage.
46pub 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
58/// Storage key of the outbound message lane state in the runtime storage.
59pub 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
67/// Storage key of the inbound message lane state in the runtime storage.
68pub 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		// If this test fails, then something has been changed in module storage that is possibly
88		// breaking all existing message relays.
89		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		// If this test fails, then something has been changed in module storage that is breaking
101		// all previously crafted messages proofs.
102		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		// check backwards compatibility
112		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		// If this test fails, then something has been changed in module storage that is breaking
124		// all previously crafted outbound lane state proofs.
125		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		// check backwards compatibility
135		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		// If this test fails, then something has been changed in module storage that is breaking
147		// all previously crafted inbound lane state proofs.
148		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		// check backwards compatibility
158		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}