Atomic commit protocol
Atomic commit protocol is a protocol that allows a set of multiple operations to be executed as a single operation. Typical examples include Two-phase commit and Three-phase commit. In our context, it refers to a protocol that allows multiple blockchain transactions to be executed as a single transaction.
Cross Framework currently supports two protocols, Simple commit protocol and Two-phase commit protocol, which can be specified when creating a transaction. In these protocols, there are two roles: the participants, or cohorts, who execute each process specified in the transaction and the coordinator who coordinates the participants to obtain a common decision. Simple protocol is limited to two participants, and one of the participants is the coordinator. On the other hand, Two-phase commit protocol is not limited to participants and can use a coordinator who is not a participant. However, compared to Simple protocol, the number of steps in the flow is larger, and the completion time is longer. Details of each of these methods are described in Two-phase commit protocol and Simple commit protocol. Also, details on transaction creation are described in Cross-chain Transaction.
Two-phase commit protocol
Two-phase commit (2PC) is a commit protocol that executes a two-phase flow of commit request and commit between the coordinator and participants.
The protocol flow is shown in the figure below, where X is the coordinator, A, B, and C are the respective participants, and the arrows indicate the direction of the request for each step of the flow.
Initiate step
MsgInitiateTx
is submitted to the Initiator chain for validation and authentication. Note: This process is common to all commit protocols.- After authentication, the Initiator chain sends a
PacketPrepare
, a packet requestingPrepare
to each participant chain specified in theMsgInitiateTx
as coordinator.
Prepare step
- Each Participant chain executes the contract function specified in
ResolvedContractTransaction
on receiving aPacketPrepare
. - If the execution is successful, a lock is acquired to prevent conflicts between saving changes to the State store and concurrent transactions. This operation is available via State store. Finally, set the
Status
of thePacketPrepareAcknowledgement
toPREPARE_RESULT_OK
indicating success and send it to the coordinator - If the execution fails, the change operation on the State store is discarded, and the
Status
ofPacketPrepareAcknowledgement
is set toPREPARE_RESULT_FAILED
indicating failure and sent to the coordinator.
- Each Participant chain executes the contract function specified in
Confirm step
The Coordinator chain receives the
PacketPrepareAcknowledgement
sent by each Participant chain and performs the following state transitions.(1) Wait for the next acknowledgement to be received
(2) If the
Status
of the received acknowledgement isPREPARE_RESULT_OK
and there is an unreceivedPacketPrepareAcknowledgement
, transit to (1). . If all acknowledgements are received, setCOMMIT
toStatus
ofPacketCommit
to send a commit request to each participant chain, and proceed to the commit step(3) If the
Status
of the received acknowledgement isPREPARE_RESULT_FAILED
, setABORT
to theStatus
of thePacketCommit
to request abort to each participant chain, send it, and proceed to the commit step
Commit step
- When a commit request is received (
Status
ofPacketCommit
isCOMMIT
), each participant chain applies the change operation saved in the Prepare step to the State store, removes the lock, and sends aPacketCommitAcknowledgement
to the coordinator chain indicating that it has been completed - When each participant chain receives an abort request (
Status
ofPacketCommit
isABORT
), it deletes the change operation and locks saved in the prepare step, and sends aPacketCommitAcknowledgement
to the coordinator chain indicating that it has been completed.
- When a commit request is received (
Simple commit protocol
Simple commit protocol is a commit protocol that allows non-coordinator participants to avoid locking during Atomic Commit between two parties. Therefore, in the Simple commit protocol, the Initiator chain also serves both the coordinator and participant roles.
Initiate step
MsgInitiateTx
is submitted to the Initiator chain for validation and authentication. Note: This process is common to all commit protocols.
Prepare step(A)
- Participant A executes the
ContractTransaction
specified inMsgInitiateTx
. - If the execution is successful, a lock is acquired to save the changes to the state store and prevent conflicts with concurrent transactions. After that, it creates a packet
PacketDataCall
containing the information of participant B's contract function call, and sends it to the channel with B. - If the execution fails, the process of this transaction is terminated by abort.
- Participant A executes the
Commit step(B)
- B receives the
PacketDataCall
and executes the specified contract function. - If execution is successful, commit is performed. After that, set
Commit_OK
asStatus
inPacketCallAcknowledgement
and send it to the channel with A. - If the execution fails, abort is performed. Then, set
COMMIT_FAILED
asStatus
inPacketCallAcknowledgement
and send it to the channel with A.
- B receives the
Commit step(A)
- A receives a
PacketCallAcknowledgement
and checks itsStatus
. - If the
Status
isCOMMIT_OK
, A commits the state store change operation saved at Prepare and removes the lock. - If
Status
isCOMMIT_FAILED
, A discards the state store change operation saved at the prepare step and deletes the lock.
- A receives a