Skip to content

Commit 22b1e45

Browse files
svyatonikbkchr
authored andcommitted
move storage keys computation to primitivs (#1254)
1 parent 3aff81a commit 22b1e45

File tree

16 files changed

+240
-123
lines changed

16 files changed

+240
-123
lines changed

bridges/bin/rialto/runtime/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,14 +1030,13 @@ impl_runtime_apis! {
10301030
params: MessageProofParams,
10311031
) -> (millau_messages::FromMillauMessagesProof, Weight) {
10321032
use crate::millau_messages::WithMillauMessageBridge;
1033-
use bp_messages::MessageKey;
1033+
use bp_messages::{MessageKey, storage_keys};
10341034
use bridge_runtime_common::{
10351035
messages::MessageBridge,
10361036
messages_benchmarking::{ed25519_sign, prepare_message_proof},
10371037
};
10381038
use codec::Encode;
10391039
use frame_support::weights::GetDispatchInfo;
1040-
use pallet_bridge_messages::storage_keys;
10411040
use sp_runtime::traits::{Header, IdentifyAccount};
10421041

10431042
let remark = match params.size {
@@ -1115,7 +1114,7 @@ impl_runtime_apis! {
11151114

11161115
prepare_message_delivery_proof::<WithMillauMessageBridge, bp_millau::Hasher, Runtime, (), _, _>(
11171116
params,
1118-
|lane_id| pallet_bridge_messages::storage_keys::inbound_lane_data_key(
1117+
|lane_id| bp_messages::storage_keys::inbound_lane_data_key(
11191118
<WithMillauMessageBridge as MessageBridge>::BRIDGED_MESSAGES_PALLET_NAME,
11201119
&lane_id,
11211120
).0,

bridges/bin/runtime-common/src/messages.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ pub mod source {
428428
// Messages delivery proof is just proof of single storage key read => any error
429429
// is fatal.
430430
let storage_inbound_lane_data_key =
431-
pallet_bridge_messages::storage_keys::inbound_lane_data_key(B::BRIDGED_MESSAGES_PALLET_NAME, &lane);
431+
bp_messages::storage_keys::inbound_lane_data_key(B::BRIDGED_MESSAGES_PALLET_NAME, &lane);
432432
let raw_inbound_lane_data = storage
433433
.read_value(storage_inbound_lane_data_key.0.as_ref())
434434
.map_err(|_| "Failed to read inbound lane state from storage proof")?
@@ -674,16 +674,15 @@ pub mod target {
674674
B: MessageBridge,
675675
{
676676
fn read_raw_outbound_lane_data(&self, lane_id: &LaneId) -> Option<Vec<u8>> {
677-
let storage_outbound_lane_data_key =
678-
pallet_bridge_messages::storage_keys::outbound_lane_data_key(
679-
B::BRIDGED_MESSAGES_PALLET_NAME,
680-
lane_id,
681-
);
677+
let storage_outbound_lane_data_key = bp_messages::storage_keys::outbound_lane_data_key(
678+
B::BRIDGED_MESSAGES_PALLET_NAME,
679+
lane_id,
680+
);
682681
self.storage.read_value(storage_outbound_lane_data_key.0.as_ref()).ok()?
683682
}
684683

685684
fn read_raw_message(&self, message_key: &MessageKey) -> Option<Vec<u8>> {
686-
let storage_message_key = pallet_bridge_messages::storage_keys::message_key(
685+
let storage_message_key = bp_messages::storage_keys::message_key(
687686
B::BRIDGED_MESSAGES_PALLET_NAME,
688687
&message_key.lane_id,
689688
message_key.nonce,

bridges/modules/messages/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "master
3030
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
3131

3232
[dev-dependencies]
33-
hex = "0.4"
34-
hex-literal = "0.3"
3533
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
3634
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
3735

bridges/modules/messages/src/lib.rs

Lines changed: 22 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -801,32 +801,6 @@ pub mod pallet {
801801
}
802802
}
803803

804-
/// Getting storage keys for messages and lanes states. These keys are normally used when building
805-
/// messages and lanes states proofs.
806-
pub mod storage_keys {
807-
use super::*;
808-
use sp_core::storage::StorageKey;
809-
810-
/// Storage key of the outbound message in the runtime storage.
811-
pub fn message_key(pallet_prefix: &str, lane: &LaneId, nonce: MessageNonce) -> StorageKey {
812-
bp_runtime::storage_map_final_key_blake2_128concat(
813-
pallet_prefix,
814-
"OutboundMessages",
815-
&MessageKey { lane_id: *lane, nonce }.encode(),
816-
)
817-
}
818-
819-
/// Storage key of the outbound message lane state in the runtime storage.
820-
pub fn outbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
821-
bp_runtime::storage_map_final_key_blake2_128concat(pallet_prefix, "OutboundLanes", lane)
822-
}
823-
824-
/// Storage key of the inbound message lane state in the runtime storage.
825-
pub fn inbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
826-
bp_runtime::storage_map_final_key_blake2_128concat(pallet_prefix, "InboundLanes", lane)
827-
}
828-
}
829-
830804
/// AccountId of the shared relayer fund account.
831805
///
832806
/// This account is passed to `MessageDeliveryAndDispatchPayment` trait, and depending
@@ -1159,9 +1133,8 @@ mod tests {
11591133
REGULAR_PAYLOAD, TEST_LANE_ID, TEST_RELAYER_A, TEST_RELAYER_B,
11601134
};
11611135
use bp_messages::{UnrewardedRelayer, UnrewardedRelayersState};
1162-
use frame_support::{assert_noop, assert_ok, weights::Weight};
1136+
use frame_support::{assert_noop, assert_ok, storage::generator::StorageMap, weights::Weight};
11631137
use frame_system::{EventRecord, Pallet as System, Phase};
1164-
use hex_literal::hex;
11651138
use sp_runtime::DispatchError;
11661139

11671140
fn get_ready_for_events() {
@@ -1889,45 +1862,6 @@ mod tests {
18891862
});
18901863
}
18911864

1892-
#[test]
1893-
fn storage_message_key_computed_properly() {
1894-
// If this test fails, then something has been changed in module storage that is breaking
1895-
// all previously crafted messages proofs.
1896-
let storage_key = storage_keys::message_key("BridgeMessages", &*b"test", 42).0;
1897-
assert_eq!(
1898-
storage_key,
1899-
hex!("dd16c784ebd3390a9bc0357c7511ed018a395e6242c6813b196ca31ed0547ea79446af0e09063bd4a7874aef8a997cec746573742a00000000000000").to_vec(),
1900-
"Unexpected storage key: {}",
1901-
hex::encode(&storage_key),
1902-
);
1903-
}
1904-
1905-
#[test]
1906-
fn outbound_lane_data_key_computed_properly() {
1907-
// If this test fails, then something has been changed in module storage that is breaking
1908-
// all previously crafted outbound lane state proofs.
1909-
let storage_key = storage_keys::outbound_lane_data_key("BridgeMessages", &*b"test").0;
1910-
assert_eq!(
1911-
storage_key,
1912-
hex!("dd16c784ebd3390a9bc0357c7511ed0196c246acb9b55077390e3ca723a0ca1f44a8995dd50b6657a037a7839304535b74657374").to_vec(),
1913-
"Unexpected storage key: {}",
1914-
hex::encode(&storage_key),
1915-
);
1916-
}
1917-
1918-
#[test]
1919-
fn inbound_lane_data_key_computed_properly() {
1920-
// If this test fails, then something has been changed in module storage that is breaking
1921-
// all previously crafted inbound lane state proofs.
1922-
let storage_key = storage_keys::inbound_lane_data_key("BridgeMessages", &*b"test").0;
1923-
assert_eq!(
1924-
storage_key,
1925-
hex!("dd16c784ebd3390a9bc0357c7511ed01e5f83cf83f2127eb47afdc35d6e43fab44a8995dd50b6657a037a7839304535b74657374").to_vec(),
1926-
"Unexpected storage key: {}",
1927-
hex::encode(&storage_key),
1928-
);
1929-
}
1930-
19311865
#[test]
19321866
fn actual_dispatch_weight_does_not_overlow() {
19331867
run_test(|| {
@@ -2359,4 +2293,25 @@ mod tests {
23592293
);
23602294
});
23612295
}
2296+
2297+
#[test]
2298+
fn storage_keys_computed_properly() {
2299+
assert_eq!(
2300+
OutboundMessages::<TestRuntime>::storage_map_final_key(MessageKey {
2301+
lane_id: TEST_LANE_ID,
2302+
nonce: 42
2303+
}),
2304+
bp_messages::storage_keys::message_key("Messages", &TEST_LANE_ID, 42).0,
2305+
);
2306+
2307+
assert_eq!(
2308+
OutboundLanes::<TestRuntime>::storage_map_final_key(TEST_LANE_ID),
2309+
bp_messages::storage_keys::outbound_lane_data_key("Messages", &TEST_LANE_ID).0,
2310+
);
2311+
2312+
assert_eq!(
2313+
InboundLanes::<TestRuntime>::storage_map_final_key(TEST_LANE_ID),
2314+
bp_messages::storage_keys::inbound_lane_data_key("Messages", &TEST_LANE_ID).0,
2315+
);
2316+
}
23622317
}

bridges/modules/token-swap/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,6 @@ pub mod weights_ext;
9595

9696
pub use pallet::*;
9797

98-
/// Name of the `PendingSwaps` storage map.
99-
pub const PENDING_SWAPS_MAP_NAME: &str = "PendingSwaps";
100-
10198
// comes from #[pallet::event]
10299
#[allow(clippy::unused_unit)]
103100
#[frame_support::pallet]
@@ -639,7 +636,7 @@ pub mod pallet {
639636
mod tests {
640637
use super::*;
641638
use crate::mock::*;
642-
use frame_support::{assert_noop, assert_ok};
639+
use frame_support::{assert_noop, assert_ok, storage::generator::StorageMap};
643640

644641
const CAN_START_BLOCK_NUMBER: u64 = 10;
645642
const CAN_CLAIM_BLOCK_NUMBER: u64 = CAN_START_BLOCK_NUMBER + 1;
@@ -1130,4 +1127,12 @@ mod tests {
11301127
);
11311128
});
11321129
}
1130+
1131+
#[test]
1132+
fn storage_keys_computed_properly() {
1133+
assert_eq!(
1134+
PendingSwaps::<TestRuntime>::storage_map_final_key(test_swap_hash()),
1135+
bp_token_swap::storage_keys::pending_swaps_key("TokenSwap", test_swap_hash()).0,
1136+
);
1137+
}
11331138
}

bridges/primitives/messages/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ bp-runtime = { path = "../runtime", default-features = false }
2121

2222
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2323
frame-system = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
24+
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2425
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
2526

27+
[dev-dependencies]
28+
hex = "0.4"
29+
hex-literal = "0.3"
30+
2631
[features]
2732
default = ["std"]
2833
std = [
@@ -32,5 +37,6 @@ std = [
3237
"frame-system/std",
3338
"scale-info/std",
3439
"serde",
40+
"sp-core/std",
3541
"sp-std/std"
3642
]

bridges/primitives/messages/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use scale_info::TypeInfo;
3030
use sp_std::{collections::vec_deque::VecDeque, prelude::*};
3131

3232
pub mod source_chain;
33+
pub mod storage_keys;
3334
pub mod target_chain;
3435

3536
// Weight is reexported to avoid additional frame-support dependencies in related crates.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Copyright 2019-2021 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 `OutboundMessages` storage map.
20+
pub const OUTBOUND_MESSAGES_MAP_NAME: &str = "OutboundMessages";
21+
/// Name of the `OutboundLanes` storage map.
22+
pub const OUTBOUND_LANES_MAP_NAME: &str = "OutboundLanes";
23+
/// Name of the `InboundLanes` storage map.
24+
pub const INBOUND_LANES_MAP_NAME: &str = "InboundLanes";
25+
26+
use crate::{LaneId, MessageKey, MessageNonce};
27+
28+
use codec::Encode;
29+
use frame_support::Blake2_128Concat;
30+
use sp_core::storage::StorageKey;
31+
32+
/// Storage key of the outbound message in the runtime storage.
33+
pub fn message_key(pallet_prefix: &str, lane: &LaneId, nonce: MessageNonce) -> StorageKey {
34+
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
35+
pallet_prefix,
36+
OUTBOUND_MESSAGES_MAP_NAME,
37+
&MessageKey { lane_id: *lane, nonce }.encode(),
38+
)
39+
}
40+
41+
/// Storage key of the outbound message lane state in the runtime storage.
42+
pub fn outbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
43+
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
44+
pallet_prefix,
45+
OUTBOUND_LANES_MAP_NAME,
46+
lane,
47+
)
48+
}
49+
50+
/// Storage key of the inbound message lane state in the runtime storage.
51+
pub fn inbound_lane_data_key(pallet_prefix: &str, lane: &LaneId) -> StorageKey {
52+
bp_runtime::storage_map_final_key::<Blake2_128Concat>(
53+
pallet_prefix,
54+
INBOUND_LANES_MAP_NAME,
55+
lane,
56+
)
57+
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
use hex_literal::hex;
63+
64+
#[test]
65+
fn storage_message_key_computed_properly() {
66+
// If this test fails, then something has been changed in module storage that is breaking
67+
// all previously crafted messages proofs.
68+
let storage_key = message_key("BridgeMessages", &*b"test", 42).0;
69+
assert_eq!(
70+
storage_key,
71+
hex!("dd16c784ebd3390a9bc0357c7511ed018a395e6242c6813b196ca31ed0547ea79446af0e09063bd4a7874aef8a997cec746573742a00000000000000").to_vec(),
72+
"Unexpected storage key: {}",
73+
hex::encode(&storage_key),
74+
);
75+
}
76+
77+
#[test]
78+
fn outbound_lane_data_key_computed_properly() {
79+
// If this test fails, then something has been changed in module storage that is breaking
80+
// all previously crafted outbound lane state proofs.
81+
let storage_key = outbound_lane_data_key("BridgeMessages", &*b"test").0;
82+
assert_eq!(
83+
storage_key,
84+
hex!("dd16c784ebd3390a9bc0357c7511ed0196c246acb9b55077390e3ca723a0ca1f44a8995dd50b6657a037a7839304535b74657374").to_vec(),
85+
"Unexpected storage key: {}",
86+
hex::encode(&storage_key),
87+
);
88+
}
89+
90+
#[test]
91+
fn inbound_lane_data_key_computed_properly() {
92+
// If this test fails, then something has been changed in module storage that is breaking
93+
// all previously crafted inbound lane state proofs.
94+
let storage_key = inbound_lane_data_key("BridgeMessages", &*b"test").0;
95+
assert_eq!(
96+
storage_key,
97+
hex!("dd16c784ebd3390a9bc0357c7511ed01e5f83cf83f2127eb47afdc35d6e43fab44a8995dd50b6657a037a7839304535b74657374").to_vec(),
98+
"Unexpected storage key: {}",
99+
hex::encode(&storage_key),
100+
);
101+
}
102+
}

bridges/primitives/runtime/src/lib.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -201,47 +201,22 @@ impl<BlockNumber: Copy + Into<u64>, BlockHash: Copy> TransactionEra<BlockNumber,
201201
}
202202

203203
/// This is a copy of the
204-
/// `frame_support::storage::generator::StorageMap::storage_map_final_key` for `Blake2_128Concat`
205-
/// maps.
204+
/// `frame_support::storage::generator::StorageMap::storage_map_final_key` for maps based
205+
/// on selected hasher.
206206
///
207207
/// We're using it because to call `storage_map_final_key` directly, we need access to the runtime
208208
/// and pallet instance, which (sometimes) is impossible.
209-
pub fn storage_map_final_key_blake2_128concat(
209+
pub fn storage_map_final_key<H: StorageHasher>(
210210
pallet_prefix: &str,
211211
map_name: &str,
212212
key: &[u8],
213213
) -> StorageKey {
214-
storage_map_final_key_identity(
215-
pallet_prefix,
216-
map_name,
217-
&frame_support::Blake2_128Concat::hash(key),
218-
)
219-
}
220-
221-
///
222-
pub fn storage_map_final_key_twox64_concat(
223-
pallet_prefix: &str,
224-
map_name: &str,
225-
key: &[u8],
226-
) -> StorageKey {
227-
storage_map_final_key_identity(pallet_prefix, map_name, &frame_support::Twox64Concat::hash(key))
228-
}
229-
230-
/// This is a copy of the
231-
/// `frame_support::storage::generator::StorageMap::storage_map_final_key` for `Identity` maps.
232-
///
233-
/// We're using it because to call `storage_map_final_key` directly, we need access to the runtime
234-
/// and pallet instance, which (sometimes) is impossible.
235-
pub fn storage_map_final_key_identity(
236-
pallet_prefix: &str,
237-
map_name: &str,
238-
key_hashed: &[u8],
239-
) -> StorageKey {
214+
let key_hashed = H::hash(key);
240215
let pallet_prefix_hashed = frame_support::Twox128::hash(pallet_prefix.as_bytes());
241216
let storage_prefix_hashed = frame_support::Twox128::hash(map_name.as_bytes());
242217

243218
let mut final_key = Vec::with_capacity(
244-
pallet_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.len(),
219+
pallet_prefix_hashed.len() + storage_prefix_hashed.len() + key_hashed.as_ref().len(),
245220
);
246221

247222
final_key.extend_from_slice(&pallet_prefix_hashed[..]);

0 commit comments

Comments
 (0)