/
id.go
153 lines (130 loc) · 4.07 KB
/
id.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package v1beta1
import (
"fmt"
dtypes "github.com/akash-network/node/x/deployment/types/v1beta1"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// MakeOrderID returns OrderID instance with provided groupID details and oseq
func MakeOrderID(id dtypes.GroupID, oseq uint32) OrderID {
return OrderID{
Owner: id.Owner,
DSeq: id.DSeq,
GSeq: id.GSeq,
OSeq: oseq,
}
}
// GroupID method returns groupID details for specific order
func (id OrderID) GroupID() dtypes.GroupID {
return dtypes.GroupID{
Owner: id.Owner,
DSeq: id.DSeq,
GSeq: id.GSeq,
}
}
// Equals method compares specific order with provided order
func (id OrderID) Equals(other OrderID) bool {
return id.GroupID().Equals(other.GroupID()) && id.OSeq == other.OSeq
}
// Validate method for OrderID and returns nil
func (id OrderID) Validate() error {
if err := id.GroupID().Validate(); err != nil {
return sdkerrors.Wrap(err, "OrderID: Invalid GroupID")
}
if id.OSeq == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidSequence, "OrderID: Invalid Order Sequence")
}
return nil
}
// String provides stringer interface to save reflected formatting.
func (id OrderID) String() string {
return fmt.Sprintf("%s/%v", id.GroupID(), id.OSeq)
}
// MakeBidID returns BidID instance with provided order details and provider
func MakeBidID(id OrderID, provider sdk.AccAddress) BidID {
return BidID{
Owner: id.Owner,
DSeq: id.DSeq,
GSeq: id.GSeq,
OSeq: id.OSeq,
Provider: provider.String(),
}
}
// Equals method compares specific bid with provided bid
func (id BidID) Equals(other BidID) bool {
return id.OrderID().Equals(other.OrderID()) &&
id.Provider == other.Provider
}
// LeaseID method returns lease details of bid
func (id BidID) LeaseID() LeaseID {
return LeaseID(id)
}
// OrderID method returns OrderID details with specific bid details
func (id BidID) OrderID() OrderID {
return OrderID{
Owner: id.Owner,
DSeq: id.DSeq,
GSeq: id.GSeq,
OSeq: id.OSeq,
}
}
// String method for consistent output.
func (id BidID) String() string {
return fmt.Sprintf("%s/%v", id.OrderID(), id.Provider)
}
// GroupID method returns GroupID details with specific bid details
func (id BidID) GroupID() dtypes.GroupID {
return id.OrderID().GroupID()
}
// DeploymentID method returns deployment details with specific bid details
func (id BidID) DeploymentID() dtypes.DeploymentID {
return id.GroupID().DeploymentID()
}
// Validate validates bid instance and returns nil
func (id BidID) Validate() error {
if err := id.OrderID().Validate(); err != nil {
return sdkerrors.Wrap(err, "BidID: Invalid OrderID")
}
if _, err := sdk.AccAddressFromBech32(id.Provider); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "BidID: Invalid Provider Address")
}
if id.Owner == id.Provider {
return sdkerrors.Wrap(sdkerrors.ErrConflict, "BidID: self-bid")
}
return nil
}
// MakeLeaseID returns LeaseID instance with provided bid details
func MakeLeaseID(id BidID) LeaseID {
return LeaseID(id)
}
// Equals method compares specific lease with provided lease
func (id LeaseID) Equals(other LeaseID) bool {
return id.BidID().Equals(other.BidID())
}
// Validate calls the BidID's validator and returns any error.
func (id LeaseID) Validate() error {
if err := id.BidID().Validate(); err != nil {
return sdkerrors.Wrap(err, "LeaseID: Invalid BidID")
}
return nil
}
// BidID method returns BidID details with specific LeaseID
func (id LeaseID) BidID() BidID {
return BidID(id)
}
// OrderID method returns OrderID details with specific lease details
func (id LeaseID) OrderID() OrderID {
return id.BidID().OrderID()
}
// GroupID method returns GroupID details with specific lease details
func (id LeaseID) GroupID() dtypes.GroupID {
return id.OrderID().GroupID()
}
// DeploymentID method returns deployment details with specific lease details
func (id LeaseID) DeploymentID() dtypes.DeploymentID {
return id.GroupID().DeploymentID()
}
// String method provides human readable representation of LeaseID.
func (id LeaseID) String() string {
return id.BidID().String()
}