Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Commit

Permalink
Merge pull request #68 from vikramrajkumar/btsx
Browse files Browse the repository at this point in the history
Merge upstream
  • Loading branch information
bitsha256 committed Sep 24, 2014
2 parents e090e24 + bebfa23 commit 5c9d8b4
Show file tree
Hide file tree
Showing 43 changed files with 818 additions and 372 deletions.
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
[submodule "vendor"]
path = vendor
url = https://github.com/BitShares/vendor.git
ignore = dirty
[submodule "libraries/fc"]
path = libraries/fc
url = https://github.com/InvictusInnovations/fc.git
ignore = dirty
[submodule "programs/web_wallet"]
path = programs/web_wallet
url = https://github.com/dacsunlimited/web_wallet.git
ignore = dirty
[submodule "programs/qt_wallet"]
path = programs/qt_wallet
url = https://github.com/dacsunlimited/qt_wallet.git
ignore = dirty
[submodule "libraries/leveldb"]
path = libraries/leveldb
url = https://github.com/bitcoin/leveldb.git
ignore = dirty
[submodule "programs/btsx_bots"]
path = programs/btsx_bots
url = https://github.com/freetradebots/btsx_bots
ignore = dirty
2 changes: 1 addition & 1 deletion libraries/blockchain/genesis.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"timestamp" : "20140916T000000",
"timestamp" : "20140924T000000",
"market_assets": [
{
"symbol": "PTS",
Expand Down
2 changes: 1 addition & 1 deletion libraries/blockchain/include/bts/blockchain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/* Comment out this line for a non-test network */
//#define BTS_TEST_NETWORK
#define BTS_TEST_NETWORK_VERSION 28
#define BTS_TEST_NETWORK_VERSION 29

/** @file bts/blockchain/config.hpp
* @brief Defines global constants that determine blockchain behavior
Expand Down
33 changes: 8 additions & 25 deletions libraries/blockchain/market_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class market_engine
cancel_current_short( mtrx, market_idx.order_price.quote_asset_id );
push_market_transaction( mtrx );
}

_pending_state->apply_changes();
}

void execute( asset_id_type quote_id, asset_id_type base_id, const fc::time_point_sec& timestamp )
Expand Down Expand Up @@ -167,17 +169,10 @@ class market_engine
mtrx.bid_collateral = mtrx.bid_paid / collateral_rate;

// Handle rounding errors
if( (*mtrx.bid_collateral - _current_bid->get_balance()).amount < BTS_BLOCKCHAIN_PRECISION )
if( ((_current_bid->get_balance() - *mtrx.bid_collateral).amount < 100 ) &&
(_current_bid->get_balance() > *mtrx.bid_collateral) )
mtrx.bid_collateral = _current_bid->get_balance();

// If too little collateral at this price
if( *mtrx.bid_collateral < mtrx.ask_paid )
{
edump( (mtrx) );
_current_bid.reset();
continue;
}

pay_current_short( mtrx, *quote_asset );
pay_current_cover( mtrx, *quote_asset );

Expand Down Expand Up @@ -262,7 +257,7 @@ class market_engine
// Bound collateral ratio
price collateral_rate = _current_bid->get_price();
if( collateral_rate > _market_stat.center_price )
collateral_rate = _market_stat.center_price;
collateral_rate = _market_stat.center_price;

const asset ask_quantity_usd = _current_ask->get_quote_quantity();
const asset short_quantity_usd = _current_bid->get_balance() / collateral_rate;
Expand All @@ -278,17 +273,10 @@ class market_engine
mtrx.bid_collateral = mtrx.bid_paid / collateral_rate;

// Handle rounding errors
if( (*mtrx.bid_collateral - _current_bid->get_balance()).amount < BTS_BLOCKCHAIN_PRECISION )
if( ((_current_bid->get_balance() - *mtrx.bid_collateral).amount < 100 ) &&
(_current_bid->get_balance() > *mtrx.bid_collateral) )
mtrx.bid_collateral = _current_bid->get_balance();

// If too little collateral at this price
if( *mtrx.bid_collateral < mtrx.ask_paid )
{
edump( (mtrx) );
_current_bid.reset();
continue;
}

pay_current_short( mtrx, *quote_asset );
pay_current_ask( mtrx, *quote_asset );

Expand Down Expand Up @@ -460,15 +448,10 @@ class market_engine
FC_ASSERT( _current_bid->type == short_order );
FC_ASSERT( mtrx.bid_type == short_order );

/** NOTE: the short may pay extra XTS to the collateral in the event rounding occurs. This
* just checks to make sure it is reasonable.
*/
FC_ASSERT( mtrx.ask_paid <= *mtrx.bid_collateral, "", ("mtrx",mtrx) );

quote_asset.current_share_supply += mtrx.bid_paid.amount;

auto collateral = *mtrx.bid_collateral + mtrx.ask_paid;
if( mtrx.bid_paid.amount <= 0 ) // WHY is this ever negitive??
if( mtrx.bid_paid.amount <= 0 ) // WHY is this ever negative??
{
FC_ASSERT( mtrx.bid_paid.amount >= 0 );
_current_bid->state.balance -= mtrx.bid_collateral->amount;
Expand Down
26 changes: 16 additions & 10 deletions libraries/blockchain/market_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace bts { namespace blockchain {
auto market_stat = eval_state._current_state->get_market_status( ask_index.order_price.quote_asset_id, ask_index.order_price.base_asset_id );

if( !market_stat )
market_stat = market_status(ask_index.order_price.quote_asset_id, ask_index.order_price.base_asset_id, 0,0);
market_stat = market_status( ask_index.order_price.quote_asset_id, ask_index.order_price.base_asset_id, 0, 0 );
market_stat->ask_depth += delta_amount.amount;

eval_state._current_state->store_market_status( *market_stat );
Expand All @@ -133,16 +133,21 @@ namespace bts { namespace blockchain {
asset delta_amount = this->get_amount();
asset delta_quote = delta_amount * this->short_index.order_price;

// Only allow using BTSX as collateral
FC_ASSERT( delta_amount.asset_id == 0 );

/** if the USD amount of the order is effectively then don't bother */
FC_ASSERT( llabs(delta_quote.amount) > 0, "", ("delta_quote",delta_quote)("order",*this));
FC_ASSERT( llabs( delta_quote.amount ) > 0, "", ("delta_quote",delta_quote)("order",*this));

eval_state.validate_asset( delta_amount );
auto asset_to_short = eval_state._current_state->get_asset_record( short_index.order_price.quote_asset_id );
FC_ASSERT( asset_to_short.valid() );
FC_ASSERT( asset_to_short->is_market_issued(), "${symbol} is not a market issued asset", ("symbol",asset_to_short->symbol) );

if( eval_state._current_state->get_head_block_num() >= BTSX_MARKET_FORK_8_BLOCK_NUM )
{
FC_ASSERT( !this->short_price_limit || *(this->short_price_limit) >= this->short_index.order_price, "Insufficient collateral at price limit" );
}

auto current_short = eval_state._current_state->get_short_record( this->short_index );
//if( current_short ) wdump( (current_short) );
Expand Down Expand Up @@ -177,7 +182,7 @@ namespace bts { namespace blockchain {

auto market_stat = eval_state._current_state->get_market_status( short_index.order_price.quote_asset_id, short_index.order_price.base_asset_id );
if( !market_stat )
market_stat = market_status(short_index.order_price.quote_asset_id, short_index.order_price.base_asset_id, 0,0);
market_stat = market_status( short_index.order_price.quote_asset_id, short_index.order_price.base_asset_id, 0, 0 );

market_stat->bid_depth += delta_amount.amount;

Expand Down Expand Up @@ -237,19 +242,19 @@ namespace bts { namespace blockchain {

if( current_cover->payoff_balance > 0 )
{
auto new_call_price = asset(current_cover->payoff_balance, delta_amount.asset_id) /
asset((current_cover->collateral_balance*2)/3, 0);
auto new_call_price = asset( current_cover->payoff_balance, delta_amount.asset_id) /
asset( (current_cover->collateral_balance*2)/3, cover_index.order_price.base_asset_id );

if( this->new_cover_price && (*this->new_cover_price > new_call_price) )
eval_state._current_state->store_collateral_record( market_index_key( *this->new_cover_price, this->cover_index.owner),
eval_state._current_state->store_collateral_record( market_index_key( *this->new_cover_price, this->cover_index.owner ),
*current_cover );
else
eval_state._current_state->store_collateral_record( market_index_key( new_call_price, this->cover_index.owner),
eval_state._current_state->store_collateral_record( market_index_key( new_call_price, this->cover_index.owner ),
*current_cover );
}
else // withdraw the collateral to the transaction to be deposited at owners discretion / cover fees
{
eval_state.add_balance( asset( current_cover->collateral_balance, 0 ) );
eval_state.add_balance( asset( current_cover->collateral_balance, cover_index.order_price.base_asset_id ) );

auto market_stat = eval_state._current_state->get_market_status( cover_index.order_price.quote_asset_id, cover_index.order_price.base_asset_id );
FC_ASSERT( market_stat, "this should be valid for there to even be a position to cover" );
Expand Down Expand Up @@ -284,8 +289,8 @@ namespace bts { namespace blockchain {
// and insert a new one.
eval_state._current_state->store_collateral_record( this->cover_index, collateral_record() );

auto new_call_price = asset(current_cover->payoff_balance, delta_amount.asset_id) /
asset((current_cover->collateral_balance*2)/3, 0);
auto new_call_price = asset( current_cover->payoff_balance, cover_index.order_price.quote_asset_id ) /
asset( (current_cover->collateral_balance*2)/3, cover_index.order_price.base_asset_id );

eval_state._current_state->store_collateral_record( market_index_key( new_call_price, this->cover_index.owner),
*current_cover );
Expand All @@ -300,6 +305,7 @@ namespace bts { namespace blockchain {
void remove_collateral_operation::evaluate( transaction_evaluation_state& eval_state )
{
// Should this even be allowed?
FC_ASSERT( !"Not implemented!" );
}

} } // bts::blockchain
31 changes: 23 additions & 8 deletions libraries/cli/print_result.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ namespace bts { namespace cli {
return;
}

out << std::string(18, ' ') << "BIDS (* Short Wall)"
out << std::string(18, ' ') << "BIDS (* Short)"
<< std::string(39, ' ') << " | "
<< std::string(34, ' ') << "ASKS"
<< std::string(34, ' ') << "\n"
Expand Down Expand Up @@ -736,6 +736,11 @@ namespace bts { namespace cli {
shorts = client->blockchain_market_list_shorts(arguments[0].as_string());
else
shorts = client->blockchain_market_list_shorts(arguments[0].as_string(), arguments[2].as_int64());

std::copy_if(shorts.begin(), shorts.end(), std::back_inserter(bids_asks.first), [&max_short_price](const market_order& order) -> bool {
return order.state.short_price_limit && *order.state.short_price_limit < max_short_price;
});

shorts.erase(std::remove_if(shorts.begin(), shorts.end(), [&max_short_price](const market_order& short_order) -> bool {
//Filter out if insufficient collateral (i.e. XTS/USD < 1/max_short_price) or if price limit (i.e. USD/XTS) is less than short execution price
return short_order.get_price() > max_short_price || (short_order.state.short_price_limit.valid()?
Expand All @@ -754,22 +759,32 @@ namespace bts { namespace cli {
return !(a.market_index == b.market_index) && !(a.market_index < b.market_index);
});
if (short_wall.state.balance != 0)
{
bids_asks.first.insert(pos, short_wall);
bid_itr = bids_asks.first.begin();
}
}

//bid_itr may be invalidated by now... reset it.
bid_itr = bids_asks.first.begin();

while(bid_itr != bids_asks.first.end() || ask_itr != bids_asks.second.end())
{
if(bid_itr != bids_asks.first.end())
{
bool short_wall = (bid_itr->get_owner() == address());
out << std::left << std::setw(26) << client->get_chain()->to_pretty_asset(bid_itr->get_balance())
<< std::setw(20) << client->get_chain()->to_pretty_asset(bid_itr->get_quantity())
<< std::right << std::setw(30) << (fc::to_string(client->get_chain()->to_pretty_price_double(bid_itr->get_price())) + " " + quote_asset_record->symbol);
bool is_short_order = bid_itr->type == short_order;

if (is_short_order)
{
asset quantity(bid_itr->get_quote_quantity() * (*bid_itr->state.short_price_limit));
out << std::left << std::setw(26) << client->get_chain()->to_pretty_asset(bid_itr->get_quote_quantity())
<< std::setw(20) << client->get_chain()->to_pretty_asset(quantity)
<< std::right << std::setw(30) << (fc::to_string(client->get_chain()->to_pretty_price_double(*bid_itr->state.short_price_limit)) + " " + quote_asset_record->symbol);
} else {
out << std::left << std::setw(26) << client->get_chain()->to_pretty_asset(bid_itr->get_balance())
<< std::setw(20) << client->get_chain()->to_pretty_asset(bid_itr->get_quantity())
<< std::right << std::setw(30) << (fc::to_string(client->get_chain()->to_pretty_price_double(bid_itr->get_price())) + " " + quote_asset_record->symbol);
}

if (short_wall)
if (short_wall || is_short_order)
out << "*";
else
out << " ";
Expand Down
17 changes: 10 additions & 7 deletions libraries/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,9 @@ config load_config( const fc::path& datadir, bool enable_ulog )
fc::thread* _thread;
};

client_impl(bts::client::client* self) :
client_impl(bts::client::client* self, const std::string& user_agent) :
_self(self),
_user_agent(user_agent),
_last_sync_status_message_indicated_in_sync(true),
_last_sync_status_head_block(0),
_remaining_items_to_sync(0),
Expand Down Expand Up @@ -668,7 +669,8 @@ config load_config( const fc::path& datadir, bool enable_ulog )
virtual void error_encountered(const std::string& message, const fc::oexception& error) override;
/// @}

bts::client::client* _self = nullptr;
bts::client::client* _self;
std::string _user_agent;
bts::cli::cli* _cli = nullptr;

#ifndef DISABLE_DELEGATE_NETWORK
Expand Down Expand Up @@ -1617,13 +1619,14 @@ config load_config( const fc::path& datadir, bool enable_ulog )

} // end namespace detail

client::client()
:my( new detail::client_impl(this))
client::client(const std::string& user_agent)
:my(new detail::client_impl(this, user_agent))
{
}

client::client(bts::net::simulated_network_ptr network_to_connect_to)
: my( new detail::client_impl(this) )
client::client(const std::string& user_agent,
bts::net::simulated_network_ptr network_to_connect_to)
: my( new detail::client_impl(this, user_agent) )
{
network_to_connect_to->add_node_delegate(my.get());
my->_p2p_node = network_to_connect_to;
Expand Down Expand Up @@ -1721,7 +1724,7 @@ config load_config( const fc::path& datadir, bool enable_ulog )

//if we are using a simulated network, _p2p_node will already be set by client's constructor
if (!my->_p2p_node)
my->_p2p_node = std::make_shared<bts::net::node>();
my->_p2p_node = std::make_shared<bts::net::node>(my->_user_agent);
my->_p2p_node->set_node_delegate(my.get());

my->start_rebroadcast_pending_loop();
Expand Down
5 changes: 3 additions & 2 deletions libraries/client/include/bts/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,9 @@ namespace bts { namespace client {
public std::enable_shared_from_this<client>
{
public:
client();
client(bts::net::simulated_network_ptr network_to_connect_to);
client(const std::string& user_agent);
client(const std::string& user_agent,
bts::net::simulated_network_ptr network_to_connect_to);

void simulate_disconnect( bool state );

Expand Down
5 changes: 3 additions & 2 deletions libraries/mail/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <bts/mail/server.hpp>
#include <bts/db/level_map.hpp>
#include <bts/db/cached_level_map.hpp>
#include <bts/blockchain/time.hpp>

#include <fc/network/tcp_socket.hpp>
#include <fc/io/buffered_iostream.hpp>
Expand Down Expand Up @@ -278,7 +279,7 @@ class client_impl {
}

//TODO: Contact mail servers, get their PoW requirements, set target to min() of these
email.proof_of_work_target = ripemd160("000000fffdeadbeeffffffffffffffffffffffff");
email.proof_of_work_target = BTS_MAIL_PROOF_OF_WORK_TARGET;
_processing_db.store(message_id, email);

schedule_proof_of_work(message_id);
Expand Down Expand Up @@ -324,7 +325,7 @@ class client_impl {
std::unique_ptr<fc::future<void>> slave_handle_uptr(new fc::future<void>());
fc::future<void>* slave_handle = slave_handle_uptr.get();
while (email->content.id() > email->proof_of_work_target) {
email->content.timestamp = fc::time_point::now();
email->content.timestamp = blockchain::now();
_processing_db.store(email->id, *email);

try {
Expand Down
3 changes: 3 additions & 0 deletions libraries/mail/include/bts/mail/config.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma once

#define BTS_MAIL_INVENTORY_FETCH_LIMIT 4096
#define BTS_MAIL_MAX_MESSAGE_SIZE_BYTES (1024*1024)
#define BTS_MAIL_MAX_MESSAGE_AGE (fc::minutes(5))
#define BTS_MAIL_PROOF_OF_WORK_TARGET (fc::ripemd160("000000fffdeadbeeffffffffffffffffffffffff"))
12 changes: 12 additions & 0 deletions libraries/mail/include/bts/mail/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <fc/exception/exception.hpp>

namespace bts { namespace mail {
FC_DECLARE_EXCEPTION(mail_exception, 70000, "Mail Exception");
FC_DECLARE_DERIVED_EXCEPTION(timestamp_too_old, mail_exception, 70001, "timestamp too old");
FC_DECLARE_DERIVED_EXCEPTION(timestamp_in_future, mail_exception, 70002, "timestamp in future");
FC_DECLARE_DERIVED_EXCEPTION(invalid_proof_of_work, mail_exception, 70003, "invalid proof-of-work");
FC_DECLARE_DERIVED_EXCEPTION(message_too_large, mail_exception, 70004, "message too large");
FC_DECLARE_DERIVED_EXCEPTION(message_already_stored, mail_exception, 70005, "message already stored");
} } // namespace bts::mail
Loading

0 comments on commit 5c9d8b4

Please sign in to comment.