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.

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