Heritable.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict'
  2. import increaseTime from './helpers/increaseTime'
  3. import expectThrow from './helpers/expectThrow';
  4. const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'
  5. const Heritable = artifacts.require('../contracts/ownership/Heritable.sol')
  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 remove heir', async function() {
  34. const newHeir = accounts[1]
  35. await heritable.setHeir(newHeir, {from: owner})
  36. let heir = await heritable.heir()
  37. assert.notStrictEqual(heir, NULL_ADDRESS)
  38. await heritable.removeHeir()
  39. heir = await heritable.heir()
  40. assert.isTrue(heir === NULL_ADDRESS)
  41. })
  42. it('heir can claim ownership only if owner is dead and timeout was reached', async function() {
  43. const heir = accounts[1]
  44. await heritable.setHeir(heir, {from: owner})
  45. await expectThrow(heritable.claimHeirOwnership({from: heir}))
  46. await heritable.proclaimDeath({from: heir})
  47. await increaseTime(1)
  48. await expectThrow(heritable.claimHeirOwnership({from: heir}))
  49. await increaseTime(4141)
  50. await heritable.claimHeirOwnership({from: heir})
  51. assert.isTrue(await heritable.heir() === heir)
  52. })
  53. it('heir can\'t claim ownership if owner heartbeats', async function() {
  54. const heir = accounts[1]
  55. await heritable.setHeir(heir, {from: owner})
  56. await heritable.proclaimDeath({from: heir})
  57. await heritable.heartbeat({from: owner})
  58. await expectThrow(heritable.claimHeirOwnership({from: heir}))
  59. await heritable.proclaimDeath({from: heir})
  60. await increaseTime(4141)
  61. await heritable.heartbeat({from: owner})
  62. await expectThrow(heritable.claimHeirOwnership({from: heir}))
  63. })
  64. it('should log events appropriately', async function() {
  65. const heir = accounts[1]
  66. const setHeirLogs = (await heritable.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 heritable.heartbeat({from: owner})).logs
  71. const heartbeatEvent = heartbeatLogs.find(e => e.event === 'OwnerHeartbeated')
  72. assert.isTrue(heartbeatEvent.args.owner === owner)
  73. const proclaimDeathLogs = (await heritable.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 claimHeirOwnershipLogs = (await heritable.claimHeirOwnership({from: heir})).logs
  79. const ownershipTransferredEvent = claimHeirOwnershipLogs.find(e => e.event === 'OwnershipTransferred')
  80. const heirOwnershipClaimedEvent = claimHeirOwnershipLogs.find(e => e.event === 'HeirOwnershipClaimed')
  81. assert.isTrue(ownershipTransferredEvent.args.previousOwner === owner)
  82. assert.isTrue(ownershipTransferredEvent.args.newOwner === heir)
  83. assert.isTrue(heirOwnershipClaimedEvent.args.previousOwner === owner)
  84. assert.isTrue(heirOwnershipClaimedEvent.args.newOwner === heir)
  85. })
  86. })