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.

525 lines
20 KiB

4 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, amount *big.Int) (*types.Transaction, error)
  120. WDelayerEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*WDelayerEvents, error)
  121. WDelayerConstants() (*common.WDelayerConstants, error)
  122. WDelayerEventInit() (*WDelayerEventInitialize, int64, error)
  123. }
  124. //
  125. // Implementation
  126. //
  127. // WDelayerClient is the implementation of the interface to the WithdrawDelayer Smart Contract in ethereum.
  128. type WDelayerClient struct {
  129. client *EthereumClient
  130. address ethCommon.Address
  131. wdelayer *WithdrawalDelayer.WithdrawalDelayer
  132. contractAbi abi.ABI
  133. opts *bind.CallOpts
  134. }
  135. // NewWDelayerClient creates a new WDelayerClient
  136. func NewWDelayerClient(client *EthereumClient, address ethCommon.Address) (*WDelayerClient, error) {
  137. contractAbi, err := abi.JSON(strings.NewReader(string(WithdrawalDelayer.WithdrawalDelayerABI)))
  138. if err != nil {
  139. return nil, tracerr.Wrap(err)
  140. }
  141. wdelayer, err := WithdrawalDelayer.NewWithdrawalDelayer(address, client.Client())
  142. if err != nil {
  143. return nil, tracerr.Wrap(err)
  144. }
  145. return &WDelayerClient{
  146. client: client,
  147. address: address,
  148. wdelayer: wdelayer,
  149. contractAbi: contractAbi,
  150. opts: newCallOpts(),
  151. }, nil
  152. }
  153. // WDelayerGetHermezGovernanceAddress is the interface to call the smart contract function
  154. func (c *WDelayerClient) WDelayerGetHermezGovernanceAddress() (hermezGovernanceAddress *ethCommon.Address, err error) {
  155. var _hermezGovernanceAddress ethCommon.Address
  156. if err := c.client.Call(func(ec *ethclient.Client) error {
  157. _hermezGovernanceAddress, err = c.wdelayer.GetHermezGovernanceAddress(c.opts)
  158. return tracerr.Wrap(err)
  159. }); err != nil {
  160. return nil, tracerr.Wrap(err)
  161. }
  162. return &_hermezGovernanceAddress, nil
  163. }
  164. // WDelayerTransferGovernance is the interface to call the smart contract function
  165. func (c *WDelayerClient) WDelayerTransferGovernance(newAddress ethCommon.Address) (tx *types.Transaction, err error) {
  166. if tx, err = c.client.CallAuth(
  167. 0,
  168. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  169. return c.wdelayer.TransferGovernance(auth, newAddress)
  170. },
  171. ); err != nil {
  172. return nil, tracerr.Wrap(fmt.Errorf("Failed transfer hermezGovernanceAddress: %w", err))
  173. }
  174. return tx, nil
  175. }
  176. // WDelayerClaimGovernance is the interface to call the smart contract function
  177. func (c *WDelayerClient) WDelayerClaimGovernance() (tx *types.Transaction, err error) {
  178. if tx, err = c.client.CallAuth(
  179. 0,
  180. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  181. return c.wdelayer.ClaimGovernance(auth)
  182. },
  183. ); err != nil {
  184. return nil, tracerr.Wrap(fmt.Errorf("Failed claim hermezGovernanceAddress: %w", err))
  185. }
  186. return tx, nil
  187. }
  188. // WDelayerGetEmergencyCouncil is the interface to call the smart contract function
  189. func (c *WDelayerClient) WDelayerGetEmergencyCouncil() (emergencyCouncilAddress *ethCommon.Address, err error) {
  190. var _emergencyCouncilAddress ethCommon.Address
  191. if err := c.client.Call(func(ec *ethclient.Client) error {
  192. _emergencyCouncilAddress, err = c.wdelayer.GetEmergencyCouncil(c.opts)
  193. return tracerr.Wrap(err)
  194. }); err != nil {
  195. return nil, tracerr.Wrap(err)
  196. }
  197. return &_emergencyCouncilAddress, nil
  198. }
  199. // WDelayerTransferEmergencyCouncil is the interface to call the smart contract function
  200. func (c *WDelayerClient) WDelayerTransferEmergencyCouncil(newAddress ethCommon.Address) (tx *types.Transaction, err error) {
  201. if tx, err = c.client.CallAuth(
  202. 0,
  203. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  204. return c.wdelayer.TransferEmergencyCouncil(auth, newAddress)
  205. },
  206. ); err != nil {
  207. return nil, tracerr.Wrap(fmt.Errorf("Failed transfer EmergencyCouncil: %w", err))
  208. }
  209. return tx, nil
  210. }
  211. // WDelayerClaimEmergencyCouncil is the interface to call the smart contract function
  212. func (c *WDelayerClient) WDelayerClaimEmergencyCouncil() (tx *types.Transaction, err error) {
  213. if tx, err = c.client.CallAuth(
  214. 0,
  215. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  216. return c.wdelayer.ClaimEmergencyCouncil(auth)
  217. },
  218. ); err != nil {
  219. return nil, tracerr.Wrap(fmt.Errorf("Failed claim EmergencyCouncil: %w", err))
  220. }
  221. return tx, nil
  222. }
  223. // WDelayerIsEmergencyMode is the interface to call the smart contract function
  224. func (c *WDelayerClient) WDelayerIsEmergencyMode() (ermergencyMode bool, err error) {
  225. if err := c.client.Call(func(ec *ethclient.Client) error {
  226. ermergencyMode, err = c.wdelayer.IsEmergencyMode(c.opts)
  227. return tracerr.Wrap(err)
  228. }); err != nil {
  229. return false, tracerr.Wrap(err)
  230. }
  231. return ermergencyMode, nil
  232. }
  233. // WDelayerGetWithdrawalDelay is the interface to call the smart contract function
  234. func (c *WDelayerClient) WDelayerGetWithdrawalDelay() (withdrawalDelay int64, err error) {
  235. var _withdrawalDelay uint64
  236. if err := c.client.Call(func(ec *ethclient.Client) error {
  237. _withdrawalDelay, err = c.wdelayer.GetWithdrawalDelay(c.opts)
  238. return tracerr.Wrap(err)
  239. }); err != nil {
  240. return 0, tracerr.Wrap(err)
  241. }
  242. return int64(_withdrawalDelay), nil
  243. }
  244. // WDelayerGetEmergencyModeStartingTime is the interface to call the smart contract function
  245. func (c *WDelayerClient) WDelayerGetEmergencyModeStartingTime() (emergencyModeStartingTime int64, err error) {
  246. var _emergencyModeStartingTime uint64
  247. if err := c.client.Call(func(ec *ethclient.Client) error {
  248. _emergencyModeStartingTime, err = c.wdelayer.GetEmergencyModeStartingTime(c.opts)
  249. return tracerr.Wrap(err)
  250. }); err != nil {
  251. return 0, tracerr.Wrap(err)
  252. }
  253. return int64(_emergencyModeStartingTime), nil
  254. }
  255. // WDelayerEnableEmergencyMode is the interface to call the smart contract function
  256. func (c *WDelayerClient) WDelayerEnableEmergencyMode() (tx *types.Transaction, err error) {
  257. if tx, err = c.client.CallAuth(
  258. 0,
  259. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  260. return c.wdelayer.EnableEmergencyMode(auth)
  261. },
  262. ); err != nil {
  263. return nil, tracerr.Wrap(fmt.Errorf("Failed setting enable emergency mode: %w", err))
  264. }
  265. return tx, nil
  266. }
  267. // WDelayerChangeWithdrawalDelay is the interface to call the smart contract function
  268. func (c *WDelayerClient) WDelayerChangeWithdrawalDelay(newWithdrawalDelay uint64) (tx *types.Transaction, err error) {
  269. if tx, err = c.client.CallAuth(
  270. 0,
  271. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  272. return c.wdelayer.ChangeWithdrawalDelay(auth, newWithdrawalDelay)
  273. },
  274. ); err != nil {
  275. return nil, tracerr.Wrap(fmt.Errorf("Failed setting withdrawal delay: %w", err))
  276. }
  277. return tx, nil
  278. }
  279. // WDelayerDepositInfo is the interface to call the smart contract function
  280. func (c *WDelayerClient) WDelayerDepositInfo(owner, token ethCommon.Address) (depositInfo DepositState, err error) {
  281. if err := c.client.Call(func(ec *ethclient.Client) error {
  282. amount, depositTimestamp, err := c.wdelayer.DepositInfo(c.opts, owner, token)
  283. depositInfo.Amount = amount
  284. depositInfo.DepositTimestamp = depositTimestamp
  285. return tracerr.Wrap(err)
  286. }); err != nil {
  287. return depositInfo, tracerr.Wrap(err)
  288. }
  289. return depositInfo, nil
  290. }
  291. // WDelayerDeposit is the interface to call the smart contract function
  292. func (c *WDelayerClient) WDelayerDeposit(owner, token ethCommon.Address, amount *big.Int) (tx *types.Transaction, err error) {
  293. if tx, err = c.client.CallAuth(
  294. 0,
  295. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  296. return c.wdelayer.Deposit(auth, owner, token, amount)
  297. },
  298. ); err != nil {
  299. return nil, tracerr.Wrap(fmt.Errorf("Failed deposit: %w", err))
  300. }
  301. return tx, nil
  302. }
  303. // WDelayerWithdrawal is the interface to call the smart contract function
  304. func (c *WDelayerClient) WDelayerWithdrawal(owner, token ethCommon.Address) (tx *types.Transaction, err error) {
  305. if tx, err = c.client.CallAuth(
  306. 0,
  307. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  308. return c.wdelayer.Withdrawal(auth, owner, token)
  309. },
  310. ); err != nil {
  311. return nil, tracerr.Wrap(fmt.Errorf("Failed withdrawal: %w", err))
  312. }
  313. return tx, nil
  314. }
  315. // WDelayerEscapeHatchWithdrawal is the interface to call the smart contract function
  316. func (c *WDelayerClient) WDelayerEscapeHatchWithdrawal(to, token ethCommon.Address, amount *big.Int) (tx *types.Transaction, err error) {
  317. if tx, err = c.client.CallAuth(
  318. 0,
  319. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  320. return c.wdelayer.EscapeHatchWithdrawal(auth, to, token, amount)
  321. },
  322. ); err != nil {
  323. return nil, tracerr.Wrap(fmt.Errorf("Failed escapeHatchWithdrawal: %w", err))
  324. }
  325. return tx, nil
  326. }
  327. // WDelayerConstants returns the Constants of the WDelayer Smart Contract
  328. func (c *WDelayerClient) WDelayerConstants() (constants *common.WDelayerConstants, err error) {
  329. constants = new(common.WDelayerConstants)
  330. if err := c.client.Call(func(ec *ethclient.Client) error {
  331. constants.MaxWithdrawalDelay, err = c.wdelayer.MAXWITHDRAWALDELAY(c.opts)
  332. if err != nil {
  333. return tracerr.Wrap(err)
  334. }
  335. constants.MaxEmergencyModeTime, err = c.wdelayer.MAXEMERGENCYMODETIME(c.opts)
  336. if err != nil {
  337. return tracerr.Wrap(err)
  338. }
  339. constants.HermezRollup, err = c.wdelayer.HermezRollupAddress(c.opts)
  340. if err != nil {
  341. return tracerr.Wrap(err)
  342. }
  343. return tracerr.Wrap(err)
  344. }); err != nil {
  345. return constants, tracerr.Wrap(err)
  346. }
  347. return constants, nil
  348. }
  349. var (
  350. logWDelayerDeposit = crypto.Keccak256Hash([]byte("Deposit(address,address,uint192,uint64)"))
  351. logWDelayerWithdraw = crypto.Keccak256Hash([]byte("Withdraw(address,address,uint192)"))
  352. logWDelayerEmergencyModeEnabled = crypto.Keccak256Hash([]byte("EmergencyModeEnabled()"))
  353. logWDelayerNewWithdrawalDelay = crypto.Keccak256Hash([]byte("NewWithdrawalDelay(uint64)"))
  354. logWDelayerEscapeHatchWithdrawal = crypto.Keccak256Hash([]byte("EscapeHatchWithdrawal(address,address,address,uint256)"))
  355. logWDelayerNewEmergencyCouncil = crypto.Keccak256Hash([]byte("NewEmergencyCouncil(address)"))
  356. logWDelayerNewHermezGovernanceAddress = crypto.Keccak256Hash([]byte("NewHermezGovernanceAddress(address)"))
  357. logWDelayerInitialize = crypto.Keccak256Hash([]byte(
  358. "InitializeWithdrawalDelayerEvent(uint64,address,address)"))
  359. )
  360. // WDelayerEventInit returns the initialize event with its corresponding block number
  361. func (c *WDelayerClient) WDelayerEventInit() (*WDelayerEventInitialize, int64, error) {
  362. query := ethereum.FilterQuery{
  363. Addresses: []ethCommon.Address{
  364. c.address,
  365. },
  366. Topics: [][]ethCommon.Hash{{logWDelayerInitialize}},
  367. }
  368. logs, err := c.client.client.FilterLogs(context.Background(), query)
  369. if err != nil {
  370. return nil, 0, tracerr.Wrap(err)
  371. }
  372. if len(logs) != 1 {
  373. return nil, 0, tracerr.Wrap(fmt.Errorf("no event of type InitializeWithdrawalDelayerEvent found"))
  374. }
  375. vLog := logs[0]
  376. if vLog.Topics[0] != logWDelayerInitialize {
  377. return nil, 0, tracerr.Wrap(fmt.Errorf("event is not InitializeWithdrawalDelayerEvent"))
  378. }
  379. var wDelayerInit WDelayerEventInitialize
  380. if err := c.contractAbi.UnpackIntoInterface(&wDelayerInit, "InitializeWithdrawalDelayerEvent",
  381. vLog.Data); err != nil {
  382. return nil, 0, tracerr.Wrap(err)
  383. }
  384. return &wDelayerInit, int64(vLog.BlockNumber), tracerr.Wrap(err)
  385. }
  386. // WDelayerEventsByBlock returns the events in a block that happened in the
  387. // WDelayer Smart Contract.
  388. // To query by blockNum, set blockNum >= 0 and blockHash == nil.
  389. // To query by blockHash set blockHash != nil, and blockNum will be ignored.
  390. // If there are no events in that block the result is nil.
  391. func (c *WDelayerClient) WDelayerEventsByBlock(blockNum int64,
  392. blockHash *ethCommon.Hash) (*WDelayerEvents, error) {
  393. var wdelayerEvents WDelayerEvents
  394. var blockNumBigInt *big.Int
  395. if blockHash == nil {
  396. blockNumBigInt = big.NewInt(blockNum)
  397. }
  398. query := ethereum.FilterQuery{
  399. BlockHash: blockHash,
  400. FromBlock: blockNumBigInt,
  401. ToBlock: blockNumBigInt,
  402. Addresses: []ethCommon.Address{
  403. c.address,
  404. },
  405. Topics: [][]ethCommon.Hash{},
  406. }
  407. logs, err := c.client.client.FilterLogs(context.Background(), query)
  408. if err != nil {
  409. return nil, tracerr.Wrap(err)
  410. }
  411. if len(logs) == 0 {
  412. return nil, nil
  413. }
  414. for _, vLog := range logs {
  415. if blockHash != nil && vLog.BlockHash != *blockHash {
  416. log.Errorw("Block hash mismatch", "expected", blockHash.String(), "got", vLog.BlockHash.String())
  417. return nil, tracerr.Wrap(ErrBlockHashMismatchEvent)
  418. }
  419. switch vLog.Topics[0] {
  420. case logWDelayerDeposit:
  421. var deposit WDelayerEventDeposit
  422. err := c.contractAbi.UnpackIntoInterface(&deposit, "Deposit", vLog.Data)
  423. if err != nil {
  424. return nil, tracerr.Wrap(err)
  425. }
  426. deposit.Owner = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  427. deposit.Token = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  428. deposit.TxHash = vLog.TxHash
  429. wdelayerEvents.Deposit = append(wdelayerEvents.Deposit, deposit)
  430. case logWDelayerWithdraw:
  431. var withdraw WDelayerEventWithdraw
  432. err := c.contractAbi.UnpackIntoInterface(&withdraw, "Withdraw", vLog.Data)
  433. if err != nil {
  434. return nil, tracerr.Wrap(err)
  435. }
  436. withdraw.Token = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  437. withdraw.Owner = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  438. wdelayerEvents.Withdraw = append(wdelayerEvents.Withdraw, withdraw)
  439. case logWDelayerEmergencyModeEnabled:
  440. var emergencyModeEnabled WDelayerEventEmergencyModeEnabled
  441. wdelayerEvents.EmergencyModeEnabled = append(wdelayerEvents.EmergencyModeEnabled, emergencyModeEnabled)
  442. case logWDelayerNewWithdrawalDelay:
  443. var withdrawalDelay WDelayerEventNewWithdrawalDelay
  444. err := c.contractAbi.UnpackIntoInterface(&withdrawalDelay, "NewWithdrawalDelay", vLog.Data)
  445. if err != nil {
  446. return nil, tracerr.Wrap(err)
  447. }
  448. wdelayerEvents.NewWithdrawalDelay = append(wdelayerEvents.NewWithdrawalDelay, withdrawalDelay)
  449. case logWDelayerEscapeHatchWithdrawal:
  450. var escapeHatchWithdrawal WDelayerEventEscapeHatchWithdrawal
  451. err := c.contractAbi.UnpackIntoInterface(&escapeHatchWithdrawal, "EscapeHatchWithdrawal", vLog.Data)
  452. if err != nil {
  453. return nil, tracerr.Wrap(err)
  454. }
  455. escapeHatchWithdrawal.Who = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  456. escapeHatchWithdrawal.To = ethCommon.BytesToAddress(vLog.Topics[2].Bytes())
  457. escapeHatchWithdrawal.Token = ethCommon.BytesToAddress(vLog.Topics[3].Bytes())
  458. wdelayerEvents.EscapeHatchWithdrawal = append(wdelayerEvents.EscapeHatchWithdrawal, escapeHatchWithdrawal)
  459. case logWDelayerNewEmergencyCouncil:
  460. var emergencyCouncil WDelayerEventNewEmergencyCouncil
  461. err := c.contractAbi.UnpackIntoInterface(&emergencyCouncil, "NewEmergencyCouncil", vLog.Data)
  462. if err != nil {
  463. return nil, tracerr.Wrap(err)
  464. }
  465. wdelayerEvents.NewEmergencyCouncil = append(wdelayerEvents.NewEmergencyCouncil, emergencyCouncil)
  466. case logWDelayerNewHermezGovernanceAddress:
  467. var governanceAddress WDelayerEventNewHermezGovernanceAddress
  468. err := c.contractAbi.UnpackIntoInterface(&governanceAddress, "NewHermezGovernanceAddress", vLog.Data)
  469. if err != nil {
  470. return nil, tracerr.Wrap(err)
  471. }
  472. wdelayerEvents.NewHermezGovernanceAddress = append(wdelayerEvents.NewHermezGovernanceAddress, governanceAddress)
  473. }
  474. }
  475. return &wdelayerEvents, nil
  476. }