Skip to content

Commit

Permalink
refactor(cosmos): rely on contextual defaults for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 31, 2023
1 parent 919c02a commit 8faa03a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 146 deletions.
12 changes: 3 additions & 9 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,10 @@ func normalizeModuleAccount(ctx sdk.Context, ak authkeeper.AccountKeeper, name s
}

type cosmosInitAction struct {
Type string `json:"type"`
Type string `json:"type" default:"AG_COSMOS_INIT"`
ChainID string `json:"chainID"`
BlockTime int64 `json:"blockTime,omitempty"`
BlockHeight int64 `json:"blockHeight"`
IsBootstrap bool `json:"isBootstrap"`
Params swingset.Params `json:"params"`
SupplyCoins sdk.Coins `json:"supplyCoins"`
Expand Down Expand Up @@ -925,16 +926,9 @@ func (app *GaiaApp) initController(ctx sdk.Context, bootstrap bool) {
app.CheckControllerInited(false)
app.controllerInited = true

var blockTime int64 = 0
if bootstrap || app.upgradePlan != nil {
blockTime = ctx.BlockTime().Unix()
}

// Begin initializing the controller here.
action := &cosmosInitAction{
Type: "AG_COSMOS_INIT",
ChainID: ctx.ChainID(),
BlockTime: blockTime,
IsBootstrap: bootstrap,
Params: app.SwingSetKeeper.GetParams(ctx),
SupplyCoins: sdk.NewCoins(app.BankKeeper.GetSupply(ctx, "uist")),
Expand All @@ -945,7 +939,7 @@ func (app *GaiaApp) initController(ctx sdk.Context, bootstrap bool) {
VbankPort: app.vbankPort,
VibcPort: app.vibcPort,
}
// This really abuses `BlockingSend` to get back at `sendToController`
// This uses `BlockingSend` as a friendly wrapper for `sendToController`
out, err := app.SwingSetKeeper.BlockingSend(ctx, action)

// fmt.Fprintf(os.Stderr, "AG_COSMOS_INIT Returned from SwingSet: %s, %v\n", out, err)
Expand Down
2 changes: 1 addition & 1 deletion golang/cosmos/vm/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Jsonable interface{}
// ActionPusher enqueues data for later consumption by the controller.
type ActionPusher func(ctx sdk.Context, action Jsonable) error

// WithActionqContext interprets `default:"..."` tags and adds other fields from
// WithActionContext interprets `default:"..."` tags and adds other fields from
// the SDK context to the action.
func WithActionContext(ctx sdk.Context, action Jsonable) Jsonable {
oldRv := reflect.Indirect(reflect.ValueOf(action))
Expand Down
48 changes: 23 additions & 25 deletions golang/cosmos/x/swingset/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ import (
)

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

type endBlockAction struct {
Type string `json:"type"`
Type string `json:"type" default:"END_BLOCK"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}

type commitBlockAction struct {
Type string `json:"type"`
Type string `json:"type" default:"COMMIT_BLOCK"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}

type afterCommitBlockAction struct {
Type string `json:"type" default:"AFTER_COMMIT_BLOCK"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}
Expand All @@ -37,11 +43,8 @@ func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, keeper Keeper) erro
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyBeginBlocker)

action := &beginBlockAction{
Type: "BEGIN_BLOCK",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
ChainID: ctx.ChainID(),
Params: keeper.GetParams(ctx),
ChainID: ctx.ChainID(),
Params: keeper.GetParams(ctx),
}
_, err := keeper.BlockingSend(ctx, action)
// fmt.Fprintf(os.Stderr, "BEGIN_BLOCK Returned from SwingSet: %s, %v\n", out, err)
Expand All @@ -60,11 +63,7 @@ var endBlockTime int64
func EndBlock(ctx sdk.Context, req abci.RequestEndBlock, keeper Keeper) ([]abci.ValidatorUpdate, error) {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

action := &endBlockAction{
Type: "END_BLOCK",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
}
action := &endBlockAction{}
_, err := keeper.BlockingSend(ctx, action)

// fmt.Fprintf(os.Stderr, "END_BLOCK Returned from SwingSet: %s, %v\n", out, err)
Expand All @@ -81,15 +80,18 @@ func EndBlock(ctx sdk.Context, req abci.RequestEndBlock, keeper Keeper) ([]abci.
return []abci.ValidatorUpdate{}, nil
}

func getEndBlockContext() sdk.Context {
return sdk.Context{}.
WithContext(context.Background()).
WithBlockHeight(endBlockHeight).
WithBlockTime(time.Unix(endBlockTime, 0))
}

func CommitBlock(keeper Keeper) error {
defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), "commit_blocker")

action := &commitBlockAction{
Type: "COMMIT_BLOCK",
BlockHeight: endBlockHeight,
BlockTime: endBlockTime,
}
_, err := keeper.BlockingSend(sdk.Context{}.WithContext(context.Background()), action)
action := &commitBlockAction{}
_, err := keeper.BlockingSend(getEndBlockContext(), action)

// fmt.Fprintf(os.Stderr, "COMMIT_BLOCK Returned from SwingSet: %s, %v\n", out, err)
if err != nil {
Expand All @@ -103,12 +105,8 @@ func CommitBlock(keeper Keeper) error {
func AfterCommitBlock(keeper Keeper) error {
// defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), "commit_blocker")

action := &commitBlockAction{
Type: "AFTER_COMMIT_BLOCK",
BlockHeight: endBlockHeight,
BlockTime: endBlockTime,
}
_, err := keeper.BlockingSend(sdk.Context{}.WithContext(context.Background()), action)
action := &afterCommitBlockAction{}
_, err := keeper.BlockingSend(getEndBlockContext(), action)

// fmt.Fprintf(os.Stderr, "AFTER_COMMIT_BLOCK Returned from SwingSet: %s, %v\n", out, err)
if err != nil {
Expand Down
38 changes: 10 additions & 28 deletions golang/cosmos/x/swingset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {
var _ types.MsgServer = msgServer{}

type deliverInboundAction struct {
Type string `json:"type"`
Type string `json:"type" default:"DELIVER_INBOUND"`
Peer string `json:"peer"`
Messages [][]interface{} `json:"messages"`
Ack uint64 `json:"ack"`
Expand Down Expand Up @@ -53,12 +53,9 @@ func (keeper msgServer) DeliverInbound(goCtx context.Context, msg *types.MsgDeli
}

action := &deliverInboundAction{
Type: "DELIVER_INBOUND",
Peer: msg.Submitter.String(),
Messages: messages,
Ack: msg.Ack,
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
Peer: msg.Submitter.String(),
Messages: messages,
Ack: msg.Ack,
}

err := keeper.routeAction(ctx, msg, action)
Expand All @@ -70,7 +67,7 @@ func (keeper msgServer) DeliverInbound(goCtx context.Context, msg *types.MsgDeli
}

type walletAction struct {
Type string `json:"type"` // WALLET_ACTION
Type string `json:"type" default:"WALLET_ACTION"`
Owner string `json:"owner"`
Action string `json:"action"`
BlockHeight int64 `json:"blockHeight"`
Expand All @@ -86,11 +83,8 @@ func (keeper msgServer) WalletAction(goCtx context.Context, msg *types.MsgWallet
}

action := &walletAction{
Type: "WALLET_ACTION",
Owner: msg.Owner.String(),
Action: msg.Action,
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
Owner: msg.Owner.String(),
Action: msg.Action,
}
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)

Expand All @@ -103,7 +97,7 @@ func (keeper msgServer) WalletAction(goCtx context.Context, msg *types.MsgWallet
}

type walletSpendAction struct {
Type string `json:"type"` // WALLET_SPEND_ACTION
Type string `json:"type" default:"WALLET_SPEND_ACTION"`
Owner string `json:"owner"`
SpendAction string `json:"spendAction"`
BlockHeight int64 `json:"blockHeight"`
Expand All @@ -119,11 +113,8 @@ func (keeper msgServer) WalletSpendAction(goCtx context.Context, msg *types.MsgW
}

action := &walletSpendAction{
Type: "WALLET_SPEND_ACTION",
Owner: msg.Owner.String(),
SpendAction: msg.SpendAction,
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
}
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)
err = keeper.routeAction(ctx, msg, action)
Expand All @@ -135,7 +126,7 @@ func (keeper msgServer) WalletSpendAction(goCtx context.Context, msg *types.MsgW

type provisionAction struct {
*types.MsgProvision
Type string `json:"type"` // PLEASE_PROVISION
Type string `json:"type" default:"PLEASE_PROVISION"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
AutoProvision bool `json:"autoProvision"`
Expand All @@ -162,9 +153,6 @@ func (keeper msgServer) provisionIfNeeded(ctx sdk.Context, owner sdk.AccAddress)

action := &provisionAction{
MsgProvision: msg,
Type: "PLEASE_PROVISION",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
AutoProvision: true,
}

Expand All @@ -187,9 +175,6 @@ func (keeper msgServer) Provision(goCtx context.Context, msg *types.MsgProvision

action := &provisionAction{
MsgProvision: msg,
Type: "PLEASE_PROVISION",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
}

// Create the account, if it doesn't already exist.
Expand All @@ -210,7 +195,7 @@ func (keeper msgServer) Provision(goCtx context.Context, msg *types.MsgProvision

type installBundleAction struct {
*types.MsgInstallBundle
Type string `json:"type"` // INSTALL_BUNDLE
Type string `json:"type" default:"INSTALL_BUNDLE"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}
Expand All @@ -224,9 +209,6 @@ func (keeper msgServer) InstallBundle(goCtx context.Context, msg *types.MsgInsta
}
action := &installBundleAction{
MsgInstallBundle: msg,
Type: "INSTALL_BUNDLE",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
}

err = keeper.routeAction(ctx, msg, action)
Expand Down
7 changes: 2 additions & 5 deletions golang/cosmos/x/swingset/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

type coreEvalAction struct {
Type string `json:"type"` // CORE_EVAL
Type string `json:"type" default:"CORE_EVAL"`
Evals []types.CoreEval `json:"evals"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
Expand All @@ -16,10 +16,7 @@ type coreEvalAction struct {
// CoreEvalProposal tells SwingSet to evaluate the given JS code.
func (k Keeper) CoreEvalProposal(ctx sdk.Context, p *types.CoreEvalProposal) error {
action := &coreEvalAction{
Type: "CORE_EVAL",
Evals: p.Evals,
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
Evals: p.Evals,
}

return k.PushHighPriorityAction(ctx, action)
Expand Down
2 changes: 1 addition & 1 deletion golang/cosmos/x/vbank/vbank.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (vbu vbankManyBalanceUpdates) Swap(i int, j int) {
}

type vbankBalanceUpdate struct {
Nonce uint64 `json:"nonce"`
Type string `json:"type"`
Nonce uint64 `json:"nonce"`
Updated vbankManyBalanceUpdates `json:"updated"`
}

Expand Down
8 changes: 2 additions & 6 deletions golang/cosmos/x/vibc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func NewHandler(keeper Keeper, bankKeeper types.BankKeeper) sdk.Handler {

type sendPacketAction struct {
*MsgSendPacket
Type string `json:"type"` // IBC_EVENT
Event string `json:"event"` // sendPacket
Type string `json:"type" default:"IBC_EVENT"`
Event string `json:"event" default:"sendPacket"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}
Expand All @@ -47,10 +47,6 @@ func handleMsgSendPacket(

action := &sendPacketAction{
MsgSendPacket: msg,
Type: "IBC_EVENT",
Event: "sendPacket",
BlockHeight: ctx.BlockHeight(),
BlockTime: ctx.BlockTime().Unix(),
}
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)

Expand Down
Loading

0 comments on commit 8faa03a

Please sign in to comment.