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.

553 lines
20 KiB

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
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
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. "context"
  4. "fmt"
  5. "math/big"
  6. "strings"
  7. "github.com/ethereum/go-ethereum"
  8. "github.com/ethereum/go-ethereum/accounts/abi"
  9. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  10. ethCommon "github.com/ethereum/go-ethereum/common"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/crypto"
  13. "github.com/ethereum/go-ethereum/ethclient"
  14. "github.com/hermeznetwork/hermez-node/common"
  15. WithdrawalDelayer "github.com/hermeznetwork/hermez-node/eth/contracts/withdrawdelayer"
  16. "github.com/hermeznetwork/hermez-node/log"
  17. "github.com/hermeznetwork/tracerr"
  18. )
  19. // DepositState is the state of Deposit
  20. type DepositState struct {
  21. Amount *big.Int
  22. DepositTimestamp uint64
  23. }
  24. // WDelayerEventInitialize is the InitializeWithdrawalDelayerEvent event of the
  25. // Smart Contract
  26. type WDelayerEventInitialize struct {
  27. InitialWithdrawalDelay uint64
  28. InitialHermezGovernanceAddress ethCommon.Address
  29. InitialEmergencyCouncil ethCommon.Address
  30. }
  31. // WDelayerVariables returns the WDelayerVariables from the initialize event
  32. func (ei *WDelayerEventInitialize) WDelayerVariables() *common.WDelayerVariables {
  33. return &common.WDelayerVariables{
  34. EthBlockNum: 0,
  35. HermezGovernanceAddress: ei.InitialHermezGovernanceAddress,
  36. EmergencyCouncilAddress: ei.InitialEmergencyCouncil,
  37. WithdrawalDelay: ei.InitialWithdrawalDelay,
  38. EmergencyModeStartingBlock: 0,
  39. EmergencyMode: false,
  40. }
  41. }
  42. // WDelayerEventDeposit is an event of the WithdrawalDelayer Smart Contract
  43. type WDelayerEventDeposit struct {
  44. Owner ethCommon.Address
  45. Token ethCommon.Address
  46. Amount *big.Int
  47. DepositTimestamp uint64
  48. TxHash ethCommon.Hash // Hash of the transaction that generated this event
  49. }
  50. // WDelayerEventWithdraw is an event of the WithdrawalDelayer Smart Contract
  51. type WDelayerEventWithdraw struct {
  52. Owner ethCommon.Address
  53. Token ethCommon.Address
  54. Amount *big.Int
  55. }
  56. // WDelayerEventEmergencyModeEnabled an event of the WithdrawalDelayer Smart Contract
  57. type WDelayerEventEmergencyModeEnabled struct {
  58. }
  59. // WDelayerEventNewWithdrawalDelay an event of the WithdrawalDelayer Smart Contract
  60. type WDelayerEventNewWithdrawalDelay struct {
  61. WithdrawalDelay uint64
  62. }
  63. // WDelayerEventEscapeHatchWithdrawal an event of the WithdrawalDelayer Smart Contract
  64. type WDelayerEventEscapeHatchWithdrawal struct {
  65. Who ethCommon.Address
  66. To ethCommon.Address
  67. Token ethCommon.Address
  68. Amount *big.Int
  69. }
  70. // WDelayerEventNewEmergencyCouncil an event of the WithdrawalDelayer Smart Contract
  71. type WDelayerEventNewEmergencyCouncil struct {
  72. NewEmergencyCouncil ethCommon.Address
  73. }
  74. // WDelayerEventNewHermezGovernanceAddress an event of the WithdrawalDelayer Smart Contract
  75. type WDelayerEventNewHermezGovernanceAddress struct {
  76. NewHermezGovernanceAddress ethCommon.Address
  77. }
  78. // WDelayerEvents is the lis of events in a block of the WithdrawalDelayer Smart Contract
  79. type WDelayerEvents struct {
  80. Deposit []WDelayerEventDeposit
  81. Withdraw []WDelayerEventWithdraw
  82. EmergencyModeEnabled []WDelayerEventEmergencyModeEnabled
  83. NewWithdrawalDelay []WDelayerEventNewWithdrawalDelay
  84. EscapeHatchWithdrawal []WDelayerEventEscapeHatchWithdrawal
  85. NewEmergencyCouncil []WDelayerEventNewEmergencyCouncil
  86. NewHermezGovernanceAddress []WDelayerEventNewHermezGovernanceAddress
  87. }
  88. // NewWDelayerEvents creates an empty WDelayerEvents with the slices initialized.
  89. func NewWDelayerEvents() WDelayerEvents {
  90. return WDelayerEvents{
  91. Deposit: make([]WDelayerEventDeposit, 0),
  92. Withdraw: make([]WDelayerEventWithdraw, 0),
  93. EmergencyModeEnabled: make([]WDelayerEventEmergencyModeEnabled, 0),
  94. NewWithdrawalDelay: make([]WDelayerEventNewWithdrawalDelay, 0),
  95. EscapeHatchWithdrawal: make([]WDelayerEventEscapeHatchWithdrawal, 0),
  96. NewEmergencyCouncil: make([]WDelayerEventNewEmergencyCouncil, 0),
  97. NewHermezGovernanceAddress: make([]WDelayerEventNewHermezGovernanceAddress, 0),
  98. }
  99. }
  100. // WDelayerInterface is the inteface to WithdrawalDelayer Smart Contract
  101. type WDelayerInterface interface {
  102. //
  103. // Smart Contract Methods
  104. //
  105. WDelayerGetHermezGovernanceAddress() (*ethCommon.Address, error)
  106. WDelayerTransferGovernance(newAddress ethCommon.Address) (*types.Transaction, error)
  107. WDelayerClaimGovernance() (*types.Transaction, error)
  108. WDelayerGetEmergencyCouncil() (*ethCommon.Address, error)
  109. WDelayerTransferEmergencyCouncil(newAddress ethCommon.Address) (*types.Transaction, error)
  110. WDelayerClaimEmergencyCouncil() (*types.Transaction, error)
  111. WDelayerIsEmergencyMode() (bool, error)
  112. WDelayerGetWithdrawalDelay() (int64, error)
  113. WDelayerGetEmergencyModeStartingTime() (int64, error)
  114. WDelayerEnableEmergencyMode() (*types.Transaction, error)
  115. WDelayerChangeWithdrawalDelay(newWithdrawalDelay uint64) (*types.Transaction, error)
  116. WDelayerDepositInfo(owner, token ethCommon.Address) (depositInfo DepositState, err error)
  117. WDelayerDeposit(onwer, token ethCommon.Address, amount *big.Int) (*types.Transaction, error)
  118. WDelayerWithdrawal(owner, token ethCommon.Address) (*types.Transaction, error)
  119. WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address,
  120. amount *big.Int) (*types.Transaction, error)
  121. WDelayerEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*WDelayerEvents, error)
  122. WDelayerConstants() (*common.WDelayerConstants, error)
  123. WDelayerEventInit() (*WDelayerEventInitialize, int64, error)
  124. }
  125. //
  126. // Implementation
  127. //
  128. // WDelayerClient is the implementation of the interface to the WithdrawDelayer
  129. // Smart Contract in ethereum.
  130. type WDelayerClient struct {
  131. client *EthereumClient
  132. address ethCommon.Address
  133. wdelayer *WithdrawalDelayer.WithdrawalDelayer
  134. contractAbi abi.ABI
  135. opts *bind.CallOpts
  136. }
  137. // NewWDelayerClient creates a new WDelayerClient
  138. func NewWDelayerClient(client *EthereumClient, address ethCommon.Address) (*WDelayerClient, error) {
  139. contractAbi, err := abi.JSON(strings.NewReader(string(WithdrawalDelayer.WithdrawalDelayerABI)))
  140. if err != nil {
  141. return nil, tracerr.Wrap(err)
  142. }
  143. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(address, client.Client())
  144. if err != nil {
  145. return nil, tracerr.Wrap(err)
  146. }
  147. return &WDelayerClient{
  148. client: client,
  149. address: address,
  150. wdelayer: wdelayer,
  151. contractAbi: contractAbi,
  152. opts: newCallOpts(),
  153. }, nil
  154. }
  155. // WDelayerGetHermezGovernanceAddress is the interface to call the smart contract function
  156. func (c *WDelayerClient) WDelayerGetHermezGovernanceAddress() (
  157. hermezGovernanceAddress *ethCommon.Address, err error) {
  158. var _hermezGovernanceAddress ethCommon.Address
  159. if err := c.client.Call(func(ec *ethclient.Client) error {
  160. _hermezGovernanceAddress, err = c.wdelayer.GetHermezGovernanceAddress(c.opts)
  161. return tracerr.Wrap(err)
  162. }); err != nil {
  163. return nil, tracerr.Wrap(err)
  164. }
  165. return &_hermezGovernanceAddress, nil
  166. }
  167. // WDelayerTransferGovernance is the interface to call the smart contract function
  168. func (c *WDelayerClient) WDelayerTransferGovernance(newAddress ethCommon.Address) (
  169. tx *types.Transaction, err error) {
  170. if tx, err = c.client.CallAuth(
  171. 0,
  172. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  173. return c.wdelayer.TransferGovernance(auth, newAddress)
  174. },
  175. ); err != nil {
  176. return nil, tracerr.Wrap(fmt.Errorf("Failed transfer hermezGovernanceAddress: %w", err))
  177. }
  178. return tx, nil
  179. }
  180. // WDelayerClaimGovernance is the interface to call the smart contract function
  181. func (c *WDelayerClient) WDelayerClaimGovernance() (tx *types.Transaction, err error) {
  182. if tx, err = c.client.CallAuth(
  183. 0,
  184. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  185. return c.wdelayer.ClaimGovernance(auth)
  186. },
  187. ); err != nil {
  188. return nil, tracerr.Wrap(fmt.Errorf("Failed claim hermezGovernanceAddress: %w", err))
  189. }
  190. return tx, nil
  191. }
  192. // WDelayerGetEmergencyCouncil is the interface to call the smart contract function
  193. func (c *WDelayerClient) WDelayerGetEmergencyCouncil() (emergencyCouncilAddress *ethCommon.Address,
  194. err error) {
  195. var _emergencyCouncilAddress ethCommon.Address
  196. if err := c.client.Call(func(ec *ethclient.Client) error {
  197. _emergencyCouncilAddress, err = c.wdelayer.GetEmergencyCouncil(c.opts)
  198. return tracerr.Wrap(err)
  199. }); err != nil {
  200. return nil, tracerr.Wrap(err)
  201. }
  202. return &_emergencyCouncilAddress, nil
  203. }
  204. // WDelayerTransferEmergencyCouncil is the interface to call the smart contract function
  205. func (c *WDelayerClient) WDelayerTransferEmergencyCouncil(newAddress ethCommon.Address) (
  206. tx *types.Transaction, err error) {
  207. if tx, err = c.client.CallAuth(
  208. 0,
  209. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  210. return c.wdelayer.TransferEmergencyCouncil(auth, newAddress)
  211. },
  212. ); err != nil {
  213. return nil, tracerr.Wrap(fmt.Errorf("Failed transfer EmergencyCouncil: %w", err))
  214. }
  215. return tx, nil
  216. }
  217. // WDelayerClaimEmergencyCouncil is the interface to call the smart contract function
  218. func (c *WDelayerClient) WDelayerClaimEmergencyCouncil() (tx *types.Transaction, err error) {
  219. if tx, err = c.client.CallAuth(
  220. 0,
  221. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  222. return c.wdelayer.ClaimEmergencyCouncil(auth)
  223. },
  224. ); err != nil {
  225. return nil, tracerr.Wrap(fmt.Errorf("Failed claim EmergencyCouncil: %w", err))
  226. }
  227. return tx, nil
  228. }
  229. // WDelayerIsEmergencyMode is the interface to call the smart contract function
  230. func (c *WDelayerClient) WDelayerIsEmergencyMode() (ermergencyMode bool, err error) {
  231. if err := c.client.Call(func(ec *ethclient.Client) error {
  232. ermergencyMode, err = c.wdelayer.IsEmergencyMode(c.opts)
  233. return tracerr.Wrap(err)
  234. }); err != nil {
  235. return false, tracerr.Wrap(err)
  236. }
  237. return ermergencyMode, nil
  238. }
  239. // WDelayerGetWithdrawalDelay is the interface to call the smart contract function
  240. func (c *WDelayerClient) WDelayerGetWithdrawalDelay() (withdrawalDelay int64, err error) {
  241. var _withdrawalDelay uint64
  242. if err := c.client.Call(func(ec *ethclient.Client) error {
  243. _withdrawalDelay, err = c.wdelayer.GetWithdrawalDelay(c.opts)
  244. return tracerr.Wrap(err)
  245. }); err != nil {
  246. return 0, tracerr.Wrap(err)
  247. }
  248. return int64(_withdrawalDelay), nil
  249. }
  250. // WDelayerGetEmergencyModeStartingTime is the interface to call the smart contract function
  251. func (c *WDelayerClient) WDelayerGetEmergencyModeStartingTime() (emergencyModeStartingTime int64,
  252. err error) {
  253. var _emergencyModeStartingTime uint64
  254. if err := c.client.Call(func(ec *ethclient.Client) error {
  255. _emergencyModeStartingTime, err = c.wdelayer.GetEmergencyModeStartingTime(c.opts)
  256. return tracerr.Wrap(err)
  257. }); err != nil {
  258. return 0, tracerr.Wrap(err)
  259. }
  260. return int64(_emergencyModeStartingTime), nil
  261. }
  262. // WDelayerEnableEmergencyMode is the interface to call the smart contract function
  263. func (c *WDelayerClient) WDelayerEnableEmergencyMode() (tx *types.Transaction, err error) {
  264. if tx, err = c.client.CallAuth(
  265. 0,
  266. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  267. return c.wdelayer.EnableEmergencyMode(auth)
  268. },
  269. ); err != nil {
  270. return nil, tracerr.Wrap(fmt.Errorf("Failed setting enable emergency mode: %w", err))
  271. }
  272. return tx, nil
  273. }
  274. // WDelayerChangeWithdrawalDelay is the interface to call the smart contract function
  275. func (c *WDelayerClient) WDelayerChangeWithdrawalDelay(newWithdrawalDelay uint64) (
  276. tx *types.Transaction, err error) {
  277. if tx, err = c.client.CallAuth(
  278. 0,
  279. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  280. return c.wdelayer.ChangeWithdrawalDelay(auth, newWithdrawalDelay)
  281. },
  282. ); err != nil {
  283. return nil, tracerr.Wrap(fmt.Errorf("Failed setting withdrawal delay: %w", err))
  284. }
  285. return tx, nil
  286. }
  287. // WDelayerDepositInfo is the interface to call the smart contract function
  288. func (c *WDelayerClient) WDelayerDepositInfo(owner, token ethCommon.Address) (
  289. depositInfo DepositState, err error) {
  290. if err := c.client.Call(func(ec *ethclient.Client) error {
  291. amount, depositTimestamp, err := c.wdelayer.DepositInfo(c.opts, owner, token)
  292. depositInfo.Amount = amount
  293. depositInfo.DepositTimestamp = depositTimestamp
  294. return tracerr.Wrap(err)
  295. }); err != nil {
  296. return depositInfo, tracerr.Wrap(err)
  297. }
  298. return depositInfo, nil
  299. }
  300. // WDelayerDeposit is the interface to call the smart contract function
  301. func (c *WDelayerClient) WDelayerDeposit(owner, token ethCommon.Address, amount *big.Int) (
  302. tx *types.Transaction, err error) {
  303. if tx, err = c.client.CallAuth(
  304. 0,
  305. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  306. return c.wdelayer.Deposit(auth, owner, token, amount)
  307. },
  308. ); err != nil {
  309. return nil, tracerr.Wrap(fmt.Errorf("Failed deposit: %w", err))
  310. }
  311. return tx, nil
  312. }
  313. // WDelayerWithdrawal is the interface to call the smart contract function
  314. func (c *WDelayerClient) WDelayerWithdrawal(owner, token ethCommon.Address) (tx *types.Transaction,
  315. err error) {
  316. if tx, err = c.client.CallAuth(
  317. 0,
  318. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  319. return c.wdelayer.Withdrawal(auth, owner, token)
  320. },
  321. ); err != nil {
  322. return nil, tracerr.Wrap(fmt.Errorf("Failed withdrawal: %w", err))
  323. }
  324. return tx, nil
  325. }
  326. // WDelayerEscapeHatchWithdrawal is the interface to call the smart contract function
  327. func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address,
  328. amount *big.Int) (tx *types.Transaction, err error) {
  329. if tx, err = c.client.CallAuth(
  330. 0,
  331. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  332. return c.wdelayer.EscapeHatchWithdrawal(auth, to, token, amount)
  333. },
  334. ); err != nil {
  335. return nil, tracerr.Wrap(fmt.Errorf("Failed escapeHatchWithdrawal: %w", err))
  336. }
  337. return tx, nil
  338. }
  339. // WDelayerConstants returns the Constants of the WDelayer Smart Contract
  340. func (c *WDelayerClient) WDelayerConstants() (constants *common.WDelayerConstants, err error) {
  341. constants = new(common.WDelayerConstants)
  342. if err := c.client.Call(func(ec *ethclient.Client) error {
  343. constants.MaxWithdrawalDelay, err = c.wdelayer.MAXWITHDRAWALDELAY(c.opts)
  344. if err != nil {
  345. return tracerr.Wrap(err)
  346. }
  347. constants.MaxEmergencyModeTime, err = c.wdelayer.MAXEMERGENCYMODETIME(c.opts)
  348. if err != nil {
  349. return tracerr.Wrap(err)
  350. }
  351. constants.HermezRollup, err = c.wdelayer.HermezRollupAddress(c.opts)
  352. if err != nil {
  353. return tracerr.Wrap(err)
  354. }
  355. return tracerr.Wrap(err)
  356. }); err != nil {
  357. return constants, tracerr.Wrap(err)
  358. }
  359. return constants, nil
  360. }
  361. var (
  362. logWDelayerDeposit = crypto.Keccak256Hash([]byte(
  363. "Deposit(address,address,uint192,uint64)"))
  364. logWDelayerWithdraw = crypto.Keccak256Hash([]byte(
  365. "Withdraw(address,address,uint192)"))
  366. logWDelayerEmergencyModeEnabled = crypto.Keccak256Hash([]byte(
  367. "EmergencyModeEnabled()"))
  368. logWDelayerNewWithdrawalDelay = crypto.Keccak256Hash([]byte(
  369. "NewWithdrawalDelay(uint64)"))
  370. logWDelayerEscapeHatchWithdrawal = crypto.Keccak256Hash([]byte(
  371. "EscapeHatchWithdrawal(address,address,address,uint256)"))
  372. logWDelayerNewEmergencyCouncil = crypto.Keccak256Hash([]byte(
  373. "NewEmergencyCouncil(address)"))
  374. logWDelayerNewHermezGovernanceAddress = crypto.Keccak256Hash([]byte(
  375. "NewHermezGovernanceAddress(address)"))
  376. logWDelayerInitialize = crypto.Keccak256Hash([]byte(
  377. "InitializeWithdrawalDelayerEvent(uint64,address,address)"))
  378. )
  379. // WDelayerEventInit returns the initialize event with its corresponding block number
  380. func (c *WDelayerClient) WDelayerEventInit() (*WDelayerEventInitialize, int64, error) {
  381. query := ethereum.FilterQuery{
  382. Addresses: []ethCommon.Address{
  383. c.address,
  384. },
  385. Topics: [][]ethCommon.Hash{{logWDelayerInitialize}},
  386. }
  387. logs, err := c.client.client.FilterLogs(context.Background(), query)
  388. if err != nil {
  389. return nil, 0, tracerr.Wrap(err)
  390. }
  391. if len(logs) != 1 {
  392. return nil, 0, tracerr.Wrap(fmt.Errorf("no event of type InitializeWithdrawalDelayerEvent found"))
  393. }
  394. vLog := logs[0]
  395. if vLog.Topics[0] != logWDelayerInitialize {
  396. return nil, 0, tracerr.Wrap(fmt.Errorf("event is not InitializeWithdrawalDelayerEvent"))
  397. }
  398. var wDelayerInit WDelayerEventInitialize
  399. if err := c.contractAbi.UnpackIntoInterface(&wDelayerInit, "InitializeWithdrawalDelayerEvent",
  400. vLog.Data); err != nil {
  401. return nil, 0, tracerr.Wrap(err)
  402. }
  403. return &wDelayerInit, int64(vLog.BlockNumber), tracerr.Wrap(err)
  404. }
  405. // WDelayerEventsByBlock returns the events in a block that happened in the
  406. // WDelayer Smart Contract.
  407. // To query by blockNum, set blockNum >= 0 and blockHash == nil.
  408. // To query by blockHash set blockHash != nil, and blockNum will be ignored.
  409. // If there are no events in that block the result is nil.
  410. func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64,
  411. blockHash *ethCommon.Hash) (*WDelayerEvents, error) {
  412. var wdelayerEvents WDelayerEvents
  413. var blockNumBigInt *big.Int
  414. if blockHash == nil {
  415. blockNumBigInt = big.NewInt(blockNum)
  416. }
  417. query := ethereum.FilterQuery{
  418. BlockHash: blockHash,
  419. FromBlock: blockNumBigInt,
  420. ToBlock: blockNumBigInt,
  421. Addresses: []ethCommon.Address{
  422. c.address,
  423. },
  424. Topics: [][]ethCommon.Hash{},
  425. }
  426. logs, err := c.client.client.FilterLogs(context.Background(), query)
  427. if err != nil {
  428. return nil, tracerr.Wrap(err)
  429. }
  430. if len(logs) == 0 {
  431. return nil, nil
  432. }
  433. for _, vLog := range logs {
  434. if blockHash != nil && vLog.BlockHash != *blockHash {
  435. log.Errorw("Block hash mismatch", "expected", blockHash.String(), "got", vLog.BlockHash.String())
  436. return nil, tracerr.Wrap(ErrBlockHashMismatchEvent)
  437. }
  438. switch vLog.Topics[0] {
  439. case logWDelayerDeposit:
  440. var deposit WDelayerEventDeposit
  441. err := c.contractAbi.UnpackIntoInterface(&deposit, "Deposit", vLog.Data)
  442. if err != nil {
  443. return nil, tracerr.Wrap(err)
  444. }
  445. deposit.Owner = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  446. deposit.Token = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  447. deposit.TxHash = vLog.TxHash
  448. wdelayerEvents.Deposit = append(wdelayerEvents.Deposit, deposit)
  449. case logWDelayerWithdraw:
  450. var withdraw WDelayerEventWithdraw
  451. err := c.contractAbi.UnpackIntoInterface(&withdraw, "Withdraw", vLog.Data)
  452. if err != nil {
  453. return nil, tracerr.Wrap(err)
  454. }
  455. withdraw.Token = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  456. withdraw.Owner = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  457. wdelayerEvents.Withdraw = append(wdelayerEvents.Withdraw, withdraw)
  458. case logWDelayerEmergencyModeEnabled:
  459. var emergencyModeEnabled WDelayerEventEmergencyModeEnabled
  460. wdelayerEvents.EmergencyModeEnabled =
  461. append(wdelayerEvents.EmergencyModeEnabled, emergencyModeEnabled)
  462. case logWDelayerNewWithdrawalDelay:
  463. var withdrawalDelay WDelayerEventNewWithdrawalDelay
  464. err := c.contractAbi.UnpackIntoInterface(&withdrawalDelay,
  465. "NewWithdrawalDelay", vLog.Data)
  466. if err != nil {
  467. return nil, tracerr.Wrap(err)
  468. }
  469. wdelayerEvents.NewWithdrawalDelay =
  470. append(wdelayerEvents.NewWithdrawalDelay, withdrawalDelay)
  471. case logWDelayerEscapeHatchWithdrawal:
  472. var escapeHatchWithdrawal WDelayerEventEscapeHatchWithdrawal
  473. err := c.contractAbi.UnpackIntoInterface(&escapeHatchWithdrawal,
  474. "EscapeHatchWithdrawal", vLog.Data)
  475. if err != nil {
  476. return nil, tracerr.Wrap(err)
  477. }
  478. escapeHatchWithdrawal.Who = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  479. escapeHatchWithdrawal.To = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  480. escapeHatchWithdrawal.Token = ethCommon.BytesToAddress(vLog.Topics[3].Bytes())
  481. wdelayerEvents.EscapeHatchWithdrawal =
  482. append(wdelayerEvents.EscapeHatchWithdrawal, escapeHatchWithdrawal)
  483. case logWDelayerNewEmergencyCouncil:
  484. var emergencyCouncil WDelayerEventNewEmergencyCouncil
  485. err := c.contractAbi.UnpackIntoInterface(&emergencyCouncil,
  486. "NewEmergencyCouncil", vLog.Data)
  487. if err != nil {
  488. return nil, tracerr.Wrap(err)
  489. }
  490. wdelayerEvents.NewEmergencyCouncil =
  491. append(wdelayerEvents.NewEmergencyCouncil, emergencyCouncil)
  492. case logWDelayerNewHermezGovernanceAddress:
  493. var governanceAddress WDelayerEventNewHermezGovernanceAddress
  494. err := c.contractAbi.UnpackIntoInterface(&governanceAddress,
  495. "NewHermezGovernanceAddress", vLog.Data)
  496. if err != nil {
  497. return nil, tracerr.Wrap(err)
  498. }
  499. wdelayerEvents.NewHermezGovernanceAddress =
  500. append(wdelayerEvents.NewHermezGovernanceAddress, governanceAddress)
  501. }
  502. }
  503. return &wdelayerEvents, nil
  504. }