지능 계약 설계 모델 - COP

Design by contract
solidity 라 는 언어 디자인 방향 은 무엇 입 니까?COP 가 뭐 예요?
COP
조건 부 프로 그래 밍 (COP) 은 계약 프로 그래 밍 을 위 한 하위 영역 으로 함수 와 명령 식 프로 그래 밍 을 위 한 혼합 모드 입 니 다.COP 는 이 문 제 를 해결 하고 프로그래머 가 모든 조건 을 표시 해 야 한다.논리 가 납작 해 지고 조건 없 는 상태 변화.조건 부 세 션 은 정확 한 문서 화, 재 활용 이 가능 하 며 수요 와 실현 에 따라 추정 할 수 있 습 니 다.중요 한 것 은 COP 가 프로 그래 밍 에서 사전 조건 을 1 등 시민 으로 간주 하 는 것 이다.이런 모델 규범 은 계약 의 안전 을 보장 할 수 있다.
post-condition
contract PostCheck {

    uint public data = 0;

    // Check that the 'data' field was set to the value of '_data'.
    modifier data_is_valid(uint _data) {
        _
        if (_data != data)
            throw;
    }

    function setData(uint _data) data_is_valid(_data) {
        data = _data;
    }

}

pre- and post-conditions
"_"
contract PrePostCheck {

    uint public data = 0;

    // Check that the input '_data' value is not the same as the value
    // already stored in 'data'.
    modifier data_is_valid(uint _data) {
        if (_data == data)
           throw;
        _
    }

    // Check that the 'data' field was set to the value of '_data'.
    modifier data_was_updated(uint _data) {
        _
        if (_data != data)
            throw;
    }

    function setData(uint _data) data_is_valid(_data) data_was_updated(_data) {
        data = _data;
    }

}

FEATURES
  • 함수 주체 무조건 판단
  • 예:
    contract Token {
        // The balance of everyone
        mapping (address => uint) public balances;
        // Constructor - we're a millionaire!
        function Token() {
            balances[msg.sender] = 1000000;
        }
        // Transfer `_amount` tokens of ours to `_dest`.
        function transfer(uint _amount, address _dest) {
            balances[msg.sender] -= _amount;
            balances[_dest] += _amount;
        }
    }
    

    개선 후:
    function transfer(uint _amount, address _dest) {
        if (balances[msg.sender] < _amount)
            return;
        balances[msg.sender] -= _amount;
        balances[_dest] += _amount;
    }
    

    COP 의 스타일.
    modifier only_with_at_least(uint x) {
        if (balances[msg.sender] >= x) _;
    }
    
    function transfer(uint _amount, address _dest)
    only_with_at_least(_amount) {
        balances[msg.sender] -= _amount;
        balances[_dest] += _amount;
    }
    

    확장 읽 기:
  • Condition-Orientated Programming
  • 좋은 웹페이지 즐겨찾기