具有抽象方法的智能合约



我有一个问题,我试图实现一个考虑到"未来可维护性"软件设计的智能合约。假设有一个代表一个人的合同personal.sol我们有另一个合同叫做record.col如果警察部门想要向personal.sol发送一些数据并更改其状态,则开始时的此记录只需要由警察局处理。稍后需要修改此record.sol以供医院使用hospital.sol

需要继承和抽象方法,现在,我完全不知道该怎么做。下面的代码应该进一步阐明我想要实现的目标。

人索尔

contract Person 
{
Record[] records
strut Record{
string name;
uint time;
}
function updateRecords(string _name, uint _time){
Record _record = Record({name:_name,time:_time});
records.push(_record);
}
}

记录.sol

contract Record{
contract Person {
struct Record{} // can this work? Object properties are defined in Person
function updateRecords(string _name, uint _time){};
}
function commit(address _personaddr, string _name, uint _time){
_personaddr.transfer(address(this).balance;
Person person = Person.at(_personaddr); // creates an instance of the Person contract located at _personaddr address
person.updateRecords(_name,_time);   
}
function abstractMethod() {}
// an abstract method which must be defined in contracts extending this
}
}

警察

contract Police is Record {
//inherits updateRecords & abstract abstractMethod
function policeNewMethod(address _personaddr, string _name, uint _time){
// does something neww
commit(address _personaddr, string _name, uint _time);    
}
function abstractMethod(){
//police own implementation of the method
} 
}

医院溶胶

contract Hospital is Record {
//inherits updateRecords & abstract abstractMethod
function HospitalNewMethod{
// does something
commit(address _personaddr, string _name, uint _time);
}
function abstractMethod(){
//police own implementation of the method
}
}

我不希望扩展Record.sol的合约直接与Person.solupdateRecords()方法交互。相反,应该实施一个检查来验证合约调用updateRecords()确实是 solidity 文件的扩展Record.sol有没有办法像在 JavainstanceOf.getClass中那样检查这种类型

很抱歉格式不好,但我都在编辑器中写了。它不能顺利地转换缩进

简短的回答是否定的,至少在简单的坚固性中,您可以使用汇编来做到这一点,但我不知道,您可以在 Record.sol 中放置一个函数,例如

function isRecord() public pure returns(bool){
return true;
}

并从 Person 协定调用它,以验证调用协定是否包含记录类。尽管这可能会使您面临安全漏洞,具体取决于您将如何处理这些合同。

最新更新