Skip to content

Commit

Permalink
Merge branch 'dev' into p-1295-add-native_submitplainrequest-rpc-method
Browse files Browse the repository at this point in the history
  • Loading branch information
silva-fj authored Feb 3, 2025
2 parents 3369c78 + f7a577d commit de7479e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 46 deletions.
1 change: 1 addition & 0 deletions tee-worker/omni-executor/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tee-worker/omni-executor/executor-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
16 changes: 6 additions & 10 deletions tee-worker/omni-executor/executor-storage/src/account_store.rs
Original file line number Diff line number Diff line change
@@ -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<DB>,
Expand All @@ -15,15 +15,11 @@ impl AccountStoreStorage {
pub fn new(db: Arc<DB>) -> Self {
Self { db }
}

fn storage_key(account_id: &AccountId) -> Vec<u8> {
(STORAGE_NAME, account_id).encode()
}
}

impl Storage<AccountId, AccountStore> for AccountStoreStorage {
fn get(&self, account_id: &AccountId) -> Option<AccountStore> {
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);
Expand All @@ -39,19 +35,19 @@ impl Storage<AccountId, AccountStore> 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()))
}
}
18 changes: 16 additions & 2 deletions tee-worker/omni-executor/executor-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,14 @@ pub trait Storage<K, V> {
fn contains_key(&self, key: &K) -> bool;
}

fn storage_key(storage_name: &str, key: &[u8]) -> Vec<u8> {
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<Arc<StorageDB>, ()> {
let db = Arc::new(StorageDB::open_default(STORAGE_DB_PATH).map_err(|e| {
log::error!("Could not open db: {:?}", e);
Expand All @@ -44,19 +53,24 @@ pub async fn init_storage(ws_rpc_endpoint: &str) -> Result<Arc<StorageDB>, ()> {
Ok(db)
}

const ACCOUNT_STORE_KEYS_PAGE_SIZE: u32 = 300;

async fn init_omni_account_storages(
client: &mut SubxtClient<CustomConfig>,
storage_db: Arc<StorageDB>,
) -> Result<(), ()> {
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<Vec<u8>> = 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DB>,
Expand All @@ -14,15 +14,11 @@ impl MemberOmniAccountStorage {
pub fn new(db: Arc<DB>) -> Self {
Self { db }
}

fn storage_key(member_identity: &Hash) -> Vec<u8> {
(STORAGE_NAME, member_identity).encode()
}
}

impl Storage<Hash, AccountId> for MemberOmniAccountStorage {
fn get(&self, member_identity: &Hash) -> Option<AccountId> {
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");
Expand All @@ -33,19 +29,21 @@ impl Storage<Hash, AccountId> 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()))
}
}
Original file line number Diff line number Diff line change
@@ -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<DB>,
Expand All @@ -14,15 +14,11 @@ impl OAuth2StateVerifierStorage {
pub fn new(db: Arc<DB>) -> Self {
Self { db }
}

fn storage_key(identity_hash: &Hash) -> Vec<u8> {
(STORAGE_NAME, identity_hash).encode()
}
}

impl Storage<Hash, String> for OAuth2StateVerifierStorage {
fn get(&self, identity_hash: &Hash) -> Option<String> {
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");
Expand All @@ -33,19 +29,19 @@ impl Storage<Hash, String> 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()))
}
}
22 changes: 10 additions & 12 deletions tee-worker/omni-executor/executor-storage/src/verification_code.rs
Original file line number Diff line number Diff line change
@@ -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<DB>,
Expand All @@ -14,15 +14,11 @@ impl VerificationCodeStorage {
pub fn new(db: Arc<DB>) -> Self {
Self { db }
}

fn storage_key(identity_hash: &Hash) -> Vec<u8> {
(STORAGE_NAME, identity_hash).encode()
}
}

impl Storage<Hash, String> for VerificationCodeStorage {
fn get(&self, identity_hash: &Hash) -> Option<String> {
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");
Expand All @@ -32,18 +28,20 @@ impl Storage<Hash, String> 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()))
}
}

0 comments on commit de7479e

Please sign in to comment.