Heritable.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import increaseTime from './helpers/increaseTime';
  2. import expectThrow from './helpers/expectThrow';
  3. import assertRevert from './helpers/assertRevert';
  4. const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
  5. const Heritable = artifacts.require('Heritable');
  6. contract('Heritable', function (accounts) {
  7. let heritable;
  8. let owner;
  9. beforeEach(async function () {
  10. heritable = await Heritable.new(4141);
  11. owner = await heritable.owner();
  12. });
  13. it('should start off with an owner, but without heir', async function () {
  14. const heir = await heritable.heir();
  15. assert.equal(typeof (owner), 'string');
  16. assert.equal(typeof (heir), 'string');
  17. assert.notStrictEqual(
  18. owner, NULL_ADDRESS,
  19. 'Owner shouldn\'t be the null address'
  20. );
  21. assert.isTrue(
  22. heir === NULL_ADDRESS,
  23. 'Heir should be the null address'
  24. );
  25. });
  26. it('only owner should set heir', async function () {
  27. const newHeir = accounts[1];
  28. const someRandomAddress = accounts[2];
  29. assert.isTrue(owner !== someRandomAddress);
  30. await heritable.setHeir(newHeir, { from: owner });
  31. await expectThrow(heritable.setHeir(newHeir, { from: someRandomAddress }));
  32. });
  33. it('owner can\'t be heir', async function () {
  34. await assertRevert(heritable.setHeir(owner, { from: owner }));
  35. });
  36. it('owner can remove heir', async function () {
  37. const newHeir = accounts[1];
  38. await heritable.setHeir(newHeir, { from: owner });
  39. let heir = await heritable.heir();
  40. assert.notStrictEqual(heir, NULL_ADDRESS);
  41. await heritable.removeHeir();
  42. heir = await heritable.heir();
  43. assert.isTrue(heir === NULL_ADDRESS);
  44. });
  45. it('heir can claim ownership only if owner is dead and timeout was reached', async function () {
  46. const heir = accounts[1];
  47. await heritable.setHeir(heir, { from: owner });
  48. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  49. await heritable.proclaimDeath({ from: heir });
  50. await increaseTime(1);
  51. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  52. await increaseTime(4141);
  53. await heritable.claimHeirOwnership({ from: heir });
  54. assert.isTrue(await heritable.heir() === heir);
  55. });
  56. it('only heir can proclaim death', async function () {
  57. const someRandomAddress = accounts[2];
  58. await assertRevert(heritable.proclaimDeath({ from: owner }));
  59. await assertRevert(heritable.proclaimDeath({ from: someRandomAddress }));
  60. });
  61. it('heir can\'t proclaim death if owner is death', async function () {
  62. const heir = accounts[1];
  63. await heritable.setHeir(heir, { from: owner });
  64. await heritable.proclaimDeath({ from: heir });
  65. await assertRevert(heritable.proclaimDeath({ from: heir }));
  66. });
  67. it('heir can\'t claim ownership if owner heartbeats', async function () {
  68. const heir = accounts[1];
  69. await heritable.setHeir(heir, { from: owner });
  70. await heritable.proclaimDeath({ from: heir });
  71. await heritable.heartbeat({ from: owner });
  72. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  73. await heritable.proclaimDeath({ from: heir });
  74. await increaseTime(4141);
  75. await heritable.heartbeat({ from: owner });
  76. await expectThrow(heritable.claimHeirOwnership({ from: heir }));
  77. });
  78. it('should log events appropriately', async function () {
  79. const heir = accounts[1];
  80. const setHeirLogs = (await heritable.setHeir(heir, { from: owner })).logs;
  81. const setHeirEvent = setHeirLogs.find(e => e.event === 'HeirChanged');
  82. assert.isTrue(setHeirEvent.args.owner === owner);
  83. assert.isTrue(setHeirEvent.args.newHeir === heir);
  84. const heartbeatLogs = (await heritable.heartbeat({ from: owner })).logs;
  85. const heartbeatEvent = heartbeatLogs.find(e => e.event === 'OwnerHeartbeated');
  86. assert.isTrue(heartbeatEvent.args.owner === owner);
  87. const proclaimDeathLogs = (await heritable.proclaimDeath({ from: heir })).logs;
  88. const ownerDeadEvent = proclaimDeathLogs.find(e => e.event === 'OwnerProclaimedDead');
  89. assert.isTrue(ownerDeadEvent.args.owner === owner);
  90. assert.isTrue(ownerDeadEvent.args.heir === heir);
  91. await increaseTime(4141);
  92. const claimHeirOwnershipLogs = (await heritable.claimHeirOwnership({ from: heir })).logs;
  93. const ownershipTransferredEvent = claimHeirOwnershipLogs.find(e => e.event === 'OwnershipTransferred');
  94. const heirOwnershipClaimedEvent = claimHeirOwnershipLogs.find(e => e.event === 'HeirOwnershipClaimed');
  95. assert.isTrue(ownershipTransferredEvent.args.previousOwner === owner);
  96. assert.isTrue(ownershipTransferredEvent.args.newOwner === heir);
  97. assert.isTrue(heirOwnershipClaimedEvent.args.previousOwner === owner);
  98. assert.isTrue(heirOwnershipClaimedEvent.args.newOwner === heir);
  99. });
  100. it('timeOfDeath can be queried', async function () {
  101. assert.equal(await heritable.timeOfDeath(), 0);
  102. });
  103. it('heartbeatTimeout can be queried', async function () {
  104. assert.equal(await heritable.heartbeatTimeout(), 4141);
  105. });
  106. });