Skip to content

Commit

Permalink
1. remove std::cout related code 2. add account-asset-database 3. add…
Browse files Browse the repository at this point in the history
… block_chain_impl unit test file
  • Loading branch information
wdyoschina committed Dec 3, 2016
1 parent 88ed3c0 commit 4b50f1f
Show file tree
Hide file tree
Showing 15 changed files with 780 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ BC_CONSTEXPR size_t ASSET_DETAIL_FIX_SIZE = ASSET_DETAIL_SYMBOL_FIX_SIZE
class BC_API asset_detail
{
public:
typedef std::vector<asset_detail> list;
asset_detail();
asset_detail(std::string symbol, uint64_t maximum_supply,
uint32_t asset_type, std::string issuer,
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/bitcoin/chain/business_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class BC_API business_address_asset
typedef std::vector<business_address_asset> list;

std::string address;
uint8_t status;
uint8_t status; // 0 -- unspent 1 -- confirmed 2 -- local asset not issued
uint64_t quantity;
asset_detail detail;
};
Expand Down
17 changes: 14 additions & 3 deletions include/bitcoin/blockchain/block_chain_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,29 +210,40 @@ class BCB_API block_chain_impl
/* begin store account related info into database */
inline hash_digest get_hash(const std::string& str);
inline short_hash get_short_hash(const std::string& str);

// account related api
std::shared_ptr<account> is_account_passwd_valid(const std::string& name, const std::string& passwd);
bool is_account_exist(const std::string& name);
operation_result store_account(std::shared_ptr<account> acc);
std::shared_ptr<account> get_account(const std::string& name);
std::shared_ptr<std::vector<business_address_asset>> get_account_asset(const std::string& name, const std::string& asset);
std::shared_ptr<std::vector<business_address_asset>> get_account_assets(const std::string& name);
account_status get_account_user_status(const std::string& name);
account_status get_account_system_status(const std::string& name);
bool set_account_user_status(const std::string& name, uint8_t status);
bool set_account_system_status(const std::string& name, uint8_t status);

// account asset api
operation_result store_account_asset(std::shared_ptr<asset_detail> detail);
std::shared_ptr<std::vector<business_address_asset>> get_account_asset(const std::string& name, const std::string& asset);
std::shared_ptr<std::vector<business_address_asset>> get_account_assets(const std::string& name);
bool is_asset_exist(const std::string& name, const std::string& asset_name);

// account adress related api
operation_result store_account_address(std::shared_ptr<account_address> address);
std::shared_ptr<account_address> get_account_address(const std::string& name, const std::string& address);
std::shared_ptr<std::vector<account_address>> get_account_addresses(const std::string& name);

// asset related api
// asset related api

/* begin todo -- will be removed later-- not use following api
use set/get_account_asset(s) api instead ---- refer "account asset api"
*/
uint16_t get_asset_status(const std::string& name);
bool is_asset_exist(const std::string& name);
operation_result store_asset(std::shared_ptr<asset_detail> ptr);
operation_result delete_asset(const std::string& name);
std::shared_ptr<asset_detail> get_asset(const std::string& name);
/* end todo -- will be removed later -- not suggest use up api */

/* end store account related info into database */
private:
typedef std::function<bool(database::handle)> perform_read_functor;
Expand Down
4 changes: 4 additions & 0 deletions include/bitcoin/database/data_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <bitcoin/database/databases/account_address_database.hpp>
#include <bitcoin/database/databases/asset_database.hpp>
#include <bitcoin/database/databases/address_asset_database.hpp>
#include <bitcoin/database/databases/account_asset_database.hpp>
//#include <bitcoin/bitcoin/wallet/payment_address.hpp>

using namespace libbitcoin::wallet;
Expand Down Expand Up @@ -80,6 +81,8 @@ class BCD_API data_base
path assets_lookup;
path address_assets_lookup;
path address_assets_rows;
path account_assets_lookup;
path account_assets_rows;
path account_addresses_lookup;
path account_addresses_rows;
/* end database for account, asset, address_asset relationship */
Expand Down Expand Up @@ -249,6 +252,7 @@ class BCD_API data_base
account_database accounts;
asset_database assets;
address_asset_database address_assets;
account_asset_database account_assets;
account_address_database account_addresses;
/* end database for account, asset, address_asset relationship */
};
Expand Down
114 changes: 114 additions & 0 deletions include/bitcoin/database/databases/account_asset_database.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Copyright (c) 2011-2015 metaverse developers (see AUTHORS)
*
* This file is part of mvsd.
*
* libbitcoin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License with
* additional permissions to the one published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. For more information see LICENSE.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MVS_DATABASE_ACCOUNT_ASSET_DATABASE_HPP
#define MVS_DATABASE_ACCOUNT_ASSET_DATABASE_HPP

#include <memory>
#include <boost/filesystem.hpp>
#include <bitcoin/bitcoin.hpp>
#include <bitcoin/database/define.hpp>
#include <bitcoin/database/memory/memory_map.hpp>
#include <bitcoin/database/primitives/record_multimap.hpp>
#include <bitcoin/bitcoin/chain/attachment/asset/asset_detail.hpp>
#include <bitcoin/bitcoin/chain/business_data.hpp>

using namespace libbitcoin::chain;

namespace libbitcoin {
namespace database {

struct BCD_API account_asset_statinfo
{
/// Number of buckets used in the hashtable.
/// load factor = addrs / buckets
const size_t buckets;

/// Total number of unique addresses in the database.
const size_t addrs;

/// Total number of rows across all addresses.
const size_t rows;
};

/// This is a multimap where the key is the Bitcoin address hash,
/// which returns several rows giving the account_asset for that address.
class BCD_API account_asset_database
{
public:
/// Construct the database.
account_asset_database(const boost::filesystem::path& lookup_filename,
const boost::filesystem::path& rows_filename,
std::shared_ptr<shared_mutex> mutex=nullptr);

/// Close the database (all threads must first be stopped).
~account_asset_database();

/// Initialize a new account_asset database.
bool create();

/// Call before using the database.
bool start();

/// Call to signal a stop of current operations.
bool stop();

/// Call to unload the memory map.
bool close();

void store(const short_hash& key, const asset_detail& account_asset);

void delete_last_row(const short_hash& key);

asset_detail::list get(const short_hash& key) const;

std::shared_ptr<asset_detail> get(const short_hash& key, const std::string& address) const;

/// get assets whose status is not issued and stored in local database (not in blockchain)
std::shared_ptr<std::vector<business_address_asset>> get_unissued_assets(const short_hash& key) const;

/// Synchonise with disk.
void sync();

/// Return statistical info about the database.
account_asset_statinfo statinfo() const;

private:
typedef record_hash_table<short_hash> record_map;
typedef record_multimap<short_hash> record_multiple_map;

/// Hash table used for start index lookup for linked list by address hash.
memory_map lookup_file_;
record_hash_table_header lookup_header_;
record_manager lookup_manager_;
record_map lookup_map_;

/// List of account_asset rows.
memory_map rows_file_;
record_manager rows_manager_;
record_list rows_list_;
record_multiple_map rows_multimap_;
};

} // namespace database
} // namespace libbitcoin

#endif


3 changes: 1 addition & 2 deletions src/lib/bitcoin/chain/attachment/account/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ data_chunk account::to_data() const
data_sink ostream(data);
to_data(ostream);
ostream.flush();
std::cout<<"to-data: "<<data.size()<<" "<<serialized_size();
//BITCOIN_ASSERT(data.size() == serialized_size());
//BITCOIN_ASSERT(data.size() == serialized_size()); // serialized_size is not used
return data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ data_chunk account_address::to_data() const
data_sink ostream(data);
to_data(ostream);
ostream.flush();
std::cout<<"to-data: "<<data.size()<<" "<<serialized_size();
//BITCOIN_ASSERT(data.size() == serialized_size());
return data;
}
Expand Down
1 change: 0 additions & 1 deletion src/lib/bitcoin/chain/attachment/asset/asset_detail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ data_chunk asset_detail::to_data() const
data_sink ostream(data);
to_data(ostream);
ostream.flush();
std::cout<<"to-data: "<<data.size()<<" "<<serialized_size();
//BITCOIN_ASSERT(data.size() == serialized_size());
return data;
}
Expand Down
42 changes: 36 additions & 6 deletions src/lib/blockchain/block_chain_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ std::shared_ptr<account> block_chain_impl::get_account(const std::string& name)
{
return database_.accounts.get_account_result(get_hash(name)).get_account_detail();
}

/// just store data into database "not do same address check" -- todo
operation_result block_chain_impl::store_account_address(std::shared_ptr<account_address> address)
{
if (stopped())
Expand Down Expand Up @@ -1004,6 +1004,27 @@ std::shared_ptr<std::vector<account_address>> block_chain_impl::get_account_addr
}
return sp_addr;
}
operation_result block_chain_impl::store_account_asset(std::shared_ptr<asset_detail> detail)
{
if (stopped())
{
return operation_result::failure;
}
if (!(detail))
{
throw std::runtime_error{"nullptr for asset"};
}
///////////////////////////////////////////////////////////////////////////
// Critical Section.
unique_lock lock(mutex_);

const auto hash = get_short_hash(detail->get_issuer());
database_.account_assets.store(hash, *detail);
database_.account_assets.sync();
///////////////////////////////////////////////////////////////////////////
return operation_result::okay;
}

std::shared_ptr<std::vector<business_address_asset>> block_chain_impl::get_account_asset(const std::string& name, const std::string& asset_name)
{
auto sp_asset_vec = get_account_assets(name);
Expand All @@ -1021,8 +1042,7 @@ std::shared_ptr<std::vector<business_address_asset>> block_chain_impl::get_accou
}
std::shared_ptr<std::vector<business_address_asset>> block_chain_impl::get_account_assets(const std::string& name)
{
auto account_addr_vec =
get_account_addresses(name);
auto account_addr_vec = get_account_addresses(name);
auto sp_asset_vec = std::make_shared<std::vector<business_address_asset>>();

// copy each asset_vec element to sp_asset
Expand All @@ -1039,6 +1059,10 @@ get_account_addresses(name);
};
std::for_each(account_addr_vec->begin(), account_addr_vec->end(), action);

// get account asset which is not issued (not in blockchain)
auto no_issued_assets = database_.account_assets.get_unissued_assets(get_short_hash(name));
std::for_each(no_issued_assets->begin(), no_issued_assets->end(), add_asset);

return sp_asset_vec;
}
account_status block_chain_impl::get_account_user_status(const std::string& name)
Expand Down Expand Up @@ -1081,7 +1105,13 @@ bool block_chain_impl::set_account_system_status(const std::string& name, uint8_
}
return ret_val;
}
bool block_chain_impl::is_asset_exist(const std::string& name, const std::string& asset_name)
{
auto sh_vec = get_account_asset(name, asset_name);
return sh_vec->size() != 0;
}

/* begin todo -- will be removed later-- not use following api */
uint16_t block_chain_impl::get_asset_status(const std::string& name)
{
return 0;
Expand All @@ -1090,7 +1120,7 @@ bool block_chain_impl::is_asset_exist(const std::string& name)
{
return nullptr != get_asset(name);
}

/// just store data into database "not do same address check" -- todo
operation_result block_chain_impl::store_asset(std::shared_ptr<asset_detail> ptr)
{
if (stopped())
Expand Down Expand Up @@ -1120,8 +1150,8 @@ operation_result block_chain_impl::delete_asset(const std::string& name)
std::shared_ptr<asset_detail> block_chain_impl::get_asset(const std::string& name)
{
return database_.assets.get_asset_result(get_hash(name)).get_asset_detail();
}

}
/* end todo -- will be removed later */

/* end store account related info into database */
} // namespace blockchain
Expand Down
Loading

0 comments on commit 4b50f1f

Please sign in to comment.