Heritable.test.js 4.5 KB

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