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 committed Jan 6, 2024
1 parent a710d68 commit 021fd0f
Show file tree
Hide file tree
Showing 9 changed files with 124 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 @@ -866,18 +866,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 @@ -900,16 +899,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 @@ -920,7 +912,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
57 changes: 24 additions & 33 deletions golang/cosmos/x/swingset/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,34 @@ import (
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 @@ -60,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 @@ -81,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{}.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 +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{}.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
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 @@ -51,12 +49,9 @@ func (keeper msgServer) DeliverInbound(goCtx context.Context, msg *types.MsgDeli
messages[i] = []interface{}{msg.Nums[i], message}
}
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 @@ -68,11 +63,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 @@ -84,11 +77,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 @@ -101,11 +91,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 @@ -117,11 +105,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 @@ -132,11 +117,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 @@ -160,9 +143,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 @@ -185,9 +165,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 @@ -207,10 +184,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 @@ -222,9 +197,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
15 changes: 8 additions & 7 deletions golang/cosmos/x/vbank/vbank.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,22 @@ func (vbu vbankManyBalanceUpdates) Swap(i int, j int) {
}

type vbankBalanceUpdate struct {
Nonce uint64 `json:"nonce"`
Type string `json:"type"`
Updated vbankManyBalanceUpdates `json:"updated"`
vm.ActionHeader `actionType:"VBANK_BALANCE_UPDATE"`
Nonce uint64 `json:"nonce"`
Updated vbankManyBalanceUpdates `json:"updated"`
}

// getBalanceUpdate returns a bridge message containing the current bank balance
// for the given addresses each for the specified denominations. Coins are used
// only to track the set of denoms, not for the particular nonzero amounts.
func getBalanceUpdate(ctx sdk.Context, keeper Keeper, addressToUpdate map[string]sdk.Coins) vm.Jsonable {
func getBalanceUpdate(ctx sdk.Context, keeper Keeper, addressToUpdate map[string]sdk.Coins) vm.Action {
nentries := len(addressToUpdate)
if nentries == 0 {
return nil
}

nonce := keeper.GetNextSequence(ctx)
event := vbankBalanceUpdate{
Type: "VBANK_BALANCE_UPDATE",
Nonce: nonce,
Updated: make([]vbankSingleBalanceUpdate, 0, nentries),
}
Expand All @@ -111,7 +110,9 @@ func getBalanceUpdate(ctx sdk.Context, keeper Keeper, addressToUpdate map[string

// Ensure we have a deterministic order of updates.
sort.Sort(event.Updated)
return event

// Populate the event default fields (even though event does not embed vm.ActionHeader)
return vm.PopulateAction(ctx, event)
}

func marshal(event vm.Jsonable) ([]byte, error) {
Expand Down Expand Up @@ -243,7 +244,7 @@ func (ch portHandler) Receive(cctx context.Context, str string) (ret string, err
return
}

func (am AppModule) PushAction(ctx sdk.Context, action vm.Jsonable) error {
func (am AppModule) PushAction(ctx sdk.Context, action vm.Action) error {
// vbank actions are not triggered by a swingset message in a transaction, so we need to
// synthesize unique context information.
// We use a fixed placeholder value for the txHash context, and can simply use `0` for the
Expand Down
Loading

0 comments on commit 021fd0f

Please sign in to comment.