You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
9.0 KiB

  1. package eth
  2. import (
  3. "math/big"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. )
  9. var wdelayerClient *WDelayerClient
  10. var wdelayerClientTest *WDelayerClient
  11. var initWithdrawalDelay = big.NewInt(60)
  12. var newWithdrawalDelay = big.NewInt(79)
  13. var maxEmergencyModeTime = time.Hour * 24 * 7 * 26
  14. var maxWithdrawalDelay = time.Hour * 24 * 7 * 2
  15. func TestWDelayerConstants(t *testing.T) {
  16. wDelayerConstants, err := wdelayerClientTest.WDelayerConstants()
  17. require.Nil(t, err)
  18. assert.Equal(t, uint64(maxWithdrawalDelay.Seconds()), wDelayerConstants.MaxWithdrawalDelay)
  19. assert.Equal(t, uint64(maxEmergencyModeTime.Seconds()), wDelayerConstants.MaxEmergencyModeTime)
  20. assert.Equal(t, hermezRollupTestAddressConst, wDelayerConstants.HermezRollup)
  21. }
  22. func TestWDelayerGetHermezGovernanceAddress(t *testing.T) {
  23. governanceAddress, err := wdelayerClientTest.WDelayerGetHermezGovernanceAddress()
  24. require.Nil(t, err)
  25. assert.Equal(t, &governanceAddressConst, governanceAddress)
  26. }
  27. func TestWDelayerSetHermezGovernanceAddress(t *testing.T) {
  28. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
  29. require.Nil(t, err)
  30. _, err = wdelayerClientTest.WDelayerTransferGovernance(auxAddressConst)
  31. require.Nil(t, err)
  32. _, err = wdelayerClientAux.WDelayerClaimGovernance()
  33. require.Nil(t, err)
  34. auxAddress, err := wdelayerClientTest.WDelayerGetHermezGovernanceAddress()
  35. require.Nil(t, err)
  36. assert.Equal(t, &auxAddressConst, auxAddress)
  37. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  38. require.Nil(t, err)
  39. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  40. require.Nil(t, err)
  41. assert.Equal(t, auxAddressConst, wdelayerEvents.NewHermezGovernanceAddress[0].NewHermezGovernanceAddress)
  42. _, err = wdelayerClientAux.WDelayerTransferGovernance(governanceAddressConst)
  43. require.Nil(t, err)
  44. _, err = wdelayerClientTest.WDelayerClaimGovernance()
  45. require.Nil(t, err)
  46. }
  47. func TestWDelayerGetEmergencyCouncil(t *testing.T) {
  48. emergencyCouncil, err := wdelayerClientTest.WDelayerGetEmergencyCouncil()
  49. require.Nil(t, err)
  50. assert.Equal(t, &emergencyCouncilAddressConst, emergencyCouncil)
  51. }
  52. func TestWDelayerSetEmergencyCouncil(t *testing.T) {
  53. wdelayerClientEmergencyCouncil, err := NewWDelayerClient(ethereumClientEmergencyCouncil, wdelayerTestAddressConst)
  54. require.Nil(t, err)
  55. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
  56. require.Nil(t, err)
  57. _, err = wdelayerClientEmergencyCouncil.WDelayerTransferEmergencyCouncil(auxAddressConst)
  58. require.Nil(t, err)
  59. _, err = wdelayerClientAux.WDelayerClaimEmergencyCouncil()
  60. require.Nil(t, err)
  61. auxAddress, err := wdelayerClientTest.WDelayerGetEmergencyCouncil()
  62. require.Nil(t, err)
  63. assert.Equal(t, &auxAddressConst, auxAddress)
  64. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  65. require.Nil(t, err)
  66. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  67. require.Nil(t, err)
  68. assert.Equal(t, auxAddressConst, wdelayerEvents.NewEmergencyCouncil[0].NewEmergencyCouncil)
  69. _, err = wdelayerClientAux.WDelayerTransferEmergencyCouncil(emergencyCouncilAddressConst)
  70. require.Nil(t, err)
  71. _, err = wdelayerClientEmergencyCouncil.WDelayerClaimEmergencyCouncil()
  72. require.Nil(t, err)
  73. }
  74. func TestWDelayerIsEmergencyMode(t *testing.T) {
  75. emergencyMode, err := wdelayerClientTest.WDelayerIsEmergencyMode()
  76. require.Nil(t, err)
  77. assert.Equal(t, false, emergencyMode)
  78. }
  79. func TestWDelayerGetWithdrawalDelay(t *testing.T) {
  80. withdrawalDelay, err := wdelayerClientTest.WDelayerGetWithdrawalDelay()
  81. require.Nil(t, err)
  82. assert.Equal(t, initWithdrawalDelay, withdrawalDelay)
  83. }
  84. func TestWDelayerChangeWithdrawalDelay(t *testing.T) {
  85. _, err := wdelayerClientTest.WDelayerChangeWithdrawalDelay(newWithdrawalDelay.Uint64())
  86. require.Nil(t, err)
  87. withdrawalDelay, err := wdelayerClientTest.WDelayerGetWithdrawalDelay()
  88. require.Nil(t, err)
  89. assert.Equal(t, newWithdrawalDelay, withdrawalDelay)
  90. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  91. require.Nil(t, err)
  92. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  93. require.Nil(t, err)
  94. assert.Equal(t, newWithdrawalDelay.Uint64(), wdelayerEvents.NewWithdrawalDelay[0].WithdrawalDelay)
  95. }
  96. func TestWDelayerDeposit(t *testing.T) {
  97. amount := new(big.Int)
  98. amount.SetString("1100000000000000000", 10)
  99. wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
  100. require.Nil(t, err)
  101. _, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHEZAddressConst, amount)
  102. require.Nil(t, err)
  103. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  104. require.Nil(t, err)
  105. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  106. require.Nil(t, err)
  107. assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
  108. assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
  109. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Deposit[0].Token)
  110. }
  111. func TestWDelayerDepositInfo(t *testing.T) {
  112. amount := new(big.Int)
  113. amount.SetString("1100000000000000000", 10)
  114. state, err := wdelayerClientTest.WDelayerDepositInfo(auxAddressConst, tokenHEZAddressConst)
  115. require.Nil(t, err)
  116. assert.Equal(t, state.Amount, amount)
  117. }
  118. func TestWDelayerWithdrawal(t *testing.T) {
  119. amount := new(big.Int)
  120. amount.SetString("1100000000000000000", 10)
  121. _, err := wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst)
  122. require.Contains(t, err.Error(), "WITHDRAWAL_NOT_ALLOWED")
  123. addTime(float64(newWithdrawalDelay.Int64()), ethClientDialURL)
  124. addBlock(ethClientDialURL)
  125. _, err = wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst)
  126. require.Nil(t, err)
  127. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  128. require.Nil(t, err)
  129. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  130. require.Nil(t, err)
  131. assert.Equal(t, amount, wdelayerEvents.Withdraw[0].Amount)
  132. assert.Equal(t, auxAddressConst, wdelayerEvents.Withdraw[0].Owner)
  133. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Withdraw[0].Token)
  134. }
  135. func TestWDelayerSecondDeposit(t *testing.T) {
  136. amount := new(big.Int)
  137. amount.SetString("1100000000000000000", 10)
  138. wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
  139. require.Nil(t, err)
  140. _, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHEZAddressConst, amount)
  141. require.Nil(t, err)
  142. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  143. require.Nil(t, err)
  144. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  145. require.Nil(t, err)
  146. assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
  147. assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
  148. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Deposit[0].Token)
  149. }
  150. func TestWDelayerEnableEmergencyMode(t *testing.T) {
  151. _, err := wdelayerClientTest.WDelayerEnableEmergencyMode()
  152. require.Nil(t, err)
  153. emergencyMode, err := wdelayerClientTest.WDelayerIsEmergencyMode()
  154. require.Nil(t, err)
  155. assert.Equal(t, true, emergencyMode)
  156. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  157. require.Nil(t, err)
  158. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  159. require.Nil(t, err)
  160. auxEvent := new(WDelayerEventEmergencyModeEnabled)
  161. assert.Equal(t, auxEvent, &wdelayerEvents.EmergencyModeEnabled[0])
  162. }
  163. func TestWDelayerGetEmergencyModeStartingTime(t *testing.T) {
  164. emergencyModeStartingTime, err := wdelayerClientTest.WDelayerGetEmergencyModeStartingTime()
  165. require.Nil(t, err)
  166. // `emergencyModeStartingTime` is initialized to 0 in the smart
  167. // contract construction. Since we called WDelayerEnableEmergencyMode
  168. // previously, `emergencyModeStartingTime` is set to the time when the
  169. // call was made, so it's > 0.
  170. assert.True(t, emergencyModeStartingTime.Cmp(big.NewInt(0)) == 1)
  171. }
  172. func TestWDelayerEscapeHatchWithdrawal(t *testing.T) {
  173. amount := new(big.Int)
  174. amount.SetString("10000000000000000", 10)
  175. wdelayerClientEmergencyCouncil, err := NewWDelayerClient(ethereumClientEmergencyCouncil, wdelayerTestAddressConst)
  176. require.Nil(t, err)
  177. _, err = wdelayerClientEmergencyCouncil.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHEZAddressConst, amount)
  178. require.Contains(t, err.Error(), "NO_MAX_EMERGENCY_MODE_TIME")
  179. seconds := maxEmergencyModeTime.Seconds()
  180. addTime(seconds, ethClientDialURL)
  181. _, err = wdelayerClientEmergencyCouncil.WDelayerEscapeHatchWithdrawal(governanceAddressConst, tokenHEZAddressConst, amount)
  182. require.Nil(t, err)
  183. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  184. require.Nil(t, err)
  185. wdelayerEvents, _, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum)
  186. require.Nil(t, err)
  187. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
  188. assert.Equal(t, governanceAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].To)
  189. assert.Equal(t, emergencyCouncilAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Who)
  190. assert.Equal(t, amount, wdelayerEvents.EscapeHatchWithdrawal[0].Amount)
  191. }