Skip to content

Commit

Permalink
cleanup: move id methods to types package
Browse files Browse the repository at this point in the history
  • Loading branch information
boz committed Jun 1, 2018
1 parent 9c4bd22 commit 25fa2c7
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 340 deletions.
2 changes: 1 addition & 1 deletion app/deployment/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (a *app) doQuery(key keys.Deployment) tmtypes.ResponseQuery {
if dep == nil {
return tmtypes.ResponseQuery{
Code: code.NOT_FOUND,
Log: fmt.Sprintf("deployment %v not found", key.Path()),
Log: fmt.Sprintf("deployment %v not found", key),
}
}

Expand Down
5 changes: 1 addition & 4 deletions app/deployment/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/ovrclk/akash/app/order"
"github.com/ovrclk/akash/app/provider"
apptypes "github.com/ovrclk/akash/app/types"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/query"
pstate "github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/testutil"
Expand Down Expand Up @@ -90,9 +89,7 @@ func TestCreateTx(t *testing.T) {
goodgroup := groups.GetItems()[0].DeploymentGroupID

{
path := fmt.Sprintf("%v%v",
pstate.DeploymentPath,
keys.DeploymentGroupID(badgroup).Path())
path := fmt.Sprintf("%v%v", pstate.DeploymentPath, badgroup)
resp := app.Query(tmtypes.RequestQuery{Path: path})
assert.NotEmpty(t, resp.Log)
require.False(t, resp.IsOK())
Expand Down
6 changes: 2 additions & 4 deletions cmd/akash/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,12 @@ func createDeployment(session session.Session, cmd *cobra.Command, args []string
handler := marketplace.NewBuilder().
OnTxCreateFulfillment(func(tx *types.TxCreateFulfillment) {
if bytes.Equal(tx.Deployment, address) {
fmt.Printf("Group %v/%v Fulfillment: %v\n", tx.Group, len(groups),
keys.FulfillmentID(tx.FulfillmentID).Path())
fmt.Printf("Group %v/%v Fulfillment: %v\n", tx.Group, len(groups), tx.FulfillmentID)
}
}).
OnTxCreateLease(func(tx *types.TxCreateLease) {
if bytes.Equal(tx.Deployment, address) {
fmt.Printf("Group %v/%v Lease: %v\n", tx.Group, len(groups),
keys.LeaseID(tx.LeaseID).Path())
fmt.Printf("Group %v/%v Lease: %v\n", tx.Group, len(groups), tx.LeaseID)
// get lease provider
prov, err := session.QueryClient().Provider(session.Ctx(), tx.Provider)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/akash/marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/ovrclk/akash/cmd/akash/session"
"github.com/ovrclk/akash/cmd/common"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/marketplace"
"github.com/ovrclk/akash/state"
"github.com/ovrclk/akash/types"
Expand Down Expand Up @@ -65,10 +64,10 @@ func marketplaceMonitorHandler() marketplace.Handler {
fmt.Printf("DEPLOYMENT CLOSED\t%v\n", X(tx.Deployment))
}).
OnTxCloseFulfillment(func(tx *types.TxCloseFulfillment) {
fmt.Printf("FULFILLMENT CLOSED\t%v\n", keys.FulfillmentID(tx.FulfillmentID).Path())
fmt.Printf("FULFILLMENT CLOSED\t%v\n", tx.FulfillmentID)
}).
OnTxCloseLease(func(tx *types.TxCloseLease) {
fmt.Printf("LEASE CLOSED\t%v\n", keys.LeaseID(tx.LeaseID).Path())
fmt.Printf("LEASE CLOSED\t%v\n", tx.LeaseID)
}).
Create()
}
2 changes: 1 addition & 1 deletion cmd/akash/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func doSendCommand(session session.Session, cmd *cobra.Command, args []string) e
return err
}

fmt.Printf("Sent %v tokens to %v in block %v\n", amount, to.Path(), result.Height)
fmt.Printf("Sent %v tokens to %v in block %v\n", amount, to, result.Height)

return nil
}
28 changes: 0 additions & 28 deletions keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package keys
import (
"bytes"
"encoding/binary"
"strconv"

"github.com/ovrclk/akash/types"
"github.com/ovrclk/akash/types/base"
"github.com/ovrclk/akash/util"
)

// XXX: interim hack (iteration!)

type Key interface {
Path() string
Bytes() []byte
}

Expand All @@ -25,10 +22,6 @@ type (
Account = Address
)

func (k Address) Path() string {
return util.X(k)
}

func (k Address) Bytes() []byte {
return k
}
Expand Down Expand Up @@ -60,10 +53,6 @@ func (k DeploymentGroup) Bytes() []byte {
return buf.Bytes()
}

func (k DeploymentGroup) Path() string {
return util.X(k.Deployment) + "/" + strconv.FormatUint(k.Seq, 10)
}

type Order struct {
types.OrderID
}
Expand All @@ -84,12 +73,6 @@ func (k Order) Bytes() []byte {
return buf.Bytes()
}

func (k Order) Path() string {
return util.X(k.Deployment) + "/" +
strconv.FormatUint(k.Group, 10) + "/" +
strconv.FormatUint(k.Seq, 10)
}

func (k Order) GroupKey() DeploymentGroup {
return DeploymentGroupID(types.DeploymentGroupID{
Deployment: k.Deployment,
Expand Down Expand Up @@ -118,13 +101,6 @@ func (k Fulfillment) Bytes() []byte {
return buf.Bytes()
}

func (k Fulfillment) Path() string {
return util.X(k.Deployment) + "/" +
strconv.FormatUint(k.Group, 10) + "/" +
strconv.FormatUint(k.Order, 10) + "/" +
util.X(k.Provider)
}

func (k Fulfillment) OrderKey() Order {
return OrderID(types.OrderID{
Deployment: k.Deployment,
Expand Down Expand Up @@ -153,10 +129,6 @@ func (k Lease) Bytes() []byte {
return k.FulfillmentKey().Bytes()
}

func (k Lease) Path() string {
return k.FulfillmentKey().Path()
}

func (k Lease) FulfillmentKey() Fulfillment {
return FulfillmentID(types.FulfillmentID{
Deployment: k.Deployment,
Expand Down
12 changes: 4 additions & 8 deletions provider/bidengine/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
// order manages bidding and general lifecycle handling of an order.
type order struct {
order types.OrderID
group *types.DeploymentGroupID

session session.Session
cluster cluster.Cluster
Expand All @@ -42,11 +41,8 @@ func newOrder(e *service, ev *event.TxCreateOrder) (*order, error) {
log := session.Log().
With("order", keys.OrderID(ev.OrderID).Path())

group := ev.GroupID()

order := &order{
order: ev.OrderID,
group: &group,
session: session,
cluster: e.cluster,
bus: e.bus,
Expand Down Expand Up @@ -113,20 +109,20 @@ loop:
case *event.TxCreateLease:

// different group
if o.group.Compare(ev.GroupID()) != 0 {
o.log.Info("ignoring group", "group", keys.DeploymentGroupID(ev.GroupID()).Path())
if o.order.GroupID().Compare(ev.GroupID()) != 0 {
o.log.Info("ignoring group", "group", ev.GroupID())
break
}

// check winning provider
if bytes.Compare(o.session.Provider().Address, ev.Provider) != 0 {
o.log.Info("lease lost", "lease-id", keys.LeaseID(ev.LeaseID).Path())
o.log.Info("lease lost", "lease-id", ev.LeaseID)
break loop
}

// TODO: sanity check (price, state, etc...)

o.log.Info("lease won", "lease-id", keys.LeaseID(ev.LeaseID).Path())
o.log.Info("lease won", "lease-id", ev.LeaseID)

o.bus.Publish(event.LeaseWon{
LeaseID: ev.LeaseID,
Expand Down
3 changes: 1 addition & 2 deletions provider/cluster/kube/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"

"github.com/juju/errors"
"github.com/ovrclk/akash/keys"
"github.com/ovrclk/akash/provider/cluster"
"github.com/ovrclk/akash/types"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -204,7 +203,7 @@ func (c *client) deployServices(oid types.OrderID, group *types.ManifestGroup) e
}

func oidNS(oid types.OrderID) string {
path := strings.Replace(keys.OrderID(oid).Path(), "/", ".", -1)
path := oid.String()
sha := sha1.Sum([]byte(path))
return hex.EncodeToString(sha[:])
}
Binary file removed provider/manifest/debug.test
Binary file not shown.
102 changes: 102 additions & 0 deletions types/id.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
package types

import (
"bytes"
"strconv"

"github.com/ovrclk/akash/types/base"
)

func (id DeploymentGroupID) String() string {
return id.Deployment.String() + "/" + strconv.FormatUint(id.Seq, 10)
}

func (id DeploymentGroupID) Path() string {
return id.String()
}

func (id DeploymentGroupID) Compare(that interface{}) int {
switch that := that.(type) {
case DeploymentGroupID:
if cmp := bytes.Compare(id.Deployment, that.Deployment); cmp != 0 {
return cmp
}
return int(id.Seq - that.Seq)
case *DeploymentGroupID:
if cmp := bytes.Compare(id.Deployment, that.Deployment); cmp != 0 {
return cmp
}
return int(id.Seq - that.Seq)
default:
return 1
}
}

func (id DeploymentGroupID) DeploymentID() base.Bytes {
return id.Deployment
}

func (id OrderID) Compare(that interface{}) int {
switch that := that.(type) {
case OrderID:
if cmp := id.GroupID().Compare(that.GroupID()); cmp != 0 {
return cmp
}
return int(id.Seq - that.Seq)
case *OrderID:
if cmp := id.GroupID().Compare(that.GroupID()); cmp != 0 {
return cmp
}
return int(id.Seq - that.Seq)
default:
return 1
}
}

func (id OrderID) String() string {
return id.Deployment.String() + "/" +
strconv.FormatUint(id.Group, 10) + "/" +
strconv.FormatUint(id.Seq, 10)
}

func (id OrderID) Path() string {
return id.String()
}

func (id OrderID) GroupID() DeploymentGroupID {
return DeploymentGroupID{
Deployment: id.Deployment,
Expand All @@ -19,6 +74,34 @@ func (id OrderID) DeploymentID() base.Bytes {
return id.Deployment
}

func (id FulfillmentID) String() string {
return id.Deployment.String() + "/" +
strconv.FormatUint(id.Group, 10) + "/" +
strconv.FormatUint(id.Order, 10) + "/" +
id.Provider.String()
}

func (id FulfillmentID) Path() string {
return id.String()
}

func (id FulfillmentID) Compare(that interface{}) int {
switch that := that.(type) {
case FulfillmentID:
if cmp := id.OrderID().Compare(that.OrderID()); cmp != 0 {
return cmp
}
return bytes.Compare(id.Provider, that.Provider)
case *FulfillmentID:
if cmp := id.OrderID().Compare(that.OrderID()); cmp != 0 {
return cmp
}
return bytes.Compare(id.Provider, that.Provider)
default:
return 1
}
}

func (id FulfillmentID) LeaseID() LeaseID {
return LeaseID{
Deployment: id.Deployment,
Expand All @@ -44,6 +127,25 @@ func (id FulfillmentID) DeploymentID() base.Bytes {
return id.Deployment
}

func (id LeaseID) String() string {
return id.FulfillmentID().String()
}

func (id LeaseID) Path() string {
return id.String()
}

func (id LeaseID) Compare(that interface{}) int {
switch that := that.(type) {
case LeaseID:
return id.FulfillmentID().Compare(that.FulfillmentID())
case *LeaseID:
return id.FulfillmentID().Compare(that.FulfillmentID())
default:
return 1
}
}

func (id LeaseID) FulfillmentID() FulfillmentID {
return FulfillmentID{
Deployment: id.Deployment,
Expand Down
Loading

0 comments on commit 25fa2c7

Please sign in to comment.