<commit_before>#include <bts/blockchain/market_engine.hpp>
#include <fc/real128.hpp>

namespace bts { namespace blockchain { namespace detail {

  market_engine::market_engine( pending_chain_state_ptr ps, chain_database_impl& cdi )
  :_pending_state(ps),_db_impl(cdi)
  {
      _pending_state = std::make_shared<pending_chain_state>( ps );
      _prior_state = ps;
  }

  void market_engine::cancel_all_shorts()
  {
      for( auto short_itr = _db_impl._short_db.begin(); short_itr.valid(); ++short_itr )
      {
          const market_index_key market_idx = short_itr.key();
          const order_record order_rec = short_itr.value();
          _current_bid = market_order( short_order, market_idx, order_rec );

          // Initialize the market transaction
          market_transaction mtrx;
          mtrx.bid_owner = _current_bid->get_owner();
          mtrx.bid_type = short_order;

          cancel_current_short( mtrx, market_idx.order_price.quote_asset_id );
          push_market_transaction( mtrx );
      }

      _pending_state->apply_changes();
  }

  bool market_engine::execute( asset_id_type quote_id, asset_id_type base_id, const fc::time_point_sec& timestamp )
  {
      try
      {
          _quote_id = quote_id;
          _base_id = base_id;

          oasset_record quote_asset = _pending_state->get_asset_record( _quote_id );
          oasset_record base_asset = _pending_state->get_asset_record( _base_id );
          FC_ASSERT( quote_asset.valid() && base_asset.valid() );

          // The order book is sorted from low to high price. So to get the last item (highest bid),
          // we need to go to the first item in the next market class and then back up one
          const price next_pair = (base_id+1 == quote_id) ? price( 0, quote_id+1, 0 ) : price( 0, quote_id, base_id+1 );
          _bid_itr        = _db_impl._bid_db.lower_bound( market_index_key( next_pair ) );
          _ask_itr        = _db_impl._ask_db.lower_bound( market_index_key( price( 0, quote_id, base_id) ) );
          _short_itr      = _db_impl._short_db.lower_bound( market_index_key( next_pair ) );
          _collateral_itr = _db_impl._collateral_db.lower_bound( market_index_key( next_pair ) );

          int last_orders_filled = -1;
          asset trading_volume(0, base_id);
          price opening_price, closing_price;

          if( !_ask_itr.valid() )
          {
            wlog( "ask iter invalid..." );
            _ask_itr = _db_impl._ask_db.begin();
          }

          if( _bid_itr.valid() )   --_bid_itr;
          else _bid_itr = _db_impl._bid_db.last();

          if( _collateral_itr.valid() )   --_collateral_itr;
          else _collateral_itr = _db_impl._collateral_db.last();

          if( _short_itr.valid() )   --_short_itr;
          else _short_itr = _db_impl._short_db.last();

          _feed_price = _db_impl.self->get_median_delegate_price( _quote_id, _base_id );
          // Market issued assets cannot match until the first time there is a median feed
          if( quote_asset->is_market_issued() )
          {
              const omarket_status market_stat = _pending_state->get_market_status( _quote_id, _base_id );
              if( !base_asset->is_market_issued() && (!market_stat.valid() || !market_stat->last_valid_feed_price.valid()) && !_feed_price.valid() )
                  FC_CAPTURE_AND_THROW( insufficient_feeds, (quote_id)(base_id) );
          }<commit_msg>Add merge warning<commit_after>#include <bts/blockchain/market_engine.hpp>
#include <fc/real128.hpp>

namespace bts { namespace blockchain { namespace detail {

  market_engine::market_engine( pending_chain_state_ptr ps, chain_database_impl& cdi )
  :_pending_state(ps),_db_impl(cdi)
  {
      _pending_state = std::make_shared<pending_chain_state>( ps );
      _prior_state = ps;
  }

  void market_engine::cancel_all_shorts()
  {
      for( auto short_itr = _db_impl._short_db.begin(); short_itr.valid(); ++short_itr )
      {
          const market_index_key market_idx = short_itr.key();
          const order_record order_rec = short_itr.value();
          _current_bid = market_order( short_order, market_idx, order_rec );

          // Initialize the market transaction
          market_transaction mtrx;
          mtrx.bid_owner = _current_bid->get_owner();
          mtrx.bid_type = short_order;

          cancel_current_short( mtrx, market_idx.order_price.quote_asset_id );
          push_market_transaction( mtrx );
      }

      _pending_state->apply_changes();
  }

  bool market_engine::execute( asset_id_type quote_id, asset_id_type base_id, const fc::time_point_sec& timestamp )
  {
      try
      {
          _quote_id = quote_id;
          _base_id = base_id;

          oasset_record quote_asset = _pending_state->get_asset_record( _quote_id );
          oasset_record base_asset = _pending_state->get_asset_record( _base_id );
          FC_ASSERT( quote_asset.valid() && base_asset.valid() );

          // The order book is sorted from low to high price. So to get the last item (highest bid),
          // we need to go to the first item in the next market class and then back up one
          const price next_pair = (base_id+1 == quote_id) ? price( 0, quote_id+1, 0 ) : price( 0, quote_id, base_id+1 );
          _bid_itr        = _db_impl._bid_db.lower_bound( market_index_key( next_pair ) );
          _ask_itr        = _db_impl._ask_db.lower_bound( market_index_key( price( 0, quote_id, base_id) ) );
          _short_itr      = _db_impl._short_db.lower_bound( market_index_key( next_pair ) );
          _collateral_itr = _db_impl._collateral_db.lower_bound( market_index_key( next_pair ) );

          int last_orders_filled = -1;
          asset trading_volume(0, base_id);
          price opening_price, closing_price;

          if( !_ask_itr.valid() )
          {
            wlog( "ask iter invalid..." );
            _ask_itr = _db_impl._ask_db.begin();
          }

          if( _bid_itr.valid() )   --_bid_itr;
          else _bid_itr = _db_impl._bid_db.last();

          if( _collateral_itr.valid() )   --_collateral_itr;
          else _collateral_itr = _db_impl._collateral_db.last();

          if( _short_itr.valid() )   --_short_itr;
          else _short_itr = _db_impl._short_db.last();

          _feed_price = _db_impl.self->get_median_delegate_price( _quote_id, _base_id );
          // Market issued assets cannot match until the first time there is a median feed
#ifndef WIN32
#warning [BTS] Possible market hardfork
#endif
          //if( quote_asset->is_market_issued() )
          if( quote_asset->is_market_issued() && !base_asset->is_market_issued() )
          {
              const omarket_status market_stat = _pending_state->get_market_status( _quote_id, _base_id );
              if( (!market_stat.valid() || !market_stat->last_valid_feed_price.valid()) && !_feed_price.valid() )
                  FC_CAPTURE_AND_THROW( insufficient_feeds, (quote_id)(base_id) );
          }
{'fraction_non_alphanumeric': 0.10012929176842408, 'fraction_numerical': 0.0037350955322511133, 'mean_word_length': 2.7170315002669514, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '657150', 'n_tokens_mistral': 2286, 'n_tokens_neox': 2260, 'n_words': 454}