Optimistic Contracts

Posted mutourend

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Optimistic Contracts相关的知识,希望对你有一定的参考价值。

1. 引言

Optimistic Contracts:Allowing for cheaper data submission when validation is expensive。

Optimistic Contracts 与 “bond and slash” 模式结合,具有实用价值。

Optimistic Contracts主要分为2类:

  • Immediate Optimistic:accepts the submission of data immediately, and keeps the stake allowing slashing to happen at any time。
  • Deferred Optimistic:accepts data submissions, there is then a slashing period within which any challenger can try and win the stake. The data is not yet committed. After the challenge period the data can be committed and the stake is returned to the submitter.

相关代码示例有:

2. Immediate Optimistic

Immediate Optimistic:accepts the submission of data immediately, and keeps the stake allowing slashing to happen at any time。

相关代码示例为:【提交即accept到dataStorage】

pragma solidity ^0.5.0;

import "./Optimist.sol";
import "./DataStorageWithRemoval.sol";

contract ImmediateOptimist is Optimist 

    struct Commitment 
        bytes input;
        uint256 key;
    

    DataStorageWithRemoval public dataStorage;

    uint256 public stake;
    uint256 public cooldown;

    Commitment[] public commitments;

    event Committed(address indexed committer, uint256 index);

    /// @param _stake The required stake amount in wei.
    /// @param _cooldown The cooldown time in seconds.
    /// @param _storage The storage contract.
    constructor(uint256 _stake, uint256 _cooldown, DataStorageWithRemoval _storage) public 
        stake = _stake;
        cooldown = _cooldown;
        dataStorage = _storage;
    

    /// @dev This function submits data.
    /// @param input The input data to submit.
    function submit(bytes calldata input) external payable 
        require(msg.value == stake);

        uint256 key = dataStorage.submit(input); //提交即accept。

        commitments.push(
            Commitment(
                input: input,
                key: key
            )
        );

        emit Submitted(msg.sender, (commitments.length - 1), input);
    

    /// @dev This function challenges a submission by calling the validation function.
    /// @param id The id of the submission to challenge.
    function challenge(uint256 id) external 
        Commitment storage commitment = commitments[id];

        require(commitment.key != 0);

        require(!dataStorage.isValid(commitment.input));

        uint256 key = commitment.key;
        delete commitments[id];

        dataStorage.remove(key);

        msg.sender.transfer(stake);

        emit Challenged(msg.sender, id);
    

3. Deferred Optimistic

Deferred Optimistic:accepts data submissions, there is then a slashing period within which any challenger can try and win the stake. The data is not yet committed. After the challenge period the data can be committed and the stake is returned to the submitter.

相关代码示例为:【过了slash period才accept到dataStorage】

pragma solidity ^0.5.0;

import "./Optimist.sol";
import "./DataStorage.sol";

contract DeferredOptimist is Optimist 

    struct Commitment 
        bytes input;
        uint256 submitted;
        address payable submitter;
    

    DataStorage public dataStorage;

    uint256 public stake;
    uint256 public cooldown;

    Commitment[] public commitments;

    event Committed(address indexed committer, uint256 index);

    /// @param _stake The required stake amount in wei.
    /// @param _cooldown The cooldown time in seconds.
    /// @param _storage The storage contract.
    constructor(uint256 _stake, uint256 _cooldown, DataStorage _storage) public 
        stake = _stake;
        cooldown = _cooldown;
        dataStorage = _storage;
    

    /// @dev This function submits data, starting the challenge period.
    /// @param input The input data to submit.
    function submit(bytes calldata input) external payable 
        require(msg.value == stake);

        commitments.push(
            Commitment(
                input: input,
                submitted: now,
                submitter: msg.sender
            )
        );

        emit Submitted(msg.sender, (commitments.length - 1), input);
    

    /// @dev This function challenges a submission by calling the validation function.
    /// @param id The id of the submission to challenge.
    function challenge(uint256 id) external 
        Commitment storage commitment = commitments[id];

        require(commitment.submitter != address(0x0));
        require(commitment.submitted + cooldown >= now);

        require(!dataStorage.isValid(commitment.input));

        delete commitments[id];
        msg.sender.transfer(stake);

        emit Challenged(msg.sender, id);
    

    /// @dev This function finalizes a submission by adding it to the storage.
    /// @param id The id of the submission to finalize.
    function commit(uint256 id) external 
        Commitment storage commitment = commitments[id];

        require(commitment.submitter != address(0x0));
        require(commitment.submitted + cooldown < now);

        dataStorage.submit(commitment.input); //过了slash period才accept。

        address payable submitter = commitment.submitter;
        delete commitments[id];
        submitter.transfer(stake);

        emit Committed(msg.sender, id);
    

参考资料

[1] Optimistic Contracts

以上是关于Optimistic Contracts的主要内容,如果未能解决你的问题,请参考以下文章

Pessimistic and Optimistic locking

Pessimistic and optimistic locking

Optimistic Locking(乐观锁)

从 Hibernate Optimistic Locking 异常中恢复

Hibernate 乐观锁(Optimistic Locking)

Pessimistic and Optimistic locking