raiden-network / raidex

MIT License
9 stars 5 forks source link

[WIP] How the Raidex protocol works #82

Open fredo opened 5 years ago

fredo commented 5 years ago

There is an Introduction about the fair exchange problem #81

Overview

The whole process of a successful exchange is subdivided into different phases. These phases are different in their characterization, importance and finality of the agreed exchange. They require different actions in case of irregularities. The first phase is called matching phase. After two offers are matched together the commitment phase begins. The final phase is called the settlement phase. The phases are separated by certain events within the lifetime of the whole exchange process. After the offer is spawned it will be matched at a certain moment. After the match both parties must agree on the current state and will commit to the agreed exchange. Finally the exchange is settled by transferring the assets to each other. This can happen in multiple ways.

timeline

Roles

Participants

Modules

Assumption

We assume that Maker and Taker have an open channel of each token network that they want to exchange with. Both channels are funded by each participant with the number of token they want to exchange. We assume that maker is going to offer 3 R-Token in exchange for 3 B-Token.

high-level-start-state

Protocol

Event: Offer Spawn

An offer is spawned by the maker. He sends a signed message to the order book. The message also includes a list of one or a set of commitment services he wish to collaborate with. The order book can be a p2p messaging service and built up locally on the client side or even a centralized server which is not decided yet. The CS's order book will see that he is potentially acquired to provide its service to the participants.

Phase: Matching Phase

high-level-matching

After the Offer Spawning the Matching Phase begins. The corresponding CS is also listening to the order book and searches for offer matches. A taker can accept an offer by contacting the CS that it wants to engage in an exchange.

Event: Offer Match

Upon receipt of the taker's message the CS will match maker and taker together. It will then initiate the State Validation & Commitment Phase.

Phase: State Validation & Commitment Phase

high-level-commitment

This phase is guided by the CS in order to guarantee sync between the parties and enforce pre-defined timeouts. These timeouts are defined to reduce the free option problem to a minimum until it is basically non-existent and can be neglected. A short period of time is needed to prepare the commitment message and let the parties sign it. It is actually synchronizing asynchrounous messages. It is important to state out that this phase is the pre-commitment phase. There is no commitment given by either party. But since a match is already found this phase is kind of in the middle of nothing and everything. The CS needs to make sure to make this phase as short as possible and increase the probability of commitment to a maximum. This is achieved by the least amount of communication necessary, well predefined timeouts and even economic incentivization for the parties to aim for commitment. Through a short communication protocol the CS will ask for the participants current states and guarantee the agreement on the current state. It will then prepare a message binding the two transfers together - the so called commitment. After both parties signed the commitment it will finally authenticate the validity of the message and share it amongst the participants. The transaction is now seen as final.

Event: Commitment

Upon receipt of the commitment message the effectiveness of the exchange is given. From this point on the participants have the proof - the multi signed commitment - that the exchange will happen eventually. There is no reason for the participant to not engage in the final settlement on the raiden network since both participants could claim the effectiveness of the trade at the escrow level - that network contracts.

Phase: Settlement Phase

high-level-settlement

The Settlement Phase starts upon receipt of the commitment by the participants. Both parties hold the commitment which enforceable on the blockchain upon channel settlement. Since this is expensive it is more affordable executing a payment within the raiden network. This payment is replacing the actual commitment. To be more precise the given commitment is a mutual promise to make a raiden payment to each other and as a fallback it is enforceable on the blockchain directly. It can be seen as a mutual signed “double balance proof” bound to each other and only valid at the same time. Having this understanding of the commitment message it can be compared with a locked transfer and the secret which is a promise for an unlocked transfer and as a fallback enforceable on the blockchain.

note: The commitment message exchange could even be covered with a HTLC restricting the execution of the promised raiden payment and guaranteeing the receipt of the commitment message by the participants by having a mechanism such as the secret request. This is not to be confused with a raiden payment though.

note: After the successful transmission of the signed commitment message there is no need for the CS to be involved in the settlement process. The settlement can be completely done by the respective raiden clients or raidex client.

Event: Settlement

Upon reaching to this event the exchange process can be seen as completed. The event is either reached by the parties receiving their respective balance proofs of the committed trade through normal raiden payments- the happy case. Or in case of a dispute a channel settlement on chain registering either party can submit the commitment message in the commitment registry on chain and will thus make both transfers enforceable.