Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new wallet storage and impl #998

Merged
merged 4 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion storage/src/accumulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use starcoin_accumulator::{
};
use std::io::Write;
use std::mem::size_of;
use std::sync::Arc;

define_storage!(
AccumulatorNodeStore,
Expand Down
1 change: 0 additions & 1 deletion storage/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use serde::{Deserialize, Serialize};
use starcoin_types::block::{Block, BlockBody, BlockHeader, BlockNumber, BlockState};
use std::io::Write;
use std::mem::size_of;
use std::sync::Arc;

#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct StorageBlock {
Expand Down
1 change: 0 additions & 1 deletion storage/src/block_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use anyhow::Result;
use crypto::HashValue;
use scs::SCSCodec;
use starcoin_types::block::BlockInfo;
use std::sync::Arc;

pub trait BlockInfoStore {
fn save_block_info(&self, block_info: BlockInfo) -> Result<()>;
Expand Down
1 change: 0 additions & 1 deletion storage/src/contract_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use anyhow::Result;
use crypto::HashValue;
use scs::SCSCodec;
use starcoin_types::contract_event::ContractEvent;
use std::sync::Arc;

define_storage!(
ContractEventStorage,
Expand Down
34 changes: 22 additions & 12 deletions storage/src/db_storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,59 @@ use std::path::Path;

pub struct DBStorage {
db: DB,
cfs: Vec<ColumnFamilyName>,
}

impl DBStorage {
pub fn new<P: AsRef<Path> + Clone>(db_root_path: P) -> Self {
let path = db_root_path.as_ref().join("starcoindb");
Self::open(path, false).expect("Unable to open StarcoinDB")
Self::open_with_cfs(path, VEC_PREFIX_NAME.to_vec(), false)
.expect("Unable to open StarcoinDB")
}
pub fn open(path: impl AsRef<Path>, readonly: bool) -> Result<Self> {
let column_families = VEC_PREFIX_NAME.to_vec();

pub fn open_with_cfs(
root_path: impl AsRef<Path>,
column_families: Vec<ColumnFamilyName>,
readonly: bool,
) -> Result<Self> {
let path = root_path.as_ref();

let cfs_set: HashSet<_> = column_families.iter().collect();
{
ensure!(
cfs_set.len() == column_families.len(),
"Duplicate column family name found.",
);
}
if Self::db_exists(path.as_ref()) {
let cf_vec = Self::list_cf(path.as_ref())?;
if Self::db_exists(path) {
let cf_vec = Self::list_cf(path)?;
for cf in cf_vec {
if cf != DEFAULT_PREFIX_NAME && cfs_set.get(&cf.as_str()).is_none() {
error!(
"db path cf: {:?} is not equal,please clear dir: {:?}",
path.as_ref(),
cf
path, cf
);
std::process::exit(100);
}
}
}

let db = if readonly {
Self::open_readonly(path.as_ref(), column_families)?
Self::open_readonly(path, column_families.clone())?
} else {
let mut db_opts = rocksdb::Options::default();
db_opts.create_if_missing(true);
db_opts.create_missing_column_families(true);
// For now we set the max total WAL size to be 1G. This config can be useful when column
// families are updated at non-uniform frequencies.
db_opts.set_max_total_wal_size(1 << 30);
Self::open_inner(&db_opts, path.as_ref(), column_families)?
Self::open_inner(&db_opts, path, column_families.clone())?
};

Ok(DBStorage { db })
Ok(DBStorage {
db,
cfs: column_families,
})
}

fn open_inner(
Expand Down Expand Up @@ -88,7 +98,7 @@ impl DBStorage {
}

pub fn drop_cf(&mut self) -> Result<(), Error> {
for cf in &VEC_PREFIX_NAME.to_vec() {
for cf in self.cfs.clone() {
self.db.drop_cf(cf)?;
}
Ok(())
Expand All @@ -97,7 +107,7 @@ impl DBStorage {
/// Flushes all memtable data. This is only used for testing `get_approximate_sizes_cf` in unit
/// tests.
pub fn flush_all(&self) -> Result<()> {
for cf_name in VEC_PREFIX_NAME.to_vec() {
for cf_name in &self.cfs {
let cf_handle = self.get_cf_handle(cf_name)?;
self.db.flush_cf(cf_handle)?;
}
Expand Down
1 change: 0 additions & 1 deletion storage/src/state_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crypto::HashValue;
use forkable_jellyfish_merkle::node_type::Node;
use starcoin_state_store_api::{StateNode, StateNodeStore};
use std::collections::BTreeMap;
use std::sync::Arc;

define_storage!(StateStorage, HashValue, StateNode, STATE_NODE_PREFIX_NAME);

Expand Down
22 changes: 11 additions & 11 deletions storage/src/storage_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ macro_rules! define_storage {
($storage_type: ident, $key_type: ty, $value_type: ty, $prefix_name: expr) => {
#[derive(Clone)]
pub struct $storage_type {
store: CodecStorage<$key_type, $value_type>,
store: $crate::storage::CodecStorage<$key_type, $value_type>,
}

impl $storage_type {
const PREFIX_NAME: $crate::ColumnFamilyName = $prefix_name;
pub fn new(instance: crate::storage::StorageInstance) -> Self {
let inner_storage = crate::storage::InnerStorage::new(instance, Self::PREFIX_NAME);
const PREFIX_NAME: $crate::storage::ColumnFamilyName = $prefix_name;
pub fn new(instance: $crate::storage::StorageInstance) -> Self {
let inner_storage = $crate::storage::InnerStorage::new(instance, Self::PREFIX_NAME);
Self {
store: CodecStorage::new(Arc::new(inner_storage)),
store: CodecStorage::new(std::sync::Arc::new(inner_storage)),
}
}
pub fn put(&self, key: $key_type, value: $value_type) -> Result<()> {
pub fn put(&self, key: $key_type, value: $value_type) -> anyhow::Result<()> {
self.store.put(key, value)
}
pub fn get(&self, key: $key_type) -> Result<Option<$value_type>> {
pub fn get(&self, key: $key_type) -> anyhow::Result<Option<$value_type>> {
self.store.get(key)
}
pub fn remove(&self, key: $key_type) -> Result<()> {
pub fn remove(&self, key: $key_type) -> anyhow::Result<()> {
self.store.remove(key)
}
pub fn write_batch(&self, batch: WriteBatch) -> Result<()> {
pub fn write_batch(&self, batch: WriteBatch) -> anyhow::Result<()> {
self.store.write_batch(batch)
}
pub fn get_len(&self) -> Result<u64> {
pub fn get_len(&self) -> anyhow::Result<u64> {
self.store.get_len()
}
pub fn keys(&self) -> Result<Vec<Vec<u8>>> {
pub fn keys(&self) -> anyhow::Result<Vec<Vec<u8>>> {
self.store.keys()
}
}
Expand Down
7 changes: 5 additions & 2 deletions storage/src/tests/test_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ extern crate chrono;
use crate::cache_storage::CacheStorage;
use crate::db_storage::DBStorage;
use crate::storage::{InnerStore, StorageInstance, ValueCodec, CACHE_NONE_OBJECT};
use crate::{Storage, TransactionInfoStore, DEFAULT_PREFIX_NAME, TRANSACTION_INFO_PREFIX_NAME};
use crate::{
Storage, TransactionInfoStore, DEFAULT_PREFIX_NAME, TRANSACTION_INFO_PREFIX_NAME,
VEC_PREFIX_NAME,
};
use anyhow::Result;
use crypto::HashValue;
use starcoin_types::transaction::TransactionInfo;
Expand Down Expand Up @@ -45,7 +48,7 @@ fn test_open_read_only() {
let result = db.put(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec());
assert!(result.is_ok());
let path = tmpdir.as_ref().join("starcoindb");
let db = DBStorage::open(path, true).unwrap();
let db = DBStorage::open_with_cfs(path, VEC_PREFIX_NAME.to_vec(), true).unwrap();
let result = db.put(DEFAULT_PREFIX_NAME, key.to_vec(), value.to_vec());
assert!(result.is_err());
let result = db.get(DEFAULT_PREFIX_NAME, key.to_vec()).unwrap();
Expand Down
1 change: 0 additions & 1 deletion storage/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use anyhow::Result;
use crypto::HashValue;
use scs::SCSCodec;
use starcoin_types::transaction::Transaction;
use std::sync::Arc;

define_storage!(
TransactionStorage,
Expand Down
1 change: 0 additions & 1 deletion storage/src/transaction_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use anyhow::{bail, Error, Result};
use crypto::HashValue;
use scs::SCSCodec;
use starcoin_types::transaction::TransactionInfo;
use std::sync::Arc;

define_storage!(
TransactionInfoStorage,
Expand Down
1 change: 1 addition & 0 deletions wallet/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ starcoin-types = { path = "../../types"}
starcoin-crypto = { path = "../../commons/crypto"}
rand = "0.7.3"
rand_core = { version = "0.5.1", default-features = false }
futures = "0.3"

[dev-dependencies]

Expand Down
3 changes: 2 additions & 1 deletion wallet/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-License-Identifier: Apache-2.0

pub mod error;
mod rich_wallet;
mod service;
mod store;
mod types;
mod wallet;

pub use rich_wallet::*;
pub use service::*;
pub use store::*;
pub use types::*;
Expand Down
3 changes: 3 additions & 0 deletions wallet/api/src/mock/mock_wallet_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl WalletAsyncService for MockWalletService {
.wallet
.unlock_account(address, password.as_str(), duration)?)
}
async fn lock_account(self, address: AccountAddress) -> ServiceResult<()> {
Ok(self.wallet.lock_account(address)?)
}

async fn import_account(
self,
Expand Down
64 changes: 64 additions & 0 deletions wallet/api/src/rich_wallet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use anyhow::Result;
use futures::Stream;
use serde::Deserialize;
use serde::Serialize;
use starcoin_types::account_address::AccountAddress;
use starcoin_types::account_config::token_code::TokenCode;
use starcoin_types::contract_event::EventWithProof;
use starcoin_types::event::EventKey;
use starcoin_types::transaction::{RawUserTransaction, SignedUserTransaction, TransactionPayload};
use std::time::Duration;

pub trait ChainEventWatcher {
fn watch_event<S>(&self, key: EventKey) -> S
where
S: Stream<Item = EventWithProof>;
}

pub trait TransactionSubmitter {
fn submit_transaction(&self, txn: SignedUserTransaction) -> Result<()>;
}

pub trait RichWallet {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个名字是不是不太合适?但我也没想到更好的。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里面的 trait 暂时没用。名字可以忽略。

目前没有抽象接口。
我是想等功能完整后,看看那些需要抽离出来。

fn set_default_expiration_timeout(&mut self, timeout: Duration);
fn set_default_gas_price(&mut self, gas_price: u64);
fn set_default_gas_token(&mut self, token: TokenCode);

fn get_accepted_tokns(&self) -> Result<Vec<TokenCode>>;

fn build_transaction(
&self,
// if not specified, use default sender.
sender: Option<AccountAddress>,
payload: TransactionPayload,
max_gas_amount: u64,
// if not specified, uses default settings.
gas_unit_price: Option<u64>,
gas_token_code: Option<String>,
expiration_timestamp_secs: Option<u64>,
) -> Result<RawUserTransaction>;

fn sign_transaction(
&self,
raw: RawUserTransaction,
address: Option<AccountAddress>,
) -> Result<SignedUserTransaction>;
fn submit_txn(&mut self, txn: SignedUserTransaction) -> Result<()>;
fn get_next_available_seq_number(&self) -> Result<u64>;

// ...other functionality of origin wallets.
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct Setting {
default_expiration_timeout: u64,
default_gas_price: u64,
default_gas_token: TokenCode,
}

pub trait WalletStorageTrait {
fn save_default_settings(&self, setting: Setting) -> Result<()>;

fn save_accepted_token(&self, token: TokenCode) -> Result<()>;
fn contain_wallet(&self, address: AccountAddress) -> Result<bool>;
}
1 change: 1 addition & 0 deletions wallet/api/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub trait WalletAsyncService: Clone + std::marker::Unpin + Send + Sync {
password: String,
duration: std::time::Duration,
) -> ServiceResult<()>;
async fn lock_account(self, address: AccountAddress) -> ServiceResult<()>;
async fn import_account(
self,
address: AccountAddress,
Expand Down
8 changes: 6 additions & 2 deletions wallet/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ actix = "0.10.0-alpha.3"
actix-rt = "1.1"
async-trait = "0.1"
rand = "0.7.3"
parking_lot = "0.9"
serde = "1"
rand_core = { version = "0.5.1", default-features = false }
wallet-api = {path = "../api",package = "starcoin-wallet-api"}
scs ={package= "starcoin-canonical-serialization", path = "../../commons/scs"}

starcoin-wallet-api = {path = "../api",package = "starcoin-wallet-api"}
starcoin-canonical-serialization ={package= "starcoin-canonical-serialization", path = "../../commons/scs"}
starcoin-types = { path = "../../types"}
starcoin-crypto = { path = "../../commons/crypto"}
starcoin-decrypt = {path = "../../commons/decrypt"}
starcoin-storage = {path = "../../storage"}
[dev-dependencies]
tempfile="3"
6 changes: 3 additions & 3 deletions wallet/lib/src/file_wallet_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Result};
use scs::SCSCodec;
use starcoin_canonical_serialization as scs;
use starcoin_canonical_serialization::SCSCodec;
use starcoin_types::account_address::AccountAddress;
use starcoin_wallet_api::{WalletAccount, WalletStore};
use std::fs::OpenOptions;
use std::path::Path;
use std::{
Expand All @@ -12,8 +14,6 @@ use std::{
io::{Read, Write},
path::PathBuf,
};
use wallet_api::WalletAccount;
use wallet_api::WalletStore;

pub const DEFAULT_ACCOUNT_FILE_NAME: &str = "account";

Expand Down
2 changes: 1 addition & 1 deletion wallet/lib/src/keystore_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use starcoin_types::{
account_address::{self, AccountAddress},
transaction::{RawUserTransaction, SignedUserTransaction},
};
use starcoin_wallet_api::{error::WalletError, Wallet, WalletAccount, WalletStore};
use std::collections::HashMap;
use std::convert::TryFrom;
use std::ops::Add;
use std::sync::{Mutex, RwLock};
use std::time::Duration;
use std::time::Instant;
use wallet_api::{error::WalletError, Wallet, WalletAccount, WalletStore};

type KeyPair = starcoin_crypto::test_utils::KeyPair<Ed25519PrivateKey, Ed25519PublicKey>;
pub type Result<T> = std::result::Result<T, WalletError>;
Expand Down
Loading