bitshares / bitshares1-core

Software to run the old chain (before 2015-10-13). Code for current chain is https://github.com/bitshares/bitshares-core
https://bitshares.org/
The Unlicense
219 stars 174 forks source link

Possible refactoring of market engine #1368

Closed theoreticalbts closed 9 years ago

theoreticalbts commented 9 years ago

The main purpose of this ticket is to provide a place to carefully evaluate the costs and benefits before deciding whether to move forward with a refactoring of the market engine for 0.8.0.

The details of the proposed refactoring are documented here: https://github.com/theoreticalbts/bitshares/blob/market_refactor/docs/market-engine-0.8.0.md

The document will be of some use even if we do not proceed with the refactoring, as it establishes some useful terminology.

theoreticalbts commented 9 years ago

This post summarizes the reasons why I feel the refactoring is necessary.

The current code suffers from serious deficiencies. The biggest one is that the code dealing with feed-dependent nature of short orders will need to be re-written. As @vikramrajkumar cogently pointed out in #1370 the develop branch's present implementation does not use pending_chain_state and is therefore not undoable. Lack of undo semantics will likely result in severe network problems and failure to reach consensus.

Margin orders also need split into two types based on expiration / cover (or actually three types, if you consider inactive orders to be a type).

Since we're doing the splitting logic for two order types anyway, and we have to rewrite most of it anyway, we might as well make a general framework for splitting orders which can be adapted to relative orders as well; see #1342.

The problems are so severe that I considered recommending backing out the market engine changes and moving relative orders out of 1.0. Unfortunately, the bug that started all this -- #1315 -- is indisputably alive and well on the master branch.

So given that we must rewrite substantial parts of the market engine anyway, we may as well do it right and make a clean implementation with conceptual clarity and explicitly documented design.

Since I've done substantial thinking about the issues (see link in OP), I think I'm the best person to claim this task. I'd like to start the rewrite immediately, and in the interests of avoiding wasted or duplicated effort, I'd like to warn everyone (including @bytemaster) to hold off on making any changes to the market engine code, as further patches will likely need to be tossed or heavily modified after the refactoring.

I should have a working (or at least compiling) version of the refactored market engine (hopefully ~48 hours).

theoreticalbts commented 9 years ago

Here's a list of problems I've spotted which may arise from the architecture, based on a brief read-through of the code:

arhag commented 9 years ago

So given that we must rewrite substantial parts of the market engine anyway, we may as well do it right and make a clean implementation with conceptual clarity and explicitly documented design.

+5%

I should have a working (or at least compiling) version of the refactored market engine (hopefully ~48 hours).

Looks way too optimistic but I wish you luck!

Other things to say after reading your document:

vikramrajkumar commented 9 years ago

@theoreticalbts You can refer to account_db_interface::store and account_db_interface::remove for an example of how to ensure index consistency through arbitrary state transitions and rewinds. account_records are currently our most complex record type in terms of additional indices.

bytemaster commented 9 years ago

While I agree with the need to refactor, I disagree with doing it at this point in time. A refactor will easily take a month (not 48 hours).

The refactor needs to factor in far more than what has even been discussed in this ticket.

theoreticalbts commented 9 years ago

A refactor will easily take a month (not 48 hours).

We'll see about that :)

But if I don't have it finished (at least roughly) by tomorrow -- the big question becomes, what are we going to do?

The alternative is to launch our shiny new 1.0 release with the current market engine in master, which we know doesn't work correctly due to fundamental architectural issues ( #1315 ).

We cannot use the relative orders implementation currently in the develop branch because it has a serious risk of forking the network (since it doesn't use pending_chain_state). We could pour substantial development effort into making it work "well enough" with the current engine, but the value of that is questionable for two reasons:

The refactor needs to factor in far more than what has even been discussed in this ticket.

Please be specific!

vikramrajkumar commented 9 years ago

@theoreticalbts Please see my revised comments on the pending state issue here: https://github.com/BitShares/bitshares/issues/1370#issuecomment-73932688

theoreticalbts commented 9 years ago

After much internal discussion, we've finally decided that the solution is that, in the interests of stability and getting 1.0 out the door, relative orders must go. Anything that can be done with relative orders can be done better with a trading bot script, because relative orders by definition are limited to moving at the same speed as feed updates.