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.

1005 lines
38 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
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. "strconv"
  7. "strings"
  8. "github.com/ethereum/go-ethereum"
  9. "github.com/ethereum/go-ethereum/accounts/abi"
  10. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  11. ethCommon "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/core/types"
  13. "github.com/ethereum/go-ethereum/crypto"
  14. "github.com/ethereum/go-ethereum/ethclient"
  15. "github.com/hermeznetwork/hermez-node/common"
  16. Hermez "github.com/hermeznetwork/hermez-node/eth/contracts/hermez"
  17. HEZ "github.com/hermeznetwork/hermez-node/eth/contracts/tokenHEZ"
  18. "github.com/hermeznetwork/hermez-node/log"
  19. "github.com/hermeznetwork/tracerr"
  20. "github.com/iden3/go-iden3-crypto/babyjub"
  21. )
  22. // QueueStruct is the queue of L1Txs for a batch
  23. type QueueStruct struct {
  24. L1TxQueue []common.L1Tx
  25. TotalL1TxFee *big.Int
  26. }
  27. // NewQueueStruct creates a new clear QueueStruct.
  28. func NewQueueStruct() *QueueStruct {
  29. return &QueueStruct{
  30. L1TxQueue: make([]common.L1Tx, 0),
  31. TotalL1TxFee: big.NewInt(0),
  32. }
  33. }
  34. // RollupState represents the state of the Rollup in the Smart Contract
  35. type RollupState struct {
  36. StateRoot *big.Int
  37. ExitRoots []*big.Int
  38. // ExitNullifierMap map[[256 / 8]byte]bool
  39. ExitNullifierMap map[int64]map[int64]bool // batchNum -> idx -> bool
  40. TokenList []ethCommon.Address
  41. TokenMap map[ethCommon.Address]bool
  42. MapL1TxQueue map[int64]*QueueStruct
  43. LastL1L2Batch int64
  44. CurrentToForgeL1TxsNum int64
  45. LastToForgeL1TxsNum int64
  46. CurrentIdx int64
  47. }
  48. // RollupEventInitialize is the InitializeHermezEvent event of the
  49. // Smart Contract
  50. type RollupEventInitialize struct {
  51. ForgeL1L2BatchTimeout uint8
  52. FeeAddToken *big.Int
  53. WithdrawalDelay uint64
  54. }
  55. // RollupVariables returns the RollupVariables from the initialize event
  56. func (ei *RollupEventInitialize) RollupVariables() *common.RollupVariables {
  57. var buckets [common.RollupConstNumBuckets]common.BucketParams
  58. for i := range buckets {
  59. buckets[i] = common.BucketParams{
  60. CeilUSD: big.NewInt(0),
  61. Withdrawals: big.NewInt(0),
  62. BlockWithdrawalRate: big.NewInt(0),
  63. MaxWithdrawals: big.NewInt(0),
  64. }
  65. }
  66. return &common.RollupVariables{
  67. EthBlockNum: 0,
  68. FeeAddToken: ei.FeeAddToken,
  69. ForgeL1L2BatchTimeout: int64(ei.ForgeL1L2BatchTimeout),
  70. WithdrawalDelay: ei.WithdrawalDelay,
  71. Buckets: buckets,
  72. SafeMode: false,
  73. }
  74. }
  75. // RollupEventL1UserTx is an event of the Rollup Smart Contract
  76. type RollupEventL1UserTx struct {
  77. // ToForgeL1TxsNum int64 // QueueIndex *big.Int
  78. // Position int // TransactionIndex *big.Int
  79. L1UserTx common.L1Tx
  80. }
  81. // RollupEventL1UserTxAux is an event of the Rollup Smart Contract
  82. type rollupEventL1UserTxAux struct {
  83. ToForgeL1TxsNum uint64 // QueueIndex *big.Int
  84. Position uint8 // TransactionIndex *big.Int
  85. L1UserTx []byte
  86. }
  87. // RollupEventAddToken is an event of the Rollup Smart Contract
  88. type RollupEventAddToken struct {
  89. TokenAddress ethCommon.Address
  90. TokenID uint32
  91. }
  92. // RollupEventForgeBatch is an event of the Rollup Smart Contract
  93. type RollupEventForgeBatch struct {
  94. BatchNum int64
  95. // Sender ethCommon.Address
  96. EthTxHash ethCommon.Hash
  97. L1UserTxsLen uint16
  98. }
  99. // RollupEventUpdateForgeL1L2BatchTimeout is an event of the Rollup Smart Contract
  100. type RollupEventUpdateForgeL1L2BatchTimeout struct {
  101. NewForgeL1L2BatchTimeout int64
  102. }
  103. // RollupEventUpdateFeeAddToken is an event of the Rollup Smart Contract
  104. type RollupEventUpdateFeeAddToken struct {
  105. NewFeeAddToken *big.Int
  106. }
  107. // RollupEventWithdraw is an event of the Rollup Smart Contract
  108. type RollupEventWithdraw struct {
  109. Idx uint64
  110. NumExitRoot uint64
  111. InstantWithdraw bool
  112. TxHash ethCommon.Hash // Hash of the transaction that generated this event
  113. }
  114. type rollupEventUpdateBucketWithdrawAux struct {
  115. NumBucket uint8
  116. BlockStamp *big.Int
  117. Withdrawals *big.Int
  118. }
  119. // RollupEventUpdateBucketWithdraw is an event of the Rollup Smart Contract
  120. type RollupEventUpdateBucketWithdraw struct {
  121. NumBucket int
  122. BlockStamp int64 // blockNum
  123. Withdrawals *big.Int
  124. }
  125. // RollupEventUpdateWithdrawalDelay is an event of the Rollup Smart Contract
  126. type RollupEventUpdateWithdrawalDelay struct {
  127. NewWithdrawalDelay uint64
  128. }
  129. // RollupUpdateBucketsParameters are the bucket parameters used in an update
  130. type RollupUpdateBucketsParameters struct {
  131. CeilUSD *big.Int
  132. Withdrawals *big.Int
  133. BlockWithdrawalRate *big.Int
  134. MaxWithdrawals *big.Int
  135. }
  136. type rollupEventUpdateBucketsParametersAux struct {
  137. ArrayBuckets [common.RollupConstNumBuckets][4]*big.Int
  138. }
  139. // RollupEventUpdateBucketsParameters is an event of the Rollup Smart Contract
  140. type RollupEventUpdateBucketsParameters struct {
  141. // ArrayBuckets [common.RollupConstNumBuckets][4]*big.Int
  142. ArrayBuckets [common.RollupConstNumBuckets]RollupUpdateBucketsParameters
  143. SafeMode bool
  144. }
  145. // RollupEventUpdateTokenExchange is an event of the Rollup Smart Contract
  146. type RollupEventUpdateTokenExchange struct {
  147. AddressArray []ethCommon.Address
  148. ValueArray []uint64
  149. }
  150. // RollupEventSafeMode is an event of the Rollup Smart Contract
  151. type RollupEventSafeMode struct {
  152. }
  153. // RollupEvents is the list of events in a block of the Rollup Smart Contract
  154. type RollupEvents struct {
  155. L1UserTx []RollupEventL1UserTx
  156. AddToken []RollupEventAddToken
  157. ForgeBatch []RollupEventForgeBatch
  158. UpdateForgeL1L2BatchTimeout []RollupEventUpdateForgeL1L2BatchTimeout
  159. UpdateFeeAddToken []RollupEventUpdateFeeAddToken
  160. Withdraw []RollupEventWithdraw
  161. UpdateWithdrawalDelay []RollupEventUpdateWithdrawalDelay
  162. UpdateBucketWithdraw []RollupEventUpdateBucketWithdraw
  163. UpdateBucketsParameters []RollupEventUpdateBucketsParameters
  164. UpdateTokenExchange []RollupEventUpdateTokenExchange
  165. SafeMode []RollupEventSafeMode
  166. }
  167. // NewRollupEvents creates an empty RollupEvents with the slices initialized.
  168. func NewRollupEvents() RollupEvents {
  169. return RollupEvents{
  170. L1UserTx: make([]RollupEventL1UserTx, 0),
  171. AddToken: make([]RollupEventAddToken, 0),
  172. ForgeBatch: make([]RollupEventForgeBatch, 0),
  173. UpdateForgeL1L2BatchTimeout: make([]RollupEventUpdateForgeL1L2BatchTimeout, 0),
  174. UpdateFeeAddToken: make([]RollupEventUpdateFeeAddToken, 0),
  175. Withdraw: make([]RollupEventWithdraw, 0),
  176. }
  177. }
  178. // RollupForgeBatchArgs are the arguments to the ForgeBatch function in the Rollup Smart Contract
  179. type RollupForgeBatchArgs struct {
  180. NewLastIdx int64
  181. NewStRoot *big.Int
  182. NewExitRoot *big.Int
  183. L1UserTxs []common.L1Tx
  184. L1CoordinatorTxs []common.L1Tx
  185. L1CoordinatorTxsAuths [][]byte // Authorization for accountCreations for each L1CoordinatorTx
  186. L2TxsData []common.L2Tx
  187. FeeIdxCoordinator []common.Idx
  188. // Circuit selector
  189. VerifierIdx uint8
  190. L1Batch bool
  191. ProofA [2]*big.Int
  192. ProofB [2][2]*big.Int
  193. ProofC [2]*big.Int
  194. }
  195. // RollupForgeBatchArgsAux are the arguments to the ForgeBatch function in the Rollup Smart Contract
  196. type rollupForgeBatchArgsAux struct {
  197. NewLastIdx *big.Int
  198. NewStRoot *big.Int
  199. NewExitRoot *big.Int
  200. EncodedL1CoordinatorTx []byte
  201. L1L2TxsData []byte
  202. FeeIdxCoordinator []byte
  203. // Circuit selector
  204. VerifierIdx uint8
  205. L1Batch bool
  206. ProofA [2]*big.Int
  207. ProofB [2][2]*big.Int
  208. ProofC [2]*big.Int
  209. }
  210. // RollupInterface is the inteface to to Rollup Smart Contract
  211. type RollupInterface interface {
  212. //
  213. // Smart Contract Methods
  214. //
  215. // Public Functions
  216. RollupForgeBatch(*RollupForgeBatchArgs, *bind.TransactOpts) (*types.Transaction, error)
  217. RollupAddToken(tokenAddress ethCommon.Address, feeAddToken, deadline *big.Int) (*types.Transaction, error)
  218. RollupWithdrawMerkleProof(babyPubKey babyjub.PublicKeyComp, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (*types.Transaction, error)
  219. RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error)
  220. RollupL1UserTxERC20ETH(fromBJJ babyjub.PublicKeyComp, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (*types.Transaction, error)
  221. RollupL1UserTxERC20Permit(fromBJJ babyjub.PublicKeyComp, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error)
  222. // Governance Public Functions
  223. RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout int64) (*types.Transaction, error)
  224. RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (*types.Transaction, error)
  225. // Viewers
  226. RollupRegisterTokensCount() (*big.Int, error)
  227. RollupLastForgedBatch() (int64, error)
  228. //
  229. // Smart Contract Status
  230. //
  231. RollupConstants() (*common.RollupConstants, error)
  232. RollupEventsByBlock(blockNum int64, blockHash *ethCommon.Hash) (*RollupEvents, error)
  233. RollupForgeBatchArgs(ethCommon.Hash, uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error)
  234. RollupEventInit() (*RollupEventInitialize, int64, error)
  235. }
  236. //
  237. // Implementation
  238. //
  239. // RollupClient is the implementation of the interface to the Rollup Smart Contract in ethereum.
  240. type RollupClient struct {
  241. client *EthereumClient
  242. chainID *big.Int
  243. address ethCommon.Address
  244. tokenHEZCfg TokenConfig
  245. hermez *Hermez.Hermez
  246. tokenHEZ *HEZ.HEZ
  247. contractAbi abi.ABI
  248. opts *bind.CallOpts
  249. consts *common.RollupConstants
  250. }
  251. // NewRollupClient creates a new RollupClient
  252. func NewRollupClient(client *EthereumClient, address ethCommon.Address, tokenHEZCfg TokenConfig) (*RollupClient, error) {
  253. contractAbi, err := abi.JSON(strings.NewReader(string(Hermez.HermezABI)))
  254. if err != nil {
  255. return nil, tracerr.Wrap(err)
  256. }
  257. hermez, err := Hermez.NewHermez(address, client.Client())
  258. if err != nil {
  259. return nil, tracerr.Wrap(err)
  260. }
  261. tokenHEZ, err := HEZ.NewHEZ(tokenHEZCfg.Address, client.Client())
  262. if err != nil {
  263. return nil, tracerr.Wrap(err)
  264. }
  265. chainID, err := client.EthChainID()
  266. if err != nil {
  267. return nil, tracerr.Wrap(err)
  268. }
  269. c := &RollupClient{
  270. client: client,
  271. chainID: chainID,
  272. address: address,
  273. tokenHEZCfg: tokenHEZCfg,
  274. hermez: hermez,
  275. tokenHEZ: tokenHEZ,
  276. contractAbi: contractAbi,
  277. opts: newCallOpts(),
  278. }
  279. consts, err := c.RollupConstants()
  280. if err != nil {
  281. return nil, tracerr.Wrap(fmt.Errorf("RollupConstants at %v: %w", address, err))
  282. }
  283. c.consts = consts
  284. return c, nil
  285. }
  286. // RollupForgeBatch is the interface to call the smart contract function
  287. func (c *RollupClient) RollupForgeBatch(args *RollupForgeBatchArgs, auth *bind.TransactOpts) (tx *types.Transaction, err error) {
  288. if auth == nil {
  289. auth, err = c.client.NewAuth()
  290. if err != nil {
  291. return nil, tracerr.Wrap(err)
  292. }
  293. auth.GasLimit = 1000000
  294. }
  295. nLevels := c.consts.Verifiers[args.VerifierIdx].NLevels
  296. lenBytes := nLevels / 8 //nolint:gomnd
  297. newLastIdx := big.NewInt(int64(args.NewLastIdx))
  298. // L1CoordinatorBytes
  299. var l1CoordinatorBytes []byte
  300. for i := 0; i < len(args.L1CoordinatorTxs); i++ {
  301. l1 := args.L1CoordinatorTxs[i]
  302. bytesl1, err := l1.BytesCoordinatorTx(args.L1CoordinatorTxsAuths[i])
  303. if err != nil {
  304. return nil, tracerr.Wrap(err)
  305. }
  306. l1CoordinatorBytes = append(l1CoordinatorBytes, bytesl1[:]...)
  307. }
  308. // L1L2TxData
  309. var l1l2TxData []byte
  310. for i := 0; i < len(args.L1UserTxs); i++ {
  311. l1User := args.L1UserTxs[i]
  312. bytesl1User, err := l1User.BytesDataAvailability(uint32(nLevels))
  313. if err != nil {
  314. return nil, tracerr.Wrap(err)
  315. }
  316. l1l2TxData = append(l1l2TxData, bytesl1User[:]...)
  317. }
  318. for i := 0; i < len(args.L1CoordinatorTxs); i++ {
  319. l1Coord := args.L1CoordinatorTxs[i]
  320. bytesl1Coord, err := l1Coord.BytesDataAvailability(uint32(nLevels))
  321. if err != nil {
  322. return nil, tracerr.Wrap(err)
  323. }
  324. l1l2TxData = append(l1l2TxData, bytesl1Coord[:]...)
  325. }
  326. for i := 0; i < len(args.L2TxsData); i++ {
  327. l2 := args.L2TxsData[i]
  328. bytesl2, err := l2.BytesDataAvailability(uint32(nLevels))
  329. if err != nil {
  330. return nil, tracerr.Wrap(err)
  331. }
  332. l1l2TxData = append(l1l2TxData, bytesl2[:]...)
  333. }
  334. // FeeIdxCoordinator
  335. var feeIdxCoordinator []byte
  336. if len(args.FeeIdxCoordinator) > common.RollupConstMaxFeeIdxCoordinator {
  337. return nil, tracerr.Wrap(fmt.Errorf("len(args.FeeIdxCoordinator) > %v",
  338. common.RollupConstMaxFeeIdxCoordinator))
  339. }
  340. for i := 0; i < common.RollupConstMaxFeeIdxCoordinator; i++ {
  341. feeIdx := common.Idx(0)
  342. if i < len(args.FeeIdxCoordinator) {
  343. feeIdx = args.FeeIdxCoordinator[i]
  344. }
  345. bytesFeeIdx, err := feeIdx.Bytes()
  346. if err != nil {
  347. return nil, tracerr.Wrap(err)
  348. }
  349. feeIdxCoordinator = append(feeIdxCoordinator,
  350. bytesFeeIdx[len(bytesFeeIdx)-int(lenBytes):]...)
  351. }
  352. tx, err = c.hermez.ForgeBatch(auth, newLastIdx, args.NewStRoot, args.NewExitRoot,
  353. l1CoordinatorBytes, l1l2TxData, feeIdxCoordinator, args.VerifierIdx, args.L1Batch,
  354. args.ProofA, args.ProofB, args.ProofC)
  355. if err != nil {
  356. return nil, tracerr.Wrap(fmt.Errorf("Hermez.ForgeBatch: %w", err))
  357. }
  358. return tx, nil
  359. }
  360. // RollupAddToken is the interface to call the smart contract function.
  361. // `feeAddToken` is the amount of HEZ tokens that will be paid to add the
  362. // token. `feeAddToken` must match the public value of the smart contract.
  363. func (c *RollupClient) RollupAddToken(tokenAddress ethCommon.Address, feeAddToken, deadline *big.Int) (tx *types.Transaction, err error) {
  364. if tx, err = c.client.CallAuth(
  365. 0,
  366. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  367. owner := c.client.account.Address
  368. spender := c.address
  369. nonce, err := c.tokenHEZ.Nonces(c.opts, owner)
  370. if err != nil {
  371. return nil, tracerr.Wrap(err)
  372. }
  373. tokenName := c.tokenHEZCfg.Name
  374. tokenAddr := c.tokenHEZCfg.Address
  375. digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, feeAddToken, nonce, deadline, tokenName)
  376. signature, _ := c.client.ks.SignHash(*c.client.account, digest)
  377. permit := createPermit(owner, spender, feeAddToken, deadline, digest, signature)
  378. return c.hermez.AddToken(auth, tokenAddress, permit)
  379. },
  380. ); err != nil {
  381. return nil, tracerr.Wrap(fmt.Errorf("Failed add Token %w", err))
  382. }
  383. return tx, nil
  384. }
  385. // RollupWithdrawMerkleProof is the interface to call the smart contract function
  386. func (c *RollupClient) RollupWithdrawMerkleProof(fromBJJ babyjub.PublicKeyComp, tokenID uint32, numExitRoot, idx int64, amount *big.Int, siblings []*big.Int, instantWithdraw bool) (tx *types.Transaction, err error) {
  387. if tx, err = c.client.CallAuth(
  388. 0,
  389. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  390. pkCompB := common.SwapEndianness(fromBJJ[:])
  391. babyPubKey := new(big.Int).SetBytes(pkCompB)
  392. numExitRootB := uint32(numExitRoot)
  393. idxBig := big.NewInt(idx)
  394. return c.hermez.WithdrawMerkleProof(auth, tokenID, amount, babyPubKey, numExitRootB, siblings, idxBig, instantWithdraw)
  395. },
  396. ); err != nil {
  397. return nil, tracerr.Wrap(fmt.Errorf("Failed update WithdrawMerkleProof: %w", err))
  398. }
  399. return tx, nil
  400. }
  401. // RollupWithdrawCircuit is the interface to call the smart contract function
  402. func (c *RollupClient) RollupWithdrawCircuit(proofA, proofC [2]*big.Int, proofB [2][2]*big.Int, tokenID uint32, numExitRoot, idx int64, amount *big.Int, instantWithdraw bool) (*types.Transaction, error) {
  403. log.Error("TODO")
  404. return nil, tracerr.Wrap(errTODO)
  405. }
  406. // RollupL1UserTxERC20ETH is the interface to call the smart contract function
  407. func (c *RollupClient) RollupL1UserTxERC20ETH(fromBJJ babyjub.PublicKeyComp, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64) (tx *types.Transaction, err error) {
  408. if tx, err = c.client.CallAuth(
  409. 0,
  410. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  411. var babyPubKey *big.Int
  412. if fromBJJ != common.EmptyBJJComp {
  413. pkCompB := common.SwapEndianness(fromBJJ[:])
  414. babyPubKey = new(big.Int).SetBytes(pkCompB)
  415. } else {
  416. babyPubKey = big.NewInt(0)
  417. }
  418. fromIdxBig := big.NewInt(fromIdx)
  419. toIdxBig := big.NewInt(toIdx)
  420. depositAmountF, err := common.NewFloat40(depositAmount)
  421. if err != nil {
  422. return nil, tracerr.Wrap(err)
  423. }
  424. amountF, err := common.NewFloat40(amount)
  425. if err != nil {
  426. return nil, tracerr.Wrap(err)
  427. }
  428. if tokenID == 0 {
  429. auth.Value = depositAmount
  430. }
  431. var permit []byte
  432. return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(depositAmountF),
  433. uint16(amountF), tokenID, toIdxBig, permit)
  434. },
  435. ); err != nil {
  436. return nil, tracerr.Wrap(fmt.Errorf("Failed add L1 Tx ERC20/ETH: %w", err))
  437. }
  438. return tx, nil
  439. }
  440. // RollupL1UserTxERC20Permit is the interface to call the smart contract function
  441. func (c *RollupClient) RollupL1UserTxERC20Permit(fromBJJ babyjub.PublicKeyComp, fromIdx int64, depositAmount *big.Int, amount *big.Int, tokenID uint32, toIdx int64, deadline *big.Int) (tx *types.Transaction, err error) {
  442. if tx, err = c.client.CallAuth(
  443. 0,
  444. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  445. var babyPubKey *big.Int
  446. if fromBJJ != common.EmptyBJJComp {
  447. pkCompB := common.SwapEndianness(fromBJJ[:])
  448. babyPubKey = new(big.Int).SetBytes(pkCompB)
  449. } else {
  450. babyPubKey = big.NewInt(0)
  451. }
  452. fromIdxBig := big.NewInt(fromIdx)
  453. toIdxBig := big.NewInt(toIdx)
  454. depositAmountF, err := common.NewFloat40(depositAmount)
  455. if err != nil {
  456. return nil, tracerr.Wrap(err)
  457. }
  458. amountF, err := common.NewFloat40(amount)
  459. if err != nil {
  460. return nil, tracerr.Wrap(err)
  461. }
  462. if tokenID == 0 {
  463. auth.Value = depositAmount
  464. }
  465. owner := c.client.account.Address
  466. spender := c.address
  467. nonce, err := c.tokenHEZ.Nonces(c.opts, owner)
  468. if err != nil {
  469. return nil, tracerr.Wrap(err)
  470. }
  471. tokenName := c.tokenHEZCfg.Name
  472. tokenAddr := c.tokenHEZCfg.Address
  473. digest, _ := createPermitDigest(tokenAddr, owner, spender, c.chainID, amount, nonce, deadline, tokenName)
  474. signature, _ := c.client.ks.SignHash(*c.client.account, digest)
  475. permit := createPermit(owner, spender, amount, deadline, digest, signature)
  476. return c.hermez.AddL1Transaction(auth, babyPubKey, fromIdxBig, uint16(depositAmountF),
  477. uint16(amountF), tokenID, toIdxBig, permit)
  478. },
  479. ); err != nil {
  480. return nil, tracerr.Wrap(fmt.Errorf("Failed add L1 Tx ERC20Permit: %w", err))
  481. }
  482. return tx, nil
  483. }
  484. // RollupRegisterTokensCount is the interface to call the smart contract function
  485. func (c *RollupClient) RollupRegisterTokensCount() (registerTokensCount *big.Int, err error) {
  486. if err := c.client.Call(func(ec *ethclient.Client) error {
  487. registerTokensCount, err = c.hermez.RegisterTokensCount(c.opts)
  488. return tracerr.Wrap(err)
  489. }); err != nil {
  490. return nil, tracerr.Wrap(err)
  491. }
  492. return registerTokensCount, nil
  493. }
  494. // RollupLastForgedBatch is the interface to call the smart contract function
  495. func (c *RollupClient) RollupLastForgedBatch() (lastForgedBatch int64, err error) {
  496. if err := c.client.Call(func(ec *ethclient.Client) error {
  497. _lastForgedBatch, err := c.hermez.LastForgedBatch(c.opts)
  498. lastForgedBatch = int64(_lastForgedBatch)
  499. return tracerr.Wrap(err)
  500. }); err != nil {
  501. return 0, tracerr.Wrap(err)
  502. }
  503. return lastForgedBatch, nil
  504. }
  505. // RollupUpdateForgeL1L2BatchTimeout is the interface to call the smart contract function
  506. func (c *RollupClient) RollupUpdateForgeL1L2BatchTimeout(newForgeL1L2BatchTimeout int64) (tx *types.Transaction, err error) {
  507. if tx, err = c.client.CallAuth(
  508. 0,
  509. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  510. return c.hermez.UpdateForgeL1L2BatchTimeout(auth, uint8(newForgeL1L2BatchTimeout))
  511. },
  512. ); err != nil {
  513. return nil, tracerr.Wrap(fmt.Errorf("Failed update ForgeL1L2BatchTimeout: %w", err))
  514. }
  515. return tx, nil
  516. }
  517. // RollupUpdateFeeAddToken is the interface to call the smart contract function
  518. func (c *RollupClient) RollupUpdateFeeAddToken(newFeeAddToken *big.Int) (tx *types.Transaction, err error) {
  519. if tx, err = c.client.CallAuth(
  520. 0,
  521. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  522. return c.hermez.UpdateFeeAddToken(auth, newFeeAddToken)
  523. },
  524. ); err != nil {
  525. return nil, tracerr.Wrap(fmt.Errorf("Failed update FeeAddToken: %w", err))
  526. }
  527. return tx, nil
  528. }
  529. // RollupUpdateBucketsParameters is the interface to call the smart contract function
  530. func (c *RollupClient) RollupUpdateBucketsParameters(
  531. arrayBuckets [common.RollupConstNumBuckets]RollupUpdateBucketsParameters,
  532. ) (tx *types.Transaction, err error) {
  533. params := [common.RollupConstNumBuckets][4]*big.Int{}
  534. for i, bucket := range arrayBuckets {
  535. params[i][0] = bucket.CeilUSD
  536. params[i][1] = bucket.Withdrawals
  537. params[i][2] = bucket.BlockWithdrawalRate
  538. params[i][3] = bucket.MaxWithdrawals
  539. }
  540. if tx, err = c.client.CallAuth(
  541. 12500000, //nolint:gomnd
  542. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  543. return c.hermez.UpdateBucketsParameters(auth, params)
  544. },
  545. ); err != nil {
  546. return nil, tracerr.Wrap(fmt.Errorf("Failed update Buckets Parameters: %w", err))
  547. }
  548. return tx, nil
  549. }
  550. // RollupUpdateTokenExchange is the interface to call the smart contract function
  551. func (c *RollupClient) RollupUpdateTokenExchange(addressArray []ethCommon.Address, valueArray []uint64) (tx *types.Transaction, err error) {
  552. if tx, err = c.client.CallAuth(
  553. 0,
  554. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  555. return c.hermez.UpdateTokenExchange(auth, addressArray, valueArray)
  556. },
  557. ); err != nil {
  558. return nil, tracerr.Wrap(fmt.Errorf("Failed update Token Exchange: %w", err))
  559. }
  560. return tx, nil
  561. }
  562. // RollupUpdateWithdrawalDelay is the interface to call the smart contract function
  563. func (c *RollupClient) RollupUpdateWithdrawalDelay(newWithdrawalDelay int64) (tx *types.Transaction, err error) {
  564. if tx, err = c.client.CallAuth(
  565. 0,
  566. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  567. return c.hermez.UpdateWithdrawalDelay(auth, uint64(newWithdrawalDelay))
  568. },
  569. ); err != nil {
  570. return nil, tracerr.Wrap(fmt.Errorf("Failed update WithdrawalDelay: %w", err))
  571. }
  572. return tx, nil
  573. }
  574. // RollupSafeMode is the interface to call the smart contract function
  575. func (c *RollupClient) RollupSafeMode() (tx *types.Transaction, err error) {
  576. if tx, err = c.client.CallAuth(
  577. 0,
  578. func(ec *ethclient.Client, auth *bind.TransactOpts) (*types.Transaction, error) {
  579. return c.hermez.SafeMode(auth)
  580. },
  581. ); err != nil {
  582. return nil, tracerr.Wrap(fmt.Errorf("Failed update Safe Mode: %w", err))
  583. }
  584. return tx, nil
  585. }
  586. // RollupInstantWithdrawalViewer is the interface to call the smart contract function
  587. func (c *RollupClient) RollupInstantWithdrawalViewer(tokenAddress ethCommon.Address, amount *big.Int) (instantAllowed bool, err error) {
  588. if err := c.client.Call(func(ec *ethclient.Client) error {
  589. instantAllowed, err = c.hermez.InstantWithdrawalViewer(c.opts, tokenAddress, amount)
  590. return tracerr.Wrap(err)
  591. }); err != nil {
  592. return false, tracerr.Wrap(err)
  593. }
  594. return instantAllowed, nil
  595. }
  596. // RollupConstants returns the Constants of the Rollup Smart Contract
  597. func (c *RollupClient) RollupConstants() (rollupConstants *common.RollupConstants, err error) {
  598. rollupConstants = new(common.RollupConstants)
  599. if err := c.client.Call(func(ec *ethclient.Client) error {
  600. absoluteMaxL1L2BatchTimeout, err := c.hermez.ABSOLUTEMAXL1L2BATCHTIMEOUT(c.opts)
  601. if err != nil {
  602. return tracerr.Wrap(err)
  603. }
  604. rollupConstants.AbsoluteMaxL1L2BatchTimeout = int64(absoluteMaxL1L2BatchTimeout)
  605. rollupConstants.TokenHEZ, err = c.hermez.TokenHEZ(c.opts)
  606. if err != nil {
  607. return tracerr.Wrap(err)
  608. }
  609. rollupVerifiersLength, err := c.hermez.RollupVerifiersLength(c.opts)
  610. if err != nil {
  611. return tracerr.Wrap(err)
  612. }
  613. for i := int64(0); i < rollupVerifiersLength.Int64(); i++ {
  614. var newRollupVerifier common.RollupVerifierStruct
  615. rollupVerifier, err := c.hermez.RollupVerifiers(c.opts, big.NewInt(i))
  616. if err != nil {
  617. return tracerr.Wrap(err)
  618. }
  619. newRollupVerifier.MaxTx = rollupVerifier.MaxTx.Int64()
  620. newRollupVerifier.NLevels = rollupVerifier.NLevels.Int64()
  621. rollupConstants.Verifiers = append(rollupConstants.Verifiers, newRollupVerifier)
  622. }
  623. rollupConstants.HermezAuctionContract, err = c.hermez.HermezAuctionContract(c.opts)
  624. if err != nil {
  625. return tracerr.Wrap(err)
  626. }
  627. rollupConstants.HermezGovernanceAddress, err = c.hermez.HermezGovernanceAddress(c.opts)
  628. if err != nil {
  629. return tracerr.Wrap(err)
  630. }
  631. rollupConstants.WithdrawDelayerContract, err = c.hermez.WithdrawDelayerContract(c.opts)
  632. return tracerr.Wrap(err)
  633. }); err != nil {
  634. return nil, tracerr.Wrap(err)
  635. }
  636. return rollupConstants, nil
  637. }
  638. var (
  639. logHermezL1UserTxEvent = crypto.Keccak256Hash([]byte("L1UserTxEvent(uint32,uint8,bytes)"))
  640. logHermezAddToken = crypto.Keccak256Hash([]byte("AddToken(address,uint32)"))
  641. logHermezForgeBatch = crypto.Keccak256Hash([]byte("ForgeBatch(uint32,uint16)"))
  642. logHermezUpdateForgeL1L2BatchTimeout = crypto.Keccak256Hash([]byte("UpdateForgeL1L2BatchTimeout(uint8)"))
  643. logHermezUpdateFeeAddToken = crypto.Keccak256Hash([]byte("UpdateFeeAddToken(uint256)"))
  644. logHermezWithdrawEvent = crypto.Keccak256Hash([]byte("WithdrawEvent(uint48,uint32,bool)"))
  645. logHermezUpdateBucketWithdraw = crypto.Keccak256Hash([]byte("UpdateBucketWithdraw(uint8,uint256,uint256)"))
  646. logHermezUpdateWithdrawalDelay = crypto.Keccak256Hash([]byte("UpdateWithdrawalDelay(uint64)"))
  647. logHermezUpdateBucketsParameters = crypto.Keccak256Hash([]byte("UpdateBucketsParameters(uint256[4][" +
  648. strconv.Itoa(common.RollupConstNumBuckets) + "])"))
  649. logHermezUpdateTokenExchange = crypto.Keccak256Hash([]byte("UpdateTokenExchange(address[],uint64[])"))
  650. logHermezSafeMode = crypto.Keccak256Hash([]byte("SafeMode()"))
  651. logHermezInitialize = crypto.Keccak256Hash([]byte("InitializeHermezEvent(uint8,uint256,uint64)"))
  652. )
  653. // RollupEventInit returns the initialize event with its corresponding block number
  654. func (c *RollupClient) RollupEventInit() (*RollupEventInitialize, int64, error) {
  655. query := ethereum.FilterQuery{
  656. Addresses: []ethCommon.Address{
  657. c.address,
  658. },
  659. Topics: [][]ethCommon.Hash{{logHermezInitialize}},
  660. }
  661. logs, err := c.client.client.FilterLogs(context.Background(), query)
  662. if err != nil {
  663. return nil, 0, tracerr.Wrap(err)
  664. }
  665. if len(logs) != 1 {
  666. return nil, 0, tracerr.Wrap(fmt.Errorf("no event of type InitializeHermezEvent found"))
  667. }
  668. vLog := logs[0]
  669. if vLog.Topics[0] != logHermezInitialize {
  670. return nil, 0, tracerr.Wrap(fmt.Errorf("event is not InitializeHermezEvent"))
  671. }
  672. var rollupInit RollupEventInitialize
  673. if err := c.contractAbi.UnpackIntoInterface(&rollupInit, "InitializeHermezEvent", vLog.Data); err != nil {
  674. return nil, 0, tracerr.Wrap(err)
  675. }
  676. return &rollupInit, int64(vLog.BlockNumber), tracerr.Wrap(err)
  677. }
  678. // RollupEventsByBlock returns the events in a block that happened in the
  679. // Rollup Smart Contract.
  680. // To query by blockNum, set blockNum >= 0 and blockHash == nil.
  681. // To query by blockHash set blockHash != nil, and blockNum will be ignored.
  682. // If there are no events in that block the result is nil.
  683. func (c *RollupClient) RollupEventsByBlock(blockNum int64,
  684. blockHash *ethCommon.Hash) (*RollupEvents, error) {
  685. var rollupEvents RollupEvents
  686. var blockNumBigInt *big.Int
  687. if blockHash == nil {
  688. blockNumBigInt = big.NewInt(blockNum)
  689. }
  690. query := ethereum.FilterQuery{
  691. BlockHash: blockHash,
  692. FromBlock: blockNumBigInt,
  693. ToBlock: blockNumBigInt,
  694. Addresses: []ethCommon.Address{
  695. c.address,
  696. },
  697. Topics: [][]ethCommon.Hash{},
  698. }
  699. logs, err := c.client.client.FilterLogs(context.Background(), query)
  700. if err != nil {
  701. return nil, tracerr.Wrap(err)
  702. }
  703. if len(logs) == 0 {
  704. return nil, nil
  705. }
  706. for _, vLog := range logs {
  707. if blockHash != nil && vLog.BlockHash != *blockHash {
  708. log.Errorw("Block hash mismatch", "expected", blockHash.String(), "got", vLog.BlockHash.String())
  709. return nil, tracerr.Wrap(ErrBlockHashMismatchEvent)
  710. }
  711. switch vLog.Topics[0] {
  712. case logHermezL1UserTxEvent:
  713. var L1UserTxAux rollupEventL1UserTxAux
  714. var L1UserTx RollupEventL1UserTx
  715. err := c.contractAbi.UnpackIntoInterface(&L1UserTxAux, "L1UserTxEvent", vLog.Data)
  716. if err != nil {
  717. return nil, tracerr.Wrap(err)
  718. }
  719. L1Tx, err := common.L1UserTxFromBytes(L1UserTxAux.L1UserTx)
  720. if err != nil {
  721. return nil, tracerr.Wrap(err)
  722. }
  723. toForgeL1TxsNum := new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  724. L1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
  725. L1Tx.Position = int(new(big.Int).SetBytes(vLog.Topics[2][:]).Int64())
  726. L1Tx.UserOrigin = true
  727. L1UserTx.L1UserTx = *L1Tx
  728. rollupEvents.L1UserTx = append(rollupEvents.L1UserTx, L1UserTx)
  729. case logHermezAddToken:
  730. var addToken RollupEventAddToken
  731. err := c.contractAbi.UnpackIntoInterface(&addToken, "AddToken", vLog.Data)
  732. if err != nil {
  733. return nil, tracerr.Wrap(err)
  734. }
  735. addToken.TokenAddress = ethCommon.BytesToAddress(vLog.Topics[1].Bytes())
  736. rollupEvents.AddToken = append(rollupEvents.AddToken, addToken)
  737. case logHermezForgeBatch:
  738. var forgeBatch RollupEventForgeBatch
  739. err := c.contractAbi.UnpackIntoInterface(&forgeBatch, "ForgeBatch", vLog.Data)
  740. if err != nil {
  741. return nil, tracerr.Wrap(err)
  742. }
  743. forgeBatch.BatchNum = new(big.Int).SetBytes(vLog.Topics[1][:]).Int64()
  744. forgeBatch.EthTxHash = vLog.TxHash
  745. // forgeBatch.Sender = vLog.Address
  746. rollupEvents.ForgeBatch = append(rollupEvents.ForgeBatch, forgeBatch)
  747. case logHermezUpdateForgeL1L2BatchTimeout:
  748. var updateForgeL1L2BatchTimeout struct {
  749. NewForgeL1L2BatchTimeout uint8
  750. }
  751. err := c.contractAbi.UnpackIntoInterface(&updateForgeL1L2BatchTimeout, "UpdateForgeL1L2BatchTimeout", vLog.Data)
  752. if err != nil {
  753. return nil, tracerr.Wrap(err)
  754. }
  755. rollupEvents.UpdateForgeL1L2BatchTimeout = append(rollupEvents.UpdateForgeL1L2BatchTimeout,
  756. RollupEventUpdateForgeL1L2BatchTimeout{
  757. NewForgeL1L2BatchTimeout: int64(updateForgeL1L2BatchTimeout.NewForgeL1L2BatchTimeout),
  758. })
  759. case logHermezUpdateFeeAddToken:
  760. var updateFeeAddToken RollupEventUpdateFeeAddToken
  761. err := c.contractAbi.UnpackIntoInterface(&updateFeeAddToken, "UpdateFeeAddToken", vLog.Data)
  762. if err != nil {
  763. return nil, tracerr.Wrap(err)
  764. }
  765. rollupEvents.UpdateFeeAddToken = append(rollupEvents.UpdateFeeAddToken, updateFeeAddToken)
  766. case logHermezWithdrawEvent:
  767. var withdraw RollupEventWithdraw
  768. withdraw.Idx = new(big.Int).SetBytes(vLog.Topics[1][:]).Uint64()
  769. withdraw.NumExitRoot = new(big.Int).SetBytes(vLog.Topics[2][:]).Uint64()
  770. instantWithdraw := new(big.Int).SetBytes(vLog.Topics[3][:]).Uint64()
  771. if instantWithdraw == 1 {
  772. withdraw.InstantWithdraw = true
  773. }
  774. withdraw.TxHash = vLog.TxHash
  775. rollupEvents.Withdraw = append(rollupEvents.Withdraw, withdraw)
  776. case logHermezUpdateBucketWithdraw:
  777. var updateBucketWithdrawAux rollupEventUpdateBucketWithdrawAux
  778. var updateBucketWithdraw RollupEventUpdateBucketWithdraw
  779. err := c.contractAbi.UnpackIntoInterface(&updateBucketWithdrawAux, "UpdateBucketWithdraw", vLog.Data)
  780. if err != nil {
  781. return nil, tracerr.Wrap(err)
  782. }
  783. updateBucketWithdraw.Withdrawals = updateBucketWithdrawAux.Withdrawals
  784. updateBucketWithdraw.NumBucket = int(new(big.Int).SetBytes(vLog.Topics[1][:]).Int64())
  785. updateBucketWithdraw.BlockStamp = new(big.Int).SetBytes(vLog.Topics[2][:]).Int64()
  786. rollupEvents.UpdateBucketWithdraw = append(rollupEvents.UpdateBucketWithdraw, updateBucketWithdraw)
  787. case logHermezUpdateWithdrawalDelay:
  788. var withdrawalDelay RollupEventUpdateWithdrawalDelay
  789. err := c.contractAbi.UnpackIntoInterface(&withdrawalDelay, "UpdateWithdrawalDelay", vLog.Data)
  790. if err != nil {
  791. return nil, tracerr.Wrap(err)
  792. }
  793. rollupEvents.UpdateWithdrawalDelay = append(rollupEvents.UpdateWithdrawalDelay, withdrawalDelay)
  794. case logHermezUpdateBucketsParameters:
  795. var bucketsParametersAux rollupEventUpdateBucketsParametersAux
  796. var bucketsParameters RollupEventUpdateBucketsParameters
  797. err := c.contractAbi.UnpackIntoInterface(&bucketsParametersAux, "UpdateBucketsParameters", vLog.Data)
  798. if err != nil {
  799. return nil, tracerr.Wrap(err)
  800. }
  801. for i, bucket := range bucketsParametersAux.ArrayBuckets {
  802. bucketsParameters.ArrayBuckets[i].CeilUSD = bucket[0]
  803. bucketsParameters.ArrayBuckets[i].Withdrawals = bucket[1]
  804. bucketsParameters.ArrayBuckets[i].BlockWithdrawalRate = bucket[2]
  805. bucketsParameters.ArrayBuckets[i].MaxWithdrawals = bucket[3]
  806. }
  807. rollupEvents.UpdateBucketsParameters = append(rollupEvents.UpdateBucketsParameters, bucketsParameters)
  808. case logHermezUpdateTokenExchange:
  809. var tokensExchange RollupEventUpdateTokenExchange
  810. err := c.contractAbi.UnpackIntoInterface(&tokensExchange, "UpdateTokenExchange", vLog.Data)
  811. if err != nil {
  812. return nil, tracerr.Wrap(err)
  813. }
  814. rollupEvents.UpdateTokenExchange = append(rollupEvents.UpdateTokenExchange, tokensExchange)
  815. case logHermezSafeMode:
  816. var safeMode RollupEventSafeMode
  817. rollupEvents.SafeMode = append(rollupEvents.SafeMode, safeMode)
  818. // Also add an UpdateBucketsParameter with
  819. // SafeMode=true to keep the order between `safeMode`
  820. // and `UpdateBucketsParameters`
  821. bucketsParameters := RollupEventUpdateBucketsParameters{
  822. SafeMode: true,
  823. }
  824. for i := range bucketsParameters.ArrayBuckets {
  825. bucketsParameters.ArrayBuckets[i].CeilUSD = big.NewInt(0)
  826. bucketsParameters.ArrayBuckets[i].Withdrawals = big.NewInt(0)
  827. bucketsParameters.ArrayBuckets[i].BlockWithdrawalRate = big.NewInt(0)
  828. bucketsParameters.ArrayBuckets[i].MaxWithdrawals = big.NewInt(0)
  829. }
  830. rollupEvents.UpdateBucketsParameters = append(rollupEvents.UpdateBucketsParameters,
  831. bucketsParameters)
  832. }
  833. }
  834. return &rollupEvents, nil
  835. }
  836. // RollupForgeBatchArgs returns the arguments used in a ForgeBatch call in the
  837. // Rollup Smart Contract in the given transaction, and the sender address.
  838. func (c *RollupClient) RollupForgeBatchArgs(ethTxHash ethCommon.Hash, l1UserTxsLen uint16) (*RollupForgeBatchArgs, *ethCommon.Address, error) {
  839. tx, _, err := c.client.client.TransactionByHash(context.Background(), ethTxHash)
  840. if err != nil {
  841. return nil, nil, tracerr.Wrap(fmt.Errorf("TransactionByHash: %w", err))
  842. }
  843. txData := tx.Data()
  844. method, err := c.contractAbi.MethodById(txData[:4])
  845. if err != nil {
  846. return nil, nil, tracerr.Wrap(err)
  847. }
  848. receipt, err := c.client.client.TransactionReceipt(context.Background(), ethTxHash)
  849. if err != nil {
  850. return nil, nil, tracerr.Wrap(err)
  851. }
  852. sender, err := c.client.client.TransactionSender(context.Background(), tx, receipt.Logs[0].BlockHash, receipt.Logs[0].Index)
  853. if err != nil {
  854. return nil, nil, tracerr.Wrap(err)
  855. }
  856. var aux rollupForgeBatchArgsAux
  857. if values, err := method.Inputs.Unpack(txData[4:]); err != nil {
  858. return nil, nil, tracerr.Wrap(err)
  859. } else if err := method.Inputs.Copy(&aux, values); err != nil {
  860. return nil, nil, tracerr.Wrap(err)
  861. }
  862. rollupForgeBatchArgs := RollupForgeBatchArgs{
  863. L1Batch: aux.L1Batch,
  864. NewExitRoot: aux.NewExitRoot,
  865. NewLastIdx: aux.NewLastIdx.Int64(),
  866. NewStRoot: aux.NewStRoot,
  867. ProofA: aux.ProofA,
  868. ProofB: aux.ProofB,
  869. ProofC: aux.ProofC,
  870. VerifierIdx: aux.VerifierIdx,
  871. L1CoordinatorTxs: []common.L1Tx{},
  872. L1CoordinatorTxsAuths: [][]byte{},
  873. L2TxsData: []common.L2Tx{},
  874. FeeIdxCoordinator: []common.Idx{},
  875. }
  876. nLevels := c.consts.Verifiers[rollupForgeBatchArgs.VerifierIdx].NLevels
  877. lenL1L2TxsBytes := int((nLevels/8)*2 + common.Float40BytesLength + 1)
  878. numBytesL1TxUser := int(l1UserTxsLen) * lenL1L2TxsBytes
  879. numTxsL1Coord := len(aux.EncodedL1CoordinatorTx) / common.RollupConstL1CoordinatorTotalBytes
  880. numBytesL1TxCoord := numTxsL1Coord * lenL1L2TxsBytes
  881. numBeginL2Tx := numBytesL1TxCoord + numBytesL1TxUser
  882. l1UserTxsData := []byte{}
  883. if l1UserTxsLen > 0 {
  884. l1UserTxsData = aux.L1L2TxsData[:numBytesL1TxUser]
  885. }
  886. for i := 0; i < int(l1UserTxsLen); i++ {
  887. l1Tx, err := common.L1TxFromDataAvailability(l1UserTxsData[i*lenL1L2TxsBytes:(i+1)*lenL1L2TxsBytes], uint32(nLevels))
  888. if err != nil {
  889. return nil, nil, tracerr.Wrap(err)
  890. }
  891. rollupForgeBatchArgs.L1UserTxs = append(rollupForgeBatchArgs.L1UserTxs, *l1Tx)
  892. }
  893. l2TxsData := []byte{}
  894. if numBeginL2Tx < len(aux.L1L2TxsData) {
  895. l2TxsData = aux.L1L2TxsData[numBeginL2Tx:]
  896. }
  897. numTxsL2 := len(l2TxsData) / lenL1L2TxsBytes
  898. for i := 0; i < numTxsL2; i++ {
  899. l2Tx, err := common.L2TxFromBytesDataAvailability(l2TxsData[i*lenL1L2TxsBytes:(i+1)*lenL1L2TxsBytes], int(nLevels))
  900. if err != nil {
  901. return nil, nil, tracerr.Wrap(err)
  902. }
  903. rollupForgeBatchArgs.L2TxsData = append(rollupForgeBatchArgs.L2TxsData, *l2Tx)
  904. }
  905. for i := 0; i < numTxsL1Coord; i++ {
  906. bytesL1Coordinator := aux.EncodedL1CoordinatorTx[i*common.RollupConstL1CoordinatorTotalBytes : (i+1)*common.RollupConstL1CoordinatorTotalBytes]
  907. var signature []byte
  908. v := bytesL1Coordinator[0]
  909. s := bytesL1Coordinator[1:33]
  910. r := bytesL1Coordinator[33:65]
  911. signature = append(signature, r[:]...)
  912. signature = append(signature, s[:]...)
  913. signature = append(signature, v)
  914. l1Tx, err := common.L1CoordinatorTxFromBytes(bytesL1Coordinator, c.chainID, c.address)
  915. if err != nil {
  916. return nil, nil, tracerr.Wrap(err)
  917. }
  918. rollupForgeBatchArgs.L1CoordinatorTxs = append(rollupForgeBatchArgs.L1CoordinatorTxs, *l1Tx)
  919. rollupForgeBatchArgs.L1CoordinatorTxsAuths = append(rollupForgeBatchArgs.L1CoordinatorTxsAuths, signature)
  920. }
  921. lenFeeIdxCoordinatorBytes := int(nLevels / 8) //nolint:gomnd
  922. numFeeIdxCoordinator := len(aux.FeeIdxCoordinator) / lenFeeIdxCoordinatorBytes
  923. for i := 0; i < numFeeIdxCoordinator; i++ {
  924. var paddedFeeIdx [6]byte
  925. // TODO: This check is not necessary: the first case will always work. Test it before removing the if.
  926. if lenFeeIdxCoordinatorBytes < common.IdxBytesLen {
  927. copy(paddedFeeIdx[6-lenFeeIdxCoordinatorBytes:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  928. } else {
  929. copy(paddedFeeIdx[:], aux.FeeIdxCoordinator[i*lenFeeIdxCoordinatorBytes:(i+1)*lenFeeIdxCoordinatorBytes])
  930. }
  931. feeIdxCoordinator, err := common.IdxFromBytes(paddedFeeIdx[:])
  932. if err != nil {
  933. return nil, nil, tracerr.Wrap(err)
  934. }
  935. if feeIdxCoordinator != common.Idx(0) {
  936. rollupForgeBatchArgs.FeeIdxCoordinator = append(rollupForgeBatchArgs.FeeIdxCoordinator, feeIdxCoordinator)
  937. }
  938. }
  939. return &rollupForgeBatchArgs, &sender, nil
  940. }