On December 18, 2019, we started to work with WeDEX to test the integration of Loopring Protocol v3, the Lightcone relayer, and WeDEX’s front-end on Ethereum mainnet. For WeDEX, the main goal is to fix integration bugs and improve their DEX’s user experience; for Loopring, the objective is to measure our relayer’s performance bottlenecks, cost, and other metrics while handling real trading requests from users and partners, so we can make better improvement plans.
Please recall that Lightcone is Loopring’s Relayer implementation, tasked with handling the off-chain components of what makes a Loopring v3 DEX. This means order handling/matching, Merkle Tree updating, zkSNARK proof generation, interacting with the Loopring smart contracts, and more. While the Loopring v3 protocol and ZKP circuits are open source, Lightcone is a private effort that is offered to partners and would-be DEX owners as Relayer-as-a-Service. If you’d like to learn more about utilizing Lightcone for your high-performance, non-custodial exchange, please reach out to firstname.lastname@example.org. Of course, any v3 DEX has the option to build and operate their own Relayer, which we’d love to see!
WeDEX invited more than 100 volunteers from the Loopring community to participate in the invitation-only stage. Most users submitted orders manually through WeDEX’s UI (Chinese language for now), while a few participants used Lightcone’s APIs to place orders. WeDEX also used APIs to simulate user behaviors for different levels of stress testing on the system.
Phase 1 of the beta testing has been completed. We’d like to share some data with the Ethereum community and hope to provide some reference points for other developers and teams that use Zero-Knowledge Proofs or zkRollup on Ethereum. We’ve shared some lighter data recaps on twitter threads (here and here), but below is a much more comprehensive look. We’d also sincerely like to thank the Ethereum, ZKP, and other communities for the tremendous support they’ve shown us recently as we’ve shared the mainnet numbers.
Data and Analysis
The following is the data as of ~10 AM on January 4, 2020, Shanghai.
The Lightcone relayer sent a total of 14,264 Ethereum transactions for WeDEX, of which 7,102 are of the typecommitBlock (block commitment on-chain with the corresponding block data); 7,102 areverifyBlocks (block verification using a zero-knowledge proof); and 60 are distributeWithdrawals (automatic distribution of funds withdrawn by users back to their wallet).
- Through these Ethereum transactions, the Lightcone relayer settled a total of 666,746 trades, processed 1,134 deposit requests and 93 withdrawal requests. [Note: 666k trades in 2 weeks are not purely ‘organic’; some traders were incentivized by small rewards by WeDEX to ignite the stress testing].
- The relayer consumed a total of 3,472,724,257 gas, with the total actual gas consumed being 12.27 ETH, and the average gas price being 3.532 Gwei (configured to be slightly above the average gas price at the time of transactions). The trade settlement consumed 3,334,000,169 gas, about 11.78 ETH. The average settlement cost per trade is about 5,000 gas, or 0.0000177 ETH. Assuming ETH’s price is $140, the on-chain cost of each trade is about $0.0025. This fee is only one-tenth or less of the fees of other DEXes.
- On-chain costs are composed of the 1) block commitment + the transaction data cost for the on-chain Data Availability (DA) and, 2) the Zero-Knowledge Proof (ZKP) verification. Currently, the cost of the ZKP verification is 60% of the total cost on-chain and the cost of the block commitment + DA is 40% of the total cost on-chain. This ratio will change, and likely flip — with the ZKP portion becoming the smaller side — due to optimizations such as the support of larger blocks (batches) in the future (see next paragraph).
- Although the Loopring protocol supports a maximum of around 4096 trades/block (a block is a batch processing unit verified by a zero-knowledge proof, and a block of size 1024 contains 1024 trades), for now, we have only done the phase 2 trusted setup up to 1024 trades/block for practical reasons. The Lightcone relayer currently also only supports up to 128 trades/block. The figure below shows the distribution of the numbers of different block sizes tested at this stage.
It can be seen that although the current Lightcone relayer has a 73% probability of generating blocks of size 128, there are still many very small blocks, and so the per-trade cost in these small blocks will be higher, as they bear the greater brunt of the cost. Bigger blocks are a requirement to lower the costs per trade on-chain. When that happens, the current 60/40 ratio of ZKP vs DA on-chain costs will shift largely towards the DA part, as the ZKP verification cost stays constant per block (not per trade), but the DA increases with the number of trades in the block.
Please note: on-chain DA is the most secure construction for a zkRollup, and is our focus. Removing this constraint and storing data off-chain is doable to boost performance, but compromising this Ethereum-level security guarantee is less interesting for us and the DEXes building atop currently.
The figure below shows that the cost per trade for a block of size 128 is only 1627 gas, while the cost in a block of size 1 is as high as 165,000 gas, which is more than 100 times greater than the former.
At present, the average cost of each zero-knowledge proof verification is 291,378 gas. The proof verification cost is independent of block type or size, so to get the cost per trade we can simply divide by the number of trades in the block. For example, for a block containing 128 trades, the cost for each trade is 2,276 gas.
Theoretically, the lowest cost per trade on the Loopring Protocol with on-chain DA is merely 385 gas. To achieve this goal, many ideal conditions need to be achieved (including having a level of demand which brings the requests up that high to be more broadly amortized, or simply waiting longer to ‘seal’ the batches with a proof), but the current average cost per trade is 5000 gas. While this is 60x better than the 300,000 gas cost of prior Loopring versions, it is still too high for our liking. Thankfully, there is still a lot of room for optimizations in the Lightcone relayer.
We have been using AWS Hong Kong servers to generate our zkSNARK proofs. Since different server models were tried during the test phase, the off-chain cost calculations were not completely accurate during the test. We estimate the actual zero-knowledge proof cost for each trade during the test to be between $0.002 and $0.0025.
AWS Hong Kong’s servers are relatively expensive — take m5.4xlarge models as an example, AWS Hong Kong charges $1.056 per hour. We can reduce costs by switching to servers in other cloud platform or data centers, such as:
- AWS Ningxia: $0.786 per hour
- AWS Virginia: $0.768 per hour
- Digital Ocean: $0.47 per hour
- Google Cloud: $0.76 per hour
- Alibaba Cloud: $0.405 per hour
Therefore, the average cost per trade in ZKP proof generation on alternate servers should be managed at about $0.0015.
In sum, total cost to settle a trade over this phase has been $0.005, split evenly between on-chain and off-chain costs.
Cost Improvement Target
We hope that within a few months, the total cost per trade will be halved from the current cost of around $0.005 to $0.0025. How we get there has been referenced above, and a few extra points can be seen in the ‘planned improvement of the Lightcone Relayer’ section below.
Plan to Test the “Withdrawal Mode”
Next week we will work with WeDEX to force their DEX contract (WeDEX Beta1: 0x7d3d221a8d8abdd868e8e88811ffaf033e68e108) to enter the Withdrawal Mode. In this mode, we will test that all users can smoothly take out assets by submitting their Merkle Proofs — no matter what. We will also generate a second contract, WeDEX Beta2, for WeDEX, and continue to perform more tests. During the process of switching between the two contracts, WeDEX may become unavailable for a short time.
Planned Improvements of the Lightcone Relayer
- At present, our current target throughput of 200 TPS for the relayer has not yet been achieved. To be clear, the protocol can support up to 1,400 TPS right now, the bottleneck exists off-chain. One of the reasons is that the modification of the Merkle tree has not yet been completely parallelized. We already have a relatively mature solution for this, and we expect to complete coding and testing soon. Fortunately, the modification of the Merkle tree is an asynchronous process, so this bottleneck will only show up when the system is under long-term stress.
- The design of the relayer API needs to be improved, and the documentation needs to be prepared. We hope to release the first version of the relayer API and documentation in March.
- Lightcone relayer should quickly support blocks of size 256, 512, and 1024; at the same time, we need to make optimizations to avoid generating small blocks as much as possible to lower the on-chain costs.
- Similar blocks (i.e. blocks of the same type) can be verified together more efficiently. The relayer should batch the proof verification of multiple blocks as much as possible as this will significantly reduce proof-verification costs.
- We will experiment with generating the zero-knowledge proofs in cheaper data centers, further reducing costs.
Loopring Protocol 3.1.1 Has Been Released
WeDEX Beta2 will be based on the latest version of the Loopring protocol: Loopring 3.1.1. The only change in this version is that we change LRC burning to LRC transferring to the protocol fee address (Protocol Fee Vault) to reward LRC staking users. This means that the WeDEX Beta2 contract will immediately provide 225,000 LRC as a reward for staking users (since a new DEX must provide this when they spin up their contract on-chain). In March, we will launch the Hebao exchange (hebao.io) which will provide another 225,000 LRC as a reward for staking users. Based on the current 21.6 million LRC staked, the return on LRC staking in March 2020 will exceed 2%. The withdrawal mode test will also deduct LRC staked by WeDEX as a reward to ordinary LRC staking users. The protocol fee charged for trades (maker: 0.02%, taker: 0.04%) will be a long-term and more stable income for staking users.
Loopring’s DEX Explorer
Without a DEX explorer, it is now quite difficult for ordinary users to see their transaction history and account balances using the on-chain data. We are already stepping up the development of such a tool. We expect that Loopring’s DEX explorer will be tested in the first quarter of 2020 and launched in the second quarter.