Skip to content

Commit

Permalink
query: client
Browse files Browse the repository at this point in the history
move object query client from cmd/akash/query
  • Loading branch information
boz committed May 22, 2018
1 parent 0ddc681 commit 95d5a32
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 159 deletions.
6 changes: 3 additions & 3 deletions app/account/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/gogo/protobuf/proto"
apptypes "github.com/ovrclk/akash/app/types"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
"github.com/ovrclk/akash/types/code"
tmtypes "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
Expand Down Expand Up @@ -40,15 +40,15 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
}
}
id := strings.TrimPrefix(req.Path, state.AccountPath)
key, err := base.DecodeString(id)
key, err := keys.ParseAccountPath(id)
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Log: err.Error(),
}
}

acct, err := a.State().Account().Get(key)
acct, err := a.State().Account().Get(key.ID())
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Expand Down
5 changes: 3 additions & 2 deletions app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/gogo/protobuf/proto"
apptypes "github.com/ovrclk/akash/app/types"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
Expand Down Expand Up @@ -47,14 +48,14 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
return a.doRangeQuery()
}

key, err := base.DecodeString(id)
key, err := keys.ParseProviderPath(id)
if err != nil {
return tmtypes.ResponseQuery{
Code: code.ERROR,
Log: err.Error(),
}
}
return a.doQuery(key)
return a.doQuery(key.ID())
}

func (a *app) AcceptTx(ctx apptypes.Context, tx interface{}) bool {
Expand Down
13 changes: 13 additions & 0 deletions cmd/akash/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (

"github.com/ovrclk/akash/cmd/akash/constants"
"github.com/ovrclk/akash/cmd/common"
"github.com/ovrclk/akash/query"
"github.com/ovrclk/akash/txutil"

gctx "context"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-crypto/keys"
Expand All @@ -25,12 +28,14 @@ type Context interface {
Node() string
Client() *tmclient.HTTP
TxClient() (txutil.Client, error)
QueryClient() query.Client
KeyName() string
KeyType() (keys.CryptoAlgo, error)
Key() (keys.Info, error)
Nonce() (uint64, error)
Log() log.Logger
Signer() (txutil.Signer, keys.Info, error)
Ctx() gctx.Context
Wait() bool
}

Expand Down Expand Up @@ -160,6 +165,10 @@ func (ctx *context) TxClient() (txutil.Client, error) {
return txutil.NewClient(ctx.Client(), signer, key, nonce), nil
}

func (ctx *context) QueryClient() query.Client {
return query.NewClient(ctx.Client())
}

func (ctx *context) KeyName() string {
val, _ := ctx.cmd.Flags().GetString(constants.FlagKey)
return val
Expand Down Expand Up @@ -226,6 +235,10 @@ func (ctx *context) Wait() bool {
return val
}

func (ctx *context) Ctx() gctx.Context {
return gctx.Background()
}

func loadKeyManager(root string) (keys.Keybase, tmdb.DB, error) {
codec, err := words.LoadCodec(constants.Codec)
if err != nil {
Expand Down
14 changes: 6 additions & 8 deletions cmd/akash/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import (
"os"

"github.com/ovrclk/akash/cmd/akash/context"
"github.com/ovrclk/akash/cmd/akash/query"
"github.com/ovrclk/akash/cmd/common"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/manifest"
"github.com/ovrclk/akash/marketplace"
"github.com/ovrclk/akash/sdl"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
. "github.com/ovrclk/akash/util"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -110,7 +108,7 @@ func createDeployment(ctx context.Context, cmd *cobra.Command, args []string) er
fmt.Printf("Group %v/%v Lease: %v\n", tx.Group, len(groups),
keys.LeaseID(tx.LeaseID).Path())
// get lease provider
prov, err := query.Provider(ctx, tx.Provider)
prov, err := ctx.QueryClient().Provider(ctx.Ctx(), tx.Provider)
if err != nil {
fmt.Printf("ERROR: %v", err)
}
Expand Down Expand Up @@ -156,13 +154,13 @@ func closeDeployment(ctx context.Context, cmd *cobra.Command, args []string) err
return err
}

deployment, err := base.DecodeString(args[0])
deployment, err := keys.ParseDeploymentPath(args[0])
if err != nil {
return err
}

_, err = txclient.BroadcastTxCommit(&types.TxCloseDeployment{
Deployment: deployment,
Deployment: deployment.ID(),
Reason: types.TxCloseDeployment_TENANT_CLOSE,
})

Expand Down Expand Up @@ -207,17 +205,17 @@ func sendManifest(ctx context.Context, cmd *cobra.Command, args []string) error
return err
}

leaseAddr, err := base.DecodeString(args[1])
leaseAddr, err := keys.ParseLeasePath(args[1])
if err != nil {
return err
}

lease, err := query.Lease(ctx, leaseAddr)
lease, err := ctx.QueryClient().Lease(ctx.Ctx(), leaseAddr.ID())
if err != nil {
return err
}

provider, err := query.Provider(ctx, lease.Provider)
provider, err := ctx.QueryClient().Provider(ctx.Ctx(), lease.Provider)
if err != nil {
return err
}
Expand Down
14 changes: 4 additions & 10 deletions cmd/akash/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ import (

"github.com/ovrclk/akash/cmd/akash/constants"
"github.com/ovrclk/akash/cmd/akash/context"
"github.com/ovrclk/akash/cmd/akash/query"
"github.com/ovrclk/akash/cmd/common"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/manifest"
"github.com/ovrclk/akash/marketplace"
qp "github.com/ovrclk/akash/query"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
"github.com/ovrclk/akash/types/provider"
. "github.com/ovrclk/akash/util"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -132,7 +129,7 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
return err
}

provider, err := base.DecodeString(args[0])
key, err := keys.ParseProviderPath(args[0])
if err != nil {
return err
}
Expand All @@ -156,7 +153,7 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
Deployment: tx.Deployment,
Group: tx.Group,
Order: tx.Seq,
Provider: provider,
Provider: key.ID(),
},
Price: price,
}
Expand All @@ -176,7 +173,7 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
}).
OnTxCreateLease(func(tx *types.TxCreateLease) {
leaseProvider, _ := tx.Provider.Marshal()
if bytes.Equal(leaseProvider, provider) {
if bytes.Equal(leaseProvider, key.ID()) {
leases, _ := deployments[tx.Deployment.EncodeString()]
deployments[tx.Deployment.EncodeString()] = append(leases, tx.LeaseID)
fmt.Printf("Won lease for order: %v\n",
Expand All @@ -197,13 +194,10 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
func getPrice(ctx context.Context, id types.OrderID) (uint32, error) {
// get deployment group
price := uint32(0)
path := qp.DeploymentGroupPath(id.GroupID())
group := new(types.DeploymentGroup)
result, err := query.Query(ctx, path)
group, err := ctx.QueryClient().DeploymentGroup(ctx.Ctx(), id.GroupID())
if err != nil {
return 0, err
}
group.Unmarshal(result.Response.Value)
for _, group := range group.GetResources() {
price += group.Price
}
Expand Down
17 changes: 11 additions & 6 deletions cmd/akash/query/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package query

import (
"github.com/ovrclk/akash/cmd/akash/context"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/keys"
"github.com/spf13/cobra"
)

Expand All @@ -20,8 +19,14 @@ func queryAccountCommand() *cobra.Command {
}

func doQueryAccountCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
structure := new(types.Account)
account := args[0]
path := state.AccountPath + account
return doQuery(ctx, path, structure)
for _, arg := range args {
key, err := keys.ParseAccountPath(arg)
if err != nil {
return err
}
if err := handleMessage(ctx.QueryClient().Account(ctx.Ctx(), key.ID())); err != nil {
return err
}
}
return nil
}
23 changes: 13 additions & 10 deletions cmd/akash/query/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package query

import (
"github.com/ovrclk/akash/cmd/akash/context"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/keys"
"github.com/spf13/cobra"
)

Expand All @@ -19,13 +18,17 @@ func queryDeploymentCommand() *cobra.Command {
}

func doQueryDeploymentCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
path := state.DeploymentPath
if len(args) > 0 {
structure := new(types.Deployment)
path += args[0]
return doQuery(ctx, path, structure)
} else {
structure := new(types.Deployments)
return doQuery(ctx, path, structure)
if len(args) == 0 {
handleMessage(ctx.QueryClient().Deployments(ctx.Ctx()))
}
for _, arg := range args {
key, err := keys.ParseDeploymentPath(arg)
if err != nil {
return err
}
if err := handleMessage(ctx.QueryClient().Deployment(ctx.Ctx(), key.ID())); err != nil {
return err
}
}
return nil
}
50 changes: 12 additions & 38 deletions cmd/akash/query/lease.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package query

import (
"github.com/gogo/protobuf/proto"
"github.com/ovrclk/akash/cmd/akash/context"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
"github.com/ovrclk/akash/util"
"github.com/ovrclk/akash/keys"
"github.com/spf13/cobra"
)

Expand All @@ -22,39 +18,17 @@ func queryLeaseCommand() *cobra.Command {
}

func doQueryLeaseCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
path := state.LeasePath
if len(args) > 0 {
structure := new(types.Lease)
path += args[0]
return doQuery(ctx, path, structure)
} else {
structure := new(types.Leases)
return doQuery(ctx, path, structure)
if len(args) == 0 {
handleMessage(ctx.QueryClient().Leases(ctx.Ctx()))
}
}

func LeasesForDeployment(ctx context.Context, deployment *base.Bytes) (*types.Leases, error) {
leases := &types.Leases{}
path := state.LeasePath + util.X(*deployment)
result, err := Query(ctx, path)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(result.Response.Value, leases); err != nil {
return nil, err
}
return leases, nil
}

func Lease(ctx context.Context, leaseAddr []byte) (*types.Lease, error) {
lease := &types.Lease{}
path := state.LeasePath + util.X(leaseAddr)
result, err := Query(ctx, path)
if err != nil {
return nil, err
}
if err := proto.Unmarshal(result.Response.Value, lease); err != nil {
return nil, err
for _, arg := range args {
key, err := keys.ParseLeasePath(arg)
if err != nil {
return err
}
if err := handleMessage(ctx.QueryClient().Lease(ctx.Ctx(), key.ID())); err != nil {
return err
}
}
return lease, nil
return nil
}
23 changes: 13 additions & 10 deletions cmd/akash/query/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package query

import (
"github.com/ovrclk/akash/cmd/akash/context"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/keys"
"github.com/spf13/cobra"
)

Expand All @@ -19,13 +18,17 @@ func queryOrderCommand() *cobra.Command {
}

func doQueryOrderCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
path := state.OrderPath
if len(args) > 0 {
structure := new(types.Order)
path += args[0]
return doQuery(ctx, path, structure)
} else {
structure := new(types.Orders)
return doQuery(ctx, path, structure)
if len(args) == 0 {
handleMessage(ctx.QueryClient().Orders(ctx.Ctx()))
}
for _, arg := range args {
key, err := keys.ParseOrderPath(arg)
if err != nil {
return err
}
if err := handleMessage(ctx.QueryClient().Order(ctx.Ctx(), key.ID())); err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit 95d5a32

Please sign in to comment.