简体   繁体   中英

Smart Contract with Abstract Methods

I have a problem where I'm trying to implement a smart contract with 'future maintainability' software design in mind. Suppose there is a contract which represents a person personal.sol We have another contract called record.col This record at the start only requires to be handled by the police department if they want to send some data to the personal.sol and change it's state. Later this record.sol needs to be modified for it be used by the hospital hospital.sol

There will be inheritance and abstract method required and right now, I don't exactly know how to do it. The following code should further clarify what I'm trying to achieve.

Person.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);
  }
}

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
}
}

Police.sol

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
} 
}

Hospital.sol

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
 }
}

I don't want contracts extending Record.sol to interact directly with Person.sol 's updateRecords() method. Instead, a check should be implemented to verify the contract calling updateRecords() is indeed an extention of solidity file Record.sol Is there a way to check this type like it is in Java instanceOf or .getClass

Sorry for the bad formatting but I wrote it all in an editor. It doesn't translate the indentation smoothly

The short answer is no at least in plain solidity you may be able to do it using assembly but I wouldn't know about that, you could put a function in Record.sol such as

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

And call that from the Person contract to verify that the calling contract does contain the record class. Although this could open you up to security vulnerabilities depending on what you will be doing with these contracts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM