Skip to content

Commit

Permalink
new wallet storage and impl
Browse files Browse the repository at this point in the history
  • Loading branch information
nanne007 committed Jul 30, 2020
1 parent cb6399a commit d6d6cf9
Show file tree
Hide file tree
Showing 20 changed files with 701 additions and 146 deletions.
8 changes: 8 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
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 {
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>;
}
8 changes: 7 additions & 1 deletion wallet/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ futures = "0.3"
actix = "0.10.0-alpha.3"
actix-rt = "1.1"
async-trait = "0.1"
parking_lot = "0.9"
rand = "0.7"
serde = "1"
starcoin-logger = {path = "../../commons/logger"}
stest = {path = "../../commons/stest"}
starcoin-types = { path = "../../types"}
starcoin-config = { path = "../../config"}
starcoin-wallet-api = { path = "../api", features = ["mock"]}
starcoin-wallet-lib = { path = "../lib"}

starcoin-storage = {path = "../../storage"}
starcoin-crypto = {path = "../../commons/crypto"}
starcoin-decrypt = {path = "../../commons/decrypt"}
starcoin-canonical-serialization = { package="starcoin-canonical-serialization", path = "../../commons/scs"}
[dev-dependencies]
tempfile="3"
tokio = { version = "0.2", features = ["full"] }
Expand Down
Loading

0 comments on commit d6d6cf9

Please sign in to comment.