4

After user for a long time codes from Open zeppelin I found comething weird. Usually, they use public functions which only use is to call an internal function with the same name but with _ in front.

function transferOwnership(address _newOwner) public onlyOwner {
    _transferOwnership(_newOwner);
}

function _transferOwnership(address _newOwner) internal {
    require(_newOwner != address(0), "Adress can't be 0x0000...");
    emit OwnershipTransferred(owner, _newOwner);
    owner = _newOwner;
}

This repeats in a lot of contracts. Why do they do that? Is it a security thing or am I missing something. For me right now, the only thing that this gives me is more gas cost.

1 Answer 1

8

Open Zeppelin's contracts are often structured to be editable/extendable, as opposed to what might be considered a "normal coding style".

Perhaps you want to perform some additional checks before transferring ownership (such as before/after a certain date, check that the contract is in a certain state, etc). These checks are specific to your requirement, and have little to do with the actual action of an ownership change. Thus, Open Zeppelin tends to split up the entry and the action in a manner that it's easy to extend functionality in a readable way, without accidentally breaking the actual action being performed.

You will see similar patterns in other base contracts by them, such as the Crowdsale.

1
  • 2
    Ohh! Makes sense. I thought about that, but I'm paranoid about security and thought it might be something to prevent some bug/exploit. Thanks. Aug 22, 2018 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.