diff --git a/account/src/account_schemadb/accepted_token.rs b/account/src/account_schemadb/accepted_token.rs index 5b9b2c3242..9ffc5f2249 100644 --- a/account/src/account_schemadb/accepted_token.rs +++ b/account/src/account_schemadb/accepted_token.rs @@ -1,4 +1,3 @@ -use super::AccountAddressWrapper; use anyhow::Result; use bcs_ext::BCSCodec; use serde::{Deserialize, Serialize}; @@ -7,24 +6,25 @@ use starcoin_schemadb::{ schema::{KeyCodec, ValueCodec}, ColumnFamilyName, }; +use starcoin_types::account_address::AccountAddress; use starcoin_types::account_config::token_code::TokenCode; pub const ACCEPTED_TOKEN_PREFIX_NAME: ColumnFamilyName = "accepted_token"; define_schema!( AcceptedToken, - AccountAddressWrapper, + AccountAddress, AcceptedTokens, ACCEPTED_TOKEN_PREFIX_NAME ); -impl KeyCodec for AccountAddressWrapper { +impl KeyCodec for AccountAddress { fn encode_key(&self) -> Result> { - Ok(self.0.to_vec()) + Ok(self.to_vec()) } fn decode_key(data: &[u8]) -> Result { - AccountAddressWrapper::try_from(data) + AccountAddress::try_from(data).map_err(Into::into) } } diff --git a/account/src/account_schemadb/global_setting.rs b/account/src/account_schemadb/global_setting.rs index b4f8aade96..04b8d8c813 100644 --- a/account/src/account_schemadb/global_setting.rs +++ b/account/src/account_schemadb/global_setting.rs @@ -17,9 +17,8 @@ define_schema!( GLOBAL_PREFIX_NAME ); -#[derive(Default, Hash, Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] pub enum GlobalSettingKey { - #[default] DefaultAddress, /// FIXME: once db support iter, remove this. AllAddresses, diff --git a/account/src/account_schemadb/mod.rs b/account/src/account_schemadb/mod.rs index f5fc5cd9f3..a48b0006bb 100644 --- a/account/src/account_schemadb/mod.rs +++ b/account/src/account_schemadb/mod.rs @@ -1,7 +1,6 @@ use anyhow::Result; use starcoin_schemadb::schema::Schema; use starcoin_storage::cache_storage::GCacheStorage; -use starcoin_types::account_address::AccountAddress; use std::sync::Arc; mod accepted_token; @@ -17,29 +16,6 @@ pub(crate) use public_key::*; pub(crate) use setting::*; use starcoin_schemadb::{db::DBStorage as DB, SchemaBatch}; -#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)] -pub(crate) struct AccountAddressWrapper(AccountAddress); - -impl Default for AccountAddressWrapper { - fn default() -> Self { - Self(AccountAddress::ZERO) - } -} -impl From for AccountAddressWrapper { - fn from(value: AccountAddress) -> Self { - Self(value) - } -} -impl TryFrom<&[u8]> for AccountAddressWrapper { - type Error = anyhow::Error; - - fn try_from(value: &[u8]) -> Result { - AccountAddress::try_from(value) - .map(Self) - .map_err(Into::into) - } -} - #[derive(Clone)] pub(super) struct AccountStore { cache: Arc>, diff --git a/account/src/account_schemadb/private_key.rs b/account/src/account_schemadb/private_key.rs index 52fe73a852..95fdc2c3d2 100644 --- a/account/src/account_schemadb/private_key.rs +++ b/account/src/account_schemadb/private_key.rs @@ -1,27 +1,27 @@ -use super::AccountAddressWrapper; use anyhow::Result; use starcoin_schemadb::{ define_schema, schema::{KeyCodec, ValueCodec}, ColumnFamilyName, }; +use starcoin_types::account_address::AccountAddress; pub const ENCRYPTED_PRIVATE_KEY_PREFIX_NAME: ColumnFamilyName = "encrypted_private_key"; define_schema!( PrivateKey, - AccountAddressWrapper, + AccountAddress, EncryptedPrivateKey, ENCRYPTED_PRIVATE_KEY_PREFIX_NAME ); -impl KeyCodec for AccountAddressWrapper { +impl KeyCodec for AccountAddress { fn encode_key(&self) -> Result> { - Ok(self.0.to_vec()) + Ok(self.to_vec()) } fn decode_key(data: &[u8]) -> Result { - AccountAddressWrapper::try_from(data) + AccountAddress::try_from(data).map_err(Into::into) } } diff --git a/account/src/account_schemadb/public_key.rs b/account/src/account_schemadb/public_key.rs index 29ab9c71ea..e8ec0d1275 100644 --- a/account/src/account_schemadb/public_key.rs +++ b/account/src/account_schemadb/public_key.rs @@ -1,54 +1,38 @@ -use super::AccountAddressWrapper; -use anyhow::{format_err, Result}; +use anyhow::Result; +use bcs_ext::BCSCodec; use starcoin_account_api::AccountPublicKey; use starcoin_schemadb::{ define_schema, schema::{KeyCodec, ValueCodec}, ColumnFamilyName, }; +use starcoin_types::account_address::AccountAddress; pub const PUBLIC_KEY_PREFIX_NAME: ColumnFamilyName = "public_key"; define_schema!( PublicKey, - AccountAddressWrapper, - PublicKeyWrapper, + AccountAddress, + AccountPublicKey, PUBLIC_KEY_PREFIX_NAME ); -impl KeyCodec for AccountAddressWrapper { +impl KeyCodec for AccountAddress { fn encode_key(&self) -> Result> { - Ok(self.0.to_vec()) + Ok(self.to_vec()) } fn decode_key(data: &[u8]) -> Result { - AccountAddressWrapper::try_from(data) + AccountAddress::try_from(data).map_err(Into::into) } } -#[derive(Default, Debug, Clone, PartialEq, Eq)] -pub struct PublicKeyWrapper(pub(crate) Option); -impl From for PublicKeyWrapper { - fn from(s: AccountPublicKey) -> Self { - Self(Some(s)) - } -} - -impl From for AccountPublicKey { - fn from(value: PublicKeyWrapper) -> Self { - value.0.expect("NullValue") - } -} - -impl ValueCodec for PublicKeyWrapper { +impl ValueCodec for AccountPublicKey { fn encode_value(&self) -> Result> { - match &self.0 { - Some(p) => Ok(bcs_ext::to_bytes(&p)?), - None => Err(format_err!("NullValue")), - } + self.encode() } fn decode_value(data: &[u8]) -> Result { - Ok(Self::from(bcs_ext::from_bytes::(data)?)) + bcs_ext::from_bytes::(data) } } diff --git a/account/src/account_schemadb/setting.rs b/account/src/account_schemadb/setting.rs index 5efae05ec0..e5005974df 100644 --- a/account/src/account_schemadb/setting.rs +++ b/account/src/account_schemadb/setting.rs @@ -1,4 +1,3 @@ -use super::AccountAddressWrapper; use anyhow::Result; use starcoin_account_api::Setting; use starcoin_schemadb::{ @@ -6,42 +5,28 @@ use starcoin_schemadb::{ schema::{KeyCodec, ValueCodec}, ColumnFamilyName, }; +use starcoin_types::account_address::AccountAddress; pub const SETTING_PREFIX_NAME: ColumnFamilyName = "account_settings"; -define_schema!( - AccountSetting, - AccountAddressWrapper, - SettingWrapper, - SETTING_PREFIX_NAME -); +define_schema!(AccountSetting, AccountAddress, Setting, SETTING_PREFIX_NAME); -#[derive(Default, Debug, Clone, PartialEq, Eq)] -pub struct SettingWrapper(pub(crate) Setting); -impl From for SettingWrapper { - fn from(setting: Setting) -> Self { - Self(setting) - } -} - -impl KeyCodec for AccountAddressWrapper { +impl KeyCodec for AccountAddress { fn encode_key(&self) -> Result> { - Ok(self.0.to_vec()) + Ok(self.to_vec()) } fn decode_key(data: &[u8]) -> Result { - AccountAddressWrapper::try_from(data) + AccountAddress::try_from(data).map_err(Into::into) } } /// Setting use json encode/decode for support more setting field in the future. -impl ValueCodec for SettingWrapper { +impl ValueCodec for Setting { fn encode_value(&self) -> Result> { - serde_json::to_vec(&self.0).map_err(Into::into) + serde_json::to_vec(&self).map_err(Into::into) } fn decode_value(data: &[u8]) -> Result { - serde_json::from_slice::(data) - .map(Into::into) - .map_err(Into::into) + serde_json::from_slice::(data).map_err(Into::into) } } diff --git a/account/src/account_storage.rs b/account/src/account_storage.rs index 7b9d3a0e81..3fcc394078 100644 --- a/account/src/account_storage.rs +++ b/account/src/account_storage.rs @@ -2,10 +2,10 @@ // SPDX-License-Identifier: Apache-2.0 use crate::account_schemadb::{ - AcceptedToken, AcceptedTokens, AccountAddressWrapper, AccountSetting, AccountStore, - EncryptedPrivateKey, GlobalSetting, GlobalSettingKey, GlobalValue, PrivateKey, PublicKey, - SettingWrapper, ACCEPTED_TOKEN_PREFIX_NAME, ENCRYPTED_PRIVATE_KEY_PREFIX_NAME, - GLOBAL_PREFIX_NAME, PUBLIC_KEY_PREFIX_NAME, SETTING_PREFIX_NAME, + AcceptedToken, AcceptedTokens, AccountSetting, AccountStore, EncryptedPrivateKey, + GlobalSetting, GlobalSettingKey, GlobalValue, PrivateKey, PublicKey, + ACCEPTED_TOKEN_PREFIX_NAME, ENCRYPTED_PRIVATE_KEY_PREFIX_NAME, GLOBAL_PREFIX_NAME, + PUBLIC_KEY_PREFIX_NAME, SETTING_PREFIX_NAME, }; use anyhow::{Error, Result}; use starcoin_account_api::{AccountPrivateKey, AccountPublicKey, Setting}; @@ -89,7 +89,7 @@ impl AccountStorage { } pub fn contain_address(&self, address: AccountAddress) -> Result { - match self.get_public_key(&address.into())? { + match self.get_public_key(&address)? { Some(v) => { let _ = Into::::into(v); Ok(true) @@ -148,22 +148,19 @@ impl AccountStorage { Ok(value.map(|v| v.addresses).unwrap_or_default()) } - fn get_public_key(&self, address: &AccountAddressWrapper) -> Result> { - Ok(self.public_key_store.get(address)?.map(Into::into)) + fn get_public_key(&self, address: &AccountAddress) -> Result> { + self.public_key_store.get(address) } fn put_public_key(&self, key: AccountAddress, value: AccountPublicKey) -> Result<()> { - self.public_key_store.put(key.into(), value.into()) + self.public_key_store.put(key, value) } pub fn public_key(&self, address: AccountAddress) -> Result> { - self.get_public_key(&address.into()) + self.get_public_key(&address) } - fn get_private_key( - &self, - address: &AccountAddressWrapper, - ) -> Result> { + fn get_private_key(&self, address: &AccountAddress) -> Result> { self.private_key_store.get(address) } @@ -179,7 +176,7 @@ impl AccountStorage { address: AccountAddress, password: impl AsRef, ) -> Result> { - match self.get_private_key(&address.into())? { + match self.get_private_key(&address)? { None => Ok(None), Some(encrypted_key) => { let plain_key_data = decrypt(password.as_ref().as_bytes(), &encrypted_key.0)?; @@ -207,18 +204,16 @@ impl AccountStorage { let batch = SchemaBatch::default(); let encrypted_prikey = encrypt(password.as_ref().as_bytes(), &private_key.to_bytes()); self.private_key_store - .put_batch(address.into(), encrypted_prikey.into(), &batch)?; + .put_batch(address, encrypted_prikey.into(), &batch)?; let public_key = private_key.public_key(); self.public_key_store - .put_batch(address.into(), public_key.into(), &batch)?; + .put_batch(address, public_key, &batch)?; self.write_schemas(batch)?; Ok(()) } fn put_setting(&self, address: AccountAddress, setting: Setting) -> Result<()> { - let key: AccountAddressWrapper = address.into(); - let value: SettingWrapper = setting.into(); - self.setting_store.put(key, value) + self.setting_store.put(address, setting) } pub fn update_setting(&self, address: AccountAddress, setting: Setting) -> Result<()> { @@ -226,8 +221,7 @@ impl AccountStorage { } pub fn load_setting(&self, address: AccountAddress) -> Result { - let key: AccountAddressWrapper = address.into(); - Ok(self.setting_store.get(&key)?.unwrap_or_default().0) + Ok(self.setting_store.get(&address)?.unwrap_or_default()) } pub fn destroy_account(&self, address: AccountAddress) -> Result<()> { @@ -255,11 +249,10 @@ impl AccountStorage { } } - let key: AccountAddressWrapper = address.into(); - self.private_key_store.remove_batch(&key, &batch)?; - self.public_key_store.remove_batch(&key, &batch)?; - self.setting_store.remove_batch(&key, &batch)?; - self.accepted_token_store.remove_batch(&key, &batch)?; + self.private_key_store.remove_batch(&address, &batch)?; + self.public_key_store.remove_batch(&address, &batch)?; + self.setting_store.remove_batch(&address, &batch)?; + self.accepted_token_store.remove_batch(&address, &batch)?; // persist updates to underlying storage self.db @@ -270,12 +263,11 @@ impl AccountStorage { } pub fn get_accepted_tokens(&self, address: AccountAddress) -> Result> { - let key: AccountAddressWrapper = address.into(); - let ts = self.accepted_token_store.get(&key)?; + let ts = self.accepted_token_store.get(&address)?; Ok(ts.map(|t| t.0).unwrap_or_default()) } - fn put_accepted_tokens(&self, key: AccountAddressWrapper, value: AcceptedTokens) -> Result<()> { + fn put_accepted_tokens(&self, key: AccountAddress, value: AcceptedTokens) -> Result<()> { self.accepted_token_store.put(key, value) } @@ -287,7 +279,7 @@ impl AccountStorage { let mut tokens = self.get_accepted_tokens(address)?; if !tokens.contains(&token_code) { tokens.push(token_code); - self.put_accepted_tokens(address.into(), AcceptedTokens(tokens))?; + self.put_accepted_tokens(address, AcceptedTokens(tokens))?; } Ok(()) } diff --git a/storage/schemadb/src/schema.rs b/storage/schemadb/src/schema.rs index 2d63f2fc4e..d49cb76a6a 100644 --- a/storage/schemadb/src/schema.rs +++ b/storage/schemadb/src/schema.rs @@ -37,8 +37,8 @@ where pub trait Schema: Debug + Send + Sync + 'static { const COLUMN_FAMILY: &'static str; - type Key: KeyCodec + Hash + Eq + Default; - type Value: ValueCodec + Default + Clone; + type Key: KeyCodec + Hash + Eq; + type Value: ValueCodec; } #[macro_export] diff --git a/storage/src/cache_storage/mod.rs b/storage/src/cache_storage/mod.rs index dd4d361794..5d2722bbc4 100644 --- a/storage/src/cache_storage/mod.rs +++ b/storage/src/cache_storage/mod.rs @@ -15,12 +15,12 @@ use starcoin_config::DEFAULT_CACHE_SIZE; pub type CacheStorage = GCacheStorage, Vec>; -pub struct GCacheStorage { +pub struct GCacheStorage { cache: Mutex>, metrics: Option, } -impl GCacheStorage { +impl GCacheStorage { pub fn new(metrics: Option) -> Self { GCacheStorage { cache: Mutex::new(LruCache::::new(DEFAULT_CACHE_SIZE)), @@ -38,7 +38,7 @@ impl GCacheStorage { } } -impl Default for GCacheStorage { +impl Default for GCacheStorage { fn default() -> Self { Self::new(None) } @@ -136,7 +136,7 @@ fn compose_key(prefix_name: Option<&str>, source_key: Vec) -> Vec { } } -impl GCacheStorage { +impl GCacheStorage { pub fn get_inner(&self, key: &K) -> Option { self.cache.lock().get(key).cloned() }