0

Are there some patterns or some way to implement 2 phase commit with solidity?

Something similar to multi signatures and a fail safe mechanism.

0

This is just a quick-and-dirty proof of concept code withouth any specific recovery mechanism. The contract acts as the coordinator of the two-phase commit protocol, where users can signal their consent by calling the commitRequest() function with a true flag. If no user voted for abort then the contract can commit, otherwise abort. Hope this helps!

contract Coordinator {
  uint256 public endOfCommitRequest;

  uint256 public commitCounter;
  uint256 public abortCounter;

  constructor(
    uint256 _endOfCommitRequest
  ) public {
   endOfCommitRequest = _endOfCommitRequest;
  }

  function commitRequest(bool agreement) public onlyBeforeEndOfCommitRequest {
    if(agreement) {
        commitCounter++;
    } else {
        abortCounter++;
    }  
  }

  function commit() public onlyAfterCommitPhase {
    if(abortCounter == 0) {
        // commit
    }  else {
        // abort 
    }
  }

  modifier onlyBeforeEndOfCommitRequest() {
    require(now < endOfCommitRequest);  
    _;  
  }

  modifier onlyAfterCommitPhase() {
    require(now > endOfCommitRequest);  
    _;  
  }
}

Would be interesting to hear what kind of fail safe mechanism you have in mind! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.