지능 계약 설계 모델 - COP
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;
}
확장 읽 기:
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
디자인 모델 의 공장 모델, 단일 모델자바 는 23 가지 디자인 모델 (프로 그래 밍 사상/프로 그래 밍 방식) 이 있 습 니 다. 공장 모드 하나의 공장 류 를 만들어 같은 인 터 페 이 스 를 실현 한 일부 종 류 를 인 스 턴 스 로 만 드 는 것...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.