Skip to content

Commit

Permalink
refactor(cosmos): rely on contextual defaults for VM actions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig authored and mhofman committed Jan 30, 2024
1 parent d32e71e commit c9d8fc8
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 221 deletions.
36 changes: 16 additions & 20 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,18 +865,17 @@ func normalizeModuleAccount(ctx sdk.Context, ak authkeeper.AccountKeeper, name s
}

type cosmosInitAction struct {
Type string `json:"type"`
ChainID string `json:"chainID"`
BlockTime int64 `json:"blockTime,omitempty"`
IsBootstrap bool `json:"isBootstrap"`
Params swingset.Params `json:"params"`
SupplyCoins sdk.Coins `json:"supplyCoins"`
UpgradePlan *upgradetypes.Plan `json:"upgradePlan,omitempty"`
LienPort int `json:"lienPort"`
StoragePort int `json:"storagePort"`
SwingsetPort int `json:"swingsetPort"`
VbankPort int `json:"vbankPort"`
VibcPort int `json:"vibcPort"`
vm.ActionHeader `actionType:"AG_COSMOS_INIT"`
ChainID string `json:"chainID"`
IsBootstrap bool `json:"isBootstrap"`
Params swingset.Params `json:"params"`
SupplyCoins sdk.Coins `json:"supplyCoins"`
UpgradePlan *upgradetypes.Plan `json:"upgradePlan,omitempty"`
LienPort int `json:"lienPort"`
StoragePort int `json:"storagePort"`
SwingsetPort int `json:"swingsetPort"`
VbankPort int `json:"vbankPort"`
VibcPort int `json:"vibcPort"`
}

// Name returns the name of the App
Expand All @@ -899,16 +898,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 @@ -919,7 +911,11 @@ 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`
//
// CAVEAT: we are restarting after an in-consensus halt or just because this
// node felt like it. The controller must be able to handle either case
// (inConsensus := action.IsBootstrap || action.UpgradePlan != nil).
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/x/lien/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func makeTestKit() testKit {
sk := stakingkeeper.NewKeeper(cdc, stakingStoreKey, wak, bk, stakingSpace)

// lien keeper
pushAction := func(sdk.Context, vm.Jsonable) error {
pushAction := func(sdk.Context, vm.Action) error {
return nil
}
keeper := NewKeeper(cdc, lienStoreKey, wak, bk, sk, pushAction)
Expand Down
58 changes: 25 additions & 33 deletions golang/cosmos/x/swingset/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,42 @@ package swingset

import (
// "os"
"context"
"fmt"
"time"

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

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

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

type endBlockAction struct {
Type string `json:"type"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
vm.ActionHeader `actionType:"END_BLOCK"`
}

type commitBlockAction struct {
Type string `json:"type"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
vm.ActionHeader `actionType:"COMMIT_BLOCK"`
}

type afterCommitBlockAction struct {
vm.ActionHeader `actionType:"AFTER_COMMIT_BLOCK"`
}

func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, keeper Keeper) error {
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 @@ -59,11 +56,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 @@ -80,15 +73,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{}, 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 @@ -102,12 +98,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{}, 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
66 changes: 19 additions & 47 deletions golang/cosmos/x/swingset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@ func NewMsgServerImpl(keeper Keeper) types.MsgServer {
var _ types.MsgServer = msgServer{}

type deliverInboundAction struct {
Type string `json:"type"`
Peer string `json:"peer"`
Messages [][]interface{} `json:"messages"`
Ack uint64 `json:"ack"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
vm.ActionHeader `actionType:"DELIVER_INBOUND"`
Peer string `json:"peer"`
Messages [][]interface{} `json:"messages"`
Ack uint64 `json:"ack"`
}

func (keeper msgServer) routeAction(ctx sdk.Context, msg vm.ControllerAdmissionMsg, action vm.Jsonable) error {
func (keeper msgServer) routeAction(ctx sdk.Context, msg vm.ControllerAdmissionMsg, action vm.Action) error {
isHighPriority, err := msg.IsHighPriority(ctx, keeper)
if err != nil {
return err
Expand All @@ -53,12 +51,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,11 +65,9 @@ func (keeper msgServer) DeliverInbound(goCtx context.Context, msg *types.MsgDeli
}

type walletAction struct {
Type string `json:"type"` // WALLET_ACTION
Owner string `json:"owner"`
Action string `json:"action"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
vm.ActionHeader `actionType:"WALLET_ACTION"`
Owner string `json:"owner"`
Action string `json:"action"`
}

func (keeper msgServer) WalletAction(goCtx context.Context, msg *types.MsgWalletAction) (*types.MsgWalletActionResponse, error) {
Expand All @@ -86,11 +79,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,11 +93,9 @@ func (keeper msgServer) WalletAction(goCtx context.Context, msg *types.MsgWallet
}

type walletSpendAction struct {
Type string `json:"type"` // WALLET_SPEND_ACTION
Owner string `json:"owner"`
SpendAction string `json:"spendAction"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
vm.ActionHeader `actionType:"WALLET_SPEND_ACTION"`
Owner string `json:"owner"`
SpendAction string `json:"spendAction"`
}

func (keeper msgServer) WalletSpendAction(goCtx context.Context, msg *types.MsgWalletSpendAction) (*types.MsgWalletSpendActionResponse, error) {
Expand All @@ -119,11 +107,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 @@ -134,11 +119,9 @@ func (keeper msgServer) WalletSpendAction(goCtx context.Context, msg *types.MsgW
}

type provisionAction struct {
vm.ActionHeader `actionType:"PLEASE_PROVISION"`
*types.MsgProvision
Type string `json:"type"` // PLEASE_PROVISION
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
AutoProvision bool `json:"autoProvision"`
AutoProvision bool `json:"autoProvision"`
}

// provisionIfNeeded generates a provision action if no smart wallet is already
Expand All @@ -162,9 +145,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 +167,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 @@ -209,10 +186,8 @@ func (keeper msgServer) Provision(goCtx context.Context, msg *types.MsgProvision
}

type installBundleAction struct {
vm.ActionHeader `actionType:"INSTALL_BUNDLE"`
*types.MsgInstallBundle
Type string `json:"type"` // INSTALL_BUNDLE
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
}

func (keeper msgServer) InstallBundle(goCtx context.Context, msg *types.MsgInstallBundle) (*types.MsgInstallBundleResponse, error) {
Expand All @@ -224,9 +199,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
12 changes: 4 additions & 8 deletions golang/cosmos/x/swingset/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

type coreEvalAction struct {
Type string `json:"type"` // CORE_EVAL
Evals []types.CoreEval `json:"evals"`
BlockHeight int64 `json:"blockHeight"`
BlockTime int64 `json:"blockTime"`
vm.ActionHeader `actionType:"CORE_EVAL"`
Evals []types.CoreEval `json:"evals"`
}

// 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
Loading

0 comments on commit c9d8fc8

Please sign in to comment.