Skip to content

Commit

Permalink
fix: update deposit authorization with with unused amount
Browse files Browse the repository at this point in the history
fixes akash-network/support#88

Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Apr 20, 2024
1 parent 279cdbc commit 171b94b
Show file tree
Hide file tree
Showing 28 changed files with 850 additions and 688 deletions.
1 change: 1 addition & 0 deletions app/app_configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (app *AkashApp) setAkashKeepers() {
app.Keepers.Cosmos.Bank,
app.Keepers.Akash.Take,
app.Keepers.Cosmos.Distr,
app.Keepers.Cosmos.Authz,
)

app.Keepers.Akash.Deployment = deployment.NewKeeper(
Expand Down
4 changes: 2 additions & 2 deletions app/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
akeeper "github.com/akash-network/node/x/audit/keeper"
ckeeper "github.com/akash-network/node/x/cert/keeper"
dkeeper "github.com/akash-network/node/x/deployment/keeper"
escrowkeeper "github.com/akash-network/node/x/escrow/keeper"
ekeeper "github.com/akash-network/node/x/escrow/keeper"
agovkeeper "github.com/akash-network/node/x/gov/keeper"
ikeeper "github.com/akash-network/node/x/inflation/keeper"
mkeeper "github.com/akash-network/node/x/market/keeper"
Expand Down Expand Up @@ -60,7 +60,7 @@ type AppKeepers struct {
}

Akash struct {
Escrow escrowkeeper.Keeper
Escrow ekeeper.Keeper
Deployment dkeeper.IKeeper
Take tkeeper.IKeeper
Market mkeeper.IKeeper
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ module github.com/akash-network/node
go 1.21

require (
github.com/akash-network/akash-api v0.0.61
github.com/akash-network/akash-api v0.0.64
github.com/blang/semver/v4 v4.0.0
github.com/boz/go-lifecycle v0.1.1
github.com/cosmos/cosmos-sdk v0.45.16
github.com/cosmos/go-bip39 v1.0.0
github.com/cosmos/ibc-go/v4 v4.4.2
github.com/gogo/protobuf v1.3.3
github.com/google/go-github/v56 v56.0.0
Expand Down Expand Up @@ -95,7 +96,6 @@ require (
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/cosmos-db v0.0.0-20221226095112-f3c38ecb5e32 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.1 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/cosmos/iavl v0.19.5 // indirect
github.com/cosmos/ledger-cosmos-go v0.12.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/akash-network/akash-api v0.0.61 h1:Hj/IBr9cFMsFs4VjymLZCoX/5dNfZSFd4iFwUSqhBtQ=
github.com/akash-network/akash-api v0.0.61/go.mod h1:pNr61L4+0sheol7ZK0HjgK3rxpIAbYBGq1w1oH4B0+M=
github.com/akash-network/akash-api v0.0.64 h1:8WZsjCtR86Of87RNCrlRA2Lnb7+1qG5NYyI9A5xvYmM=
github.com/akash-network/akash-api v0.0.64/go.mod h1:pNr61L4+0sheol7ZK0HjgK3rxpIAbYBGq1w1oH4B0+M=
github.com/akash-network/cometbft v0.34.27-akash h1:V1dApDOr8Ee7BJzYyQ7Z9VBtrAul4+baMeA6C49dje0=
github.com/akash-network/cometbft v0.34.27-akash/go.mod h1:BcCbhKv7ieM0KEddnYXvQZR+pZykTKReJJYf7YC7qhw=
github.com/akash-network/ledger-go v0.14.3 h1:LCEFkTfgGA2xFMN2CtiKvXKE7dh0QSM77PJHCpSkaAo=
Expand Down
2 changes: 1 addition & 1 deletion make/init.mk
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ endif
GOLANGCI_LINT_VERSION ?= v1.51.2
STATIK_VERSION ?= v0.1.7
GIT_CHGLOG_VERSION ?= v0.15.1
MOCKERY_VERSION ?= 2.24.0
MOCKERY_VERSION ?= 2.42.0
COSMOVISOR_VERSION ?= v1.5.0

# ==== Build tools version tracking ====
Expand Down
4 changes: 4 additions & 0 deletions tests/upgrade/upgrade-v0.34.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"migrations": {
}
}
321 changes: 321 additions & 0 deletions tests/upgrade/v0.34.0/postupgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
//go:build e2e.upgrade

// Package v0_34_0
// nolint revive
package v0_34_0

import (
"context"
"testing"
"time"

"github.com/spf13/pflag"
"github.com/stretchr/testify/require"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/go-bip39"

cltypes "github.com/akash-network/akash-api/go/node/client/types"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"

"github.com/akash-network/node/app"
"github.com/akash-network/node/client"
"github.com/akash-network/node/cmd/common"
"github.com/akash-network/node/sdl"
uttypes "github.com/akash-network/node/tests/upgrade/types"
)

const (
mnemonicEntropySize = 256
)

const (
testSDL = `
---
version: "2.0"
services:
web:
image: nginx
expose:
- port: 80
accept:
- ahostname.com
to:
- global: true
- port: 12345
to:
- global: true
proto: udp
profiles:
compute:
web:
resources:
cpu:
units: "100m"
memory:
size: "128Mi"
storage:
size: "1Gi"
placement:
westcoast:
attributes:
region: us-west
signedBy:
anyOf:
- 1
- 2
allOf:
- 3
- 4
pricing:
web:
denom: uakt
amount: 50
deployment:
web:
westcoast:
profile: web
count: 2
`
)

func init() {
uttypes.RegisterPostUpgradeWorker("v0.34.0", &postUpgrade{})
}

type postUpgrade struct{}

var _ uttypes.TestWorker = (*postUpgrade)(nil)

func (pu *postUpgrade) Run(ctx context.Context, t *testing.T, params uttypes.TestParams) {
encodingConfig := app.MakeEncodingConfig()

rpcClient, err := sdkclient.NewClientFromNode(params.Node)
require.NoError(t, err)

cctx := sdkclient.Context{}.
WithCodec(encodingConfig.Marshaler).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
WithAccountRetriever(authtypes.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastBlock).
WithHomeDir(params.Home).
WithChainID(params.ChainID).
WithNodeURI(params.Node).
WithClient(rpcClient).
WithSkipConfirmation(true).
WithFrom(params.From).
WithKeyringDir(params.Home)

kr, err := sdkclient.NewKeyringFromBackend(cctx, params.KeyringBackend)
require.NoError(t, err)

cctx = cctx.WithKeyring(kr)

info, err := kr.Key(params.From)
require.NoError(t, err)

entropySeed, err := bip39.NewEntropy(mnemonicEntropySize)
require.NoError(t, err)

mnemonic, err := bip39.NewMnemonic(entropySeed)
require.NoError(t, err)

keyringAlgos, _ := kr.SupportedAlgorithms()

algo, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), keyringAlgos)

coinType := sdk.GetConfig().GetCoinType()
hdPath := hd.CreateHDPath(coinType, 0, 0).String()

deplAcc, err := kr.NewAccount("depl", mnemonic, "", hdPath, algo)
require.NoError(t, err)

hdPath = hd.CreateHDPath(coinType, 0, 1).String()
authz1, err := kr.NewAccount("authz1", mnemonic, "", hdPath, algo)
require.NoError(t, err)

hdPath = hd.CreateHDPath(coinType, 0, 2).String()
authz2, err := kr.NewAccount("authz2", mnemonic, "", hdPath, algo)
require.NoError(t, err)

mcctx := cctx.WithFromName(info.GetName()).
WithFromAddress(info.GetAddress())

dcctx := cctx.WithFromName(deplAcc.GetName()).
WithFromAddress(deplAcc.GetAddress())

a1cctx := cctx.WithFromName(authz1.GetName()).
WithFromAddress(authz1.GetAddress())

a2cctx := cctx.WithFromName(authz2.GetName()).
WithFromAddress(authz2.GetAddress())

opts := []cltypes.ClientOption{
cltypes.WithGasPrices("0.025uakt"),
cltypes.WithGas(flags.GasSetting{Simulate: true}),
cltypes.WithGasAdjustment(2),
}

mcl, err := client.DiscoverClient(ctx, mcctx, opts...)
require.NoError(t, err)
require.NotNil(t, mcl)

dcl, err := client.DiscoverClient(ctx, dcctx, opts...)
require.NoError(t, err)
require.NotNil(t, mcl)

a1cl, err := client.DiscoverClient(ctx, a1cctx, opts...)
require.NoError(t, err)
require.NotNil(t, mcl)

a2cl, err := client.DiscoverClient(ctx, a2cctx, opts...)
require.NoError(t, err)
require.NotNil(t, mcl)

msgs := []sdk.Msg{
&banktypes.MsgSend{
FromAddress: info.GetAddress().String(),
ToAddress: deplAcc.GetAddress().String(),
Amount: sdk.NewCoins(sdk.NewCoin("uakt", sdk.NewInt(100000000))),
},
&banktypes.MsgSend{
FromAddress: info.GetAddress().String(),
ToAddress: authz1.GetAddress().String(),
Amount: sdk.NewCoins(sdk.NewCoin("uakt", sdk.NewInt(100000000))),
},
&banktypes.MsgSend{
FromAddress: info.GetAddress().String(),
ToAddress: authz2.GetAddress().String(),
Amount: sdk.NewCoins(sdk.NewCoin("uakt", sdk.NewInt(100000000))),
},
}

// fund all new accounts with some tokens
resp, err := mcl.Tx().Broadcast(ctx, msgs)
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp := resp.(*sdk.TxResponse)
require.Equal(t, uint32(0), txResp.Code)

// give deployment key deployment deposit authorization from two accounts
spendLimit := sdk.NewCoin("uakt", sdk.NewInt(10000000))
authorization := dtypes.NewDepositDeploymentAuthorization(spendLimit)

msg, err := authz.NewMsgGrant(a1cctx.FromAddress, deplAcc.GetAddress(), authorization, time.Now().AddDate(1, 0, 0))
require.NoError(t, err)
require.NotNil(t, msg)

resp, err = a1cl.Tx().Broadcast(ctx, []sdk.Msg{msg})
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp = resp.(*sdk.TxResponse)
require.Equal(t, uint32(0), txResp.Code)

msg, err = authz.NewMsgGrant(a2cctx.FromAddress, deplAcc.GetAddress(), authorization, time.Now().AddDate(1, 0, 0))
require.NoError(t, err)
require.NotNil(t, msg)

resp, err = a2cl.Tx().Broadcast(ctx, []sdk.Msg{msg})
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp = resp.(*sdk.TxResponse)
require.Equal(t, uint32(0), txResp.Code)

tsdl, err := sdl.Read([]byte(testSDL))
require.NoError(t, err)
require.NotNil(t, tsdl)

syncInfo, err := dcl.Node().SyncInfo(ctx)
require.NoError(t, err)
require.NotNil(t, syncInfo)
require.False(t, syncInfo.CatchingUp)

dID := dtypes.DeploymentID{
Owner: deplAcc.GetAddress().String(),
DSeq: uint64(syncInfo.LatestBlockHeight),
}

dVersion, err := tsdl.Version()
require.NoError(t, err)

dGroups, err := tsdl.DeploymentGroups()
require.NoError(t, err)

deposit, err := common.DetectDeposit(ctx, &pflag.FlagSet{}, dcl.Query(), "deployment", "MinDeposits")
require.NoError(t, err)

// create deployment with deposit from owner
// should not have any errors
deploymentMsg := &dtypes.MsgCreateDeployment{
ID: dID,
Version: dVersion,
Groups: make([]dtypes.GroupSpec, 0, len(dGroups)),
Deposit: deposit,
Depositor: deplAcc.GetAddress().String(),
}

for _, group := range dGroups {
deploymentMsg.Groups = append(deploymentMsg.Groups, *group)
}

resp, err = dcl.Tx().Broadcast(ctx, []sdk.Msg{deploymentMsg})
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp = resp.(*sdk.TxResponse)
require.Equal(t, uint32(0), txResp.Code)

// should be able to fund escrow with owner as depositor
depositMsg := &dtypes.MsgDepositDeployment{
ID: dID,
Amount: deposit,
Depositor: deplAcc.GetAddress().String(),
}

resp, err = dcl.Tx().Broadcast(ctx, []sdk.Msg{depositMsg})
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp = resp.(*sdk.TxResponse)
require.Equal(t, uint32(0), txResp.Code)

// should be able to fund escrow with depositor from authz1
depositMsg = &dtypes.MsgDepositDeployment{
ID: dID,
Amount: deposit,
Depositor: authz1.GetAddress().String(),
}

resp, err = dcl.Tx().Broadcast(ctx, []sdk.Msg{depositMsg})
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp = resp.(*sdk.TxResponse)
require.Equal(t, uint32(0), txResp.Code)

// should not be able to fund escrow with depositor from authz2
depositMsg = &dtypes.MsgDepositDeployment{
ID: dID,
Amount: deposit,
Depositor: authz2.GetAddress().String(),
}

resp, err = dcl.Tx().Broadcast(ctx, []sdk.Msg{depositMsg})
require.NoError(t, err)
require.NotNil(t, resp)
require.IsType(t, &sdk.TxResponse{}, resp)
txResp = resp.(*sdk.TxResponse)
require.NotEqual(t, uint32(0), txResp.Code)
}
1 change: 1 addition & 0 deletions tests/upgrade/workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package upgrade
import (
_ "github.com/akash-network/node/tests/upgrade/v0.26.0"
_ "github.com/akash-network/node/tests/upgrade/v0.32.0"
_ "github.com/akash-network/node/tests/upgrade/v0.34.0"
)
Loading

0 comments on commit 171b94b

Please sign in to comment.