Skip to content

Commit

Permalink
Merge pull request #8602 from Agoric/mhofman/8558-auto-provision
Browse files Browse the repository at this point in the history
Auto-provision smart wallet
  • Loading branch information
mergify[bot] authored and mhofman committed Dec 6, 2023
2 parents ff38b29 + 3be8224 commit ca259d3
Show file tree
Hide file tree
Showing 16 changed files with 436 additions and 220 deletions.
8 changes: 8 additions & 0 deletions golang/cosmos/ante/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,11 @@ func (msk mockSwingsetKeeper) GetBeansPerUnit(ctx sdk.Context) map[string]sdk.Ui
func (msk mockSwingsetKeeper) ChargeBeans(ctx sdk.Context, addr sdk.AccAddress, beans sdk.Uint) error {
return fmt.Errorf("not implemented")
}

func (msk mockSwingsetKeeper) GetSmartWalletState(ctx sdk.Context, addr sdk.AccAddress) swingtypes.SmartWalletState {
panic(fmt.Errorf("not implemented"))
}

func (msk mockSwingsetKeeper) ChargeForSmartWallet(ctx sdk.Context, addr sdk.AccAddress) error {
return fmt.Errorf("not implemented")
}
6 changes: 6 additions & 0 deletions golang/cosmos/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,12 @@ func upgrade13Handler(app *GaiaApp, targetUpgrade string) func(sdk.Context, upgr
return mvm, err
}

m := swingsetkeeper.NewMigrator(app.SwingSetKeeper)
err = m.MigrateParams(ctx)
if err != nil {
return mvm, err
}

return mvm, nil
}
}
Expand Down
39 changes: 39 additions & 0 deletions golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const (
StoragePathSwingStore = "swingStore"
)

const (
// WalletStoragePathSegment matches the value of WALLET_STORAGE_PATH_SEGMENT
// packages/vats/src/core/startWalletFactory.js
WalletStoragePathSegment = "wallet"
)

const (
stateKey = "state"
swingStoreKeyPrefix = "swingStore."
Expand Down Expand Up @@ -151,6 +157,20 @@ func (k Keeper) IsHighPriorityAddress(ctx sdk.Context, addr sdk.AccAddress) (boo
return k.vstorageKeeper.HasEntry(ctx, path), nil
}

// GetSmartWalletState returns the provision state of the smart wallet for the account address
func (k Keeper) GetSmartWalletState(ctx sdk.Context, addr sdk.AccAddress) types.SmartWalletState {
// walletStoragePath is path of `walletStorageNode` constructed in
// `provideSmartWallet` from packages/smart-wallet/src/walletFactory.js
walletStoragePath := StoragePathCustom + "." + WalletStoragePathSegment + "." + addr.String()

// TODO: implement a pending provision state
if k.vstorageKeeper.HasEntry(ctx, walletStoragePath) {
return types.SmartWalletStateProvisioned
}

return types.SmartWalletStateNone
}

func (k Keeper) InboundQueueLength(ctx sdk.Context) (int32, error) {
size := sdk.NewInt(0)

Expand Down Expand Up @@ -315,6 +335,25 @@ func (k Keeper) ChargeBeans(ctx sdk.Context, addr sdk.AccAddress, beans sdk.Uint
return nil
}

// ChargeForSmartWallet charges the fee for provisioning a smart wallet.
func (k Keeper) ChargeForSmartWallet(ctx sdk.Context, addr sdk.AccAddress) error {
beansPerUnit := k.GetBeansPerUnit(ctx)
beans := beansPerUnit[types.BeansPerSmartWalletProvision]
err := k.ChargeBeans(ctx, addr, beans)
if err != nil {
return err
}

// TODO: mark that a smart wallet provision is pending. However in that case,
// auto-provisioning should still be performed (but without fees being charged),
// until the controller actually provisions the smart wallet (the operation may
// transiently fail, requiring retries until success).
// However the provisioning code is not currently idempotent, and has side
// effects when the smart wallet is already provisioned.

return nil
}

// makeFeeMenu returns a map from power flag to its fee. In the case of duplicates, the
// first one wins.
func makeFeeMenu(powerFlagFees []types.PowerFlagFee) map[string]sdk.Coins {
Expand Down
9 changes: 7 additions & 2 deletions golang/cosmos/x/swingset/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package keeper

import (
v32 "github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/legacy/v32"
"github.com/Agoric/agoric-sdk/golang/cosmos/x/swingset/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand All @@ -17,8 +17,13 @@ func NewMigrator(keeper Keeper) Migrator {

// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return m.MigrateParams(ctx)
}

// MigrateParams migrates params by setting new params to their default value
func (m Migrator) MigrateParams(ctx sdk.Context) error {
params := m.keeper.GetParams(ctx)
newParams, err := v32.UpdateParams(params)
newParams, err := types.UpdateParams(params)
if err != nil {
return err
}
Expand Down
57 changes: 52 additions & 5 deletions golang/cosmos/x/swingset/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ type walletAction struct {
func (keeper msgServer) WalletAction(goCtx context.Context, msg *types.MsgWalletAction) (*types.MsgWalletActionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := keeper.provisionIfNeeded(ctx, msg.Owner)
if err != nil {
return nil, err
}

action := &walletAction{
Type: "WALLET_ACTION",
Owner: msg.Owner.String(),
Expand All @@ -89,7 +94,7 @@ func (keeper msgServer) WalletAction(goCtx context.Context, msg *types.MsgWallet
}
// fmt.Fprintf(os.Stderr, "Context is %+v\n", ctx)

err := keeper.routeAction(ctx, msg, action)
err = keeper.routeAction(ctx, msg, action)
// fmt.Fprintln(os.Stderr, "Returned from SwingSet", out, err)
if err != nil {
return nil, err
Expand All @@ -108,6 +113,11 @@ type walletSpendAction struct {
func (keeper msgServer) WalletSpendAction(goCtx context.Context, msg *types.MsgWalletSpendAction) (*types.MsgWalletSpendActionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := keeper.provisionIfNeeded(ctx, msg.Owner)
if err != nil {
return nil, err
}

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

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

// provisionIfNeeded generates a provision action if no smart wallet is already
// provisioned for the account. This assumes that all messages for
// non-provisioned smart wallets allowed by the admission AnteHandler should
// auto-provision the smart wallet.
func (keeper msgServer) provisionIfNeeded(ctx sdk.Context, owner sdk.AccAddress) error {
// We need to generate a provision action until the smart wallet has
// been fully provisioned by the controller. This is because a provision is
// not guaranteed to succeed (e.g. lack of provision pool funds)
walletState := keeper.GetSmartWalletState(ctx, owner)
if walletState == types.SmartWalletStateProvisioned {
return nil
}

msg := &types.MsgProvision{
Address: owner,
Submitter: owner,
PowerFlags: []string{types.PowerFlagSmartWallet},
}

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

err := keeper.routeAction(ctx, msg, action)
// fmt.Fprintln(os.Stderr, "Returned from SwingSet", out, err)
if err != nil {
return err
}

return nil
}

func (keeper msgServer) Provision(goCtx context.Context, msg *types.MsgProvision) (*types.MsgProvisionResponse, error) {
Expand Down
37 changes: 0 additions & 37 deletions golang/cosmos/x/swingset/legacy/v32/params.go

This file was deleted.

133 changes: 0 additions & 133 deletions golang/cosmos/x/swingset/legacy/v32/params_test.go

This file was deleted.

Loading

0 comments on commit ca259d3

Please sign in to comment.