-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1405: ForcedExit automation (the core functionality) r=Deniallugo a=StanislavBreadless **Note: in order to split the project into several tasks as much as possible, the complete unit test coverage and some additional functionality (like periodic db cleanup) will be provided in the subsequent PRs.** The basic flow is the following (can be seen in the integration tests): - When users want to ForcedExit some funds from an account, they submit a request to the server, which remembers the request in the DB. The users receive back the stored request. - In order to pay for the request the user has to send exactly `amount + id` wei to the ForcedExit smart contract. When the contract receives the funds it emits an event with the amount received. - When the server sees the transaction, it extracts the id, checks if everything is correct (e.g. the request has not expired, the amount is correct, etc). If everything is great, then the server submits the ForcedExit request into the mempool. **Follow-up tasks:** - Fill `zksync_forced_exit_requests` with unit-tests. Also, add some more unit-tests tests for the related API and the storage. [ZKS-493](https://linear.app/matterlabs/issue/ZKS-493/unit-test-coverage-for-zksync-forced-exit-requests) - Clean DB from old unfulfilled requests. Notify when the space of the ids of the forced exit requests is close to being full. (to prevent id overflow) [ZKS-494](https://linear.app/matterlabs/issue/ZKS-494/fe-automation-dos-prevention) - When checking if a request has expired use the block timestamp of the event instead of `Utc::now()` [ZKS-495](https://linear.app/matterlabs/issue/ZKS-495/fe-automation-use-block-timestamp-to-check-the-expiration) **Some code may be very similar to the code in other crates (especially the eth_watch)**. This was done to make the forced exit automation fundamentally separate from the rest of the crates as well as provide a smaller diff to the `zksync_core` crate. Co-authored-by: Stanislav Bezkorovainyi <[email protected]>
- Loading branch information
Showing
64 changed files
with
4,131 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
pragma solidity ^0.7.0; | ||
|
||
pragma experimental ABIEncoderV2; | ||
|
||
import "./Utils.sol"; | ||
import "./Ownable.sol"; | ||
import "./ReentrancyGuard.sol"; | ||
|
||
contract ForcedExit is Ownable, ReentrancyGuard { | ||
// This is the role of the zkSync server | ||
// that will be able to withdraw the funds | ||
address payable public receiver; | ||
|
||
bool public enabled = true; | ||
|
||
constructor(address _master, address _receiver) Ownable(_master) { | ||
initializeReentrancyGuard(); | ||
|
||
// The master is the default receiver | ||
receiver = payable(_receiver); | ||
} | ||
|
||
event FundsReceived(uint256 _amount); | ||
|
||
function setReceiver(address payable _newReceiver) external { | ||
requireMaster(msg.sender); | ||
|
||
receiver = _newReceiver; | ||
} | ||
|
||
function withdrawPendingFunds(address payable _to) external nonReentrant { | ||
require( | ||
msg.sender == receiver || msg.sender == getMaster(), | ||
"Only the receiver or master can withdraw funds from the smart contract" | ||
); | ||
|
||
uint256 balance = address(this).balance; | ||
|
||
(bool success, ) = _to.call{value: balance}(""); | ||
require(success, "ETH withdraw failed"); | ||
} | ||
|
||
// We have to use fallback instead of `receive` since the ethabi | ||
// library can't decode the receive function: | ||
// https://github.com/rust-ethereum/ethabi/issues/185 | ||
fallback() external payable { | ||
emit FundsReceived(msg.value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
pragma solidity ^0.7.0; | ||
|
||
pragma experimental ABIEncoderV2; | ||
|
||
contract SelfDestruct { | ||
function destroy(address payable to) external { | ||
selfdestruct(to); | ||
} | ||
|
||
// Need this to send some funds to the contract | ||
receive() external payable {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { expect, use } from 'chai'; | ||
import { solidity } from 'ethereum-waffle'; | ||
import { Signer, utils } from 'ethers'; | ||
import { ForcedExit } from '../../typechain/ForcedExit'; | ||
import { ForcedExitFactory } from '../../typechain/ForcedExitFactory'; | ||
|
||
import * as hardhat from 'hardhat'; | ||
|
||
const TX_AMOUNT = utils.parseEther('1.0'); | ||
|
||
use(solidity); | ||
|
||
describe('ForcedExit unit tests', function () { | ||
this.timeout(50000); | ||
|
||
let forcedExitContract: ForcedExit; | ||
let wallet1: Signer; | ||
let wallet2: Signer; | ||
let wallet3: Signer; | ||
let wallet4: Signer; | ||
|
||
before(async () => { | ||
[wallet1, wallet2, wallet3, wallet4] = await hardhat.ethers.getSigners(); | ||
|
||
const forcedExitContractFactory = await hardhat.ethers.getContractFactory('ForcedExit'); | ||
const contract = await forcedExitContractFactory.deploy(wallet1.getAddress(), wallet2.getAddress()); | ||
// Connecting the wallet to a potential receiver, who can withdraw the funds | ||
// on the master's behalf | ||
forcedExitContract = ForcedExitFactory.connect(contract.address, wallet2); | ||
}); | ||
|
||
it('Check withdrawing fees', async () => { | ||
// The test checks the ability to withdraw the funds from the contract | ||
// after the user has sent them | ||
|
||
// Code style note: Could not use nested expects because | ||
// changeEtherBalance does not allow it | ||
|
||
// User sends funds to the contract | ||
const transferTxHandle = await wallet3.sendTransaction({ | ||
to: forcedExitContract.address, | ||
value: TX_AMOUNT | ||
}); | ||
// Check that the `FundsReceived` event was emitted | ||
expect(transferTxHandle).to.emit(forcedExitContract, 'FundsReceived').withArgs(TX_AMOUNT); | ||
|
||
// Withdrawing the funds from the contract to the wallet4 | ||
const withdrawTxHandle = await forcedExitContract.withdrawPendingFunds(await wallet4.getAddress()); | ||
|
||
// The pending funds have been received | ||
expect(withdrawTxHandle).to.changeEtherBalance(wallet4, TX_AMOUNT); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.