Inheritable.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. 'use strict'
  2. import increaseTime from './helpers/increaseTime'
  3. import expectThrow from './helpers/expectThrow';
  4. const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'
  5. const Inheritable = artifacts.require('../contracts/ownership/Inheritable.sol')
  6. contract('Inheritable', function(accounts) {
  7. let inheritable
  8. let owner
  9. beforeEach(async function() {
  10. inheritable = await Inheritable.new(4141)
  11. owner = await inheritable.owner()
  12. })
  13. it('should start off with an owner, but without heir', async function() {
  14. const heir = await inheritable.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 inheritable.setHeir(newHeir, {from: owner})
  31. await expectThrow(inheritable.setHeir(newHeir, {from: someRandomAddress}))
  32. })
  33. it('owner can remove heir', async function() {
  34. const newHeir = accounts[1]
  35. await inheritable.setHeir(newHeir, {from: owner})
  36. let heir = await inheritable.heir()
  37. assert.notStrictEqual(heir, NULL_ADDRESS)
  38. await inheritable.removeHeir()
  39. heir = await inheritable.heir()
  40. assert.isTrue(heir === NULL_ADDRESS)
  41. })
  42. it('heir can inherit only if owner is dead and timeout was reached', async function() {
  43. const heir = accounts[1]
  44. await inheritable.setHeir(heir, {from: owner})
  45. await expectThrow(inheritable.inherit({from: heir}))
  46. await inheritable.proclaimDeath({from: heir})
  47. await increaseTime(1)
  48. await expectThrow(inheritable.inherit({from: heir}))
  49. await increaseTime(4141)
  50. await inheritable.inherit({from: heir})
  51. assert.isTrue(await inheritable.heir() === heir)
  52. })
  53. it('heir can\'t inherit if owner heartbeats', async function() {
  54. const heir = accounts[1]
  55. await inheritable.setHeir(heir, {from: owner})
  56. await inheritable.proclaimDeath({from: heir})
  57. await inheritable.heartbeat({from: owner})
  58. await expectThrow(inheritable.inherit({from: heir}))
  59. await inheritable.proclaimDeath({from: heir})
  60. await increaseTime(4141)
  61. await inheritable.heartbeat({from: owner})
  62. await expectThrow(inheritable.inherit({from: heir}))
  63. })
  64. it('should log events appropriately', async function() {
  65. const heir = accounts[1]
  66. const setHeirLogs = (await inheritable.setHeir(heir, {from: owner})).logs
  67. const setHeirEvent = setHeirLogs.find(e => e.event === 'HeirChanged')
  68. assert.isTrue(setHeirEvent.args.owner === owner)
  69. assert.isTrue(setHeirEvent.args.newHeir === heir)
  70. const heartbeatLogs = (await inheritable.heartbeat({from: owner})).logs
  71. const heartbeatEvent = heartbeatLogs.find(e => e.event === 'OwnerHeartbeated')
  72. assert.isTrue(heartbeatEvent.args.owner === owner)
  73. const proclaimDeathLogs = (await inheritable.proclaimDeath({from: heir})).logs
  74. const ownerDeadEvent = proclaimDeathLogs.find(e => e.event === 'OwnerProclaimedDead')
  75. assert.isTrue(ownerDeadEvent.args.owner === owner)
  76. assert.isTrue(ownerDeadEvent.args.heir === heir)
  77. await increaseTime(4141)
  78. const inheritLogs = (await inheritable.inherit({from: heir})).logs
  79. const ownershipTransferredEvent = inheritLogs.find(e => e.event === 'OwnershipTransferred')
  80. assert.isTrue(ownershipTransferredEvent.args.previousOwner === owner)
  81. assert.isTrue(ownershipTransferredEvent.args.newOwner === heir)
  82. })
  83. })