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.

226 lines
9.4 KiB

Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
Fix eth events query and sync inconsistent state - kvdb - Fix path in Last when doing `setNew` - Only close if db != nil, and after closing, always set db to nil - This will avoid a panic in the case where the db is closed but there's an error soon after, and a future call tries to close again. This is because pebble.Close() will panic if the db is already closed. - Avoid calling pebble methods when a the Storage interface already implements that method (like Close). - statedb - In test, avoid calling KVDB method if the same method is available for the StateDB (like MakeCheckpoint, CurrentBatch). - eth - In *EventByBlock methods, take blockHash as input argument and use it when querying the event logs. Previously the blockHash was only taken from the logs results *only if* there was any log. This caused the following issue: if there was no logs, it was not possible to know if the result was from the expected block or an uncle block! By querying logs by blockHash we make sure that even if there are no logs, they are from the right block. - Note that now the function can either be called with a blockNum or blockHash, but not both at the same time. - sync - If there's an error during call to Sync call resetState, which internally resets the stateDB to avoid stale checkpoints (and a corresponding invalid increase in the StateDB batchNum). - During a Sync, after very batch processed, make sure that the StateDB currentBatch corresponds to the batchNum in the smart contract log/event.
3 years ago
  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 int64 = 60
  12. var newWithdrawalDelay int64 = 79
  13. var maxEmergencyModeTime = time.Hour * 24 * 7 * 26
  14. var maxWithdrawalDelay = time.Hour * 24 * 7 * 2
  15. func TestWDelayerInit(t *testing.T) {
  16. wDelayerInit, blockNum, err := wdelayerClientTest.WDelayerEventInit()
  17. require.NoError(t, err)
  18. assert.Equal(t, int64(16), blockNum)
  19. assert.Equal(t, uint64(initWithdrawalDelay), wDelayerInit.InitialWithdrawalDelay)
  20. assert.Equal(t, governanceAddressConst, wDelayerInit.InitialHermezGovernanceAddress)
  21. assert.Equal(t, emergencyCouncilAddressConst, wDelayerInit.InitialEmergencyCouncil)
  22. }
  23. func TestWDelayerConstants(t *testing.T) {
  24. wDelayerConstants, err := wdelayerClientTest.WDelayerConstants()
  25. require.Nil(t, err)
  26. assert.Equal(t, uint64(maxWithdrawalDelay.Seconds()), wDelayerConstants.MaxWithdrawalDelay)
  27. assert.Equal(t, uint64(maxEmergencyModeTime.Seconds()), wDelayerConstants.MaxEmergencyModeTime)
  28. assert.Equal(t, hermezRollupTestAddressConst, wDelayerConstants.HermezRollup)
  29. }
  30. func TestWDelayerGetHermezGovernanceAddress(t *testing.T) {
  31. governanceAddress, err := wdelayerClientTest.WDelayerGetHermezGovernanceAddress()
  32. require.Nil(t, err)
  33. assert.Equal(t, &governanceAddressConst, governanceAddress)
  34. }
  35. func TestWDelayerSetHermezGovernanceAddress(t *testing.T) {
  36. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
  37. require.Nil(t, err)
  38. _, err = wdelayerClientTest.WDelayerTransferGovernance(auxAddressConst)
  39. require.Nil(t, err)
  40. _, err = wdelayerClientAux.WDelayerClaimGovernance()
  41. require.Nil(t, err)
  42. auxAddress, err := wdelayerClientTest.WDelayerGetHermezGovernanceAddress()
  43. require.Nil(t, err)
  44. assert.Equal(t, &auxAddressConst, auxAddress)
  45. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  46. require.Nil(t, err)
  47. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  48. require.Nil(t, err)
  49. assert.Equal(t, auxAddressConst,
  50. wdelayerEvents.NewHermezGovernanceAddress[0].NewHermezGovernanceAddress)
  51. _, err = wdelayerClientAux.WDelayerTransferGovernance(governanceAddressConst)
  52. require.Nil(t, err)
  53. _, err = wdelayerClientTest.WDelayerClaimGovernance()
  54. require.Nil(t, err)
  55. }
  56. func TestWDelayerGetEmergencyCouncil(t *testing.T) {
  57. emergencyCouncil, err := wdelayerClientTest.WDelayerGetEmergencyCouncil()
  58. require.Nil(t, err)
  59. assert.Equal(t, &emergencyCouncilAddressConst, emergencyCouncil)
  60. }
  61. func TestWDelayerSetEmergencyCouncil(t *testing.T) {
  62. wdelayerClientEmergencyCouncil, err := NewWDelayerClient(ethereumClientEmergencyCouncil,
  63. wdelayerTestAddressConst)
  64. require.Nil(t, err)
  65. wdelayerClientAux, err := NewWDelayerClient(ethereumClientAux, wdelayerTestAddressConst)
  66. require.Nil(t, err)
  67. _, err = wdelayerClientEmergencyCouncil.WDelayerTransferEmergencyCouncil(auxAddressConst)
  68. require.Nil(t, err)
  69. _, err = wdelayerClientAux.WDelayerClaimEmergencyCouncil()
  70. require.Nil(t, err)
  71. auxAddress, err := wdelayerClientTest.WDelayerGetEmergencyCouncil()
  72. require.Nil(t, err)
  73. assert.Equal(t, &auxAddressConst, auxAddress)
  74. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  75. require.Nil(t, err)
  76. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  77. require.Nil(t, err)
  78. assert.Equal(t, auxAddressConst, wdelayerEvents.NewEmergencyCouncil[0].NewEmergencyCouncil)
  79. _, err = wdelayerClientAux.WDelayerTransferEmergencyCouncil(emergencyCouncilAddressConst)
  80. require.Nil(t, err)
  81. _, err = wdelayerClientEmergencyCouncil.WDelayerClaimEmergencyCouncil()
  82. require.Nil(t, err)
  83. }
  84. func TestWDelayerIsEmergencyMode(t *testing.T) {
  85. emergencyMode, err := wdelayerClientTest.WDelayerIsEmergencyMode()
  86. require.Nil(t, err)
  87. assert.Equal(t, false, emergencyMode)
  88. }
  89. func TestWDelayerGetWithdrawalDelay(t *testing.T) {
  90. withdrawalDelay, err := wdelayerClientTest.WDelayerGetWithdrawalDelay()
  91. require.Nil(t, err)
  92. assert.Equal(t, initWithdrawalDelay, withdrawalDelay)
  93. }
  94. func TestWDelayerChangeWithdrawalDelay(t *testing.T) {
  95. _, err := wdelayerClientTest.WDelayerChangeWithdrawalDelay(uint64(newWithdrawalDelay))
  96. require.Nil(t, err)
  97. withdrawalDelay, err := wdelayerClientTest.WDelayerGetWithdrawalDelay()
  98. require.Nil(t, err)
  99. assert.Equal(t, newWithdrawalDelay, withdrawalDelay)
  100. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  101. require.Nil(t, err)
  102. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  103. require.Nil(t, err)
  104. assert.Equal(t, uint64(newWithdrawalDelay), wdelayerEvents.NewWithdrawalDelay[0].WithdrawalDelay)
  105. }
  106. func TestWDelayerDeposit(t *testing.T) {
  107. amount := new(big.Int)
  108. amount.SetString("1100000000000000000", 10)
  109. wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
  110. require.Nil(t, err)
  111. _, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHEZAddressConst, amount)
  112. require.Nil(t, err)
  113. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  114. require.Nil(t, err)
  115. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  116. require.Nil(t, err)
  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), ethClientDialURL)
  134. addBlock(ethClientDialURL)
  135. _, err = wdelayerClientTest.WDelayerWithdrawal(auxAddressConst, tokenHEZAddressConst)
  136. require.Nil(t, err)
  137. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  138. require.Nil(t, err)
  139. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  140. require.Nil(t, err)
  141. assert.Equal(t, amount, wdelayerEvents.Withdraw[0].Amount)
  142. assert.Equal(t, auxAddressConst, wdelayerEvents.Withdraw[0].Owner)
  143. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Withdraw[0].Token)
  144. }
  145. func TestWDelayerSecondDeposit(t *testing.T) {
  146. amount := new(big.Int)
  147. amount.SetString("1100000000000000000", 10)
  148. wdelayerClientHermez, err := NewWDelayerClient(ethereumClientHermez, wdelayerTestAddressConst)
  149. require.Nil(t, err)
  150. _, err = wdelayerClientHermez.WDelayerDeposit(auxAddressConst, tokenHEZAddressConst, amount)
  151. require.Nil(t, err)
  152. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  153. require.Nil(t, err)
  154. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  155. require.Nil(t, err)
  156. assert.Equal(t, amount, wdelayerEvents.Deposit[0].Amount)
  157. assert.Equal(t, auxAddressConst, wdelayerEvents.Deposit[0].Owner)
  158. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.Deposit[0].Token)
  159. }
  160. func TestWDelayerEnableEmergencyMode(t *testing.T) {
  161. _, err := wdelayerClientTest.WDelayerEnableEmergencyMode()
  162. require.Nil(t, err)
  163. emergencyMode, err := wdelayerClientTest.WDelayerIsEmergencyMode()
  164. require.Nil(t, err)
  165. assert.Equal(t, true, emergencyMode)
  166. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  167. require.Nil(t, err)
  168. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  169. require.Nil(t, err)
  170. auxEvent := new(WDelayerEventEmergencyModeEnabled)
  171. assert.Equal(t, auxEvent, &wdelayerEvents.EmergencyModeEnabled[0])
  172. }
  173. func TestWDelayerGetEmergencyModeStartingTime(t *testing.T) {
  174. emergencyModeStartingTime, err := wdelayerClientTest.WDelayerGetEmergencyModeStartingTime()
  175. require.Nil(t, err)
  176. // `emergencyModeStartingTime` is initialized to 0 in the smart
  177. // contract construction. Since we called WDelayerEnableEmergencyMode
  178. // previously, `emergencyModeStartingTime` is set to the time when the
  179. // call was made, so it's > 0.
  180. assert.Greater(t, emergencyModeStartingTime, int64(0))
  181. }
  182. func TestWDelayerEscapeHatchWithdrawal(t *testing.T) {
  183. amount := new(big.Int)
  184. amount.SetString("10000000000000000", 10)
  185. wdelayerClientEmergencyCouncil, err := NewWDelayerClient(ethereumClientEmergencyCouncil,
  186. wdelayerTestAddressConst)
  187. require.Nil(t, err)
  188. _, err =
  189. wdelayerClientEmergencyCouncil.WDelayerEscapeHatchWithdrawal(governanceAddressConst,
  190. tokenHEZAddressConst, amount)
  191. require.Contains(t, err.Error(), "NO_MAX_EMERGENCY_MODE_TIME")
  192. seconds := maxEmergencyModeTime.Seconds()
  193. addTime(seconds, ethClientDialURL)
  194. _, err =
  195. wdelayerClientEmergencyCouncil.WDelayerEscapeHatchWithdrawal(governanceAddressConst,
  196. tokenHEZAddressConst, amount)
  197. require.Nil(t, err)
  198. currentBlockNum, err := wdelayerClientTest.client.EthLastBlock()
  199. require.Nil(t, err)
  200. wdelayerEvents, err := wdelayerClientTest.WDelayerEventsByBlock(currentBlockNum, nil)
  201. require.Nil(t, err)
  202. assert.Equal(t, tokenHEZAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Token)
  203. assert.Equal(t, governanceAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].To)
  204. assert.Equal(t, emergencyCouncilAddressConst, wdelayerEvents.EscapeHatchWithdrawal[0].Who)
  205. assert.Equal(t, amount, wdelayerEvents.EscapeHatchWithdrawal[0].Amount)
  206. }