Skip to content

Commit

Permalink
register wasmkeeper first before wasm.NewIBCHandler (#257)
Browse files Browse the repository at this point in the history
* register wasmkeeper first before wasm.NewIBCHandler

* format locabitsong

* bump ictest

* degrage e2e-polytone to just test good channel creation

* wip: init-from-state

* fix: staking keeper dereference

* bump govtypes on app gen

* localbitsong: check for mnemonic

* fix: cosmos-sdk fork

---------

Co-authored-by: hard-nett <hardnettt@proton.me>
  • Loading branch information
hard-nett and hard-nett authored Jan 3, 2025
1 parent 56b91cd commit bc3863e
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 514 deletions.
42 changes: 21 additions & 21 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func NewAppKeepers(
appKeepers.FeeGrantKeeper = feegrantkeeper.NewKeeper(
appCodec, keys[feegrant.StoreKey], appKeepers.AccountKeeper,
)
stakingKeeper := *stakingkeeper.NewKeeper(
stakingKeeper := stakingkeeper.NewKeeper(
appCodec, keys[stakingtypes.StoreKey], appKeepers.AccountKeeper, appKeepers.BankKeeper, govModAddress,
)
appKeepers.MintKeeper = mintkeeper.NewKeeper(
Expand Down Expand Up @@ -214,7 +214,7 @@ func NewAppKeepers(
stakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(appKeepers.DistrKeeper.Hooks(), appKeepers.SlashingKeeper.Hooks()),
)
appKeepers.StakingKeeper = &stakingKeeper
appKeepers.StakingKeeper = stakingKeeper

// ... other modules keepers

Expand Down Expand Up @@ -250,7 +250,6 @@ func NewAppKeepers(
appCodec,
keys[ibctransfertypes.StoreKey],
appKeepers.GetSubspace(ibctransfertypes.ModuleName),
//app.IBCKeeper.ChannelKeeper,
appKeepers.PacketForwardKeeper,
appKeepers.IBCKeeper.ChannelKeeper,
&appKeepers.IBCKeeper.PortKeeper,
Expand Down Expand Up @@ -292,25 +291,7 @@ func NewAppKeepers(
// Set legacy router for backwards compatibility with gov v1beta1
appKeepers.GovKeeper.SetLegacyRouter(govRouter)

// Create Transfer Stack
var transferStack porttypes.IBCModule
const middlewareTimeoutRetry = 0
transferStack = transfer.NewIBCModule(appKeepers.TransferKeeper)
transferStack = packetforward.NewIBCMiddleware(
transferStack,
appKeepers.PacketForwardKeeper,
middlewareTimeoutRetry, // retries on timeout
packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp, // forward timeout
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, // refund timeout
)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
ibcRouter.AddRoute(wasmtypes.ModuleName, wasm.NewIBCHandler(appKeepers.WasmKeeper, appKeepers.IBCKeeper.ChannelKeeper, appKeepers.IBCKeeper.ChannelKeeper))
appKeepers.IBCKeeper.SetRouter(ibcRouter)
wasmDir := filepath.Join(homePath, "data")

wasmConfig, err := wasm.ReadWasmConfig(appOpts)
if err != nil {
panic("error while reading wasm config: " + err.Error())
Expand Down Expand Up @@ -367,6 +348,25 @@ func NewAppKeepers(
wasmOpts...,
)

/// ADVANCED IBC CONFIGURATION
// Create Transfer Stack
var transferStack porttypes.IBCModule
const middlewareTimeoutRetry = 0
transferStack = transfer.NewIBCModule(appKeepers.TransferKeeper)
transferStack = packetforward.NewIBCMiddleware(
transferStack,
appKeepers.PacketForwardKeeper,
middlewareTimeoutRetry, // retries on timeout
packetforwardkeeper.DefaultForwardTransferPacketTimeoutTimestamp, // forward timeout
packetforwardkeeper.DefaultRefundTransferPacketTimeoutTimestamp, // refund timeout
)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transferStack)
ibcRouter.AddRoute(wasmtypes.ModuleName, wasm.NewIBCHandler(appKeepers.WasmKeeper, appKeepers.IBCKeeper.ChannelKeeper, appKeepers.IBCKeeper.ChannelKeeper))
appKeepers.IBCKeeper.SetRouter(ibcRouter)

appKeepers.ScopedIBCKeeper = scopedIBCKeeper
appKeepers.ScopedTransferKeeper = scopedTransferKeeper
appKeepers.ScopedWasmKeeper = scopedWasmKeeper
Expand Down
3 changes: 1 addition & 2 deletions app/upgrades/v011/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v011
import (
"github.com/bitsongofficial/go-bitsong/app/upgrades"
fantokentypes "github.com/bitsongofficial/go-bitsong/x/fantoken/types"
merkledroptypes "github.com/bitsongofficial/go-bitsong/x/merkledrop/types"
store "github.com/cosmos/cosmos-sdk/store/types"
)

Expand All @@ -15,6 +14,6 @@ var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateV11UpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{fantokentypes.ModuleName, merkledroptypes.ModuleName},
Added: []string{fantokentypes.ModuleName, "merkledrop"},
},
}
3 changes: 1 addition & 2 deletions app/upgrades/v018/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v018

import (
"github.com/bitsongofficial/go-bitsong/app/upgrades"
merkledroptypes "github.com/bitsongofficial/go-bitsong/x/merkledrop/types"
store "github.com/cosmos/cosmos-sdk/store/types"
consensustypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
Expand All @@ -17,6 +16,6 @@ var Upgrade = upgrades.Upgrade{
CreateUpgradeHandler: CreateV18UpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{crisistypes.ModuleName, consensustypes.ModuleName},
Deleted: []string{merkledroptypes.ModuleName},
Deleted: []string{"merkledrop"},
},
}
29 changes: 16 additions & 13 deletions cmd/bitsongd/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1beta1govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
v1govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"

minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
Expand All @@ -35,7 +35,7 @@ type GenesisParams struct {
StakingParams stakingtypes.Params
MintParams minttypes.Params
DistributionParams distributiontypes.Params
GovParams v1beta1govtypes.GenesisState
GovParams v1govtypes.GenesisState
SlashingParams slashingtypes.Params

CrisisConstantFee sdk.Coin
Expand Down Expand Up @@ -86,20 +86,22 @@ func MainnetGenesisParams() GenesisParams {
genParams.MintParams.MintDenom = appparams.MicroCoinUnit

genParams.DistributionParams = distributiontypes.DefaultParams()
genParams.DistributionParams.BaseProposerReward = sdk.MustNewDecFromStr("0.01")
genParams.DistributionParams.BonusProposerReward = sdk.MustNewDecFromStr("0.04")
genParams.DistributionParams.CommunityTax = sdk.MustNewDecFromStr("0.02")
genParams.DistributionParams.WithdrawAddrEnabled = true

genParams.GovParams.DepositParams.MaxDepositPeriod = time.Hour * 24 * 15 // 15 days
genParams.GovParams.DepositParams.MinDeposit = sdk.NewCoins(sdk.NewCoin(
maxDepositPeriod := time.Hour * 24 * 15
maxVotingPeriod := time.Hour * 24 * 7

genParams.GovParams.Params.MaxDepositPeriod = &maxDepositPeriod

genParams.GovParams.Params.MinDeposit = sdk.NewCoins(sdk.NewCoin(
appparams.MicroCoinUnit,
sdk.NewInt(512_000_000),
))
genParams.GovParams.TallyParams.Quorum = sdk.MustNewDecFromStr("0.4") // 40%
genParams.GovParams.TallyParams.Threshold = sdk.MustNewDecFromStr("0.5") // 50%
genParams.GovParams.TallyParams.VetoThreshold = sdk.MustNewDecFromStr("0.334") // 33.40%
genParams.GovParams.VotingParams.VotingPeriod = time.Hour * 24 * 7 // 7 days
genParams.GovParams.Params.Quorum = "0.4" // 40%
genParams.GovParams.Params.Threshold = "0.5" // 50%
genParams.GovParams.Params.VetoThreshold = "0.334" // 33.40%
genParams.GovParams.Params.VotingPeriod = &maxVotingPeriod // 7 days

genParams.SlashingParams = slashingtypes.DefaultParams()
genParams.SlashingParams.SignedBlocksWindow = int64(10000) // 10000 blocks (~13.8 hr at 5 second blocks)
Expand Down Expand Up @@ -129,8 +131,9 @@ func TestnetGenesisParams() GenesisParams {
appparams.MicroCoinUnit,
sdk.NewInt(1000000), // 1 BTSG
))
genParams.GovParams.TallyParams.Quorum = sdk.MustNewDecFromStr("0.0000000001") // 0.00000001%
genParams.GovParams.VotingParams.VotingPeriod = time.Second * 300 // 300 seconds
maxVotingPeriod := time.Second * 300
genParams.GovParams.Params.Quorum = "0.0000000001" // 0.00000001%
genParams.GovParams.Params.VotingPeriod = &maxVotingPeriod // 300 seconds

return genParams
}
Expand Down Expand Up @@ -166,7 +169,7 @@ func PrepareGenesis(clientCtx client.Context, appState map[string]json.RawMessag
}
appState[distributiontypes.ModuleName] = distributionGenStateBz

govGenState := v1beta1govtypes.DefaultGenesisState()
govGenState := v1govtypes.DefaultGenesisState()
govGenState.DepositParams = genesisParams.GovParams.DepositParams
govGenState.TallyParams = genesisParams.GovParams.TallyParams
govGenState.VotingParams = genesisParams.GovParams.VotingParams
Expand Down
50 changes: 26 additions & 24 deletions cmd/bitsongd/cmd/init_from_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"time"

errorsmod "cosmossdk.io/errors"
tmcfg "github.com/cometbft/cometbft/config"
tmcrypto "github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/libs/cli"
Expand All @@ -34,7 +35,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
v1beta1govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
v1govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/go-bip39"
ibcclienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
Expand All @@ -51,6 +52,7 @@ const (
FlagIncreaseCoinAmount = "increase-coin-amount"
)

// bitsongd init-from-state v021 export-v021.json v021 --old-moniker Cosmostation --old-account-addr bitsong1wf3q0a3uzechxvf27reuqts8nqm45sn29ykncv --increase-coin-amount 10000000000000000ubtsg --o
// InitFromStateCmd returns a command that initializes all files needed for Tendermint
// and the respective application.
func InitFromStateCmd(defaultNodeHome string) *cobra.Command {
Expand All @@ -65,12 +67,10 @@ func InitFromStateCmd(defaultNodeHome string) *cobra.Command {
config := serverCtx.Config

// Override default settings in config.toml
config.P2P.MaxNumInboundPeers = 100
config.P2P.MaxNumOutboundPeers = 50
config.P2P.MaxNumInboundPeers = 80
config.P2P.MaxNumOutboundPeers = 40
config.Mempool.Size = 10000
config.StateSync.TrustPeriod = 112 * time.Hour
// config.DeprecatedFastSyncConfig.Version = "v0"

config.SetRoot(clientCtx.HomeDir)

chainID, _ := cmd.Flags().GetString(flags.FlagChainID)
Expand All @@ -97,6 +97,7 @@ func InitFromStateCmd(defaultNodeHome string) *cobra.Command {
if err != nil {
return err
}
tmPubKey, _ := cryptocodec.ToTmPubKeyInterface(pubKey)

overwrite, _ := cmd.Flags().GetBool(FlagOverwrite)
moniker := args[0]
Expand All @@ -119,7 +120,6 @@ func InitFromStateCmd(defaultNodeHome string) *cobra.Command {
}
clientCtx.Keyring = kb

tmPubKey, _ := cryptocodec.ToTmPubKeyInterface(pubKey)
genParams := StateExportParams{
ChainID: chainID,
Moniker: moniker,
Expand Down Expand Up @@ -166,9 +166,9 @@ func initNodeFromState(config *tmcfg.Config, cliCtx client.Context, genParams St

// validate genesis state
// TODO: fix this!
/*if err = mbm.ValidateGenesis(cliCtx.Codec, cliCtx.TxConfig, appState); err != nil {
return fmt.Errorf("error validating genesis file: %s", err.Error())
}*/
// /*if err = mbm.ValidateGenesis(cliCtx.Codec, cliCtx.TxConfig, appState); err != nil {
// return fmt.Errorf("error validating genesis file: %s", err.Error())
// }*/

// save genesis
if err = genutil.ExportGenesisFile(genDoc, genFile); err != nil {
Expand Down Expand Up @@ -257,7 +257,7 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
return nil, err
}

stateBz, err := ioutil.ReadFile(params.StateFile)
stateBz, err := os.ReadFile(params.StateFile)
if err != nil {
return nil, fmt.Errorf("couldn't read state export file: %w", err)
}
Expand Down Expand Up @@ -308,9 +308,9 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
genDoc.ChainID = params.ChainID

// Update gov module
var govGenState v1beta1govtypes.GenesisState
var govGenState v1govtypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appState[govtypes.ModuleName], &govGenState)
govGenState.VotingParams.VotingPeriod = params.VotingPeriod
govGenState.Params.VotingPeriod = &params.VotingPeriod
govGenStateBz, err := clientCtx.Codec.MarshalJSON(&govGenState)
if err != nil {
return nil, fmt.Errorf("failed to marshal gov genesis state: %w", err)
Expand Down Expand Up @@ -347,7 +347,7 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
var oldValidator tmtypes.GenesisValidator

// Update tendermint validator data
for i, _ := range genDoc.Validators {
for i := range genDoc.Validators {
if genDoc.Validators[i].Name == params.OldMoniker {
oldValidator = genDoc.Validators[i]
validator := &genDoc.Validators[i]
Expand All @@ -366,7 +366,7 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
var stakingGenState stakingtypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appState[stakingtypes.ModuleName], &stakingGenState)
operatorAddr := ""
for i, _ := range stakingGenState.Validators {
for i := range stakingGenState.Validators {
validator := &stakingGenState.Validators[i]
if validator.Description.Moniker == params.OldMoniker {
valPubKey, err := cryptocodec.FromTmPubKeyInterface(params.TmPubKey)
Expand All @@ -384,11 +384,13 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
validator.OperatorAddress = sdk.ValAddress(params.TmPubKey.Address()).String()
validator.DelegatorShares = validator.DelegatorShares.Add(sdk.NewDec(params.IncreaseCoinAmount))
validator.Tokens = validator.Tokens.Add(sdk.NewInt(params.IncreaseCoinAmount))
} else {
validator.Jailed = true
}
}

// Update total power
for i, _ := range stakingGenState.LastValidatorPowers {
for i := range stakingGenState.LastValidatorPowers {
validatorPower := &stakingGenState.LastValidatorPowers[i]
if validatorPower.Address == operatorAddr {
validatorPower.Power = validatorPower.Power + (params.IncreaseCoinAmount / 1000000)
Expand All @@ -397,7 +399,7 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
stakingGenState.LastTotalPower = stakingGenState.LastTotalPower.Add(sdk.NewInt(params.IncreaseCoinAmount / 1000000))

// Update self delegation on operator address
for i, _ := range stakingGenState.Delegations {
for i := range stakingGenState.Delegations {
delegation := &stakingGenState.Delegations[i]
newAddr, _ := newAccount.GetAddress()
if delegation.DelegatorAddress == newAddr.String() {
Expand All @@ -414,7 +416,7 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
// Update genesis['app_state']['distribution']['delegator_starting_infos'] on operator address
var distrGenState distrtypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appState[distrtypes.ModuleName], &distrGenState)
for i, _ := range distrGenState.DelegatorStartingInfos {
for i := range distrGenState.DelegatorStartingInfos {
delegatorStartingInfo := &distrGenState.DelegatorStartingInfos[i]
newAddr, _ := newAccount.GetAddress()
if delegatorStartingInfo.DelegatorAddress == newAddr.String() {
Expand All @@ -431,14 +433,14 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
var bankGenState banktypes.GenesisState
clientCtx.Codec.MustUnmarshalJSON(appState[banktypes.ModuleName], &bankGenState)

for i, _ := range bankGenState.Balances {
for i := range bankGenState.Balances {
balance := &bankGenState.Balances[i]

// Add 1 BN ubtsg to bonded_tokens_pool module address
bondedPool := authtypes.NewModuleAddress(stakingtypes.BondedPoolName)
if balance.Address == bondedPool.String() {
for balanceIdx, _ := range balance.Coins {
coin := &balance.Coins[balanceIdx]
for balIdx := range balance.Coins {
coin := &balance.Coins[balIdx]
if coin.Denom == stakingGenState.Params.BondDenom {
coin.Amount = coin.Amount.Add(sdk.NewInt(params.IncreaseCoinAmount))
}
Expand All @@ -447,7 +449,7 @@ func ConvertStateExport(clientCtx client.Context, params StateExportParams) (*tm
}

// Update bank balance
for i, _ := range bankGenState.Supply {
for i := range bankGenState.Supply {
supply := &bankGenState.Supply[i]
if supply.Denom == stakingGenState.Params.BondDenom {
supply.Amount = supply.Amount.Add(sdk.NewInt(params.IncreaseCoinAmount))
Expand Down Expand Up @@ -480,7 +482,7 @@ func fetchKey(kb keyring.Keyring, keyref string) (*keyring.Record, error) {
// if the key is not there or if we have a problem with a keyring itself then we move to a
// fallback: searching for key by address.

if err == nil || !sdkerr.IsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) {
if err == nil || !errorsmod.IsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) {
return k, err
}

Expand All @@ -490,5 +492,5 @@ func fetchKey(kb keyring.Keyring, keyref string) (*keyring.Record, error) {
}

k, err = kb.KeyByAddress(accAddr)
return k, sdkerr.Wrap(err, "Invalid key")
return k, errorsmod.Wrapf(err, "Invalid key")
}
Loading

0 comments on commit bc3863e

Please sign in to comment.