move storage keys computation to primitivs (#1254)

This commit is contained in:
Svyatoslav Nikolsky
2021-12-17 13:29:51 +03:00
committed by Bastian Köcher
parent 3aff81a707
commit 22b1e456ab
16 changed files with 240 additions and 123 deletions
+11
View File
@@ -10,18 +10,29 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0"
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
# Bridge Dependencies
bp-runtime = { path = "../runtime", default-features = false }
# Substrate Dependencies
frame-support = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
[dev-dependencies]
hex = "0.4"
hex-literal = "0.3"
[features]
default = ["std"]
std = [
"bp-runtime/std",
"codec/std",
"frame-support/std",
"scale-info/std",
"sp-core/std",
"sp-io/std",
"sp-std/std",
]
+16 -1
View File
@@ -16,10 +16,13 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub mod storage_keys;
use codec::{Decode, Encode};
use frame_support::{weights::Weight, RuntimeDebug};
use scale_info::TypeInfo;
use sp_core::U256;
use sp_core::{H256, U256};
use sp_io::hashing::blake2_256;
use sp_std::vec::Vec;
/// Pending token swap state.
@@ -85,6 +88,18 @@ pub struct TokenSwap<ThisBlockNumber, ThisBalance, ThisAccountId, BridgedBalance
pub target_account_at_bridged_chain: BridgedAccountId,
}
impl<ThisBlockNumber, ThisBalance, ThisAccountId, BridgedBalance, BridgedAccountId>
TokenSwap<ThisBlockNumber, ThisBalance, ThisAccountId, BridgedBalance, BridgedAccountId>
where
TokenSwap<ThisBlockNumber, ThisBalance, ThisAccountId, BridgedBalance, BridgedAccountId>:
Encode,
{
/// Returns hash, used to identify this token swap.
pub fn hash(&self) -> H256 {
self.using_encoded(blake2_256).into()
}
}
/// SCALE-encoded `Currency::transfer` call on the bridged chain.
pub type RawBridgedTransferCall = Vec<u8>;
@@ -0,0 +1,51 @@
// Copyright 2019-2021 Parity Technologies (UK) Ltd.
// This file is part of Parity Bridges Common.
// Parity Bridges Common is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity Bridges Common is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity Bridges Common. If not, see <http://www.gnu.org/licenses/>.
//! Storage keys of bridge token swap pallet.
use frame_support::Identity;
use sp_core::{storage::StorageKey, H256};
/// Name of the `PendingSwaps` storage map.
pub const PENDING_SWAPS_MAP_NAME: &str = "PendingSwaps";
/// Storage key of `PendingSwaps` value with given token swap hash.
pub fn pending_swaps_key(pallet_prefix: &str, token_swap_hash: H256) -> StorageKey {
bp_runtime::storage_map_final_key::<Identity>(
pallet_prefix,
PENDING_SWAPS_MAP_NAME,
token_swap_hash.as_ref(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
#[test]
fn pending_swaps_key_computed_properly() {
// If this test fails, then something has been changed in module storage that may break
// all previous swaps.
let storage_key = pending_swaps_key("BridgeTokenSwap", [42u8; 32].into()).0;
assert_eq!(
storage_key,
hex!("76276da64e7a4f454760eedeb4bad11adca2227fef56ad07cc424f1f5d128b9a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a").to_vec(),
"Unexpected storage key: {}",
hex::encode(&storage_key),
);
}
}