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 #70 from vikramrajkumar/btsx
Browse files Browse the repository at this point in the history
Merge upstream
  • Loading branch information
bitsha256 committed Sep 26, 2014
2 parents cad6368 + 6c852ad commit 3d15bdd
Show file tree
Hide file tree
Showing 66 changed files with 1,545 additions and 1,032 deletions.
5 changes: 5 additions & 0 deletions libraries/api/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@
"type_name" : "order_id",
"cpp_return_type" : "bts::blockchain::order_id_type"
},
{
"type_name" : "order_ids",
"container_type" : "array",
"contained_type" : "order_id"
},
{
"type_name" : "market_order_map",
"cpp_return_type" : "std::map<bts::blockchain::order_id_type, bts::blockchain::market_order>"
Expand Down
37 changes: 36 additions & 1 deletion libraries/api/wallet_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,27 @@
"prerequisites" : ["wallet_unlocked"],
"aliases" : ["scan_transaction", "wallet_transaction_scan"]
},
{
"method_name": "wallet_scan_transaction_experimental",
"description": "Scans the specified transaction",
"return_type": "void",
"parameters" :
[
{
"name" : "transaction_id",
"type" : "string",
"description" : "the id (or id prefix) of the transaction"
},
{
"name" : "overwrite_existing",
"type" : "bool",
"description" : "true to overwrite existing wallet transaction record and false otherwise",
"default_value": false
}
],
"prerequisites" : ["wallet_unlocked"],
"aliases" : ["scanx", "st"]
},
{
"method_name": "wallet_rebroadcast_transaction",
"description": "Rebroadcasts the specified transaction",
Expand Down Expand Up @@ -1328,7 +1349,7 @@
},
{
"method_name" : "wallet_market_cancel_order",
"description" : "Cancel an order",
"description" : "Cancel an order: deprecated - use wallet_market_cancel_orders",
"return_type" : "transaction_record",
"parameters" : [
{
Expand All @@ -1340,6 +1361,20 @@
"prerequisites" : ["wallet_unlocked"],
"aliases" : []
},
{
"method_name" : "wallet_market_cancel_orders",
"description" : "Cancel more than one order at a time",
"return_type" : "transaction_record",
"parameters" : [
{
"name" : "order_ids",
"type" : "order_ids",
"description" : "the IDs of the orders to cancel"
}
],
"prerequisites" : ["wallet_unlocked"],
"aliases" : []
},
{
"method_name" : "wallet_dump_private_key",
"description" : "Reveals the private key corresponding to an account, public key, or address",
Expand Down
28 changes: 14 additions & 14 deletions libraries/blockchain/chain_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ namespace bts { namespace blockchain {
rebuild_index = true;
}
self->set_property( chain_property_enum::database_version, BTS_BLOCKCHAIN_DATABASE_VERSION );
self->set_property( chain_property_enum::dirty_markets, variant( map<asset_id_type,asset_id_type>() ) );
}
else if( database_version && !database_version->is_null() && database_version->as_int64() > BTS_BLOCKCHAIN_DATABASE_VERSION )
{
Expand Down Expand Up @@ -769,18 +768,15 @@ namespace bts { namespace blockchain {
{ try {
vector<market_transaction> market_transactions;

const auto dirty_markets = pending_state->get_dirty_markets();
//dlog( "execute markets ${e}", ("e",dirty_markets) );

const auto pending_block_num = pending_state->get_head_block_num();

if( pending_block_num == BTSX_MARKET_FORK_8_BLOCK_NUM )
{
market_engine_v4 engine( pending_state, *this );
engine.cancel_all_shorts( self->get_block_header( BTSX_MARKET_FORK_7_BLOCK_NUM ).timestamp );
market_transactions.insert( market_transactions.end(), engine._market_transactions.begin(), engine._market_transactions.end() );
}

const auto dirty_markets = self->get_dirty_markets();
for( const auto& market_pair : dirty_markets )
{
FC_ASSERT( market_pair.first > market_pair.second );
Expand Down Expand Up @@ -823,7 +819,6 @@ namespace bts { namespace blockchain {
market_transactions.insert( market_transactions.end(), engine._market_transactions.begin(), engine._market_transactions.end() );
}
}
//dlog( "market trxs: ${trx}", ("trx", fc::json::to_pretty_string( market_transactions ) ) );

if( pending_block_num < BTSX_MARKET_FORK_2_BLOCK_NUM )
pending_state->set_dirty_markets( pending_state->_dirty_markets );
Expand Down Expand Up @@ -2562,18 +2557,23 @@ namespace bts { namespace blockchain {
const string& base_symbol,
uint32_t limit )
{ try {
auto quote_asset_id = get_asset_id( quote_symbol );
auto base_asset_id = get_asset_id( base_symbol );
if( base_asset_id >= quote_asset_id )
FC_CAPTURE_AND_THROW( invalid_market, (quote_asset_id)(base_asset_id) );
auto quote_id = get_asset_id( quote_symbol );
auto base_id = get_asset_id( base_symbol );
if( base_id >= quote_id )
FC_CAPTURE_AND_THROW( invalid_market, (quote_id)(base_id) );

vector<market_order> results;
auto market_itr = my->_bid_db.lower_bound( market_index_key( price( 0, quote_asset_id, base_asset_id ) ) );
//We dance around like this because the _bid_db sorts the bids backwards, so we must iterate it backwards.
const price next_pair = (base_id+1 == quote_id) ? price( 0, quote_id+1, 0 ) : price( 0, quote_id, base_id+1 );
auto market_itr = my->_bid_db.lower_bound( market_index_key( next_pair ) );
if( market_itr.valid() ) --market_itr;
else market_itr = my->_bid_db.last();

while( market_itr.valid() )
{
auto key = market_itr.key();
if( key.order_price.quote_asset_id == quote_asset_id &&
key.order_price.base_asset_id == base_asset_id )
if( key.order_price.quote_asset_id == quote_id &&
key.order_price.base_asset_id == base_id )
{
results.push_back( {bid_order, key, market_itr.value()} );
}
Expand All @@ -2583,7 +2583,7 @@ namespace bts { namespace blockchain {
if( results.size() == limit )
return results;

++market_itr;
--market_itr;
}
return results;
} FC_CAPTURE_AND_RETHROW( (quote_symbol)(base_symbol)(limit) ) }
Expand Down
48 changes: 39 additions & 9 deletions libraries/blockchain/chain_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,34 @@ namespace bts{ namespace blockchain {

} FC_CAPTURE_AND_RETHROW( (price_to_pretty_print) ) }

asset chain_interface::to_ugly_asset(const std::string& amount, const std::string& symbol) const
{ try {
auto record = get_asset_record( symbol );
if( !record ) FC_CAPTURE_AND_THROW( unknown_asset_symbol, (symbol) );

auto decimal = amount.find(".");
if( decimal == string::npos )
return asset(atoll(amount.c_str()) * record->precision, record->id);

share_type whole = atoll(amount.substr(0, decimal).c_str()) * record->precision;
string fraction_string = amount.substr(decimal+1);
share_type fraction = atoll(fraction_string.c_str());

if( fraction_string.empty() || fraction <= 0 )
return asset(whole, record->id);

while( fraction < record->precision )
fraction *= 10;
while( fraction > record->precision )
fraction /= 10;
while( fraction_string.size() && fraction_string[0] == '0')
{
fraction /= 10;
fraction_string.erase(0, 1);
}
return asset(whole > 0? whole + fraction : whole - fraction, record->id);
} FC_CAPTURE_AND_RETHROW( (amount)(symbol) ) }

string chain_interface::to_pretty_asset( const asset& a )const
{
const auto oasset = get_asset_record( a.asset_id );
Expand Down Expand Up @@ -190,19 +218,21 @@ namespace bts{ namespace blockchain {
return base_record->collected_fees / (BTS_BLOCKCHAIN_BLOCKS_PER_DAY * 14);
}

map<asset_id_type, asset_id_type> chain_interface::get_dirty_markets()const
void chain_interface::set_dirty_markets( const std::set<std::pair<asset_id_type, asset_id_type>>& d )
{
try{
return get_property( dirty_markets ).as<map<asset_id_type,asset_id_type> >();
} catch ( ... )
{
return map<asset_id_type,asset_id_type>();
}
set_property( dirty_markets, fc::variant( d ) );
}

void chain_interface::set_dirty_markets( const map<asset_id_type,asset_id_type>& d )
std::set<std::pair<asset_id_type, asset_id_type>> chain_interface::get_dirty_markets()const
{
set_property( dirty_markets, fc::variant(d) );
try
{
return get_property( dirty_markets ).as<std::set<std::pair<asset_id_type, asset_id_type>>>();
}
catch( ... )
{
}
return std::set<std::pair<asset_id_type, asset_id_type>>();
}

} } // bts::blockchain
22 changes: 11 additions & 11 deletions libraries/blockchain/include/bts/blockchain/asset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace bts { namespace blockchain {
/**
* An asset is a fixed point number with
* 64.64 bit precision. This is used
* for accumalating dividends and
* for accumalating dividends and
* calculating prices on the built-in exchange.
*/
struct asset
Expand All @@ -34,11 +34,11 @@ namespace bts { namespace blockchain {
asset operator-()const { return asset( -amount, asset_id); }

operator std::string()const;

share_type amount;
asset_id_type asset_id;
};

/**
* A price is the result of dividing 2 asset classes and has
* the fixed point format 64.64 and -1 equals infinite.
Expand All @@ -61,7 +61,7 @@ namespace bts { namespace blockchain {

fc::uint128_t ratio; // 64.64

std::pair<asset_id_type,asset_id_type> asset_pair()const { return std::make_pair(base_asset_id,quote_asset_id); }
std::pair<asset_id_type,asset_id_type> asset_pair()const { return std::make_pair( quote_asset_id, base_asset_id ); }

asset_id_type base_asset_id;
asset_id_type quote_asset_id;
Expand All @@ -79,21 +79,21 @@ namespace bts { namespace blockchain {

inline bool operator == ( const price& l, const price& r ) { return l.ratio == r.ratio; }
inline bool operator != ( const price& l, const price& r ) { return l.ratio == r.ratio; }
inline bool operator < ( const price& l, const price& r )
{
inline bool operator < ( const price& l, const price& r )
{
if( l.quote_asset_id < r.quote_asset_id ) return true;
if( l.quote_asset_id > r.quote_asset_id ) return false;
if( l.base_asset_id < r.base_asset_id ) return true;
if( l.base_asset_id > r.base_asset_id ) return false;
return l.ratio < r.ratio;
return l.ratio < r.ratio;
}
inline bool operator > ( const price& l, const price& r )
{
inline bool operator > ( const price& l, const price& r )
{
if( l.quote_asset_id > r.quote_asset_id ) return true;
if( l.quote_asset_id < r.quote_asset_id ) return false;
if( l.base_asset_id > r.base_asset_id ) return true;
if( l.base_asset_id < r.base_asset_id ) return false;
return l.ratio > r.ratio;
return l.ratio > r.ratio;
}
inline bool operator <= ( const price& l, const price& r ) { return l.ratio <= r.ratio && l.asset_pair() == r.asset_pair(); }
inline bool operator >= ( const price& l, const price& r ) { return l.ratio >= r.ratio && l.asset_pair() == r.asset_pair(); }
Expand All @@ -114,7 +114,7 @@ namespace bts { namespace blockchain {
* could be exchanged at price p.
*
* ie: p = 3 usd/bts & a = 4 bts then result = 12 usd
* ie: p = 3 usd/bts & a = 4 usd then result = 1.333 bts
* ie: p = 3 usd/bts & a = 4 usd then result = 1.333 bts
*/
asset operator * ( const asset& a, const price& p );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ namespace bts { namespace blockchain {
* This method is called anytime a block is applied to the chain.
*/
virtual void block_applied( const block_summary& summary ) = 0;
virtual void on_pending_transaction( const transaction_evaluation_state_ptr& ) {}
};

class chain_database : public chain_interface, public std::enable_shared_from_this<chain_database>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ namespace bts { namespace blockchain {
virtual bool is_valid_symbol_name( const string& name ) const;
virtual bool is_valid_account_name( const string& name ) const;

/** convers an asset + asset_id to a more friendly representation using the symbol name */
/** converts an asset + asset_id to a more friendly representation using the symbol name */
string to_pretty_asset( const asset& a )const;
double to_pretty_price_double( const price& a )const;
string to_pretty_price( const price& a )const;
/** converts a numeric string + asset symbol to an asset */
asset to_ugly_asset( const string& amount, const string& symbol )const;

virtual void store_burn_record( const burn_record& br ) = 0;
virtual oburn_record fetch_burn_record( const burn_record_key& key )const = 0;
Expand Down Expand Up @@ -167,8 +169,8 @@ namespace bts { namespace blockchain {
const market_history_record& record ) = 0;
virtual omarket_history_record get_market_history_record( const market_history_key& key )const = 0;

virtual map<asset_id_type, asset_id_type> get_dirty_markets()const;
virtual void set_dirty_markets( const map<asset_id_type,asset_id_type>& );
virtual void set_dirty_markets( const std::set<std::pair<asset_id_type, asset_id_type>>& );
virtual std::set<std::pair<asset_id_type, asset_id_type>> get_dirty_markets()const;

virtual void set_market_transactions( vector<market_transaction> trxs ) = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ const static std::map<uint32_t, bts::blockchain::block_id_type> CHECKPOINT_BLOCK
{ 300000, bts::blockchain::block_id_type( "0d1d0b8f7f1f4590f8e083edc03869383ed74e3e" ) },
{ 400000, bts::blockchain::block_id_type( "053d398b6597d5c61365afd100d87b824bf49f65" ) },
{ 500000, bts::blockchain::block_id_type( "f02910a7115fb826984ce3a432cb371d5d7a99b8" ) },
{ 555000, bts::blockchain::block_id_type( "2ea2e95ed68c92a823341ad0b116d97c1bc0addd" ) }
{ 587000, bts::blockchain::block_id_type( "eaa291825e59712deef2d3b962067ba2b7e0708c" ) }
};
3 changes: 2 additions & 1 deletion libraries/blockchain/include/bts/blockchain/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

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

#define BTS_TEST_NETWORK_VERSION 29

/** @file bts/blockchain/config.hpp
* @brief Defines global constants that determine blockchain behavior
*/
#define BTS_BLOCKCHAIN_VERSION 109
#define BTS_BLOCKCHAIN_DATABASE_VERSION 140
#define BTS_BLOCKCHAIN_DATABASE_VERSION 142

/**
* The address prepended to string representation of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define BTSX_MARKET_FORK_6_BLOCK_NUM 451500
#define BTSX_MARKET_FORK_7_BLOCK_NUM 554800
#define BTSX_MARKET_FORK_8_BLOCK_NUM 578900
#define BTSX_MARKET_FORK_9_BLOCK_NUM 613200

#define BTSX_YIELD_FORK_1_BLOCK_NUM BTSX_MARKET_FORK_6_BLOCK_NUM
#define BTSX_YIELD_FORK_2_BLOCK_NUM 494000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,7 @@ namespace bts { namespace blockchain {
map<feed_index, feed_record> feeds;
map<burn_record_key,burn_record_value> burns;

/**
* Set of markets that have had changes to their bids/asks and therefore must
* be executed map<QUOTE,BASE>
*/
map<asset_id_type, asset_id_type> _dirty_markets;
std::set<std::pair<asset_id_type, asset_id_type>> _dirty_markets;

chain_interface_weak_ptr _prev_state;
};
Expand Down
Loading

0 comments on commit 3d15bdd

Please sign in to comment.