Inheritable.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/Inheritable2.sol')
  8. contract('Inheritable', function(accounts) {
  9. let inheritable
  10. let owner
  11. beforeEach(async function() {
  12. inheritable = await Inheritable.new()
  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('owner can set heartbeatTimeout only if she\'s alive', async function() {
  50. const newTimeout = 41414141
  51. await inheritable.setHeartbeatTimeout(newTimeout, {from: owner})
  52. assert.isTrue((await inheritable.heartbeatTimeout()).equals(new web3.BigNumber(newTimeout)))
  53. const heir = accounts[1]
  54. await inheritable.setHeir(heir, {from: owner})
  55. await inheritable.pronounceDeath({from: heir})
  56. try {
  57. await inheritable.setHeartbeatTimeout(newTimeout, {from: owner})
  58. assert.fail('should have thrown before')
  59. } catch(error) {
  60. assertJump(error)
  61. }
  62. })
  63. it('heir can inherit only if owner is dead and timeout was reached', async function() {
  64. const heir = accounts[1]
  65. await inheritable.setHeir(heir, {from: owner})
  66. await inheritable.setHeartbeatTimeout(4141, {from: owner})
  67. try {
  68. await inheritable.inherit({from: heir})
  69. assert.fail('should have thrown before')
  70. } catch(error) {
  71. assertJump(error)
  72. }
  73. await inheritable.pronounceDeath({from: heir})
  74. await increaseTime(1)
  75. try {
  76. await inheritable.inherit({from: heir})
  77. assert.fail('should have thrown before')
  78. } catch(error) {
  79. assertJump(error)
  80. }
  81. await increaseTime(4141)
  82. await inheritable.inherit({from: heir})
  83. })
  84. it('heir can\'t inherit if owner heartbeats', async function() {
  85. const heir = accounts[1]
  86. await inheritable.setHeir(heir, {from: owner})
  87. await inheritable.setHeartbeatTimeout(4141, {from: owner})
  88. await inheritable.pronounceDeath({from: heir})
  89. await inheritable.heartbeat({from: owner})
  90. try {
  91. await inheritable.inherit({from: heir})
  92. assert.fail('should have thrown before')
  93. } catch(error) {
  94. assertJump(error)
  95. }
  96. await inheritable.pronounceDeath({from: heir})
  97. await increaseTime(4141)
  98. await inheritable.heartbeat({from: owner})
  99. try {
  100. await inheritable.inherit({from: heir})
  101. assert.fail('should have thrown before')
  102. } catch(error) {
  103. assertJump(error)
  104. }
  105. })
  106. it('should log owner dead and ownership transfer', async function() {
  107. const heir = accounts[1]
  108. await inheritable.setHeir(heir, {from: owner})
  109. const { logs } = await inheritable.pronounceDeath({from: heir})
  110. const event = logs.find(e => e.event === 'OwnerPronouncedDead')
  111. assert.isTrue(event.args.owner === owner)
  112. assert.isTrue(event.args.heir === heir)
  113. })
  114. })