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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值