-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[R4R]BEP153: Native Staking Implementation #290
Merged
Merged
Conversation
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
pythonberg1997
force-pushed
the
cross_stake
branch
from
June 30, 2022 09:07
83e3eec
to
244b634
Compare
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
unclezoro
reviewed
Jul 11, 2022
pythonberg1997
force-pushed
the
cross_stake
branch
from
July 14, 2022 11:51
89c414f
to
bf1e9d4
Compare
pythonberg1997
force-pushed
the
cross_stake
branch
from
July 18, 2022 07:04
e571493
to
98a17a2
Compare
pythonberg1997
force-pushed
the
cross_stake
branch
from
July 18, 2022 07:26
98a17a2
to
ee39ccb
Compare
pythonberg1997
force-pushed
the
cross_stake
branch
from
July 20, 2022 07:04
92527c3
to
d339903
Compare
unclezoro
reviewed
Aug 1, 2022
unclezoro
reviewed
Aug 1, 2022
unclezoro
reviewed
Aug 1, 2022
unclezoro
reviewed
Aug 1, 2022
unclezoro
reviewed
Aug 1, 2022
unclezoro
reviewed
Aug 1, 2022
unclezoro
reviewed
Aug 1, 2022
pythonberg1997
force-pushed
the
cross_stake
branch
from
August 2, 2022 10:38
683428d
to
4beeeb7
Compare
LGTM now |
NashBC
approved these changes
Aug 5, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
pythonberg1997
force-pushed
the
cross_stake
branch
from
August 15, 2022 03:29
482b58f
to
1986799
Compare
unclezoro
reviewed
Aug 15, 2022
unclezoro
reviewed
Aug 15, 2022
unclezoro
reviewed
Aug 15, 2022
unclezoro
reviewed
Aug 15, 2022
unclezoro
reviewed
Aug 15, 2022
unclezoro
reviewed
Aug 15, 2022
unclezoro
changed the title
[WIP]BEP153: Native Staking Implementation
[R4R]BEP153: Native Staking Implementation
Aug 16, 2022
unclezoro
approved these changes
Aug 16, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
1. Summary
This BEP introduces a native staking protocol onto BNB Smart Chain. With this BEP, individual or institution delegators can stake BNB to specified validators and get staking rewards on the BSC side directly.
2. Abstract
This BEP introduces a new staking system contract on the BSC side, all staking-related operations on the BSC side should be initiated through this contract, and then applied across-chain to BNB Beacon Chain through the native cross-chain communication mechanism. The cross-chain staking APP on the BNB Beacon Chain side reuses the previous staking mechanism to handle these staking-related operations.
The goals of this BEP is:
3. Status
This BEP is a draft.
4.Motivation
Before this BEP, the BNB holders can only stake their assets on the BNB Beacon Chain side, which means that if their assets are on the BNB Smart Chain side, they have to transfer their assets across-chain to the BNB Beacon Chain first, which is not user-friendly enough.
With this BEP, the BNB holders can stake on the BSC side directly, and dApps can launch their staking service based on the protocol introduced by this BEP, which can diversify the BSC ecosystem.
5. Specification
5.1 Overview
There are mainly four components:
5.2 Staking System Contract
This is a new system contract deployed on BSC. It fulfills all the basic functionalities of staking service including delegation, undelegation, claiming reward and redelegation. It receives requests, encapsulates data and sends cross-chain packages. Specifically, the cross-chain package is normalized as a Cross-Chain Stake Event as below.
5.2.1 Cross-Chain Stake Event
The Cross-chain stake event will be emitted once a cross-chain stake transaction happens on BSC. It is mainly composed of channel ID, sequence ID and payload data.
The payload data is a sequence of RLP encoded bytes of {eventType, params}.
eventType: uint8, will determine the corresponding handler that handles staking requests, and also determine how to decode the data. It includes the following types.
Among them,
distribute reward
anddistribute undelegated
events will be emitted initially on BC.5.2.2 Data Structs
To keep track of delegators staking funds, we adopt multiple mappings to record data.
delegated
records every delegator’s total staked amount. When a delegator submits undelegation, his/her delegated amount will decrease and after undelegation finishes, the undelegated increase the same number.delegatedOfValidator
records every delegator’s staked amount to a specific validator.distributedReward
records every delegator’s reward that is already distributed to BSC.pendingUndelegateTime
andpendingRedelegateTime
records the estimated finish time of undelegation/redelegation. A new operation must wait until the previous operation is closed(until the time is up or receive an ack/failack package). Please be noted that this record will not always be accurate. The previous operation may end early.undelegated
records every delegator’s unlocked undelegate funds that is already distributed to BSC.5.2.3 Staking System Contract APIs
delegate(address validator, uint256 amount) payable
This method will handle the cross-chain delegating requests. Here, the
validator
represents the BC operator address of the validator to be delegated to. Cross-chain relayer fee should be included in the msg.value. The following graph represents the workflow of delegation on BSC:undelegate(address validator, uint256 amount) payable
This method will handle the cross-chain undelegating requests. The staked BNBs on BC will be unlocked after 7 days and transferred to the staking contract on BSC automatically. Delegators could claim undelegated BNBs by
claimUndelegated
(see below). Thevalidator
represents the BC operator address of the validator delegated to. Cross-chain relayer fee should be included in the msg.value.redelegate(address validatorSrc, address validatorDst, uint256 amount) payable
Undelegate amount BNBs from
validatorSrc
and delegate tovalidatorDst
.claimReward() returns(uint256 amount)
Delegators can claim their distributed reward by this method. The returned amount is the number of funds that
msg.sender
received.claimUndelegated() returns(uint256 amount)
Delegators can claim their undelegated funds by this method. The returned amount is the number of funds that
msg.sender
received.getDelegated(address delegator, address validator) external view returns(uint256 amount)
This function allows the delegator to query how much he/she has delegated to the specific validator.
getTotalDelegated(address delegator) external view returns(uint256 amount)
This function allows the delegator to query how much he/she has delegated in all.
getDistributedReward(address delegator) external view returns(uint256 amount)
This function allows the delegator to view the amount of the distributed rewards.
getPendingRedelegateTime(address delegator, address valSrc, address valDst) external view returns(uint256 amount)
This function allows the delegator to view the estimated finish time of redelegation from
valSrc
tovalDst
.getPendingUndelegateTime(address delegator, address validator) external view returns(uint256 amount)
This function allows the delegator to view the estimated finish time of undelegation from
validator
.getUndelegated(address delegator) external view returns(uint256 amount)
This function allows the delegator to view the amount of the undelegated funds.
5.3 Cross-Chain Staking APP
This is a new cross-chain app on BC side. It will be called by the BC’s bridge once it receives cross-chain staking packages. The main function of this app is to parse the data and do the staking related tasks.
5.3.1 Staking
The native staking mechanism on the BC will stay almost the same as before. The only change is that the delegation will be tagged as native or not native(cross-chain). The Cross-Chain Staking app will decode the cross-chain package depending on the event type code and call corresponding functions.
5.3.2 Contract Address on BC(CAoB)
The delegator’s corresponding address on the BC will be the XOR result between a map hash of a specific constant string and its BSC address. So no one could control it except for the cross-chain app.
5.3.3 Distribute Reward
The delegation reward will be released at each breath block to the reward CAoB, once the balance of the reward CAoB is larger than a threshold. The reward will be cross-chain transferred to the staking system contract on the BSC side automatically.
5.3.4 Distribute Undelegated
When undelegation finishes, the undelegated funds will be released to the delegate CAoB address, and the funds of the delegate CAoB will be cross-chain transferred to the staking system contract on the BSC side automatically in the next breath block.
5.4 Fees
The fees charged for the relevant transactions are based on the following principles:
5.5 Error Handle
This BEP will reuse the existing cross-chain infrastructure as much as possible. This includes and is not limited to the separation of application and communication protocol layers, uniform error handling principle and cross-chain application coding principle. As the new system contract BSC and new app on BC are all parts of the application layer, the error handling of the communication layer will remain the same as before.
5.5.1 Normal Error
Normal errors means anticipated errors. For transactions that cross-chain communication is involved, all normal errors will be relayed back. So no errors will be missed. For this type of errors, we recommend that developers should check the returned reason for failure, modify the parameters and retry the transaction. All anticipated errors and success scenarios are listed below.
Therefore, if any errors happen on BSC, the transaction will be reverted and the msg sender will know it at once. But if an error happens on BC, the msg sender will not get feedback immediately. So we recommend dApp developers run some robots to monitor related events to get informed.
5.5.2 Crash in Application Layer
The communication layer will catch the crash error of the application, record it in store or emit an event for debugging. The crash error will use the FAIL_ACK type of package if needed, and the payload of FAIL_ACK is the same as the payload of the SYNC package.
For contracts in BSC, the crash of Application will revert state change automatically, but for Application on BC we need to build another sandbox for it, only commit store after it did not crash.
When receiving the FAIL_ACK package, the communication layer will invoke a different method of Application, like handleFailAckPackage or ExecuteFailAckClaim. We will ensure the safety of users’ assets(see 5.5.4).
5.5.3 Error in Communication Layer
Communication layer only plays around with these inputs: Type, Sequence, ChannelId, RelayerFee. The simplicity of these inputs helps us build a robust system.
However, once an error happens, the communication of a channel may be blocked. We have to hard fork the chain when errors occur in the communication layer.
5.5.4 How Funds are Handled When Errors Occur During Delegation
It is very important to protect the safety of actors’ funds. As funds need to be transferred across the chain, this BEP will reuse the cross-chain transfer design as before. The key points are that funds transferred from BSC to BC will be locked in the tokenHub system contract and funds transferred from BC to BSC will be locked in the pegAccount. Necessary mechanism is employed to ensure the cross-chain reconciliation.
When a delegation request fails, an extra cross-chain package will be sent back to BSC. The funds will be unlocked from the tokenhub system contract, transferred to the staking system contract and be recorded as a part of the delegator’s pending undelegated. The delegator can get his/her funds back by claimUndelegated later. However, if the msg sender is a contract without a receive/fallback method, the transfer will fail and the funds may be lost forever. So make sure your dApp implements the receive/fallback method.