Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/heritable encapsulation #692 #702

Merged
merged 10 commits into from
Jan 26, 2018
26 changes: 21 additions & 5 deletions contracts/ownership/Heritable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import "./Ownable.sol";
* owner's death.
*/
contract Heritable is Ownable {
address public heir;
address private heir;

// Time window the owner has to notify they are alive.
uint256 public heartbeatTimeout;
uint256 private heartbeatTimeout;

// Timestamp of the owner's death, as pronounced by the heir.
uint256 public timeOfDeath;
uint256 private timeOfDeath;

event HeirChanged(address indexed owner, address indexed newHeir);
event OwnerHeartbeated(address indexed owner);
Expand Down Expand Up @@ -50,6 +50,22 @@ contract Heritable is Ownable {
heir = newHeir;
}

/**
* @dev Use these getter functions to access the internal variables in
* an inherited contract.
*/
function getHeir() public view returns(address) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As of #666 we've adopted the convention that private state variables with public getters follow the naming convention that

  • state variable has a suffix underscore _, and
  • getter is the name of the variable without suffix underscore.

In this case, for example, we'd have address private heir_ and function heir() public view.

Can you please change the PR to follow this convention?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

return heir;
}

function getHeartbeatTimeout() public view returns(uint256) {
return heartbeatTimeout;
}

function getTimeOfDeath() public view returns(uint256) {
return timeOfDeath;
}

/**
* @dev set heir = 0x0
*/
Expand All @@ -65,7 +81,7 @@ contract Heritable is Ownable {
function proclaimDeath() public onlyHeir {
require(ownerLives());
OwnerProclaimedDead(owner, heir, timeOfDeath);
timeOfDeath = now;
timeOfDeath = block.timestamp;
}

/**
Expand All @@ -81,7 +97,7 @@ contract Heritable is Ownable {
*/
function claimHeirOwnership() public onlyHeir {
require(!ownerLives());
require(now >= timeOfDeath + heartbeatTimeout);
require(block.timestamp >= timeOfDeath + heartbeatTimeout);
OwnershipTransferred(owner, heir);
HeirOwnershipClaimed(owner, heir);
owner = heir;
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
},
"dependencies": {
"dotenv": "^4.0.0",
"ethjs-abi": "^0.2.1"
"ethjs-abi": "^0.2.1",
"lint": "^1.1.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mistakenly installed this package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Fixed.

}
}
8 changes: 4 additions & 4 deletions test/Heritable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract('Heritable', function (accounts) {
});

it('should start off with an owner, but without heir', async function () {
const heir = await heritable.heir();
const heir = await heritable.getHeir();

assert.equal(typeof (owner), 'string');
assert.equal(typeof (heir), 'string');
Expand All @@ -41,11 +41,11 @@ contract('Heritable', function (accounts) {
it('owner can remove heir', async function () {
const newHeir = accounts[1];
await heritable.setHeir(newHeir, { from: owner });
let heir = await heritable.heir();
let heir = await heritable.getHeir();

assert.notStrictEqual(heir, NULL_ADDRESS);
await heritable.removeHeir();
heir = await heritable.heir();
heir = await heritable.getHeir();
assert.isTrue(heir === NULL_ADDRESS);
});

Expand All @@ -60,7 +60,7 @@ contract('Heritable', function (accounts) {

await increaseTime(4141);
await heritable.claimHeirOwnership({ from: heir });
assert.isTrue(await heritable.heir() === heir);
assert.isTrue(await heritable.getHeir() === heir);
});

it('heir can\'t claim ownership if owner heartbeats', async function () {
Expand Down