Skip to content

Commit

Permalink
BEP-159: Introduce A New Staking Mechanism on BNB Beacon Chain (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-reorg authored Dec 21, 2022
1 parent d0d4743 commit e4a5af7
Show file tree
Hide file tree
Showing 31 changed files with 2,250 additions and 125 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
build
4 changes: 1 addition & 3 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.16
go-version: 1.17
- uses: actions/checkout@v3
- uses: actions/cache@v2
with:
Expand All @@ -25,7 +25,5 @@ jobs:
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: git config --global url."https://${{ secrets.GH_ACCESS_TOKEN }}@github.com".insteadOf "https://github.com"
- run: go env -w GOPRIVATE="github.com/bnb-chain/*"
- name: golangci-lint
run: make lint
33 changes: 25 additions & 8 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ on: ["push", "pull_request", "workflow_dispatch"]
jobs:
integration-test:
runs-on: ubuntu-latest
env:
GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v3
with:
go-version: 1.16
- name: Checkout
uses: actions/checkout@v2
go-version: 1.17
- uses: actions/cache@v2
with:
path: ~/go/bin
Expand All @@ -28,10 +24,31 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- run: sudo apt-get update -y && sudo apt-get install -y expect
- run: git config --global url."https://${{ secrets.GH_ACCESS_TOKEN }}@github.com".insteadOf "https://github.com"
- run: go env -w GOPRIVATE="github.com/bnb-chain/*"
# used to debug workflow
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
- run: make integration_test
- run: make bep159_integration_test integration_test
coverage-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v3
with:
go-version: 1.17
- uses: actions/cache@v2
with:
path: ~/go/bin
key: tools-v0
- uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: make test
- run: make test_coverage
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
21 changes: 21 additions & 0 deletions .github/workflows/multi-nodes-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: multi-nodes-test

on: ["push", "pull_request", "workflow_dispatch"]

jobs:
multi-nodes-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v3
with:
go-version: 1.17
- run: make docker.build docker.generate docker.start
# fix docker permission issue
- run: chmod 777 -R ~/work/node/node/build/devnet
- run: sleep 20
# used to debug workflow
# - name: Setup tmate session
# uses: mxschmitt/action-tmate@v3
- run: make multi-nodes-test
30 changes: 28 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ install_c:
format:
@echo "-->Formatting"
$(shell go fmt ./...)
$(shell find . -name "*.go" | grep -v "vendor/" | xargs -n 1 goimports -w)
bash scripts/importssort.sh

########################################
### Lint
Expand Down Expand Up @@ -194,13 +194,20 @@ test_unit:
@echo "--> Running go test"
@go test $(PACKAGES)

test_coverage:
@echo "--> Running go test"
@go test $(PACKAGES) -race -coverprofile=coverage.txt -covermode=atomic

integration_test: build
@echo "-->Integration Test"
@./integration_test.sh

bep159_integration_test: build
@echo "-->BEP159 Integration Test"
@bash ./scripts/bep159_integration_test.sh
########################################
### Pre Commit
pre_commit: build test format lint
pre_commit: build test_unit bep159_integration_test integration_test format lint multi-nodes-test

########################################
### Local validator nodes using docker and docker-compose
Expand Down Expand Up @@ -230,6 +237,25 @@ localnet-start: localnet-stop
localnet-stop:
docker-compose down

# docker commands
docker.build:
docker build -t binance/bnbdnode .

docker.generate:
go run ./cmd/gen_devnet

docker.start:
docker compose -f build/devnet/docker-compose.yml up -d

docker.stop:
docker compose -f build/devnet/docker-compose.yml down

docker.clean:
rm -rf build/devnet

multi-nodes-test:
STAKE_ENV=multi go run ./cmd/test_client

# To avoid unintended conflicts with file names, always add to .PHONY
# unless there is a reason not to.
# https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html
Expand Down
59 changes: 48 additions & 11 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
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"
cmn "github.com/tendermint/tendermint/libs/common"
Expand Down Expand Up @@ -335,6 +334,8 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP128, upgradeConfig.BEP128Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP151, upgradeConfig.BEP151Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP153, upgradeConfig.BEP153Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP159, upgradeConfig.BEP159Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP159Phase2, upgradeConfig.BEP159Phase2Height)
upgrade.Mgr.AddUpgradeHeight(upgrade.BEP173, upgradeConfig.BEP173Height)

// register store keys of upgrade
Expand Down Expand Up @@ -373,6 +374,13 @@ func SetUpgradeConfig(upgradeConfig *config.UpgradeConfig) {
bridge.TransferOutMsg{}.Type(),
oracle.ClaimMsg{}.Type(),
)
upgrade.Mgr.RegisterMsgTypes(upgrade.BEP159,
stake.MsgCreateValidatorOpen{}.Type(),
stake.MsgEditValidator{}.Type(),
stake.MsgDelegate{}.Type(),
stake.MsgUndelegate{}.Type(),
slashing.MsgUnjail{}.Type(),
)
// register msg types of upgrade
upgrade.Mgr.RegisterMsgTypes(upgrade.BEP8,
issue.IssueMiniMsg{}.Type(),
Expand Down Expand Up @@ -558,16 +566,24 @@ func (app *BinanceChain) initStaking() {
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)
}
}

upgrade.Mgr.RegisterBeginBlocker(sdk.BEP159, func(ctx sdk.Context) {
stake.MigrateValidatorDistributionAddr(ctx, app.stakeKeeper)
params := app.stakeKeeper.GetParams(ctx)
params.RewardDistributionBatchSize = 1000
params.MinSelfDelegation = 20000e8
params.MinDelegationChange = 1e8
params.MaxStakeSnapshots = 30
params.MaxValidators = 11
params.BaseProposerRewardRatio = sdk.NewDec(1e6) // 1%
params.BonusProposerRewardRatio = sdk.NewDec(4e6) // 4%
params.FeeFromBscToBcRatio = sdk.NewDec(1e7) // 10%
app.stakeKeeper.SetParams(ctx, params)
})
upgrade.Mgr.RegisterBeginBlocker(sdk.BEP159Phase2, func(ctx sdk.Context) {
stake.MigrateWhiteLabelOracleRelayer(ctx, app.stakeKeeper)
})
app.stakeKeeper.SubscribeParamChange(app.ParamHub)
app.stakeKeeper.SubscribeBCParamChange(app.ParamHub)
app.stakeKeeper = app.stakeKeeper.WithHooks(app.slashKeeper.Hooks())
}

Expand Down Expand Up @@ -611,6 +627,19 @@ func (app *BinanceChain) initSlashing() {
DowntimeSlashFee: 10e8,
})
})
upgrade.Mgr.RegisterBeginBlocker(sdk.BEP159, func(ctx sdk.Context) {
// write slash params to beacon chain when upgrade to BEP159
app.slashKeeper.SetParams(ctx, slashing.Params{
MaxEvidenceAge: 60 * 60 * 24 * 3 * time.Second, // 3 days
DoubleSignUnbondDuration: math.MaxInt64, // forever
DowntimeUnbondDuration: 60 * 60 * 24 * 2 * time.Second, // 2 days
TooLowDelUnbondDuration: 60 * 60 * 24 * time.Second, // 1 day
DoubleSignSlashAmount: 10000e8,
SubmitterReward: 1000e8,
DowntimeSlashAmount: 50e8,
DowntimeSlashFee: 10e8,
})
})
}

func (app *BinanceChain) initIbc() {
Expand Down Expand Up @@ -644,6 +673,8 @@ func (app *BinanceChain) initGovHooks() {
app.govKeeper.AddHooks(gov.ProposalTypeSCParamsChange, scParamChangeHooks)
app.govKeeper.AddHooks(gov.ProposalTypeDelistTradingPair, delistHooks)
app.govKeeper.AddHooks(gov.ProposalTypeManageChanPermission, chanPermissionHooks)
bcParamChangeHooks := paramHub.NewBCParamsChangeHook(app.Codec)
app.govKeeper.AddHooks(gov.ProposalTypeParameterChange, bcParamChangeHooks)
}

func (app *BinanceChain) initParams() {
Expand Down Expand Up @@ -847,7 +878,12 @@ func (app *BinanceChain) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) a

app.DexKeeper.StoreTradePrices(ctx)

blockFee := distributeFee(ctx, app.AccountKeeper, app.ValAddrCache, app.publicationConfig.PublishBlockFee)
var blockFee pub.BlockFee
if sdk.IsUpgrade(upgrade.BEP159) {
blockFee = distributeFeeBEP159(ctx, app.AccountKeeper, app.ValAddrCache, app.publicationConfig.PublishBlockFee, app.stakeKeeper)
} else {
blockFee = distributeFee(ctx, app.AccountKeeper, app.ValAddrCache, app.publicationConfig.PublishBlockFee)
}

passed, failed := gov.EndBlocker(ctx, app.govKeeper)
var proposals pub.Proposals
Expand All @@ -859,6 +895,7 @@ func (app *BinanceChain) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) a
sidechain.EndBlock(ctx, app.scKeeper)
var completedUbd []stake.UnbondingDelegation
var validatorUpdates abci.ValidatorUpdates
// todo: get validatorUpdates in slashing EndBlocker
if isBreatheBlock {
validatorUpdates, completedUbd = stake.EndBreatheBlock(ctx, app.stakeKeeper)
} else if ctx.RouterCallRecord()["stake"] || sdk.IsUpgrade(upgrade.BEP128) {
Expand Down
31 changes: 14 additions & 17 deletions app/app_paramhub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ import (
"testing"
"time"

"github.com/bnb-chain/node/common/upgrade"

"github.com/stretchr/testify/assert"

"github.com/tendermint/go-amino"
abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkFees "github.com/cosmos/cosmos-sdk/types/fees"
Expand All @@ -39,8 +23,21 @@ import (
sTypes "github.com/cosmos/cosmos-sdk/x/sidechain/types"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/stretchr/testify/assert"
"github.com/tendermint/go-amino"
abcicli "github.com/tendermint/tendermint/abci/client"
"github.com/tendermint/tendermint/abci/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/tmhash"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"

ctypes "github.com/bnb-chain/node/common/types"
"github.com/bnb-chain/node/common/upgrade"
"github.com/bnb-chain/node/plugins/dex"
"github.com/bnb-chain/node/plugins/tokens"
"github.com/bnb-chain/node/wire"
Expand All @@ -49,7 +46,7 @@ import (
// util objects
var (
memDB = dbm.NewMemDB()
logger = log.NewTMLogger(os.Stdout)
logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
genAccs, addrs, pubKeys, privKeys = mock.CreateGenAccounts(4,
sdk.Coins{sdk.NewCoin("BNB", 500000e8), sdk.NewCoin("BTC-000", 200e8)})
testScParams = `[{"type": "params/StakeParamSet","value": {"unbonding_time": "604800000000000","max_validators": 11,"bond_denom": "BNB","min_self_delegation": "5000000000000","min_delegation_change": "100000000","reward_distribution_batch_size":"1000"}},{"type": "params/SlashParamSet","value": {"max_evidence_age": "259200000000000","signed_blocks_window": "0","min_signed_per_window": "0","double_sign_unbond_duration": "9223372036854775807","downtime_unbond_duration": "172800000000000","too_low_del_unbond_duration": "86400000000000","slash_fraction_double_sign": "0","slash_fraction_downtime": "0","double_sign_slash_amount": "1000000000000","downtime_slash_amount": "5000000000","submitter_reward": "100000000000","downtime_slash_fee": "1000000000"}},{"type": "params/OracleParamSet","value": {"ConsensusNeeded": "70000000"}},{"type": "params/IbcParamSet","value": {"relayer_fee": "1000000"}}]`
Expand Down
10 changes: 8 additions & 2 deletions app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (
"text/template"

"github.com/cosmos/cosmos-sdk/server"

"github.com/spf13/viper"

"github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/libs/common"
)
Expand Down Expand Up @@ -91,6 +89,10 @@ BEP128Height = {{ .UpgradeConfig.BEP128Height }}
BEP151Height = {{ .UpgradeConfig.BEP151Height }}
# Block height of BEP153 upgrade
BEP153Height = {{ .UpgradeConfig.BEP153Height }}
# Block height of BEP159 upgrade
BEP159Height = {{ .UpgradeConfig.BEP159Height }}
# Block height of BEP159Phase2 upgrade
BEP159Phase2Height = {{ .UpgradeConfig.BEP159Phase2Height }}
# Block height of BEP173 upgrade
BEP173Height = {{ .UpgradeConfig.BEP173Height }}
Expand Down Expand Up @@ -534,6 +536,8 @@ type UpgradeConfig struct {
BEP128Height int64 `mapstructure:"BEP128Height"`
BEP151Height int64 `mapstructure:"BEP151Height"`
BEP153Height int64 `mapstructure:"BEP153Height"`
BEP159Height int64 `mapstructure:"BEP159Height"`
BEP159Phase2Height int64 `mapstructure:"BEP159Phase2Height"`
BEP173Height int64 `mapstructure:"BEP173Height"`
}

Expand All @@ -557,6 +561,8 @@ func defaultUpgradeConfig() *UpgradeConfig {
BEP128Height: math.MaxInt64,
BEP151Height: math.MaxInt64,
BEP153Height: math.MaxInt64,
BEP159Height: math.MaxInt64,
BEP159Phase2Height: math.MaxInt64,
BEP173Height: math.MaxInt64,
BEP82Height: math.MaxInt64,
BEP84Height: math.MaxInt64,
Expand Down
Loading

0 comments on commit e4a5af7

Please sign in to comment.