Skip to content

Commit

Permalink
Merge #1405
Browse files Browse the repository at this point in the history
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
2 parents 283ad60 + e12f02e commit 50c899e
Show file tree
Hide file tree
Showing 64 changed files with 4,131 additions and 107 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ jobs:
- name: integration-rust-sdk
run: ci_run zk test i rust-sdk

- name: integration-withdrawal-helpers
run: ci_run zk test i withdrawal-helpers

- name: Show logs
run: |
Expand Down
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"core/bin/zksync_core",
"core/bin/zksync_eth_sender",
"core/bin/zksync_witness_generator",
"core/bin/zksync_forced_exit_requests",

# Libraries
"core/lib/circuit",
Expand Down
2 changes: 2 additions & 0 deletions changelog/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ All notable changes to the core components will be documented in this file.

- Added a stressing dev fee ticker scenario to the loadtest.
- Added a `--sloppy` mode to the `dev-fee-ticker-server` to simulate bad networks with the random delays and fails.
- Added `forced_exit_requests` functionality, which allows users to pay for ForcedExits from L1. Note that a few env
variables were added that control the behaviour of the tool.
- Possibility to use CREATE2 ChangePubKey and Transfer in a single batch.

### Fixed
Expand Down
51 changes: 51 additions & 0 deletions contracts/contracts/ForcedExit.sol
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);
}
}
14 changes: 14 additions & 0 deletions contracts/contracts/dev-contracts/SelfDestruct.sol
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 {}
}
4 changes: 4 additions & 0 deletions contracts/scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ async function main() {
if (args.contract === 'Proxies' || args.contract == null) {
await deployer.deployProxiesAndGatekeeper({ gasPrice, nonce: args.nonce });
}

if (args.contract === 'ForcedExit' || args.contract == null) {
await deployer.deployForcedExit({ gasPrice, nonce: args.nonce });
}
}

main()
Expand Down
64 changes: 51 additions & 13 deletions contracts/src.ts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
Verifier,
VerifierFactory,
ZkSync,
ZkSyncFactory
ZkSyncFactory,
ForcedExit,
ForcedExitFactory
} from '../typechain';

export interface Contracts {
Expand All @@ -25,6 +27,7 @@ export interface Contracts {
verifier;
proxy;
upgradeGatekeeper;
forcedExit;
}

export interface DeployedAddresses {
Expand All @@ -36,6 +39,7 @@ export interface DeployedAddresses {
ZkSync: string;
ZkSyncTarget: string;
DeployFactory: string;
ForcedExit: string;
}

export interface DeployerConfig {
Expand All @@ -58,17 +62,8 @@ export function readProductionContracts(): Contracts {
zkSync: readContractCode('ZkSync'),
verifier: readContractCode('Verifier'),
proxy: readContractCode('Proxy'),
upgradeGatekeeper: readContractCode('UpgradeGatekeeper')
};
}

export function readTestContracts(): Contracts {
return {
governance: readContractCode('GovernanceTest'),
zkSync: readContractCode('ZkSyncTest'),
verifier: readContractCode('VerifierTest'),
proxy: readContractCode('Proxy'),
upgradeGatekeeper: readContractCode('UpgradeGatekeeperTest')
upgradeGatekeeper: readContractCode('UpgradeGatekeeper'),
forcedExit: readContractCode('ForcedExit')
};
}

Expand All @@ -81,7 +76,8 @@ export function deployedAddressesFromEnv(): DeployedAddresses {
Verifier: process.env.CONTRACTS_VERIFIER_ADDR,
VerifierTarget: process.env.CONTRACTS_VERIFIER_TARGET_ADDR,
ZkSync: process.env.CONTRACTS_CONTRACT_ADDR,
ZkSyncTarget: process.env.CONTRACTS_CONTRACT_TARGET_ADDR
ZkSyncTarget: process.env.CONTRACTS_CONTRACT_TARGET_ADDR,
ForcedExit: process.env.CONTRACTS_FORCED_EXIT_ADDR
};
}

Expand Down Expand Up @@ -215,6 +211,38 @@ export class Deployer {
}
}

public async deployForcedExit(ethTxOptions?: ethers.providers.TransactionRequest) {
if (this.verbose) {
console.log('Deploying ForcedExit contract');
}

// Choose the this.deployWallet.address as the default receiver if the
// FORCED_EXIT_REQUESTS_SENDER_ACCOUNT_ADDRESS is not present
const receiver = process.env.FORCED_EXIT_REQUESTS_SENDER_ACCOUNT_ADDRESS || this.deployWallet.address;

const forcedExitContract = await deployContract(
this.deployWallet,
this.contracts.forcedExit,
[this.deployWallet.address, receiver],
{
gasLimit: 6000000,
...ethTxOptions
}
);
const zksRec = await forcedExitContract.deployTransaction.wait();
const zksGasUsed = zksRec.gasUsed;
const gasPrice = forcedExitContract.deployTransaction.gasPrice;
if (this.verbose) {
console.log(`CONTRACTS_FORCED_EXIT_ADDR=${forcedExitContract.address}`);
console.log(
`ForcedExit contract deployed, gasUsed: ${zksGasUsed.toString()}, eth spent: ${formatEther(
zksGasUsed.mul(gasPrice)
)}`
);
}
this.addresses.ForcedExit = forcedExitContract.address;
}

public async publishSourcesToTesseracts() {
console.log('Publishing ABI for UpgradeGatekeeper');
await publishAbiToTesseracts(this.addresses.UpgradeGatekeeper, this.contracts.upgradeGatekeeper);
Expand All @@ -224,6 +252,8 @@ export class Deployer {
await publishAbiToTesseracts(this.addresses.Verifier, this.contracts.verifier);
console.log('Publishing ABI for Governance (proxy)');
await publishAbiToTesseracts(this.addresses.Governance, this.contracts.governance);
console.log('Publishing ABI for ForcedExit');
await publishAbiToTesseracts(this.addresses.ForcedExit, this.contracts.forcedExit);
}

public async publishSourcesToEtherscan() {
Expand Down Expand Up @@ -271,13 +301,17 @@ export class Deployer {
['address']
)
);

console.log('Publishing sourcecode for ForcedExit', this.addresses.ForcedExit);
await publishSourceCodeToEtherscan(this.addresses.ForcedExit, 'ForcedExit', '');
}

public async deployAll(ethTxOptions?: ethers.providers.TransactionRequest) {
await this.deployZkSyncTarget(ethTxOptions);
await this.deployGovernanceTarget(ethTxOptions);
await this.deployVerifierTarget(ethTxOptions);
await this.deployProxiesAndGatekeeper(ethTxOptions);
await this.deployForcedExit(ethTxOptions);
}

public governanceContract(signerOrProvider: Signer | providers.Provider): Governance {
Expand All @@ -295,4 +329,8 @@ export class Deployer {
public upgradeGatekeeperContract(signerOrProvider: Signer | providers.Provider): UpgradeGatekeeper {
return UpgradeGatekeeperFactory.connect(this.addresses.UpgradeGatekeeper, signerOrProvider);
}

public forcedExitContract(signerOrProvider: Signer | providers.Provider): ForcedExit {
return ForcedExitFactory.connect(this.addresses.ForcedExit, signerOrProvider);
}
}
53 changes: 53 additions & 0 deletions contracts/test/unit_tests/forced_exit_test.ts
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);
});
});
1 change: 1 addition & 0 deletions core/bin/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ zksync_api = { path = "../zksync_api", version = "1.0" }
zksync_core = { path = "../zksync_core", version = "1.0" }
zksync_witness_generator = { path = "../zksync_witness_generator", version = "1.0" }
zksync_eth_sender = { path = "../zksync_eth_sender", version = "1.0" }
zksync_forced_exit_requests = { path = "../zksync_forced_exit_requests", version = "1.0" }

zksync_prometheus_exporter = { path = "../../lib/prometheus_exporter", version = "1.0" }
zksync_config = { path = "../../lib/config", version = "1.0" }
Expand Down
9 changes: 8 additions & 1 deletion core/bin/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use structopt::StructOpt;
use zksync_api::run_api;
use zksync_core::{genesis_init, run_core, wait_for_tasks};
use zksync_eth_sender::run_eth_sender;
use zksync_forced_exit_requests::run_forced_exit_requests_actors;
use zksync_prometheus_exporter::run_prometheus_exporter;
use zksync_witness_generator::run_prover_server;

Expand Down Expand Up @@ -78,9 +79,12 @@ async fn main() -> anyhow::Result<()> {

// Run prover server & witness generator.
vlog::info!("Starting the Prover server actors");
let database = zksync_witness_generator::database::Database::new(connection_pool);
let database = zksync_witness_generator::database::Database::new(connection_pool.clone());
run_prover_server(database, stop_signal_sender, ZkSyncConfig::from_env());

vlog::info!("Starting the ForcedExitRequests actors");
let forced_exit_requests_task_handle = run_forced_exit_requests_actors(connection_pool, config);

tokio::select! {
_ = async { wait_for_tasks(core_task_handles).await } => {
// We don't need to do anything here, since Core actors will panic upon future resolving.
Expand All @@ -97,6 +101,9 @@ async fn main() -> anyhow::Result<()> {
_ = async { counter_task_handle.unwrap().await } => {
panic!("Operation counting actor is not supposed to finish its execution")
},
_ = async { forced_exit_requests_task_handle.await } => {
panic!("ForcedExitRequests actor is not supposed to finish its execution")
},
_ = async { stop_signal_receiver.next().await } => {
vlog::warn!("Stop signal received, shutting down");
}
Expand Down
Loading

0 comments on commit 50c899e

Please sign in to comment.