Skip to content

Commit

Permalink
BEP153: Native Staking Implementation (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonberg1997 authored Aug 16, 2022
1 parent 2ba5807 commit 9c35e25
Show file tree
Hide file tree
Showing 45 changed files with 1,310 additions and 158 deletions.
9 changes: 9 additions & 0 deletions bsc/rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@ const (
BNBDecimalOnBSC = 18
)

// ConvertBCAmountToBSCAmount can only be used to convert BNB decimal
func ConvertBCAmountToBSCAmount(bcAmount int64) *big.Int {
decimals := sdk.NewIntWithDecimal(1, int(BNBDecimalOnBSC-BNBDecimalOnBC))
bscAmount := sdk.NewInt(bcAmount).Mul(decimals)
return bscAmount.BigInt()
}

// ConvertBSCAmountToBCAmount can only be used to convert BNB decimal
func ConvertBSCAmountToBCAmount(bscAmount *big.Int) int64 {
decimals := sdk.NewIntWithDecimal(1, int(BNBDecimalOnBSC-BNBDecimalOnBC))
bscAmountInt := sdk.NewIntFromBigInt(bscAmount)
bcAmount := bscAmountInt.Div(decimals)
return bcAmount.Int64()
}
1 change: 1 addition & 0 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.keyStake, app.keyStakeReward, app.tkeyStake,
app.bankKeeper, app.Pool, app.paramsKeeper.Subspace(stake.DefaultParamspace),
app.RegisterCodespace(stake.DefaultCodespace),
sdk.ChainID(0), "",
)
app.mintKeeper = mint.NewKeeper(app.cdc, app.keyMint,
app.paramsKeeper.Subspace(mint.DefaultParamspace),
Expand Down
1 change: 1 addition & 0 deletions cmd/gaia/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func NewMockGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppO
app.keyStake, app.keyStakeReward, app.tkeyStake,
app.bankKeeper, nil, app.paramsKeeper.Subspace(stake.DefaultParamspace),
app.RegisterCodespace(stake.DefaultCodespace),
sdk.ChainID(0), "",
)
app.mintKeeper = mint.NewKeeper(app.cdc, app.keyMint,
app.paramsKeeper.Subspace(mint.DefaultParamspace),
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {
validator := stake.NewValidator(valAddr, accs[i].PubKey, stake.Description{})
validator.Tokens = sdk.NewDecWithoutFra(amt)
validator.DelegatorShares = sdk.NewDecWithoutFra(amt)
delegation := stake.Delegation{accs[i].Address, valAddr, sdk.NewDecWithoutFra(amt), 0}
delegation := stake.Delegation{DelegatorAddr: accs[i].Address, ValidatorAddr: valAddr, Shares: sdk.NewDecWithoutFra(amt)}
validators = append(validators, validator)
delegations = append(delegations, delegation)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gaia/cmd/gaiadebug/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, baseAppOptions ...func(*bam.BaseAp
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams)
app.ibcKeeper = ibc.NewKeeper(app.keyIbc, app.paramsKeeper.Subspace(ibc.DefaultParamspace), ibc.DefaultCodespace, sidechain.NewKeeper(app.keySide, app.paramsKeeper.Subspace(sidechain.DefaultParamspace), app.cdc))

app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.keyStakeReward, app.tkeyStake, app.bankKeeper, nil, app.paramsKeeper.Subspace(stake.DefaultParamspace), app.RegisterCodespace(stake.DefaultCodespace))
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.keyStakeReward, app.tkeyStake, app.bankKeeper, nil, app.paramsKeeper.Subspace(stake.DefaultParamspace), app.RegisterCodespace(stake.DefaultCodespace), sdk.ChainID(0), "")
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Subspace(slashing.DefaultParamspace), app.RegisterCodespace(slashing.DefaultCodespace), app.bankKeeper)

// register message routes
Expand Down
60 changes: 60 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"strings"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/encoding/amino"
Expand All @@ -16,6 +18,8 @@ import (
const (
// AddrLen defines a valid address length
AddrLen = 20
// SmartChainAddressLength defines a valid smart chain address length
SmartChainAddressLength = 20

// Bech32PrefixAccAddr defines the Bech32 prefix of an account's address
Bech32PrefixAccAddr = "cosmos"
Expand Down Expand Up @@ -386,6 +390,62 @@ func (ca ConsAddress) Format(s fmt.State, verb rune) {
}
}

// SmartChainAddress defines a standard smart chain address
type SmartChainAddress [SmartChainAddressLength]byte

// NewSmartChainAddress is a constructor function for SmartChainAddress
func NewSmartChainAddress(addr string) (SmartChainAddress, error) {
addr = strings.ToLower(addr)
if len(addr) >= 2 && addr[:2] == "0x" {
addr = addr[2:]
}
if length := len(addr); length != 2*SmartChainAddressLength {
return SmartChainAddress{}, fmt.Errorf("invalid address hex length: %v != %v", length, 2*SmartChainAddressLength)
}

bin, err := hex.DecodeString(addr)
if err != nil {
return SmartChainAddress{}, err
}
var address SmartChainAddress
address.SetBytes(bin)
return address, nil
}

func (addr *SmartChainAddress) SetBytes(b []byte) {
if len(b) > len(addr) {
b = b[len(b)-20:]
}
copy(addr[20-len(b):], b)
}

func (addr SmartChainAddress) IsEmpty() bool {
addrValue := big.NewInt(0)
addrValue.SetBytes(addr[:])

return addrValue.Cmp(big.NewInt(0)) == 0
}

// Route should return the name of the module
func (addr SmartChainAddress) String() string {
return HexAddress(addr[:])
}

// MarshalJSON marshals the smart chain address to JSON
func (addr SmartChainAddress) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%v\"", addr.String())), nil
}

// UnmarshalJSON unmarshals an smart chain address
func (addr *SmartChainAddress) UnmarshalJSON(input []byte) error {
hexBytes, err := HexDecode(string(input[1 : len(input)-1]))
if err != nil {
return err
}
addr.SetBytes(hexBytes)
return nil
}

// ----------------------------------------------------------------------------
// auxiliary
// ----------------------------------------------------------------------------
Expand Down
17 changes: 16 additions & 1 deletion types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -178,3 +177,19 @@ func TestConsAddress(t *testing.T) {
require.NotNil(t, err)
}
}

func TestSmartChainAddress(t *testing.T) {
addrStr := "0x43121d597656E398473b992f0dF667fF0fc0791C"
address, err := types.NewSmartChainAddress(addrStr)
require.Nil(t, err, "err should be nil")
convertedAddrStr := address.String()
require.Equal(t, addrStr, convertedAddrStr, "address should be equal")

addrStr = "0x43121d597656E398473b992f0dF667fF0fc0791C1"
_, err = types.NewSmartChainAddress(addrStr)
require.NotNil(t, err, "err should not be nil")

addrStr = "0x43121d597656E398473b992f0dF667fF0fc0791"
_, err = types.NewSmartChainAddress(addrStr)
require.NotNil(t, err, "err should not be nil")
}
10 changes: 10 additions & 0 deletions types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Context struct {
eventManager *EventManager
sideChainKeyPrefix []byte
sideChainId string
crossStake bool
}

// create a new context
Expand Down Expand Up @@ -110,6 +111,10 @@ func (c Context) SideChainId() string {
return c.sideChainId
}

func (c Context) CrossStake() bool {
return c.crossStake
}

//----------------------------------------
// With* (setting a value)

Expand Down Expand Up @@ -216,6 +221,11 @@ func (c Context) WithSideChainId(sideChainId string) Context {
return c
}

func (c Context) WithCrossStake(crossStake bool) Context {
c.crossStake = crossStake
return c
}

// is context nil
func (c Context) IsZero() bool {
return c.ctx == nil && c.ms == nil
Expand Down
7 changes: 7 additions & 0 deletions types/cross_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const (
ChannelForbidden ChannelPermission = 0
)

type RewardConfig uint8

const (
RewardFromSystem RewardConfig = 0
RewardNotFromSystem RewardConfig = 1
)

func IsValidCrossChainPackageType(packageType CrossChainPackageType) bool {
return packageType == SynCrossChainPackageType || packageType == AckCrossChainPackageType || packageType == FailAckCrossChainPackageType
}
Expand Down
1 change: 1 addition & 0 deletions types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
BEP82 = "BEP82" // https://github.com/bnb-chain/BEPs/pull/82
FixFailAckPackage = "FixFailAckPackage"
BEP128 = "BEP128" //https://github.com/bnb-chain/BEPs/pull/128
BEP153 = "BEP153" //https://github.com/bnb-chain/BEPs/pull/153
)

var MainNetConfig = UpgradeConfig{
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/test_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initCoins int64,
scKeeper := sidechain.NewKeeper(keySideChain, pk.Subspace(sidechain.DefaultParamspace), cdc)
scKeeper.SetParams(ctx, sidechain.DefaultParams())
ibcKeeper := ibc.NewKeeper(keyIbc, pk.Subspace(ibc.DefaultParamspace), ibc.DefaultCodespace, scKeeper)
sk := stake.NewKeeper(cdc, keyStake, keyStakeReward, tkeyStake, ck, nil, pk.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
sk := stake.NewKeeper(cdc, keyStake, keyStakeReward, tkeyStake, ck, nil, pk.Subspace(stake.DefaultParamspace), stake.DefaultCodespace, sdk.ChainID(0), "")
sk.SetPool(ctx, stake.InitialPool())
sk.SetParams(ctx, stake.DefaultParams())
sk.SetupForSideChain(&scKeeper, &ibcKeeper)
Expand Down
2 changes: 1 addition & 1 deletion x/gov/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func getMockApp(t *testing.T, numGenAccs int) (*mock.App, bank.BaseKeeper, gov.K
scKeeper := sidechain.NewKeeper(keySideChain, pk.Subspace(sidechain.DefaultParamspace), mapp.Cdc)
ibcKeeper := ibc.NewKeeper(keyIbc, pk.Subspace(ibc.DefaultParamspace), ibc.DefaultCodespace, scKeeper)

sk := stake.NewKeeper(mapp.Cdc, keyStake, keyStakeReward, tkeyStake, ck, nil, pk.Subspace(stake.DefaultParamspace), mapp.RegisterCodespace(stake.DefaultCodespace))
sk := stake.NewKeeper(mapp.Cdc, keyStake, keyStakeReward, tkeyStake, ck, nil, pk.Subspace(stake.DefaultParamspace), mapp.RegisterCodespace(stake.DefaultCodespace), sdk.ChainID(0), "")
sk.SetupForSideChain(&scKeeper, &ibcKeeper)
keeper := gov.NewKeeper(mapp.Cdc, keyGov, pk, pk.Subspace("testgov"), ck, sk, gov.DefaultCodespace, new(sdk.Pool))

Expand Down
2 changes: 1 addition & 1 deletion x/gov/simulation/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestGovWithRandomMessages(t *testing.T) {
keySideChain := sdk.NewKVStoreKey("sc")
scKeeper := sidechain.NewKeeper(keySideChain, paramKeeper.Subspace(sidechain.DefaultParamspace), mapp.Cdc)
ibcKeeper := ibc.NewKeeper(keyIbc, paramKeeper.Subspace(ibc.DefaultParamspace), ibc.DefaultCodespace, scKeeper)
stakeKeeper := stake.NewKeeper(mapp.Cdc, stakeKey, stakeRewardKey, stakeTKey, bankKeeper, nil, paramKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
stakeKeeper := stake.NewKeeper(mapp.Cdc, stakeKey, stakeRewardKey, stakeTKey, bankKeeper, nil, paramKeeper.Subspace(stake.DefaultParamspace), stake.DefaultCodespace, sdk.ChainID(0), "")
stakeKeeper.SetupForSideChain(&scKeeper, &ibcKeeper)
govKey := sdk.NewKVStoreKey("gov")
govKeeper := gov.NewKeeper(mapp.Cdc, govKey, paramKeeper, paramKeeper.Subspace(gov.DefaultParamSpace), bankKeeper, stakeKeeper, gov.DefaultCodespace, &sdk.Pool{})
Expand Down
5 changes: 3 additions & 2 deletions x/oracle/keeper/test_common.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package keeper

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/sidechain"
"testing"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -34,7 +35,7 @@ func getMockApp(t *testing.T, numGenAccs int) (*mock.App, bank.BaseKeeper, Keepe

pk := params.NewKeeper(mapp.Cdc, keyGlobalParams, tkeyGlobalParams)
ck := bank.NewBaseKeeper(mapp.AccountKeeper)
sk := stake.NewKeeper(mapp.Cdc, keyStake, keyStakeReward, tkeyStake, ck, nil, pk.Subspace(stake.DefaultParamspace), mapp.RegisterCodespace(stake.DefaultCodespace))
sk := stake.NewKeeper(mapp.Cdc, keyStake, keyStakeReward, tkeyStake, ck, nil, pk.Subspace(stake.DefaultParamspace), mapp.RegisterCodespace(stake.DefaultCodespace), sdk.ChainID(0), "")
scK := sidechain.NewKeeper(keySideChain, pk.Subspace(sidechain.DefaultParamspace), mapp.Cdc)
ibcKeeper := ibc.NewKeeper(keyIbc, pk.Subspace(ibc.DefaultParamspace), ibc.DefaultCodespace, scK)

Expand Down
4 changes: 4 additions & 0 deletions x/paramHub/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ const (
MiniIssueFee = 3e8
MiniSetUriFee = 37500
MiniListingFee = 8e8

// Cross stake fee
CrossDistributeRewardRelayFee = 6e5 // 0.006 BNB
CrossDistributeUndelegatedRelayFee = 6e5 // 0.006 BNB
)

var DefaultGenesisState = param.GenesisState{
Expand Down
95 changes: 52 additions & 43 deletions x/paramHub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func RegisterUpgradeBeginBlocker(paramHub *ParamHub) {
}
paramHub.UpdateFeeParams(ctx, updateFeeParams)
})
sdk.UpgradeMgr.RegisterBeginBlocker(sdk.BEP153, func(ctx sdk.Context) {
crossStakeFeeParams := []param.FeeParam{
&param.FixedFeeParams{MsgType: "crossDistributeRewardRelayFee", Fee: CrossDistributeRewardRelayFee, FeeFor: sdk.FeeForAll},
&param.FixedFeeParams{MsgType: "crossDistributeUndelegatedRelayFee", Fee: CrossDistributeUndelegatedRelayFee, FeeFor: sdk.FeeForAll},
}
paramHub.UpdateFeeParams(ctx, crossStakeFeeParams)
})
}

func EndBreatheBlock(ctx sdk.Context, paramHub *ParamHub) {
Expand All @@ -92,48 +99,50 @@ func init() {
// CalculatorsGen is defined in a common package which can't import app package.
// Reasonable to init here, since fee param drive the calculator.
fees.CalculatorsGen = map[string]fees.FeeCalculatorGenerator{
"submit_proposal": fees.FixedFeeCalculatorGen,
"deposit": fees.FixedFeeCalculatorGen,
"vote": fees.FixedFeeCalculatorGen,
"side_submit_proposal": fees.FixedFeeCalculatorGen,
"side_deposit": fees.FixedFeeCalculatorGen,
"side_vote": fees.FixedFeeCalculatorGen,
"create_validator": fees.FixedFeeCalculatorGen,
"remove_validator": fees.FixedFeeCalculatorGen,
"side_create_validator": fees.FixedFeeCalculatorGen,
"side_edit_validator": fees.FixedFeeCalculatorGen,
"side_delegate": fees.FixedFeeCalculatorGen,
"side_redelegate": fees.FixedFeeCalculatorGen,
"side_undelegate": fees.FixedFeeCalculatorGen,
"bsc_submit_evidence": fees.FixedFeeCalculatorGen,
"side_chain_unjail": fees.FixedFeeCalculatorGen,
"dexList": fees.FixedFeeCalculatorGen,
"orderNew": fees.FixedFeeCalculatorGen,
"orderCancel": fees.FixedFeeCalculatorGen,
"issueMsg": fees.FixedFeeCalculatorGen,
"mintMsg": fees.FixedFeeCalculatorGen,
"tokensBurn": fees.FixedFeeCalculatorGen,
"setAccountFlags": fees.FixedFeeCalculatorGen,
"tokensFreeze": fees.FixedFeeCalculatorGen,
"timeLock": fees.FixedFeeCalculatorGen,
"timeUnlock": fees.FixedFeeCalculatorGen,
"timeRelock": fees.FixedFeeCalculatorGen,
"transferOwnership": fees.FixedFeeCalculatorGen,
"send": bank.TransferFeeCalculatorGen,
"HTLT": fees.FixedFeeCalculatorGen,
"depositHTLT": fees.FixedFeeCalculatorGen,
"claimHTLT": fees.FixedFeeCalculatorGen,
"refundHTLT": fees.FixedFeeCalculatorGen,
"crossBind": fees.FixedFeeCalculatorGen,
"crossUnbind": fees.FixedFeeCalculatorGen,
"crossTransferOut": fees.FixedFeeCalculatorGen,
"crossBindRelayFee": fees.FixedFeeCalculatorGen,
"crossUnbindRelayFee": fees.FixedFeeCalculatorGen,
"crossTransferOutRelayFee": fees.FixedFeeCalculatorGen,
"oracleClaim": fees.FixedFeeCalculatorGen,
"miniTokensSetURI": fees.FixedFeeCalculatorGen,
"dexListMini": fees.FixedFeeCalculatorGen,
"tinyIssueMsg": fees.FixedFeeCalculatorGen,
"miniIssueMsg": fees.FixedFeeCalculatorGen,
"submit_proposal": fees.FixedFeeCalculatorGen,
"deposit": fees.FixedFeeCalculatorGen,
"vote": fees.FixedFeeCalculatorGen,
"side_submit_proposal": fees.FixedFeeCalculatorGen,
"side_deposit": fees.FixedFeeCalculatorGen,
"side_vote": fees.FixedFeeCalculatorGen,
"create_validator": fees.FixedFeeCalculatorGen,
"remove_validator": fees.FixedFeeCalculatorGen,
"side_create_validator": fees.FixedFeeCalculatorGen,
"side_edit_validator": fees.FixedFeeCalculatorGen,
"side_delegate": fees.FixedFeeCalculatorGen,
"side_redelegate": fees.FixedFeeCalculatorGen,
"side_undelegate": fees.FixedFeeCalculatorGen,
"bsc_submit_evidence": fees.FixedFeeCalculatorGen,
"side_chain_unjail": fees.FixedFeeCalculatorGen,
"dexList": fees.FixedFeeCalculatorGen,
"orderNew": fees.FixedFeeCalculatorGen,
"orderCancel": fees.FixedFeeCalculatorGen,
"issueMsg": fees.FixedFeeCalculatorGen,
"mintMsg": fees.FixedFeeCalculatorGen,
"tokensBurn": fees.FixedFeeCalculatorGen,
"setAccountFlags": fees.FixedFeeCalculatorGen,
"tokensFreeze": fees.FixedFeeCalculatorGen,
"timeLock": fees.FixedFeeCalculatorGen,
"timeUnlock": fees.FixedFeeCalculatorGen,
"timeRelock": fees.FixedFeeCalculatorGen,
"transferOwnership": fees.FixedFeeCalculatorGen,
"send": bank.TransferFeeCalculatorGen,
"HTLT": fees.FixedFeeCalculatorGen,
"depositHTLT": fees.FixedFeeCalculatorGen,
"claimHTLT": fees.FixedFeeCalculatorGen,
"refundHTLT": fees.FixedFeeCalculatorGen,
"crossBind": fees.FixedFeeCalculatorGen,
"crossUnbind": fees.FixedFeeCalculatorGen,
"crossTransferOut": fees.FixedFeeCalculatorGen,
"crossBindRelayFee": fees.FixedFeeCalculatorGen,
"crossUnbindRelayFee": fees.FixedFeeCalculatorGen,
"crossTransferOutRelayFee": fees.FixedFeeCalculatorGen,
"oracleClaim": fees.FixedFeeCalculatorGen,
"miniTokensSetURI": fees.FixedFeeCalculatorGen,
"dexListMini": fees.FixedFeeCalculatorGen,
"tinyIssueMsg": fees.FixedFeeCalculatorGen,
"miniIssueMsg": fees.FixedFeeCalculatorGen,
"crossDistributeRewardRelayFee": fees.FixedFeeCalculatorGen,
"crossDistributeUndelegatedRelayFee": fees.FixedFeeCalculatorGen,
}
}
3 changes: 3 additions & 0 deletions x/paramHub/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var (
"miniIssueMsg": {},
"miniTokensSetURI": {},
"dexListMini": {},

"crossDistributeRewardRelayFee": {},
"crossDistributeUndelegatedRelayFee": {},
}

ValidTransferFeeMsgTypes = map[string]struct{}{
Expand Down
Loading

0 comments on commit 9c35e25

Please sign in to comment.