Skip to content

Commit

Permalink
feat(cosmic-swingset): add swingset governance params support
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 2, 2021
1 parent 30557b4 commit afec1ad
Show file tree
Hide file tree
Showing 12 changed files with 974 additions and 103 deletions.
3 changes: 2 additions & 1 deletion golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func NewAgoricApp(

// The SwingSetKeeper is the Keeper from the SwingSet module
app.SwingSetKeeper = swingset.NewKeeper(
appCodec, keys[swingset.StoreKey],
appCodec, keys[swingset.StoreKey], app.GetSubspace(swingset.ModuleName),
app.AccountKeeper, app.BankKeeper,
callToController,
)
Expand Down Expand Up @@ -800,6 +800,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(swingset.ModuleName)
paramsKeeper.Subspace(vbank.ModuleName)

return paramsKeeper
Expand Down
22 changes: 13 additions & 9 deletions golang/cosmos/x/swingset/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ import (
)

type beginBlockAction struct {
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
ChainID string `json:"chainID"`
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
ChainID string `json:"chainID"`
Params types.Params `json:"params"`
}

type endBlockAction struct {
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Type string `json:"type"`
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Params types.Params `json:"params"`
}

type commitBlockAction struct {
Expand All @@ -43,6 +45,7 @@ func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, keeper Keeper) erro
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
ChainID: ctx.ChainID(),
Params: keeper.GetParams(ctx),
}
b, err := json.Marshal(action)
if err != nil {
Expand All @@ -65,6 +68,7 @@ func EndBlock(ctx sdk.Context, req abci.RequestEndBlock, keeper Keeper) ([]abci.
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
StoragePort: vm.GetPort("storage"),
Params: keeper.GetParams(ctx),
}
b, err := json.Marshal(action)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions golang/cosmos/x/swingset/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func GetQueryCmd(storeKey string) *cobra.Command {
}
swingsetQueryCmd.AddCommand(
GetCmdGetEgress(storeKey),
GetCmdQueryParams(storeKey),
GetCmdGetStorage(storeKey),
GetCmdGetKeys(storeKey),
GetCmdMailbox(storeKey),
Expand All @@ -29,6 +30,31 @@ func GetQueryCmd(storeKey string) *cobra.Command {
return swingsetQueryCmd
}

func GetCmdQueryParams(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "params",
Args: cobra.NoArgs,
Short: "Query swingset params",
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.Params(cmd.Context(), &types.QueryParamsRequest{})
if err != nil {
return err
}

return clientCtx.PrintProto(&res.Params)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func GetCmdGetEgress(queryRoute string) *cobra.Command {
cmd := &cobra.Command{
Use: "egress [account]",
Expand Down
16 changes: 14 additions & 2 deletions golang/cosmos/x/swingset/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package swingset
import (
// "fmt"
"encoding/json"
"fmt"
stdlog "log"

"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
Expand All @@ -18,12 +19,20 @@ func NewGenesisState() *types.GenesisState {
}

func ValidateGenesis(data *types.GenesisState) error {
if data == nil {
return fmt.Errorf("swingset genesis data cannot be nil")
}
if err := data.Params.ValidateBasic(); err != nil {
return err
}
return nil
}

func DefaultGenesisState() *types.GenesisState {
gs := NewGenesisState()
return gs
return &types.GenesisState{
Params: types.DefaultParams(),
Storage: make(map[string]string),
}
}

type bootstrapBlockAction struct {
Expand All @@ -33,6 +42,8 @@ type bootstrapBlockAction struct {
}

func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) []abci.ValidatorUpdate {
keeper.SetParams(ctx, data.GetParams())

// NONDETERMINISM: order of SetStorage is not deterministic
for key, value := range data.Storage {
keeper.SetStorage(ctx, key, value)
Expand Down Expand Up @@ -64,6 +75,7 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data *types.GenesisState) []abc

func ExportGenesis(ctx sdk.Context, k Keeper) *types.GenesisState {
gs := NewGenesisState()
gs.Params = k.GetParams(ctx)
gs.Storage = k.ExportStorage(ctx)
return gs
}
13 changes: 13 additions & 0 deletions golang/cosmos/x/swingset/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ type Querier struct {

var _ types.QueryServer = Querier{}

func (k Querier) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)

params := k.GetParams(ctx)

return &types.QueryParamsResponse{
Params: params,
}, nil
}

func (k Querier) Storage(c context.Context, req *types.QueryStorageRequest) (*types.QueryStorageResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
Expand Down
23 changes: 20 additions & 3 deletions golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import (
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/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.Codec
storeKey sdk.StoreKey
cdc codec.Codec
paramSpace paramtypes.Subspace

accountKeeper types.AccountKeeper
bankKeeper bankkeeper.Keeper
Expand All @@ -45,20 +47,35 @@ func stringToKey(keyStr string) []byte {

// NewKeeper creates a new IBC transfer Keeper instance
func NewKeeper(
cdc codec.Codec, key sdk.StoreKey,
cdc codec.Codec, key sdk.StoreKey, paramSpace paramtypes.Subspace,
accountKeeper types.AccountKeeper, bankKeeper bankkeeper.Keeper,
callToController func(ctx sdk.Context, str string) (string, error),
) Keeper {

// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

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

func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramSpace.GetParamSet(ctx, &params)
return params
}

func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
return k.bankKeeper.GetBalance(ctx, addr, denom)
}
Expand Down
2 changes: 2 additions & 0 deletions golang/cosmos/x/swingset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type deliverInboundAction struct {
StoragePort int `json:"storagePort"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Params types.Params `json:"params"`
}

func (keeper msgServer) DeliverInbound(goCtx context.Context, msg *types.MsgDeliverInbound) (*types.MsgDeliverInboundResponse, error) {
Expand All @@ -51,6 +52,7 @@ func (keeper msgServer) DeliverInbound(goCtx context.Context, msg *types.MsgDeli
StoragePort: vm.GetPort("storage"),
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
Params: keeper.GetParams(ctx),
}
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)
b, err := json.Marshal(action)
Expand Down
86 changes: 71 additions & 15 deletions golang/cosmos/x/swingset/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit afec1ad

Please sign in to comment.