Skip to content

Commit

Permalink
fix: upgrade to latest cosmos-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 7, 2020
1 parent 5595b41 commit 876d2c8
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 140 deletions.
51 changes: 43 additions & 8 deletions packages/cosmic-swingset/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
Expand All @@ -18,6 +22,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -28,6 +33,7 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
"github.com/cosmos/cosmos-sdk/x/bank"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -50,11 +56,11 @@ import (
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/ibc"
transfer "github.com/cosmos/cosmos-sdk/x/ibc-transfer"
ibctransferkeeper "github.com/cosmos/cosmos-sdk/x/ibc-transfer/keeper"
ibctransfertypes "github.com/cosmos/cosmos-sdk/x/ibc-transfer/types"
porttypes "github.com/cosmos/cosmos-sdk/x/ibc/05-port/types"
ibchost "github.com/cosmos/cosmos-sdk/x/ibc/24-host"
transfer "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer"
ibctransferkeeper "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/keeper"
ibctransfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types"
ibckeeper "github.com/cosmos/cosmos-sdk/x/ibc/keeper"
"github.com/cosmos/cosmos-sdk/x/mint"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
Expand All @@ -78,13 +84,16 @@ import (

gaiaappparams "github.com/Agoric/cosmic-swingset/app/params"
"github.com/Agoric/cosmic-swingset/x/swingset"

// This is for the swagger file for legacy support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
)

const appName = "agoric"

var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome = os.ExpandEnv("$HOME/.ag-chain-cosmos")
DefaultNodeHome string

// ModuleBasics defines the module BasicManager is in charge of setting up basic,
// non-dependant module elements, such as codec registration
Expand Down Expand Up @@ -179,6 +188,15 @@ type GaiaApp struct {
sm *module.SimulationManager
}

func init() {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}

DefaultNodeHome = filepath.Join(userHomeDir, ".ag-chain-cosmos")
}

// NewGaia returns a reference to an initialized Gaia.
// NewSimApp returns a reference to an initialized SimApp.
func NewGaiaApp(
Expand Down Expand Up @@ -209,7 +227,7 @@ func NewAgoricApp(
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetAppVersion(version.Version)
bApp.GRPCQueryRouter().SetInterfaceRegistry(interfaceRegistry)
bApp.GRPCQueryRouter().RegisterSimulateService(bApp.Simulate, interfaceRegistry, std.DefaultPublicKeyCodec{})
bApp.GRPCQueryRouter().RegisterSimulateService(bApp.Simulate, interfaceRegistry)

keys := sdk.NewKVStoreKeys(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
Expand Down Expand Up @@ -339,6 +357,7 @@ func NewAgoricApp(
encodingConfig.TxConfig,
),
auth.NewAppModule(appCodec, app.AccountKeeper, nil),
vesting.NewAppModule(app.AccountKeeper, app.BankKeeper),
bank.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
crisis.NewAppModule(&app.CrisisKeeper),
Expand Down Expand Up @@ -371,7 +390,7 @@ func NewAgoricApp(
// so that other modules that want to create or claim capabilities afterwards in InitChain
// can do so safely.
app.mm.SetOrderInitGenesis(
capabilitytypes.ModuleName, authtypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName, banktypes.ModuleName,
capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName, stakingtypes.ModuleName,
slashingtypes.ModuleName, govtypes.ModuleName, minttypes.ModuleName, crisistypes.ModuleName,
ibchost.ModuleName, genutiltypes.ModuleName, evidencetypes.ModuleName, swingset.ModuleName, ibctransfertypes.ModuleName,
)
Expand Down Expand Up @@ -600,12 +619,28 @@ func (app *GaiaApp) SimulationManager() *module.SimulationManager {

// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *GaiaApp) RegisterAPIRoutes(apiSvr *api.Server) {
func (app *GaiaApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
clientCtx := apiSvr.ClientCtx
rpc.RegisterRoutes(clientCtx, apiSvr.Router)
authrest.RegisterTxRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterRESTRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterGRPCRoutes(apiSvr.ClientCtx, apiSvr.GRPCRouter)

// register swagger API from root so that other applications can override easily
if apiConfig.Swagger {
RegisterSwaggerAPI(clientCtx, apiSvr.Router)
}
}

// RegisterSwaggerAPI registers the swagger routes on the API router
func RegisterSwaggerAPI(ctx client.Context, rtr *mux.Router) {
statikFS, err := fs.New()
if err != nil {
panic(err)
}

staticServer := http.FileServer(statikFS)
rtr.PathPrefix("/swagger").Handler(http.StripPrefix("/swagger/", staticServer))
}

// GetMaccPerms returns a copy of the module account permissions
Expand Down
46 changes: 0 additions & 46 deletions packages/cosmic-swingset/app/benchmarks/txsize_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions packages/cosmic-swingset/app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
encodingConfig := params.MakeEncodingConfig()
std.RegisterCodec(encodingConfig.Amino)
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterCodec(encodingConfig.Amino)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)
return encodingConfig
}
36 changes: 25 additions & 11 deletions packages/cosmic-swingset/app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"encoding/json"
"log"

abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtypes "github.com/tendermint/tendermint/types"

servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
Expand All @@ -19,23 +18,32 @@ import (
// file.
func (app *GaiaApp) ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, cp *abci.ConsensusParams, err error) {
) (servertypes.ExportedApp, error) {

// as if they could withdraw from the start of the next block
ctx := app.NewContext(true, tmproto.Header{Height: app.LastBlockHeight()})

// We export at last height + 1, because that's the height at which
// Tendermint will start InitChain.
height := app.LastBlockHeight() + 1
if forZeroHeight {
height = 0
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState := app.mm.ExportGenesis(ctx, app.appCodec)
appState, err = json.MarshalIndent(genState, "", " ")
appState, err := json.MarshalIndent(genState, "", " ")
if err != nil {
return nil, nil, nil, err
return servertypes.ExportedApp{}, err
}

validators = staking.WriteValidators(ctx, app.StakingKeeper)
return appState, validators, app.BaseApp.GetConsensusParams(ctx), nil
validators := staking.WriteValidators(ctx, app.StakingKeeper)
return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
}, nil
}

// prepare for fresh start at zero height
Expand Down Expand Up @@ -66,14 +74,20 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [

// withdraw all validator commission
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val exported.ValidatorI) (stop bool) {
_, _ = app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
_, err := app.DistrKeeper.WithdrawValidatorCommission(ctx, val.GetOperator())
if err != nil {
panic(err)
}
return false
})

// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.DelegatorAddress, delegation.ValidatorAddress)
_, err := app.DistrKeeper.WithdrawDelegationRewards(ctx, delegation.GetDelegatorAddr(), delegation.GetValidatorAddr())
if err != nil {
panic(err)
}
}

// clear validator slash events
Expand All @@ -100,8 +114,8 @@ func (app *GaiaApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs [

// reinitialize all delegations
for _, del := range dels {
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.DelegatorAddress, del.ValidatorAddress)
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.DelegatorAddress, del.ValidatorAddress)
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, del.GetDelegatorAddr(), del.GetValidatorAddr())
}

// reset context height
Expand Down
5 changes: 2 additions & 3 deletions packages/cosmic-swingset/app/params/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ package params
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
)

// MakeEncodingConfig creates an EncodingConfig for an amino based test configuration.
func MakeEncodingConfig() EncodingConfig {
amino := codec.New()
amino := codec.NewLegacyAmino()
interfaceRegistry := types.NewInterfaceRegistry()
marshaler := codec.NewProtoCodec(interfaceRegistry)
txCfg := tx.NewTxConfig(marshaler, std.DefaultPublicKeyCodec{}, tx.DefaultSignModes)
txCfg := tx.NewTxConfig(marshaler, tx.DefaultSignModes)

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Expand Down
2 changes: 1 addition & 1 deletion packages/cosmic-swingset/app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"math/rand"
"time"

"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down
6 changes: 2 additions & 4 deletions packages/cosmic-swingset/app/types.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package app

import (
"encoding/json"

abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/codec"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)
Expand Down Expand Up @@ -36,7 +34,7 @@ type App interface {
// Exports the state of the application for a genesis file.
ExportAppStateAndValidators(
forZeroHeight bool, jailAllowedAddrs []string,
) (json.RawMessage, []tmtypes.GenesisValidator, *abci.ConsensusParams, error)
) (servertypes.ExportedApp, error)

// All the registered module account addreses.
ModuleAccountAddrs() map[string]bool
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmic-swingset/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func CheckExportSimulation(
) error {
if config.ExportStatePath != "" {
fmt.Println("exporting app state...")
appState, _, _, err := app.ExportAppStateAndValidators(false, nil)
exported, err := app.ExportAppStateAndValidators(false, nil)
if err != nil {
return err
}

if err := ioutil.WriteFile(config.ExportStatePath, []byte(appState), 0600); err != nil {
if err := ioutil.WriteFile(config.ExportStatePath, []byte(exported.AppState), 0600); err != nil {
return err
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/cosmic-swingset/cmd/ag-cosmos-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import "C"

import (
"os"
"path/filepath"

"github.com/Agoric/cosmic-swingset/app"
"github.com/Agoric/cosmic-swingset/lib/daemon"
)

func main() {
app.DefaultNodeHome = os.ExpandEnv("$HOME/.ag-cosmos-helper")
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}

app.DefaultNodeHome = filepath.Join(userHomeDir, ".ag-cosmos-helper")
daemon.Run()
}
Loading

0 comments on commit 876d2c8

Please sign in to comment.