Skip to content
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

BEP-159: Introduce A New Staking Mechanism on BNB Beacon Chain #880

Merged
merged 60 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
b107341
init node change
owen-reorg Aug 8, 2022
e12189d
add coverage test
owen-reorg Aug 8, 2022
d72a3ca
fix build
owen-reorg Aug 8, 2022
dca8581
add multi-nodes test
owen-reorg Aug 9, 2022
abd8185
fix go mod
owen-reorg Aug 9, 2022
bd6d6dc
fix lint
owen-reorg Aug 9, 2022
6254391
update cosmos dep
owen-reorg Aug 9, 2022
5830f51
fix clear cache
owen-reorg Aug 9, 2022
38a2782
force clean cache in breath block
owen-reorg Aug 9, 2022
8bf4750
remove useless todo
owen-reorg Aug 9, 2022
08d824a
fix docker
owen-reorg Aug 9, 2022
82b3ce6
fix test
owen-reorg Aug 9, 2022
9163330
update
owen-reorg Aug 19, 2022
750ee63
Merge branch 'develop' into staking-bep
owen-reorg Aug 19, 2022
7d6be13
fix ci
owen-reorg Aug 25, 2022
a121040
update workflow
owen-reorg Aug 25, 2022
69728b4
make stake new params governable
owen-reorg Aug 25, 2022
181b0b8
fix and format
owen-reorg Aug 26, 2022
3ebe4cb
impl white label oracle relayer
owen-reorg Aug 30, 2022
ff02ae2
update
owen-reorg Sep 1, 2022
a86dedd
Merge branch 'develop' into staking-bep
owen-reorg Sep 1, 2022
4d313c5
format
owen-reorg Sep 1, 2022
19eec5f
fix lint
owen-reorg Sep 1, 2022
fe6a393
revert Dockerfile
owen-reorg Sep 2, 2022
05eac4e
fix multi-node-test
owen-reorg Sep 2, 2022
cf3ff8c
try debug multi-node-test
owen-reorg Sep 2, 2022
37885a8
change docker compose
owen-reorg Sep 2, 2022
9606bba
impl cli and add integration test via cli
owen-reorg Sep 5, 2022
bdb661c
update cosmos-sdk dep
owen-reorg Sep 6, 2022
7ea8e72
fix multi-node-test
owen-reorg Sep 6, 2022
22d7886
fix ci
owen-reorg Sep 6, 2022
6c6a423
try fix ci
owen-reorg Sep 6, 2022
dad0503
fix ci
owen-reorg Sep 6, 2022
5a30d5e
fix multi-node-test
owen-reorg Sep 6, 2022
0f2a92b
fix msg problem
owen-reorg Sep 18, 2022
99a25bf
Merge branch 'develop' into staking-bep
owen-reorg Sep 18, 2022
a6beb5f
update dep
owen-reorg Sep 18, 2022
fe1d69c
fix ci
owen-reorg Sep 18, 2022
10e8d59
fix staking events
owen-reorg Sep 29, 2022
267473a
add save consensus key to file example
owen-reorg Oct 23, 2022
db725b2
support change params via gov on bc
owen-reorg Oct 26, 2022
bd55fff
Merge branch 'develop' into staking-bep
owen-reorg Oct 26, 2022
d1f3f54
fix lint
owen-reorg Oct 26, 2022
db56f05
update cosmos-sdk
owen-reorg Nov 7, 2022
789141d
update tendermint dep
owen-reorg Nov 9, 2022
beeb67c
fix change pubkey bug and add log for FeeCalculation
owen-reorg Nov 10, 2022
55af2c7
fix lint
owen-reorg Nov 10, 2022
54935d2
fix oracle total power issue
owen-reorg Nov 14, 2022
e633751
adjust log
owen-reorg Nov 21, 2022
d704aac
add slashing param in BEP159 to avoid panic in undelegation
owen-reorg Nov 22, 2022
26ee8b6
fix undelegate and unjail
owen-reorg Nov 30, 2022
78a8dc7
Merge branch 'develop' into staking-bep
owen-reorg Nov 30, 2022
4639909
remove the support of MsgRedelegate
unclezoro Dec 12, 2022
ac7ddea
merge with develop branch
unclezoro Dec 12, 2022
0bf8291
update dependency
unclezoro Dec 13, 2022
f338bf7
update dep
owen-reorg Dec 19, 2022
47a8047
update dependency
unclezoro Dec 20, 2022
3ae2c0a
update dep
unclezoro Dec 20, 2022
3accb68
fix fee distribution
owen-reorg Dec 21, 2022
78bd534
update dep
unclezoro Dec 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
- 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
60 changes: 49 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,14 @@ 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.MsgRedelegate{}.Type(),
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
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 +567,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 +628,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{
owen-reorg marked this conversation as resolved.
Show resolved Hide resolved
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 +674,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 +879,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 +896,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