Inheritable.js 4.2 KB

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