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.

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