Skip to content

Commit

Permalink
feat(vpurse): connect to golang
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed May 1, 2021
1 parent 276a1d3 commit d2f719d
Show file tree
Hide file tree
Showing 16 changed files with 568 additions and 21 deletions.
59 changes: 49 additions & 10 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ import (
gaiaappparams "github.com/Agoric/agoric-sdk/golang/cosmos/app/params"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/dibc"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse"

// unnamed import of statik for swagger UI support
_ "github.com/cosmos/cosmos-sdk/client/docs/statik"
Expand Down Expand Up @@ -123,6 +124,7 @@ var (
ibc.AppModuleBasic{},
swingset.AppModuleBasic{},
dibc.AppModuleBasic{},
vpurse.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
Expand All @@ -138,6 +140,7 @@ var (
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
vpurse.ModuleName: {authtypes.Minter, authtypes.Burner},
}
)

Expand All @@ -155,7 +158,8 @@ type GaiaApp struct { // nolint: golint
appCodec codec.Marshaler
interfaceRegistry types.InterfaceRegistry

ibcPort int
ibcPort int
vpursePort int

invCheckPeriod uint

Expand Down Expand Up @@ -184,6 +188,7 @@ type GaiaApp struct { // nolint: golint

SwingSetKeeper swingset.Keeper
DibcKeeper dibc.Keeper
VpurseKeeper vpurse.Keeper

// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
Expand Down Expand Up @@ -240,7 +245,9 @@ func NewAgoricApp(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey,
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, swingset.StoreKey, dibc.StoreKey, capabilitytypes.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey,
swingset.StoreKey, dibc.StoreKey, vpurse.StoreKey,
capabilitytypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
memKeys := sdk.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down Expand Up @@ -355,6 +362,14 @@ func NewAgoricApp(
ibcRouter.AddRoute(dibc.ModuleName, dibcModule)
app.IBCKeeper.SetRouter(ibcRouter)

app.VpurseKeeper = vpurse.NewKeeper(
appCodec, keys[vpurse.StoreKey],
app.BankKeeper,
callToController,
)
vpurseModule := vpurse.NewAppModule(app.VpurseKeeper)
app.vpursePort = swingset.RegisterPortHandler("bank", vpurse.NewPortHandler(app.VpurseKeeper))

// create evidence keeper with router
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
Expand Down Expand Up @@ -396,6 +411,7 @@ func NewAgoricApp(
params.NewAppModule(app.ParamsKeeper),
swingset.NewAppModule(app.SwingSetKeeper),
dibcModule,
vpurseModule,
transferModule,
)

Expand Down Expand Up @@ -484,10 +500,13 @@ func NewAgoricApp(
}

type cosmosInitAction struct {
Type string `json:"type"`
IBCPort int `json:"ibcPort"`
StoragePort int `json:"storagePort"`
ChainID string `json:"chainID"`
Type string `json:"type"`
IBCPort int `json:"ibcPort"`
StoragePort int `json:"storagePort"`
VPursePort int `json:"vpursePort"`
ChainID string `json:"chainID"`
BootstrapAddress string `json:"bootstrapAddress"`
BootstrapValue string `json:"bootstrapValue"`
}

// MakeCodecs constructs the *std.Codec and *codec.LegacyAmino instances used by
Expand All @@ -507,12 +526,32 @@ func (app *GaiaApp) MustInitController(ctx sdk.Context) {
}
app.controllerInited = true

/*
FIXME: Get this from genesis!
- name: mallory
type: local
address: agoric1z8rldx498tf49p5ze04jhakyumkh7vyxku7e0p
pubkey: agoricpub1addwnpepqtzw4lz7yjhg7qljwf0jqaykj6q94hh9epxa6nq7u0a3hxhsn7u670cgwnz
mnemonic: ""
threshold: 0
pubkeys: []
*/
bootstrapAddr, err := sdk.AccAddressFromBech32("agoric1z8rldx498tf49p5ze04jhakyumkh7vyxku7e0p")
if err != nil {
fmt.Fprintln(os.Stderr, "Cannot get bootstrap addr", err)
os.Exit(1)
}
bootstrapValue := "50000000000"

// Begin initializing the controller here.
action := &cosmosInitAction{
Type: "AG_COSMOS_INIT",
IBCPort: app.ibcPort,
StoragePort: swingset.GetPort("storage"),
ChainID: ctx.ChainID(),
Type: "AG_COSMOS_INIT",
VPursePort: app.vpursePort,
IBCPort: app.ibcPort,
StoragePort: swingset.GetPort("storage"),
ChainID: ctx.ChainID(),
BootstrapAddress: bootstrapAddr.String(),
BootstrapValue: bootstrapValue,
}
bz, err := json.Marshal(action)
if err == nil {
Expand Down
7 changes: 7 additions & 0 deletions golang/cosmos/proto/agoric/vpurse/msgs.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";
package agoric.vpurse;

option go_package = "github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types";

service Msg {
}
22 changes: 22 additions & 0 deletions golang/cosmos/x/vpurse/alias.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package vpurse

import (
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/keeper"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types"
)

const (
ModuleName = types.ModuleName
RouterKey = types.RouterKey
StoreKey = types.StoreKey
)

var (
NewKeeper = keeper.NewKeeper
ModuleCdc = types.ModuleCdc
RegisterCodec = types.RegisterCodec
)

type (
Keeper = keeper.Keeper
)
29 changes: 29 additions & 0 deletions golang/cosmos/x/vpurse/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package vpurse

import (
"fmt"

"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// NewHandler returns a handler for "vpurse" type messages.
func NewHandler(keeper Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
if swingset.IsSimulation(ctx) {
// We don't support simulation.
return &sdk.Result{}, nil
} else {
// The simulation was done, so now allow infinite gas.
ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
}

switch msg := msg.(type) {
default:
errMsg := fmt.Sprintf("Unrecognized vpurse Msg type: %v", msg.Type())
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
}
}
}
54 changes: 54 additions & 0 deletions golang/cosmos/x/vpurse/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types"
)

// Keeper maintains the link to data storage and exposes getter/setter methods for the various parts of the state machine
type Keeper struct {
storeKey sdk.StoreKey
cdc codec.Marshaler

bankKeeper bankkeeper.Keeper

// CallToController dispatches a message to the controlling process
CallToController func(ctx sdk.Context, str string) (string, error)
}

// NewKeeper creates a new vpurse Keeper instance
func NewKeeper(
cdc codec.Marshaler, key sdk.StoreKey,
bankKeeper bankkeeper.Keeper,
callToController func(ctx sdk.Context, str string) (string, error),
) Keeper {

return Keeper{
storeKey: key,
cdc: cdc,
bankKeeper: bankKeeper,
CallToController: callToController,
}
}

func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
return k.bankKeeper.GetBalance(ctx, addr, denom)
}

func (k Keeper) SendCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) error {
if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, amt); err != nil {
return err
}
return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, amt)
}

func (k Keeper) GrabCoins(ctx sdk.Context, addr sdk.AccAddress, amt sdk.Coins) error {
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, amt); err != nil {
return err
}
return k.bankKeeper.BurnCoins(ctx, types.ModuleName, amt)
}
135 changes: 135 additions & 0 deletions golang/cosmos/x/vpurse/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package vpurse

import (
"encoding/json"

"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/Agoric/agoric-sdk/golang/cosmos/x/vpurse/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/module"

sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// type check to ensure the interface is properly implemented
var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
)

// app module Basics object
type AppModuleBasic struct {
cdc codec.Marshaler
}

func (AppModuleBasic) Name() string {
return ModuleName
}

func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
RegisterCodec(cdc)
}

// RegisterInterfaces registers the module's interface types
func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
types.RegisterInterfaces(registry)
}

// DefaultGenesis returns default genesis state as raw bytes for the deployment
func (AppModuleBasic) DefaultGenesis(cdc codec.JSONMarshaler) json.RawMessage {
return nil
}

// Validation check of the Genesis
func (AppModuleBasic) ValidateGenesis(cdc codec.JSONMarshaler, config client.TxEncodingConfig, bz json.RawMessage) error {
return nil
}

// Register rest routes
func (AppModuleBasic) RegisterRESTRoutes(ctx client.Context, rtr *mux.Router) {
}

func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *runtime.ServeMux) {
}

// GetTxCmd implements AppModuleBasic interface
func (AppModuleBasic) GetTxCmd() *cobra.Command {
return nil
}

// GetQueryCmd implements AppModuleBasic interface
func (AppModuleBasic) GetQueryCmd() *cobra.Command {
return nil
}

type AppModule struct {
AppModuleBasic
keeper Keeper
}

// NewAppModule creates a new AppModule Object
func NewAppModule(k Keeper) AppModule {
am := AppModule{
AppModuleBasic: AppModuleBasic{},
keeper: k,
}
return am
}

func (AppModule) Name() string {
return ModuleName
}

// BeginBlock implements the AppModule interface
func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
}

// EndBlock implements the AppModule interface
func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}

// RegisterInvariants implements the AppModule interface
func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
// TODO
}

// Route implements the AppModule interface
func (am AppModule) Route() sdk.Route {
return sdk.NewRoute(RouterKey, NewHandler(am.keeper))
}

// QuerierRoute implements the AppModule interface
func (AppModule) QuerierRoute() string {
return ModuleName
}

// LegacyQuerierHandler implements the AppModule interface
func (am AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier {
return nil
}

// RegisterServices registers module services.
func (am AppModule) RegisterServices(cfg module.Configurator) {
tx := &types.UnimplementedMsgServer{}
types.RegisterMsgServer(cfg.MsgServer(), tx)
}

// InitGenesis performs genesis initialization for the ibc-transfer module. It returns
// no validator updates.
func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONMarshaler, data json.RawMessage) []abci.ValidatorUpdate {
return []abci.ValidatorUpdate{}
}

// ExportGenesis returns the exported genesis state as raw bytes for the ibc-transfer
// module.
func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONMarshaler) json.RawMessage {
return nil
}

0 comments on commit d2f719d

Please sign in to comment.