Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-provision smart wallet #8574

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, error) {
return swingtypes.SmartWalletStateUnspecified, fmt.Errorf("not implemented")
}

func (msk mockSwingsetKeeper) ChargeForSmartWallet(ctx sdk.Context, addr sdk.AccAddress) error {
return fmt.Errorf("not implemented")
}
5 changes: 4 additions & 1 deletion golang/cosmos/ante/vm_admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func (ad AdmissionDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate boo
// Ask the controller if we are rejecting messages.
for _, msg := range tx.GetMsgs() {
if camsg, ok := msg.(vm.ControllerAdmissionMsg); ok {
if err := camsg.CheckAdmissibility(ctx, ad.data); err != nil {
newCtx, err := camsg.CheckAdmissibility(ctx, ad.data)
if err == nil {
ctx = newCtx
} else {
// Only let admission errors interrupt the transaction if we're not
// simulating, otherwise our gas estimation will be too low.
if !simulate {
Expand Down
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)
JimLarson marked this conversation as resolved.
Show resolved Hide resolved
err = m.MigrateParams(ctx)
if err != nil {
return fromVm, err
}

return mvm, nil
}
}
Expand Down
6 changes: 5 additions & 1 deletion golang/cosmos/vm/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ type ControllerContext struct {

type ControllerAdmissionMsg interface {
sdk.Msg
CheckAdmissibility(sdk.Context, interface{}) error
// CheckAdmissibility verifies that the message may be admitted into the
// block or mempool. If no error is returned, it must return a context to use
// while processing the message. If no updated context is necessary it should
// return the input context.
CheckAdmissibility(sdk.Context, interface{}) (sdk.Context, error)
JimLarson marked this conversation as resolved.
Show resolved Hide resolved

// GetInboundMsgCount returns the number of Swingset messages which will
// be added to the inboundQueue.
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
JimLarson marked this conversation as resolved.
Show resolved Hide resolved
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, error) {
JimLarson marked this conversation as resolved.
Show resolved Hide resolved
// 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, nil
}

return types.SmartWalletStateNone, nil
}

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
59 changes: 54 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,48 @@ 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, err := keeper.GetSmartWalletState(ctx, owner)
if err != nil {
return err
} else 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
Loading