Skip to content

Commit

Permalink
feat: import wasm with Stargate (#642)
Browse files Browse the repository at this point in the history
* Package update

* Add stargate in import module

* Refactor files

* wasm app.go scaffolding

* Launchpad create module

* Root.go

* Create and import module reorganization

* Placeholder refacto

* Stargate import wasm template

* Add stargate wasm integration test

* Replace placeholder file name

* Lint

* Some renaming

* Fix wasm version issue

* Template new fixes

* Integration test

* Integration test

* Fix string error

* Support only 0.40.0

* Fix build error
  • Loading branch information
lumtis authored Jan 15, 2021
1 parent 7a9dfe7 commit 595c842
Show file tree
Hide file tree
Showing 57 changed files with 678 additions and 328 deletions.
6 changes: 3 additions & 3 deletions integration/cmd_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ func TestGenerateAnAppAndVerify(t *testing.T) {
env.EnsureAppIsSteady(path)
}

func TestGenerateAnAppWithCosmWasmAndVerify(t *testing.T) {
func TestGenerateAnAppWithWasmAndVerify(t *testing.T) {
t.Parallel()

var (
env = newEnv(t)
path = env.Scaffold("blog", Launchpad)
)

env.Must(env.Exec("add CosmWasm module",
env.Must(env.Exec("add Wasm module",
step.NewSteps(step.New(
step.Exec("starport", "module", "import", "wasm"),
step.Workdir(path),
)),
))

env.Must(env.Exec("should not add CosmWasm module second time",
env.Must(env.Exec("should not add Wasm module second time",
step.NewSteps(step.New(
step.Exec("starport", "module", "import", "wasm"),
step.Workdir(path),
Expand Down
19 changes: 13 additions & 6 deletions integration/cmd_serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
)

func TestServeLaunchpadAppWithCosmWasm(t *testing.T) {
func TestServeLaunchpadAppWithWasm(t *testing.T) {
t.Parallel()

var (
Expand All @@ -19,7 +19,7 @@ func TestServeLaunchpadAppWithCosmWasm(t *testing.T) {
servers = env.RandomizeServerPorts(apath)
)

env.Must(env.Exec("add CosmWasm module",
env.Must(env.Exec("add Wasm module",
step.NewSteps(step.New(
step.Exec("starport", "module", "import", "wasm"),
step.Workdir(apath),
Expand All @@ -34,7 +34,7 @@ func TestServeLaunchpadAppWithCosmWasm(t *testing.T) {
defer cancel()
isBackendAliveErr = env.IsAppServed(ctx, servers)
}()
env.Must(env.Serve("should serve with CosmWasm", apath, "", "", ExecCtx(ctx)))
env.Must(env.Serve("should serve with Wasm", apath, "", "", ExecCtx(ctx)))

require.NoError(t, isBackendAliveErr, "app cannot get online in time")
}
Expand All @@ -56,7 +56,7 @@ func TestServeLaunchpadAppWithCustomHomes(t *testing.T) {
defer cancel()
isBackendAliveErr = env.IsAppServed(ctx, servers)
}()
env.Must(env.Serve("should serve with CosmWasm", apath, "./home", "./clihome", ExecCtx(ctx)))
env.Must(env.Serve("should serve with Wasm", apath, "./home", "./clihome", ExecCtx(ctx)))

require.NoError(t, isBackendAliveErr, "app cannot get online in time")
}
Expand All @@ -81,12 +81,12 @@ func TestServeLaunchpadAppWithConfigHomes(t *testing.T) {
defer cancel()
isBackendAliveErr = env.IsAppServed(ctx, servers)
}()
env.Must(env.Serve("should serve with CosmWasm", apath, "", "", ExecCtx(ctx)))
env.Must(env.Serve("should serve with Wasm", apath, "", "", ExecCtx(ctx)))

require.NoError(t, isBackendAliveErr, "app cannot get online in time")
}

func TestServeStargate(t *testing.T) {
func TestServeStargateWithWasm(t *testing.T) {
t.Parallel()

var (
Expand All @@ -95,6 +95,13 @@ func TestServeStargate(t *testing.T) {
servers = env.RandomizeServerPorts(apath)
)

env.Must(env.Exec("add Wasm module",
step.NewSteps(step.New(
step.Exec("starport", "module", "import", "wasm"),
step.Workdir(apath),
)),
))

var (
ctx, cancel = context.WithTimeout(env.Ctx(), serveTimeout)
isBackendAliveErr error
Expand Down
102 changes: 69 additions & 33 deletions starport/services/scaffolder/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"context"
"errors"
"fmt"

module_create "github.com/tendermint/starport/starport/templates/module/create"
module_import "github.com/tendermint/starport/starport/templates/module/import"

"go/parser"
"go/token"
"os"
Expand All @@ -15,14 +19,14 @@ import (
"github.com/tendermint/starport/starport/pkg/cmdrunner/step"
"github.com/tendermint/starport/starport/pkg/cosmosver"
"github.com/tendermint/starport/starport/pkg/gomodulepath"
"github.com/tendermint/starport/starport/templates/module"
)

const (
wasmImport = "github.com/CosmWasm/wasmd"
apppkg = "app"
moduleDir = "x"
wasmVersionCommit = "b30902fe1fbe5237763775950f729b90bf34d53f"
wasmImport = "github.com/CosmWasm/wasmd"
apppkg = "app"
moduleDir = "x"
wasmVersionCommitLaunchpad = "b30902fe1fbe5237763775950f729b90bf34d53f"
wasmVersionCommitStargate = "f9015cba4793d03cf7a77d7253375b16ad3d3eef"
)

// CreateModule creates a new empty module in the scaffolded app
Expand All @@ -31,6 +35,7 @@ func (s *Scaffolder) CreateModule(moduleName string) error {
if err != nil {
return err
}
majorVersion := version.Major()
// Check if the module already exist
ok, err := ModuleExists(s.path, moduleName)
if err != nil {
Expand All @@ -46,18 +51,19 @@ func (s *Scaffolder) CreateModule(moduleName string) error {

var (
g *genny.Generator
opts = &module.CreateOptions{
opts = &module_create.CreateOptions{
ModuleName: moduleName,
ModulePath: path.RawPath,
AppName: path.Package,
OwnerName: owner(path.RawPath),
}
)
if version == cosmosver.Launchpad {
g, err = module.NewCreateLaunchpad(opts)
if majorVersion == cosmosver.Launchpad {
g, err = module_create.NewCreateLaunchpad(opts)
} else {
g, err = module.NewCreateStargate(opts)
g, err = module_create.NewCreateStargate(opts)
}

if err != nil {
return err
}
Expand All @@ -70,7 +76,8 @@ func (s *Scaffolder) CreateModule(moduleName string) error {
if err != nil {
return err
}
if err := s.protoc(pwd, version); err != nil {

if err := s.protoc(pwd, majorVersion); err != nil {
return err
}
return fmtProject(pwd)
Expand All @@ -82,19 +89,17 @@ func (s *Scaffolder) ImportModule(name string) error {
if err != nil {
return err
}
if version == cosmosver.Stargate {
return errors.New("importing modules currently is not supported on Stargate")
}
majorVersion := version.Major()
ok, err := isWasmImported(s.path)
if err != nil {
return err
}
if ok {
return errors.New("CosmWasm is already imported")
return errors.New("wasm is already imported")
}

// Import a specific version of ComsWasm
err = installWasm()
// import a specific version of ComsWasm
err = installWasm(version)
if err != nil {
return err
}
Expand All @@ -103,10 +108,22 @@ func (s *Scaffolder) ImportModule(name string) error {
if err != nil {
return err
}
g, err := module.NewImport(&module.ImportOptions{
Feature: name,
AppName: path.Package,
})

// run generator
var g *genny.Generator
if majorVersion == cosmosver.Launchpad {
g, err = module_import.NewImportLaunchpad(&module_import.ImportOptions{
Feature: name,
AppName: path.Package,
})
} else {
g, err = module_import.NewImportStargate(&module_import.ImportOptions{
Feature: name,
AppName: path.Package,
BinaryNamePrefix: path.Root,
})
}

if err != nil {
return err
}
Expand Down Expand Up @@ -162,18 +179,37 @@ func isWasmImported(appPath string) (bool, error) {
return false, nil
}

func installWasm() error {
return cmdrunner.
New(
cmdrunner.DefaultStderr(os.Stderr),
).
Run(context.Background(),
step.New(
step.Exec(
"go",
"get",
wasmImport+"@"+wasmVersionCommit,
func installWasm(version cosmosver.Version) error {
switch version {
case cosmosver.LaunchpadAny:
return cmdrunner.
New(
cmdrunner.DefaultStderr(os.Stderr),
).
Run(context.Background(),
step.New(
step.Exec(
"go",
"get",
wasmImport+"@"+wasmVersionCommitLaunchpad,
),
),
),
)
)
case cosmosver.StargateZeroFourtyAndAbove:
return cmdrunner.
New(
cmdrunner.DefaultStderr(os.Stderr),
).
Run(context.Background(),
step.New(
step.Exec(
"go",
"get",
wasmImport+"@"+wasmVersionCommitStargate,
),
),
)
default:
return errors.New("version not supported")
}
}
6 changes: 3 additions & 3 deletions starport/services/scaffolder/scaffolder.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func New(path string, options ...Option) *Scaffolder {
}
}

func (s *Scaffolder) version() (cosmosver.MajorVersion, error) {
func (s *Scaffolder) version() (cosmosver.Version, error) {
v, err := cosmosver.Detect(s.path)
if err != nil {
return "", err
return 0, err
}
return v.Major(), nil
return v, nil
}

func owner(modulePath string) string {
Expand Down
5 changes: 3 additions & 2 deletions starport/services/scaffolder/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (s *Scaffolder) AddType(moduleName string, stype string, fields ...string)
if err != nil {
return err
}
majorVersion := version.Major()
path, err := gomodulepath.ParseAt(s.path)
if err != nil {
return err
Expand Down Expand Up @@ -110,7 +111,7 @@ func (s *Scaffolder) AddType(moduleName string, stype string, fields ...string)
Fields: tfields,
}
)
if version == cosmosver.Launchpad {
if majorVersion == cosmosver.Launchpad {
g, err = typed.NewLaunchpad(opts)
} else {
g, err = typed.NewStargate(opts)
Expand All @@ -127,7 +128,7 @@ func (s *Scaffolder) AddType(moduleName string, stype string, fields ...string)
if err != nil {
return err
}
if err := s.protoc(pwd, version); err != nil {
if err := s.protoc(pwd, majorVersion); err != nil {
return err
}
return fmtProject(pwd)
Expand Down
33 changes: 25 additions & 8 deletions starport/templates/app/stargate/app/app.go.plush
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
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"
Expand Down Expand Up @@ -89,6 +90,23 @@ import (

const Name = "<%= AppName %>"

// this line is used by starport scaffolding # stargate/wasm/app/enabledProposals

func getGovProposalHandlers() []govclient.ProposalHandler {
var govProposalHandlers []govclient.ProposalHandler
// this line is used by starport scaffolding # stargate/app/govProposalHandlers

govProposalHandlers = append(govProposalHandlers,
paramsclient.ProposalHandler,
distrclient.ProposalHandler,
upgradeclient.ProposalHandler,
upgradeclient.CancelProposalHandler,
// this line is used by starport scaffolding # stargate/app/govProposalHandler
)

return govProposalHandlers
}

var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string
Expand All @@ -104,9 +122,7 @@ var (
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(
paramsclient.ProposalHandler, distrclient.ProposalHandler, upgradeclient.ProposalHandler, upgradeclient.CancelProposalHandler,
),
gov.NewAppModuleBasic(getGovProposalHandlers()...),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
Expand Down Expand Up @@ -199,6 +215,7 @@ type App struct {
func New(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool, skipUpgradeHeights map[int64]bool,
homePath string, invCheckPeriod uint, encodingConfig appparams.EncodingConfig,
// this line is used by starport scaffolding # stargate/app/newArgument
appOpts servertypes.AppOptions, baseAppOptions ...func(*baseapp.BaseApp),
) *App {

Expand Down Expand Up @@ -284,18 +301,13 @@ func New(
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), app.StakingKeeper, scopedIBCKeeper,
)


// register the proposal types
govRouter := govtypes.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
AddRoute(paramproposal.RouterKey, params.NewParamChangeProposalHandler(app.ParamsKeeper)).
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
AddRoute(ibchost.RouterKey, ibcclient.NewClientUpdateProposalHandler(app.IBCKeeper.ClientKeeper))
app.GovKeeper = govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
)

// Create Transfer Keepers
app.TransferKeeper = ibctransferkeeper.NewKeeper(
Expand Down Expand Up @@ -323,6 +335,11 @@ func New(

// this line is used by starport scaffolding # stargate/app/keeperDefinition

app.GovKeeper = govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
&stakingKeeper, govRouter,
)

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down
Loading

0 comments on commit 595c842

Please sign in to comment.