halibobo1205 / tips Goto Github PK
View Code? Open in Web Editor NEWThis project forked from tronprotocol/tips
TRON Improvement Proposals
This project forked from tronprotocol/tips
TRON Improvement Proposals
tip: 635
title: Optimize Reward Withdrawal To Improve TPS
author: [email protected]
discussions-to: https://github.com/tronprotocol/tips/issues/635
status: Draft
type: Standards Track
category: Core
created: 2023-12-21
This TIP aims to optimize the algorithm of the voting reward withdrawal in Phase 1 (after TIP-53 before TIP-465) to improve performance for increasing the TPS of such transactions.
There are currently two phases of the TRON protocol voting reward:
Due to the poor performance of the current Phase 1 voting reward withdrawal algorithm, when a user has both Phase 1 and Phase 2 rewards, the current Phase 1 reward withdrawal algorithm will slow down the overall performance of the transaction.
This TIP proposes an optimization plan that optimizes the relevant calculation complexity to o(1) for Phase 1 without modifying the reward calculation method, greatly optimizing the calculation performance of Phase 1 reward, and thereby improving network TPS.
According to statistics, there are currently about 16W users on the TRON main network who have Phase 1 rewards to be withdrawn. By comparing the performance of the Phase 1 reward algorithm before and after optimization in the test environment, it is expected that this optimization plan can improve performance by >20 times.
The current Phase 1 voting reward withdrawal uses truncation at the end of the decimal, resulting in the current actual result being slightly smaller than the theoretical value. The optimized algorithm will adopt higher calculation accuracy, and the calculation results will be closer to the theoretical values. At the same time, the difference in results before and after optimization will be controlled within 1 TRX, which will have almost no impact on the TRX inflation of the entire network and users.
term | note |
---|---|
maintenance period, responsible for logical processing, such as voting, voting reward calculation, and proposal validation in the current cycle. Each maintenance period of the main network is 6 hours. | |
current withdrawal algorithm used for voting reward in Phase 1 | |
withdrawal algorithm used for voting reward in Phase 1 after optimization | |
Phase 2 voting reward withdrawal algorithm | |
$ rewardViSet$ | the cumulative reward per vote for a witness set in Phase 1 |
rewardViSet root hash | |
database stores the RewardViSet | |
SR database | |
calculate RewardViSet and store the data to RewardViStore | |
total voting rewards of SR voters at maintenance i | |
total number of SR votes at maintenance i | |
flags whether the calculation of RewardViSet is completed | |
maintenance period during which the new voting reward algorithm proposal takes effect, is 4708 on mainnet | |
maintenance period user starts to vote, startCycle < newRewardCycle | |
maintenance period user cancels the voting, endCycle > newRewardCycle | |
number of use’s votes for SR | |
rewards the user receives from voting for SR | |
new voting reward withdrawal algorithm proposal for TIP-465 | |
Phase 1 adopts new voting reward algorithm withdrawal proposal |
In Phase 1 and Phase 2, the calculation logic of voting reward for a single SR vote is as follows:
Phase1.algorithm1:
userReward(s) = for i range [startCycle, newRewardCycle) { userReward(s) += userVote( s)/totalVote(i, s) * totalReward(i,s)}
Phase2.algorithm:
userReward(s) = voteCount * (accumulateRewardPerVote[newRewardCycle-1,s]- accumulateRewardPerVote[endCycle-1,s])
Phase1.algorithm1 uses an iterative method to distribute calculations for each maintenance period and then accumulates them. The algorithm complexity is o(n). Phase2.algorithm directly multiplies the number of votes and the cumulative reward value of the corresponding interval to obtain the total reward amount, and the algorithm complexity is o(1). Please refer to TIP-465 for more information.
The optimized Phase1.algorithm2 will use the same algorithm as Phase2.algorithm. Based on the previous analysis, this optimization will be divided into the following steps:
Precalculate the cumulative reward of a single vote for all SRs in Phase 1 in each maintenance period, and store it persistently. At this time, all Phase 1 reward-related transactions continue to use the algorithm1 algorithm.
Use the proposal to control whether the optimized algorithm2 algorithm should be adopted for Phase 1 rewards.
After the proposal is passed, Phase 1's voting rewards will use the algorithm2 algorithm, which will use the intermediate data persisted locally by the node to calculate the result with O(1) performance. The difference between the calculation result and the original Phase1.algorithm1 algorithm is within 1 TRX.
Note: The TRON protocol's voting reward can be triggered by a variety of transactions. Any address will automatically trigger voting reward collection when executing the following transactions:
VoteWitnessContract
UnfreezeBalanceContract
UnfreezeBalanceV2Contract
WithdrawBalanceContract
For the total amount of TRX: the core calculation logic of the optimized Phase 1 reward algorithm is consistent with the current Phase 2 reward algorithm. This optimization aims to improve the performance of the current algorithm. At the same time, the new algorithm improves the calculation accuracy while maintaining the same core calculation logic. This optimization plan has almost no impact on the circulation and inflation of the entire TRX.
Regarding user benefits: since higher precision is used, user benefits will be closer to the theoretical value. When each user receives Phase 1 earnings, the losses caused by the tail-removal algorithm used previously will be reduced. However, since there is not much difference in the calculation results before and after optimization, this optimization has little impact on users.
The most critical feature of the blockchain is the consistent ledger. This solution optimizes the voting reward calculation logic and affects consensus. Therefore, proposals are needed to control the switch to ensure data consistency on the chain.
In order to minimize the impact on node performance, precalculation is performed asynchronously. Asynchronous tasks often have some state synchronization problems, so the execution logic of precalculation tasks needs to be described in detail here.
if enabled: execute precalculation asynchronously
Not enabled: enter the next detection
Precalculation tasks:
Check if there is any Phase1 voting reward
If there is: check whether the new proposal is effective
effective: Check whether the precalculation task is completed
not effective: the transaction maintains the original execution logic
if there is not: the transaction maintains the original execution logic
According to the previous explanation, this solution has no compatibility issues with on-chain functions.
Please note that if any application party implements a similar voting reward estimation algorithm outside the chain, please adapt the changes promptly.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.