-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax trait restrictions for schemadb
1. remove Default trait restriction for account storage
- Loading branch information
Showing
9 changed files
with
58 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PublicKey> for AccountAddressWrapper { | ||
impl KeyCodec<PublicKey> for AccountAddress { | ||
fn encode_key(&self) -> Result<Vec<u8>> { | ||
Ok(self.0.to_vec()) | ||
Ok(self.to_vec()) | ||
} | ||
|
||
fn decode_key(data: &[u8]) -> Result<Self> { | ||
AccountAddressWrapper::try_from(data) | ||
AccountAddress::try_from(data).map_err(Into::into) | ||
} | ||
} | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq)] | ||
pub struct PublicKeyWrapper(pub(crate) Option<AccountPublicKey>); | ||
impl From<AccountPublicKey> for PublicKeyWrapper { | ||
fn from(s: AccountPublicKey) -> Self { | ||
Self(Some(s)) | ||
} | ||
} | ||
|
||
impl From<PublicKeyWrapper> for AccountPublicKey { | ||
fn from(value: PublicKeyWrapper) -> Self { | ||
value.0.expect("NullValue") | ||
} | ||
} | ||
|
||
impl ValueCodec<PublicKey> for PublicKeyWrapper { | ||
impl ValueCodec<PublicKey> for AccountPublicKey { | ||
fn encode_value(&self) -> Result<Vec<u8>> { | ||
match &self.0 { | ||
Some(p) => Ok(bcs_ext::to_bytes(&p)?), | ||
None => Err(format_err!("NullValue")), | ||
} | ||
self.encode() | ||
} | ||
|
||
fn decode_value(data: &[u8]) -> Result<Self> { | ||
Ok(Self::from(bcs_ext::from_bytes::<AccountPublicKey>(data)?)) | ||
bcs_ext::from_bytes::<AccountPublicKey>(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,32 @@ | ||
use super::AccountAddressWrapper; | ||
use anyhow::Result; | ||
use starcoin_account_api::Setting; | ||
use starcoin_schemadb::{ | ||
define_schema, | ||
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<Setting> for SettingWrapper { | ||
fn from(setting: Setting) -> Self { | ||
Self(setting) | ||
} | ||
} | ||
|
||
impl KeyCodec<AccountSetting> for AccountAddressWrapper { | ||
impl KeyCodec<AccountSetting> for AccountAddress { | ||
fn encode_key(&self) -> Result<Vec<u8>> { | ||
Ok(self.0.to_vec()) | ||
Ok(self.to_vec()) | ||
} | ||
|
||
fn decode_key(data: &[u8]) -> Result<Self> { | ||
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<AccountSetting> for SettingWrapper { | ||
impl ValueCodec<AccountSetting> for Setting { | ||
fn encode_value(&self) -> Result<Vec<u8>> { | ||
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<Self> { | ||
serde_json::from_slice::<Setting>(data) | ||
.map(Into::into) | ||
.map_err(Into::into) | ||
serde_json::from_slice::<Setting>(data).map_err(Into::into) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.