Skip to content

Commit

Permalink
BEP153: Native Staking Implementation (bnb-chain#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 authored Aug 17, 2022
1 parent e47afdc commit 2724e5b
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 140 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.10.1
IMPROVEMENTS
* [\#879](https://github.com/bnb-chain/node/pull/879) [Staking] Implement BEP153: Native Staking on BSC

## 0.10.0
IMPROVEMENTS
* [\#875](https://github.com/bnb-chain/node/pull/875) [DEX] Implement BEP151
Expand Down
30 changes: 30 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -26,7 +27,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/sidechain"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
cStake "github.com/cosmos/cosmos-sdk/x/stake/cross_stake"
"github.com/cosmos/cosmos-sdk/x/stake/keeper"
sTypes "github.com/cosmos/cosmos-sdk/x/stake/types"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/tmhash"
Expand Down Expand Up @@ -171,6 +174,8 @@ func NewBinanceChain(logger log.Logger, db dbm.DB, traceStore io.Writer, baseApp
common.StakeStoreKey, common.StakeRewardStoreKey, common.TStakeStoreKey,
app.CoinKeeper, app.Pool, app.ParamHub.Subspace(stake.DefaultParamspace),
app.RegisterCodespace(stake.DefaultCodespace),
sdk.ChainID(app.crossChainConfig.BscIbcChainId),
app.crossChainConfig.BscChainId,
)

app.ValAddrCache = NewValAddrCache(app.stakeKeeper)
Expand Down Expand Up @@ -329,6 +334,7 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
upgrade.Mgr.AddUpgradeHeight(upgrade.FixFailAckPackage, upgradeConfig.FixFailAckPackageHeight)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP128, upgradeConfig.BEP128Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP151, upgradeConfig.BEP151Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP153, upgradeConfig.BEP153Height)

// register store keys of upgrade
upgrade.Mgr.RegisterStoreKeys(upgrade.BEP9, common.TimeLockStoreKey.Name())
Expand Down Expand Up @@ -536,6 +542,30 @@ func (app *BinanceChain) initStaking() {
params.RewardDistributionBatchSize = 1000
app.stakeKeeper.SetParams(newCtx, params)
})
upgrade.Mgr.RegisterBeginBlocker(sdk.BEP153, func(ctx sdk.Context) {
chainId := sdk.ChainID(ServerContext.BscIbcChainId)
app.scKeeper.SetChannelSendPermission(ctx, chainId, sTypes.CrossStakeChannelID, sdk.ChannelAllow)
stakeContractAddr := "0000000000000000000000000000000000002001"
stakeContractBytes, _ := hex.DecodeString(stakeContractAddr)
_, sdkErr := app.scKeeper.CreateNewChannelToIbc(ctx, chainId, sTypes.CrossStakeChannelID, sdk.RewardNotFromSystem, stakeContractBytes)
if sdkErr != nil {
panic(sdkErr.Error())
}
crossStakeApp := cStake.NewCrossStakeApp(app.stakeKeeper)
err := app.scKeeper.RegisterChannel(sTypes.CrossStakeChannel, sTypes.CrossStakeChannelID, crossStakeApp)
if err != nil {
panic(err)
}
})

if sdk.IsUpgrade(sdk.BEP153) {
crossStakeApp := cStake.NewCrossStakeApp(app.stakeKeeper)
err := app.scKeeper.RegisterChannel(sTypes.CrossStakeChannel, sTypes.CrossStakeChannelID, crossStakeApp)
if err != nil {
panic(err)
}
}

app.stakeKeeper.SubscribeParamChange(app.ParamHub)
app.stakeKeeper = app.stakeKeeper.WithHooks(app.slashKeeper.Hooks())
}
Expand Down
4 changes: 4 additions & 0 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ EnableAccountScriptsForCrossChainTransferHeight = {{ .UpgradeConfig.EnableAccoun
BEP128Height = {{ .UpgradeConfig.BEP128Height }}
# Block height of BEP151 upgrade
BEP151Height = {{ .UpgradeConfig.BEP151Height }}
# Block height of BEP153 upgrade
BEP153Height = {{ .UpgradeConfig.BEP153Height }}
[query]
# ABCI query interface black list, suggested value: ["custom/gov/proposals", "custom/timelock/timelocks", "custom/atomicSwap/swapcreator", "custom/atomicSwap/swaprecipient"]
Expand Down Expand Up @@ -529,6 +531,7 @@ type UpgradeConfig struct {
EnableAccountScriptsForCrossChainTransferHeight int64 `mapstructure:"EnableAccountScriptsForCrossChainTransferHeight"`
BEP128Height int64 `mapstructure:"BEP128Height"`
BEP151Height int64 `mapstructure:"BEP151Height"`
BEP153Height int64 `mapstructure:"BEP153Height"`
}

func defaultUpgradeConfig() *UpgradeConfig {
Expand All @@ -550,6 +553,7 @@ func defaultUpgradeConfig() *UpgradeConfig {
LaunchBscUpgradeHeight: 1,
BEP128Height: math.MaxInt64,
BEP151Height: math.MaxInt64,
BEP153Height: math.MaxInt64,
BEP82Height: math.MaxInt64,
BEP84Height: math.MaxInt64,
BEP87Height: math.MaxInt64,
Expand Down
1 change: 1 addition & 0 deletions common/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (

BEP128 = sdk.BEP128 // https://github.com/bnb-chain/BEPs/pull/128 Staking reward distribution upgrade
BEP151 = "BEP151" // https://github.com/bnb-chain/BEPs/pull/151 Decommission Decentralized Exchange
BEP153 = sdk.BEP153 // https://github.com/bnb-chain/BEPs/pull/153 Native Staking
)

func UpgradeBEP10(before func(), after func()) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ require (
)

replace (
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/bnc-cosmos-sdk v0.25.0-binance.28
github.com/cosmos/cosmos-sdk => github.com/bnb-chain/bnc-cosmos-sdk v0.25.1
github.com/tendermint/go-amino => github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2
github.com/tendermint/iavl => github.com/bnb-chain/bnc-tendermint-iavl v0.12.0-binance.4
github.com/tendermint/tendermint => github.com/bnb-chain/bnc-tendermint v0.32.3-binance.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bnb-chain/bnc-cosmos-sdk v0.25.0-binance.28 h1:pTc+tGGDVYqWEYf3RAL8DgJU4eAWu33Y/0hKbMOFK9A=
github.com/bnb-chain/bnc-cosmos-sdk v0.25.0-binance.28/go.mod h1:+0MJRhSM5hb/ZvvoWOVt7LaihYM1Ax+PxnAMK/lHHUs=
github.com/bnb-chain/bnc-cosmos-sdk v0.25.1 h1:thuhSDcIwwUFYwPvtD3EPk295PeM4bZYbEJVMroQwHw=
github.com/bnb-chain/bnc-cosmos-sdk v0.25.1/go.mod h1:+0MJRhSM5hb/ZvvoWOVt7LaihYM1Ax+PxnAMK/lHHUs=
github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2 h1:iAlp9gqG0f2LGAauf3ZiijWlT6NI+W2r9y70HH9LI3k=
github.com/bnb-chain/bnc-go-amino v0.14.1-binance.2/go.mod h1:LiCO7jev+3HwLGAiN9gpD0z+jTz95RqgSavbse55XOY=
github.com/bnb-chain/bnc-tendermint v0.32.3-binance.7 h1:wJBrhLAm9P+Jjm/clTxynAZJl63xD7LatKmL5e3YzbY=
Expand Down
4 changes: 2 additions & 2 deletions plugins/bridge/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func BindCmd(cdc *codec.Codec) *cobra.Command {
expireTime := viper.GetInt64(flagExpireTime)

// build message
contractAddress, err := types.NewSmartChainAddress(contractAddressStr)
contractAddress, err := sdk.NewSmartChainAddress(contractAddressStr)
if err != nil {
return err
}
Expand Down Expand Up @@ -133,7 +133,7 @@ func TransferOutCmd(cdc *codec.Codec) *cobra.Command {
}

// build message
toAddress, err := types.NewSmartChainAddress(toAddressStr)
toAddress, err := sdk.NewSmartChainAddress(toAddressStr)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/bridge/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func handleTransferOutMsg(ctx sdk.Context, keeper Keeper, msg TransferOutMsg) sd
return sdkErr.Result()
}

contractAddr, err := types.NewSmartChainAddress(token.GetContractAddress())
contractAddr, err := sdk.NewSmartChainAddress(token.GetContractAddress())
if err != nil {
return types.ErrInvalidContractAddress(fmt.Sprintf("contract address is invalid, addr=%s", contractAddr)).Result()
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/bridge/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (k Keeper) GetBindRequest(ctx sdk.Context, symbol string) (types.BindReques
return bindRequest, nil
}

func (k Keeper) SetContractDecimals(ctx sdk.Context, contractAddr types.SmartChainAddress, decimals int8) {
func (k Keeper) SetContractDecimals(ctx sdk.Context, contractAddr sdk.SmartChainAddress, decimals int8) {
key := types.GetContractDecimalsKey(contractAddr[:])

kvStore := ctx.KVStore(k.storeKey)
Expand All @@ -136,7 +136,7 @@ func (k Keeper) SetContractDecimals(ctx sdk.Context, contractAddr types.SmartCha
kvStore.Set(key, []byte{byte(decimals)})
}

func (k Keeper) GetContractDecimals(ctx sdk.Context, contractAddr types.SmartChainAddress) int8 {
func (k Keeper) GetContractDecimals(ctx sdk.Context, contractAddr sdk.SmartChainAddress) int8 {
if sdk.IsUpgrade(upgrade.FixFailAckPackage) {
if strings.ToLower(contractAddr.String()) == types.BNBContractAddr {
return types.BNBContractDecimals
Expand Down
70 changes: 0 additions & 70 deletions plugins/bridge/types/address.go

This file was deleted.

23 changes: 0 additions & 23 deletions plugins/bridge/types/address_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions plugins/bridge/types/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package types
import sdk "github.com/cosmos/cosmos-sdk/types"

type BindRequest struct {
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
Amount int64 `json:"amount"`
DeductedAmount int64 `json:"deducted_amount"`
ContractAddress SmartChainAddress `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ExpireTime int64 `json:"expire_time"`
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
Amount int64 `json:"amount"`
DeductedAmount int64 `json:"deducted_amount"`
ContractAddress sdk.SmartChainAddress `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ExpireTime int64 `json:"expire_time"`
}
24 changes: 12 additions & 12 deletions plugins/bridge/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ const (
var _ sdk.Msg = BindMsg{}

type BindMsg struct {
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
Amount int64 `json:"amount"`
ContractAddress SmartChainAddress `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ExpireTime int64 `json:"expire_time"`
From sdk.AccAddress `json:"from"`
Symbol string `json:"symbol"`
Amount int64 `json:"amount"`
ContractAddress sdk.SmartChainAddress `json:"contract_address"`
ContractDecimals int8 `json:"contract_decimals"`
ExpireTime int64 `json:"expire_time"`
}

func NewBindMsg(from sdk.AccAddress, symbol string, amount int64, contractAddress SmartChainAddress, contractDecimals int8, expireTime int64) BindMsg {
func NewBindMsg(from sdk.AccAddress, symbol string, amount int64, contractAddress sdk.SmartChainAddress, contractDecimals int8, expireTime int64) BindMsg {
return BindMsg{
From: from,
Amount: amount,
Expand Down Expand Up @@ -169,13 +169,13 @@ func ParseBindStatus(input string) (BindStatus, error) {
var _ sdk.Msg = TransferOutMsg{}

type TransferOutMsg struct {
From sdk.AccAddress `json:"from"`
To SmartChainAddress `json:"to"`
Amount sdk.Coin `json:"amount"`
ExpireTime int64 `json:"expire_time"`
From sdk.AccAddress `json:"from"`
To sdk.SmartChainAddress `json:"to"`
Amount sdk.Coin `json:"amount"`
ExpireTime int64 `json:"expire_time"`
}

func NewTransferOutMsg(from sdk.AccAddress, to SmartChainAddress, amount sdk.Coin, expireTime int64) TransferOutMsg {
func NewTransferOutMsg(from sdk.AccAddress, to sdk.SmartChainAddress, amount sdk.Coin, expireTime int64) TransferOutMsg {
return TransferOutMsg{
From: from,
To: to,
Expand Down
8 changes: 4 additions & 4 deletions plugins/bridge/types/msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ import (
"github.com/stretchr/testify/require"
)

func BytesToAddress(b []byte) SmartChainAddress {
var a SmartChainAddress
func BytesToAddress(b []byte) sdk.SmartChainAddress {
var a sdk.SmartChainAddress
a.SetBytes(b)
return a
}

func TestBindMsg(t *testing.T) {
_, addrs, _, _ := mock.CreateGenAccounts(1, sdk.Coins{})

nonEmptySmartChainAddr := SmartChainAddress(BytesToAddress([]byte{1}))
emptySmartChainAddr := SmartChainAddress(BytesToAddress([]byte{0}))
nonEmptySmartChainAddr := sdk.SmartChainAddress(BytesToAddress([]byte{1}))
emptySmartChainAddr := sdk.SmartChainAddress(BytesToAddress([]byte{0}))

tests := []struct {
bindMsg BindMsg
Expand Down
Loading

0 comments on commit 2724e5b

Please sign in to comment.