diff --git a/tee-worker/omni-executor/Cargo.lock b/tee-worker/omni-executor/Cargo.lock index cae46ed0b3..73ced44979 100644 --- a/tee-worker/omni-executor/Cargo.lock +++ b/tee-worker/omni-executor/Cargo.lock @@ -2911,6 +2911,7 @@ name = "executor-storage" version = "0.1.0" dependencies = [ "env_logger", + "executor-crypto", "executor-primitives", "frame-support 39.0.0", "log", diff --git a/tee-worker/omni-executor/executor-storage/Cargo.toml b/tee-worker/omni-executor/executor-storage/Cargo.toml index a60cdfbf60..439adee0f9 100644 --- a/tee-worker/omni-executor/executor-storage/Cargo.toml +++ b/tee-worker/omni-executor/executor-storage/Cargo.toml @@ -13,6 +13,7 @@ sp-state-machine = { workspace = true, features = ["std"] } subxt-core = { workspace = true } # Local dependencies +executor-crypto = { workspace = true } executor-primitives = { workspace = true } parentchain-api-interface = { workspace = true } parentchain-rpc-client = { workspace = true } diff --git a/tee-worker/omni-executor/executor-storage/src/account_store.rs b/tee-worker/omni-executor/executor-storage/src/account_store.rs index c12c42986b..762b54777b 100644 --- a/tee-worker/omni-executor/executor-storage/src/account_store.rs +++ b/tee-worker/omni-executor/executor-storage/src/account_store.rs @@ -1,11 +1,11 @@ -use crate::Storage; +use crate::{storage_key, Storage}; use executor_primitives::AccountId; use parentchain_api_interface::omni_account::storage::types::account_store::AccountStore; use parity_scale_codec::{Decode, Encode}; use rocksdb::DB; use std::sync::Arc; -const STORAGE_NAME: &[u8; 21] = b"account_store_storage"; +const STORAGE_NAME: &str = "account_store_storage"; pub struct AccountStoreStorage { db: Arc, @@ -15,15 +15,11 @@ impl AccountStoreStorage { pub fn new(db: Arc) -> Self { Self { db } } - - fn storage_key(account_id: &AccountId) -> Vec { - (STORAGE_NAME, account_id).encode() - } } impl Storage for AccountStoreStorage { fn get(&self, account_id: &AccountId) -> Option { - match self.db.get(Self::storage_key(account_id)) { + match self.db.get(storage_key(STORAGE_NAME, &account_id.encode())) { Ok(Some(value)) => AccountStore::decode(&mut &value[..]) .map_err(|e| { log::error!("Error decoding value from storage: {:?}", e); @@ -39,19 +35,19 @@ impl Storage for AccountStoreStorage { fn insert(&self, account_id: AccountId, account_store: AccountStore) -> Result<(), ()> { self.db - .put(Self::storage_key(&account_id), account_store.encode()) + .put(storage_key(STORAGE_NAME, &account_id.encode()), account_store.encode()) .map_err(|e| { log::error!("Error inserting value into storage: {:?}", e); }) } fn remove(&self, account_id: &AccountId) -> Result<(), ()> { - self.db.delete(Self::storage_key(account_id)).map_err(|e| { + self.db.delete(storage_key(STORAGE_NAME, &account_id.encode())).map_err(|e| { log::error!("Error removing value from storage: {:?}", e); }) } fn contains_key(&self, account_id: &AccountId) -> bool { - self.db.key_may_exist(Self::storage_key(account_id)) + self.db.key_may_exist(storage_key(STORAGE_NAME, &account_id.encode())) } } diff --git a/tee-worker/omni-executor/executor-storage/src/lib.rs b/tee-worker/omni-executor/executor-storage/src/lib.rs index a7e09b942c..647c76b2a6 100644 --- a/tee-worker/omni-executor/executor-storage/src/lib.rs +++ b/tee-worker/omni-executor/executor-storage/src/lib.rs @@ -7,6 +7,7 @@ pub use account_store::AccountStoreStorage; mod oauth2_state_verifier; pub use oauth2_state_verifier::OAuth2StateVerifierStorage; +use executor_crypto::hashing::{blake2_128, twox_128}; use executor_primitives::{AccountId, MemberAccount, TryFromSubxtType}; use frame_support::sp_runtime::traits::BlakeTwo256; use frame_support::storage::storage_prefix; @@ -30,6 +31,14 @@ pub trait Storage { fn contains_key(&self, key: &K) -> bool; } +fn storage_key(storage_name: &str, key: &[u8]) -> Vec { + twox_128(storage_name.as_bytes()) + .iter() + .chain(blake2_128(key).iter().chain(key.iter())) // blake2_128_concat + .cloned() + .collect() +} + pub async fn init_storage(ws_rpc_endpoint: &str) -> Result, ()> { let db = Arc::new(StorageDB::open_default(STORAGE_DB_PATH).map_err(|e| { log::error!("Could not open db: {:?}", e); @@ -44,6 +53,8 @@ pub async fn init_storage(ws_rpc_endpoint: &str) -> Result, ()> { Ok(db) } +const ACCOUNT_STORE_KEYS_PAGE_SIZE: u32 = 300; + async fn init_omni_account_storages( client: &mut SubxtClient, storage_db: Arc, @@ -51,12 +62,15 @@ async fn init_omni_account_storages( let account_store_storage = AccountStoreStorage::new(storage_db.clone()); let member_omni_account_storage = MemberOmniAccountStorage::new(storage_db.clone()); let account_store_key_prefix = storage_prefix(b"OmniAccount", b"AccountStore"); - let page_size = 300; let mut start_key: Option> = None; loop { let storage_keys_paged = client - .get_storage_keys_paged(account_store_key_prefix.into(), page_size, start_key.clone()) + .get_storage_keys_paged( + account_store_key_prefix.into(), + ACCOUNT_STORE_KEYS_PAGE_SIZE, + start_key.clone(), + ) .await .map_err(|e| { log::error!("Could not get storage keys paged: {:?}", e); diff --git a/tee-worker/omni-executor/executor-storage/src/member_omni_account.rs b/tee-worker/omni-executor/executor-storage/src/member_omni_account.rs index bfc4164c04..149c1864f6 100644 --- a/tee-worker/omni-executor/executor-storage/src/member_omni_account.rs +++ b/tee-worker/omni-executor/executor-storage/src/member_omni_account.rs @@ -1,10 +1,10 @@ -use crate::Storage; +use crate::{storage_key, Storage}; use executor_primitives::{AccountId, Hash}; use parity_scale_codec::{Decode, Encode}; use rocksdb::DB; use std::sync::Arc; -const STORAGE_NAME: &[u8; 19] = b"member_omni_account"; +const STORAGE_NAME: &str = "member_omni_account"; pub struct MemberOmniAccountStorage { db: Arc, @@ -14,15 +14,11 @@ impl MemberOmniAccountStorage { pub fn new(db: Arc) -> Self { Self { db } } - - fn storage_key(member_identity: &Hash) -> Vec { - (STORAGE_NAME, member_identity).encode() - } } impl Storage for MemberOmniAccountStorage { fn get(&self, member_identity: &Hash) -> Option { - match self.db.get(Self::storage_key(member_identity)) { + match self.db.get(storage_key(STORAGE_NAME, &member_identity.encode())) { Ok(Some(value)) => AccountId::decode(&mut &value[..]).ok(), _ => { log::error!("Error getting member_account_hash from storage"); @@ -33,19 +29,21 @@ impl Storage for MemberOmniAccountStorage { fn insert(&self, member_identity: Hash, omni_account: AccountId) -> Result<(), ()> { self.db - .put(Self::storage_key(&member_identity), omni_account.encode()) + .put(storage_key(STORAGE_NAME, &member_identity.encode()), omni_account.encode()) .map_err(|e| { log::error!("Error inserting member_account_hash into storage: {:?}", e); }) } fn remove(&self, member_identity: &Hash) -> Result<(), ()> { - self.db.delete(Self::storage_key(member_identity)).map_err(|e| { - log::error!("Error removing member_account_hash from storage: {:?}", e); - }) + self.db + .delete(storage_key(STORAGE_NAME, &member_identity.encode())) + .map_err(|e| { + log::error!("Error removing member_account_hash from storage: {:?}", e); + }) } fn contains_key(&self, member_identity: &Hash) -> bool { - self.db.key_may_exist(Self::storage_key(member_identity)) + self.db.key_may_exist(storage_key(STORAGE_NAME, &member_identity.encode())) } } diff --git a/tee-worker/omni-executor/executor-storage/src/oauth2_state_verifier.rs b/tee-worker/omni-executor/executor-storage/src/oauth2_state_verifier.rs index ecabc80b14..de943d6c7f 100644 --- a/tee-worker/omni-executor/executor-storage/src/oauth2_state_verifier.rs +++ b/tee-worker/omni-executor/executor-storage/src/oauth2_state_verifier.rs @@ -1,10 +1,10 @@ -use crate::Storage; +use crate::{storage_key, Storage}; use executor_primitives::Hash; use parity_scale_codec::{Decode, Encode}; use rocksdb::DB; use std::sync::Arc; -const STORAGE_NAME: &[u8; 20] = b"oauth2_state_storage"; +const STORAGE_NAME: &str = "oauth2_state_storage"; pub struct OAuth2StateVerifierStorage { db: Arc, @@ -14,15 +14,11 @@ impl OAuth2StateVerifierStorage { pub fn new(db: Arc) -> Self { Self { db } } - - fn storage_key(identity_hash: &Hash) -> Vec { - (STORAGE_NAME, identity_hash).encode() - } } impl Storage for OAuth2StateVerifierStorage { fn get(&self, identity_hash: &Hash) -> Option { - match self.db.get(Self::storage_key(identity_hash)) { + match self.db.get(storage_key(STORAGE_NAME, &identity_hash.encode())) { Ok(Some(value)) => String::decode(&mut &value[..]).ok(), _ => { log::error!("Error getting oauth2_state from storage"); @@ -33,19 +29,19 @@ impl Storage for OAuth2StateVerifierStorage { fn insert(&self, identity_hash: Hash, state_verifier: String) -> Result<(), ()> { self.db - .put(Self::storage_key(&identity_hash), state_verifier.encode()) + .put(storage_key(STORAGE_NAME, &identity_hash.encode()), state_verifier.encode()) .map_err(|e| { log::error!("Error inserting oauth2_state into storage: {:?}", e); }) } fn remove(&self, identity_hash: &Hash) -> Result<(), ()> { - self.db.delete(Self::storage_key(identity_hash)).map_err(|e| { + self.db.delete(storage_key(STORAGE_NAME, &identity_hash.encode())).map_err(|e| { log::error!("Error removing oauth2_state from storage: {:?}", e); }) } fn contains_key(&self, identity_hash: &Hash) -> bool { - self.db.key_may_exist(Self::storage_key(identity_hash)) + self.db.key_may_exist(storage_key(STORAGE_NAME, &identity_hash.encode())) } } diff --git a/tee-worker/omni-executor/executor-storage/src/verification_code.rs b/tee-worker/omni-executor/executor-storage/src/verification_code.rs index 586fa00377..22b51a463f 100644 --- a/tee-worker/omni-executor/executor-storage/src/verification_code.rs +++ b/tee-worker/omni-executor/executor-storage/src/verification_code.rs @@ -1,10 +1,10 @@ -use crate::Storage; +use crate::{storage_key, Storage}; use executor_primitives::Hash; use parity_scale_codec::{Decode, Encode}; use rocksdb::DB; use std::sync::Arc; -const STORAGE_NAME: &[u8; 25] = b"verification_code_storage"; +const STORAGE_NAME: &str = "verification_code_storage"; pub struct VerificationCodeStorage { db: Arc, @@ -14,15 +14,11 @@ impl VerificationCodeStorage { pub fn new(db: Arc) -> Self { Self { db } } - - fn storage_key(identity_hash: &Hash) -> Vec { - (STORAGE_NAME, identity_hash).encode() - } } impl Storage for VerificationCodeStorage { fn get(&self, identity_hash: &Hash) -> Option { - match self.db.get(Self::storage_key(identity_hash)) { + match self.db.get(storage_key(STORAGE_NAME, &identity_hash.encode())) { Ok(Some(value)) => String::decode(&mut &value[..]).ok(), _ => { log::error!("Error getting verification_code from storage"); @@ -32,18 +28,20 @@ impl Storage for VerificationCodeStorage { } fn insert(&self, identity_hash: Hash, code: String) -> Result<(), ()> { - self.db.put(Self::storage_key(&identity_hash), code.encode()).map_err(|e| { - log::error!("Error inserting verification_code into storage: {:?}", e); - }) + self.db + .put(storage_key(STORAGE_NAME, &identity_hash.encode()), code.encode()) + .map_err(|e| { + log::error!("Error inserting verification_code into storage: {:?}", e); + }) } fn remove(&self, identity_hash: &Hash) -> Result<(), ()> { - self.db.delete(Self::storage_key(identity_hash)).map_err(|e| { + self.db.delete(storage_key(STORAGE_NAME, &identity_hash.encode())).map_err(|e| { log::error!("Error removing verification_code from storage: {:?}", e); }) } fn contains_key(&self, identity_hash: &Hash) -> bool { - self.db.key_may_exist(Self::storage_key(identity_hash)) + self.db.key_may_exist(storage_key(STORAGE_NAME, &identity_hash.encode())) } }