Skip to content

Commit 95d5a32

Browse files
committed
query: client
move object query client from cmd/akash/query
1 parent 0ddc681 commit 95d5a32

File tree

17 files changed

+311
-159
lines changed

17 files changed

+311
-159
lines changed

app/account/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77

88
"github.com/gogo/protobuf/proto"
99
apptypes "github.com/ovrclk/akash/app/types"
10+
"github.com/ovrclk/akash/keys"
1011
"github.com/ovrclk/akash/state"
1112
"github.com/ovrclk/akash/types"
12-
"github.com/ovrclk/akash/types/base"
1313
"github.com/ovrclk/akash/types/code"
1414
tmtypes "github.com/tendermint/abci/types"
1515
"github.com/tendermint/tmlibs/log"
@@ -40,15 +40,15 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
4040
}
4141
}
4242
id := strings.TrimPrefix(req.Path, state.AccountPath)
43-
key, err := base.DecodeString(id)
43+
key, err := keys.ParseAccountPath(id)
4444
if err != nil {
4545
return tmtypes.ResponseQuery{
4646
Code: code.ERROR,
4747
Log: err.Error(),
4848
}
4949
}
5050

51-
acct, err := a.State().Account().Get(key)
51+
acct, err := a.State().Account().Get(key.ID())
5252
if err != nil {
5353
return tmtypes.ResponseQuery{
5454
Code: code.ERROR,

app/provider/app.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/gogo/protobuf/proto"
1010
apptypes "github.com/ovrclk/akash/app/types"
11+
"github.com/ovrclk/akash/keys"
1112
"github.com/ovrclk/akash/state"
1213
"github.com/ovrclk/akash/types"
1314
"github.com/ovrclk/akash/types/base"
@@ -47,14 +48,14 @@ func (a *app) Query(req tmtypes.RequestQuery) tmtypes.ResponseQuery {
4748
return a.doRangeQuery()
4849
}
4950

50-
key, err := base.DecodeString(id)
51+
key, err := keys.ParseProviderPath(id)
5152
if err != nil {
5253
return tmtypes.ResponseQuery{
5354
Code: code.ERROR,
5455
Log: err.Error(),
5556
}
5657
}
57-
return a.doQuery(key)
58+
return a.doQuery(key.ID())
5859
}
5960

6061
func (a *app) AcceptTx(ctx apptypes.Context, tx interface{}) bool {

cmd/akash/context/context.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88

99
"github.com/ovrclk/akash/cmd/akash/constants"
1010
"github.com/ovrclk/akash/cmd/common"
11+
"github.com/ovrclk/akash/query"
1112
"github.com/ovrclk/akash/txutil"
1213

14+
gctx "context"
15+
1316
"github.com/spf13/cobra"
1417
"github.com/spf13/viper"
1518
"github.com/tendermint/go-crypto/keys"
@@ -25,12 +28,14 @@ type Context interface {
2528
Node() string
2629
Client() *tmclient.HTTP
2730
TxClient() (txutil.Client, error)
31+
QueryClient() query.Client
2832
KeyName() string
2933
KeyType() (keys.CryptoAlgo, error)
3034
Key() (keys.Info, error)
3135
Nonce() (uint64, error)
3236
Log() log.Logger
3337
Signer() (txutil.Signer, keys.Info, error)
38+
Ctx() gctx.Context
3439
Wait() bool
3540
}
3641

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

168+
func (ctx *context) QueryClient() query.Client {
169+
return query.NewClient(ctx.Client())
170+
}
171+
163172
func (ctx *context) KeyName() string {
164173
val, _ := ctx.cmd.Flags().GetString(constants.FlagKey)
165174
return val
@@ -226,6 +235,10 @@ func (ctx *context) Wait() bool {
226235
return val
227236
}
228237

238+
func (ctx *context) Ctx() gctx.Context {
239+
return gctx.Background()
240+
}
241+
229242
func loadKeyManager(root string) (keys.Keybase, tmdb.DB, error) {
230243
codec, err := words.LoadCodec(constants.Codec)
231244
if err != nil {

cmd/akash/deployment.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
"os"
77

88
"github.com/ovrclk/akash/cmd/akash/context"
9-
"github.com/ovrclk/akash/cmd/akash/query"
109
"github.com/ovrclk/akash/cmd/common"
1110
"github.com/ovrclk/akash/keys"
1211
"github.com/ovrclk/akash/manifest"
1312
"github.com/ovrclk/akash/marketplace"
1413
"github.com/ovrclk/akash/sdl"
1514
"github.com/ovrclk/akash/types"
16-
"github.com/ovrclk/akash/types/base"
1715
. "github.com/ovrclk/akash/util"
1816
"github.com/spf13/cobra"
1917
)
@@ -110,7 +108,7 @@ func createDeployment(ctx context.Context, cmd *cobra.Command, args []string) er
110108
fmt.Printf("Group %v/%v Lease: %v\n", tx.Group, len(groups),
111109
keys.LeaseID(tx.LeaseID).Path())
112110
// get lease provider
113-
prov, err := query.Provider(ctx, tx.Provider)
111+
prov, err := ctx.QueryClient().Provider(ctx.Ctx(), tx.Provider)
114112
if err != nil {
115113
fmt.Printf("ERROR: %v", err)
116114
}
@@ -156,13 +154,13 @@ func closeDeployment(ctx context.Context, cmd *cobra.Command, args []string) err
156154
return err
157155
}
158156

159-
deployment, err := base.DecodeString(args[0])
157+
deployment, err := keys.ParseDeploymentPath(args[0])
160158
if err != nil {
161159
return err
162160
}
163161

164162
_, err = txclient.BroadcastTxCommit(&types.TxCloseDeployment{
165-
Deployment: deployment,
163+
Deployment: deployment.ID(),
166164
Reason: types.TxCloseDeployment_TENANT_CLOSE,
167165
})
168166

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

210-
leaseAddr, err := base.DecodeString(args[1])
208+
leaseAddr, err := keys.ParseLeasePath(args[1])
211209
if err != nil {
212210
return err
213211
}
214212

215-
lease, err := query.Lease(ctx, leaseAddr)
213+
lease, err := ctx.QueryClient().Lease(ctx.Ctx(), leaseAddr.ID())
216214
if err != nil {
217215
return err
218216
}
219217

220-
provider, err := query.Provider(ctx, lease.Provider)
218+
provider, err := ctx.QueryClient().Provider(ctx.Ctx(), lease.Provider)
221219
if err != nil {
222220
return err
223221
}

cmd/akash/provider.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import (
88

99
"github.com/ovrclk/akash/cmd/akash/constants"
1010
"github.com/ovrclk/akash/cmd/akash/context"
11-
"github.com/ovrclk/akash/cmd/akash/query"
1211
"github.com/ovrclk/akash/cmd/common"
1312
"github.com/ovrclk/akash/keys"
1413
"github.com/ovrclk/akash/manifest"
1514
"github.com/ovrclk/akash/marketplace"
16-
qp "github.com/ovrclk/akash/query"
1715
"github.com/ovrclk/akash/types"
18-
"github.com/ovrclk/akash/types/base"
1916
"github.com/ovrclk/akash/types/provider"
2017
. "github.com/ovrclk/akash/util"
2118
"github.com/spf13/cobra"
@@ -132,7 +129,7 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
132129
return err
133130
}
134131

135-
provider, err := base.DecodeString(args[0])
132+
key, err := keys.ParseProviderPath(args[0])
136133
if err != nil {
137134
return err
138135
}
@@ -156,7 +153,7 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
156153
Deployment: tx.Deployment,
157154
Group: tx.Group,
158155
Order: tx.Seq,
159-
Provider: provider,
156+
Provider: key.ID(),
160157
},
161158
Price: price,
162159
}
@@ -176,7 +173,7 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
176173
}).
177174
OnTxCreateLease(func(tx *types.TxCreateLease) {
178175
leaseProvider, _ := tx.Provider.Marshal()
179-
if bytes.Equal(leaseProvider, provider) {
176+
if bytes.Equal(leaseProvider, key.ID()) {
180177
leases, _ := deployments[tx.Deployment.EncodeString()]
181178
deployments[tx.Deployment.EncodeString()] = append(leases, tx.LeaseID)
182179
fmt.Printf("Won lease for order: %v\n",
@@ -197,13 +194,10 @@ func doProviderRunCommand(ctx context.Context, cmd *cobra.Command, args []string
197194
func getPrice(ctx context.Context, id types.OrderID) (uint32, error) {
198195
// get deployment group
199196
price := uint32(0)
200-
path := qp.DeploymentGroupPath(id.GroupID())
201-
group := new(types.DeploymentGroup)
202-
result, err := query.Query(ctx, path)
197+
group, err := ctx.QueryClient().DeploymentGroup(ctx.Ctx(), id.GroupID())
203198
if err != nil {
204199
return 0, err
205200
}
206-
group.Unmarshal(result.Response.Value)
207201
for _, group := range group.GetResources() {
208202
price += group.Price
209203
}

cmd/akash/query/account.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package query
22

33
import (
44
"github.com/ovrclk/akash/cmd/akash/context"
5-
"github.com/ovrclk/akash/state"
6-
"github.com/ovrclk/akash/types"
5+
"github.com/ovrclk/akash/keys"
76
"github.com/spf13/cobra"
87
)
98

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

2221
func doQueryAccountCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
23-
structure := new(types.Account)
24-
account := args[0]
25-
path := state.AccountPath + account
26-
return doQuery(ctx, path, structure)
22+
for _, arg := range args {
23+
key, err := keys.ParseAccountPath(arg)
24+
if err != nil {
25+
return err
26+
}
27+
if err := handleMessage(ctx.QueryClient().Account(ctx.Ctx(), key.ID())); err != nil {
28+
return err
29+
}
30+
}
31+
return nil
2732
}

cmd/akash/query/deployment.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package query
22

33
import (
44
"github.com/ovrclk/akash/cmd/akash/context"
5-
"github.com/ovrclk/akash/state"
6-
"github.com/ovrclk/akash/types"
5+
"github.com/ovrclk/akash/keys"
76
"github.com/spf13/cobra"
87
)
98

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

2120
func doQueryDeploymentCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
22-
path := state.DeploymentPath
23-
if len(args) > 0 {
24-
structure := new(types.Deployment)
25-
path += args[0]
26-
return doQuery(ctx, path, structure)
27-
} else {
28-
structure := new(types.Deployments)
29-
return doQuery(ctx, path, structure)
21+
if len(args) == 0 {
22+
handleMessage(ctx.QueryClient().Deployments(ctx.Ctx()))
3023
}
24+
for _, arg := range args {
25+
key, err := keys.ParseDeploymentPath(arg)
26+
if err != nil {
27+
return err
28+
}
29+
if err := handleMessage(ctx.QueryClient().Deployment(ctx.Ctx(), key.ID())); err != nil {
30+
return err
31+
}
32+
}
33+
return nil
3134
}

cmd/akash/query/lease.go

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package query
22

33
import (
4-
"github.com/gogo/protobuf/proto"
54
"github.com/ovrclk/akash/cmd/akash/context"
6-
"github.com/ovrclk/akash/state"
7-
"github.com/ovrclk/akash/types"
8-
"github.com/ovrclk/akash/types/base"
9-
"github.com/ovrclk/akash/util"
5+
"github.com/ovrclk/akash/keys"
106
"github.com/spf13/cobra"
117
)
128

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

2420
func doQueryLeaseCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
25-
path := state.LeasePath
26-
if len(args) > 0 {
27-
structure := new(types.Lease)
28-
path += args[0]
29-
return doQuery(ctx, path, structure)
30-
} else {
31-
structure := new(types.Leases)
32-
return doQuery(ctx, path, structure)
21+
if len(args) == 0 {
22+
handleMessage(ctx.QueryClient().Leases(ctx.Ctx()))
3323
}
34-
}
35-
36-
func LeasesForDeployment(ctx context.Context, deployment *base.Bytes) (*types.Leases, error) {
37-
leases := &types.Leases{}
38-
path := state.LeasePath + util.X(*deployment)
39-
result, err := Query(ctx, path)
40-
if err != nil {
41-
return nil, err
42-
}
43-
if err := proto.Unmarshal(result.Response.Value, leases); err != nil {
44-
return nil, err
45-
}
46-
return leases, nil
47-
}
48-
49-
func Lease(ctx context.Context, leaseAddr []byte) (*types.Lease, error) {
50-
lease := &types.Lease{}
51-
path := state.LeasePath + util.X(leaseAddr)
52-
result, err := Query(ctx, path)
53-
if err != nil {
54-
return nil, err
55-
}
56-
if err := proto.Unmarshal(result.Response.Value, lease); err != nil {
57-
return nil, err
24+
for _, arg := range args {
25+
key, err := keys.ParseLeasePath(arg)
26+
if err != nil {
27+
return err
28+
}
29+
if err := handleMessage(ctx.QueryClient().Lease(ctx.Ctx(), key.ID())); err != nil {
30+
return err
31+
}
5832
}
59-
return lease, nil
33+
return nil
6034
}

cmd/akash/query/order.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ package query
22

33
import (
44
"github.com/ovrclk/akash/cmd/akash/context"
5-
"github.com/ovrclk/akash/state"
6-
"github.com/ovrclk/akash/types"
5+
"github.com/ovrclk/akash/keys"
76
"github.com/spf13/cobra"
87
)
98

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

2120
func doQueryOrderCommand(ctx context.Context, cmd *cobra.Command, args []string) error {
22-
path := state.OrderPath
23-
if len(args) > 0 {
24-
structure := new(types.Order)
25-
path += args[0]
26-
return doQuery(ctx, path, structure)
27-
} else {
28-
structure := new(types.Orders)
29-
return doQuery(ctx, path, structure)
21+
if len(args) == 0 {
22+
handleMessage(ctx.QueryClient().Orders(ctx.Ctx()))
3023
}
24+
for _, arg := range args {
25+
key, err := keys.ParseOrderPath(arg)
26+
if err != nil {
27+
return err
28+
}
29+
if err := handleMessage(ctx.QueryClient().Order(ctx.Ctx(), key.ID())); err != nil {
30+
return err
31+
}
32+
}
33+
return nil
3134
}

0 commit comments

Comments
 (0)