> For the complete documentation index, see [llms.txt](https://funarchy.gitbook.io/funarchy/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://funarchy.gitbook.io/funarchy/security-for-prediction-market/trading-mechanism/clob-risks/matching-engine-logic-errors.md).

# Matching Engine Logic Errors

#### Description

CLOB's core principles are Price Priority and Time Priority, which are essential to maintaining fairness in trading and reliability in prediction market price formation.

* Price Priority:
  * It should be implemented so that orders are executed starting with the buyer's 'highest' price.
  * It should be implemented so that orders are executed starting with the seller offering the 'lowest' price.
* Time Priority:
  * If there are multiple orders at the same price, the order should be executed in the order in which the orders were submitted (FIFO).

If the matching engine's logic fails to properly adhere to these rules, user-submitted orders may not be executed in the correct order, resulting in anomalies such as price distortion, queue delays, and improper execution.

In particular, probability-based price formation (probability pricing) in prediction markets is sensitive to even slight price differences, so even small logic errors can lead to artificial price distortion, loss of expected profits, and a decline in market information reliability.

#### CASE 1: Attack Scenario - Violation of the price priority principle

This case occurs due to a code flaw in the platform (reverse processing, processing immediately after an order is received, etc.).

<figure><img src="https://4210179539-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2DiVEbUgCTsp2iPassR9%2Fuploads%2FQigIricxWAvEj8WuSoZo%2Fimage.png?alt=media&amp;token=0ad681e0-855d-4cf9-94a7-17eddb324c3a" alt=""><figcaption></figcaption></figure>

{% stepper %}
{% step %}
User A submits a buy order at a higher price.\
And User B submits a buy order at a lower price.
{% endstep %}

{% step %}
If the operation is normal, A's order should be executed first due to price priority.
{% endstep %}

{% step %}
However, due to a logic error in the matching engine, the system executes B's lower price order first.
{% endstep %}

{% step %}
Even though A offered a higher price, the conclusion is delayed, which results in damaging the user's expected conclusion position (matching fairness).
{% endstep %}
{% endstepper %}

#### CASE 2: Attack Scenario - Violation of the time priority principle

This case occurs due to a code flaw that violates FIFO (First-In, First-Out).

<figure><img src="https://4210179539-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F2DiVEbUgCTsp2iPassR9%2Fuploads%2FLba9rQjKCKTosFNxkkNs%2Fimage.png?alt=media&amp;token=11e5b2ec-8dc0-418a-ab3e-c5650d5f59ad" alt=""><figcaption></figcaption></figure>

{% stepper %}
{% step %}
User A first submits a buy order at a specific price. \
User B then submits an order at the same price.
{% endstep %}

{% step %}
If it were normal operation, A's order should be filled first, but due to an error in the sorting algorithm within the matching engine (e.g. confusion between <= and <), B, which came in later, is recognized as the priority.
{% endstep %}

{% step %}
When a sell order at that price comes in, the system violates the time priority principle and skips A, which was waiting first, and executes B's order.
{% endstep %}

{% step %}
User A, who placed the order first, ends up waiting without receiving the quantity, and in the meantime, the market price rises, causing him to lose opportunity cost.
{% endstep %}

{% step %}
A price reversal occurs when an order with a lower price is executed first even though there is an order with a higher price, which distorts the market price.
{% endstep %}
{% endstepper %}

#### Mitigation

* **Use of strict FIFO data structure**
  * Within the same price range, a queue structure must be used to enforce first-in-first-out (FIFO) and last-in-first-out (LIFO) processing must be prevented at the data structure level.
* **Invariant verification after matching**
  * At the end of the transaction, you must include assert logic to check whether the Best Bid < Best Ask state (no crossed orders) is maintained and whether the executed price was the best price on the order book at that time.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://funarchy.gitbook.io/funarchy/security-for-prediction-market/trading-mechanism/clob-risks/matching-engine-logic-errors.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
