附例程序:
其实还是比较简单的,完成什么功能呢?大家自己看看就好啦,应该用不了几分钟的时间。
//一个比较简单规范的合约架构:
contract LogOperate {
enumErrorCode {
None,//成功
EmptyContract,//合约为空
InvalidInput,//输入错误
Unknown//未知错误
}
eventLogCreate(address contractAddress);
eventLogChange(address contractAddress);
eventLogError(uint code, string message);
eventDebugString(string key, string value);
eventDebugUInt(string key, uint value);
eventDebugAddress(string key, address value);
eventDebugBool(string key, bool value);
eventDebugBytes(string key, bytes value);
eventDebugBytes32(string key, bytes32 value);
}
contract Person is LogOperate {
int_id;
string _name;
string_info;
bool_available;
Int fee;
address_creator;
address_operation_address;
functionPerson(int id, string name, address OperContract) public {
_id = id;
_name = name;
_available = true;
_creator = tx.origin;
_operation_address = OperContract;
}
functiongetId() constant returns (int id) {
return _id;
}
functiongetName() constant returns (string name) {
return _name;
}
functiongetInfo() constant returns (string info) {
return _info;
}
function setInfo(string info) public returns (bool flg){
if(msg.sender != _operation_address) returnfalse;
_info = info;
return true;
}
functiongetState() constant returns (bool available) {
return _available;
}
functionsetState(bool state) public returns (bool flg){
if(msg.sender != _operation_address) returnfalse;
_available = state;
return true;
}
functiongetCreator() constant returns (address creator) {
return _creator;
}
functiongetOperation_address() constant returns (address operation_address) {
return _operation_address;
}
functiondumpData() constant returns (int id, string name, string info, bool available){
return (id, _name, _info, _available);
}
}
contract Operation is LogOperate {
//string_person_name;
//string_person_abi;
functionOperation() {
}
functioncreatePerson(int id, string name) public {
address _person;
_person = new Person(id, name, this);
if (getCodeSize(_person) == 0) {
LogError(uint(ErrorCode.EmptyContract),'创建合约失败。投入的Gas不足。');
return;
}
LogCreate(_person);
}
functiondeletePerson(Person person) public {
person.setState(false);
LogChange(person);
}
function changeInfo(Person person, string info) public{
person.setInfo(info);
LogChange(person);
}
functiongetCodeSize(address contractAddress) internal returns (uint) {
uint codeSize;
assembly {
codeSize := extcodesize(contractAddress)
}
return codeSize;
}
}