-
Notifications
You must be signed in to change notification settings - Fork 102
/
application.go
259 lines (227 loc) · 9.81 KB
/
application.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package types
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pokt-network/pocket-core/codec"
"strings"
"time"
"github.com/pokt-network/pocket-core/crypto"
sdk "github.com/pokt-network/pocket-core/types"
)
// Application represents a pocket network decentralized application. Applications stake in the network for relay throughput.
type Application struct {
Address sdk.Address `json:"address" yaml:"address"` // address of the application; hex encoded in JSON
PublicKey crypto.PublicKey `json:"public_key" yaml:"public_key"` // the public key of the application; hex encoded in JSON
Jailed bool `json:"jailed" yaml:"jailed"` // has the application been jailed from staked status?
Status sdk.StakeStatus `json:"status" yaml:"status"` // application status (staked/unstaking/unstaked)
Chains []string `json:"chains" yaml:"chains"` // requested chains
StakedTokens sdk.BigInt `json:"tokens" yaml:"tokens"` // tokens staked in the network
MaxRelays sdk.BigInt `json:"max_relays" yaml:"max_relays"` // maximum number of relays allowed
UnstakingCompletionTime time.Time `json:"unstaking_time" yaml:"unstaking_time"` // if unstaking, min time for the application to complete unstaking
}
// NewApplication - initialize a new instance of an application
func NewApplication(addr sdk.Address, publicKey crypto.PublicKey, chains []string, tokensToStake sdk.BigInt) Application {
return Application{
Address: addr,
PublicKey: publicKey,
Jailed: false,
Status: sdk.Staked,
Chains: chains,
StakedTokens: tokensToStake,
UnstakingCompletionTime: time.Time{}, // zero out because status: staked
}
}
// get the consensus-engine power
// a reduction of 10^6 from application tokens is applied
func (a Application) ConsensusPower() int64 {
if a.IsStaked() {
return sdk.TokensToConsensusPower(a.StakedTokens)
}
return 0
}
// RemoveStakedTokens removes tokens from a application
func (a Application) RemoveStakedTokens(tokens sdk.BigInt) (Application, error) {
if tokens.IsNegative() {
return Application{}, fmt.Errorf("should not happen: trying to remove negative tokens %v", tokens)
}
if a.StakedTokens.LT(tokens) {
return Application{}, fmt.Errorf("should not happen: only have %v tokens, trying to remove %v", a.StakedTokens, tokens)
}
a.StakedTokens = a.StakedTokens.Sub(tokens)
return a, nil
}
// AddStakedTokens tokens to staked field for a application
func (a Application) AddStakedTokens(tokens sdk.BigInt) (Application, error) {
if tokens.IsNegative() {
return Application{}, fmt.Errorf("should not happen: trying to remove negative tokens %v", tokens)
}
a.StakedTokens = a.StakedTokens.Add(tokens)
return a, nil
}
// compares the vital fields of two application structures
func (a Application) Equals(v2 Application) bool {
return a.PublicKey.Equals(v2.PublicKey) &&
bytes.Equal(a.Address, v2.Address) &&
a.Status.Equal(v2.Status) &&
a.StakedTokens.Equal(v2.StakedTokens)
}
// UpdateStatus updates the staking status
func (a Application) UpdateStatus(newStatus sdk.StakeStatus) Application {
a.Status = newStatus
return a
}
func (a Application) GetChains() []string { return a.Chains }
func (a Application) IsStaked() bool { return a.GetStatus().Equal(sdk.Staked) }
func (a Application) IsUnstaked() bool { return a.GetStatus().Equal(sdk.Unstaked) }
func (a Application) IsUnstaking() bool { return a.GetStatus().Equal(sdk.Unstaking) }
func (a Application) IsJailed() bool { return a.Jailed }
func (a Application) GetStatus() sdk.StakeStatus { return a.Status }
func (a Application) GetAddress() sdk.Address { return a.Address }
func (a Application) GetPublicKey() crypto.PublicKey { return a.PublicKey }
func (a Application) GetTokens() sdk.BigInt { return a.StakedTokens }
func (a Application) GetConsensusPower() int64 { return a.ConsensusPower() }
func (a Application) GetMaxRelays() sdk.BigInt { return a.MaxRelays }
var _ codec.ProtoMarshaler = &Application{}
func (a *Application) Reset() {
*a = Application{}
}
func (a Application) ProtoMessage() {
p := a.ToProto()
p.ProtoMessage()
}
func (a Application) Marshal() ([]byte, error) {
p := a.ToProto()
return p.Marshal()
}
func (a Application) MarshalTo(data []byte) (n int, err error) {
p := a.ToProto()
return p.MarshalTo(data)
}
func (a Application) MarshalToSizedBuffer(dAtA []byte) (int, error) {
p := a.ToProto()
return p.MarshalToSizedBuffer(dAtA)
}
func (a Application) Size() int {
p := a.ToProto()
return p.Size()
}
func (a *Application) Unmarshal(data []byte) (err error) {
var pa ProtoApplication
err = pa.Unmarshal(data)
if err != nil {
return err
}
*a, err = pa.FromProto()
return
}
func (a Application) ToProto() ProtoApplication {
return ProtoApplication{
Address: a.Address,
PublicKey: a.PublicKey.RawBytes(),
Jailed: a.Jailed,
Status: a.Status,
Chains: a.Chains,
StakedTokens: a.StakedTokens,
MaxRelays: a.MaxRelays,
UnstakingCompletionTime: a.UnstakingCompletionTime,
}
}
func (ae ProtoApplication) FromProto() (Application, error) {
pk, err := crypto.NewPublicKeyBz(ae.PublicKey)
if err != nil {
return Application{}, err
}
return Application{
Address: ae.Address,
PublicKey: pk,
Jailed: ae.Jailed,
Status: ae.Status,
Chains: ae.Chains,
StakedTokens: ae.StakedTokens,
MaxRelays: ae.MaxRelays,
UnstakingCompletionTime: ae.UnstakingCompletionTime,
}, nil
}
// Applications is a slice of type application.
type Applications []Application
func (a Applications) String() (out string) {
for _, val := range a {
out += val.String() + "\n\n"
}
return strings.TrimSpace(out)
}
// String returns a human readable string representation of a application.
func (a Application) String() string {
return fmt.Sprintf("Address:\t\t%s\nPublic Key:\t\t%s\nJailed:\t\t\t%v\nChains:\t\t\t%v\nMaxRelays:\t\t%v\nStatus:\t\t\t%s\nTokens:\t\t\t%s\nUnstaking Time:\t%v\n----\n",
a.Address, a.PublicKey.RawString(), a.Jailed, a.Chains, a.MaxRelays, a.Status, a.StakedTokens, a.UnstakingCompletionTime,
)
}
// this is a helper struct used for JSON de- and encoding only
type JSONApplication struct {
Address sdk.Address `json:"address" yaml:"address"` // the hex address of the application
PublicKey string `json:"public_key" yaml:"public_key"` // the hex consensus public key of the application
Jailed bool `json:"jailed" yaml:"jailed"` // has the application been jailed from staked status?
Chains []string `json:"chains" yaml:"chains"` // non native (external) blockchains needed for the application
MaxRelays sdk.BigInt `json:"max_relays" yaml:"max_relays"` // maximum number of relays allowed for the application
Status sdk.StakeStatus `json:"status" yaml:"status"` // application status (staked/unstaking/unstaked)
StakedTokens sdk.BigInt `json:"staked_tokens" yaml:"staked_tokens"` // how many staked tokens
UnstakingCompletionTime time.Time `json:"unstaking_time" yaml:"unstaking_time"` // if unstaking, min time for the application to complete unstaking
}
// marshal structure into JSON encoding
func (a Applications) JSON() (out []byte, err error) {
return json.Marshal(a)
}
// MarshalJSON marshals the application to JSON using raw Hex for the public key
func (a Application) MarshalJSON() ([]byte, error) {
return ModuleCdc.MarshalJSON(JSONApplication{
Address: a.Address,
PublicKey: a.PublicKey.RawString(),
Jailed: a.Jailed,
Status: a.Status,
Chains: a.Chains,
MaxRelays: a.MaxRelays,
StakedTokens: a.StakedTokens,
UnstakingCompletionTime: a.UnstakingCompletionTime,
})
}
// UnmarshalJSON unmarshals the application from JSON using raw hex for the public key
func (a *Application) UnmarshalJSON(data []byte) error {
bv := &JSONApplication{}
if err := ModuleCdc.UnmarshalJSON(data, bv); err != nil {
return err
}
consPubKey, err := crypto.NewPublicKey(bv.PublicKey)
if err != nil {
return err
}
*a = Application{
Address: bv.Address,
PublicKey: consPubKey,
Chains: bv.Chains,
MaxRelays: bv.MaxRelays,
Jailed: bv.Jailed,
StakedTokens: bv.StakedTokens,
Status: bv.Status,
UnstakingCompletionTime: bv.UnstakingCompletionTime,
}
return nil
}
// unmarshal the application
func MarshalApplication(cdc *codec.Codec, ctx sdk.Ctx, application Application) (result []byte, err error) {
return cdc.MarshalBinaryLengthPrefixed(&application, ctx.BlockHeight())
}
// unmarshal the application
func UnmarshalApplication(cdc *codec.Codec, ctx sdk.Ctx, appBytes []byte) (application Application, err error) {
err = cdc.UnmarshalBinaryLengthPrefixed(appBytes, &application, ctx.BlockHeight())
return
}
type ApplicationsPage struct {
Result Applications `json:"result"`
Total int `json:"total_pages"`
Page int `json:"page"`
}
// String returns a human readable string representation of a validator page
func (aP ApplicationsPage) String() string {
return fmt.Sprintf("Total:\t\t%d\nPage:\t\t%d\nResult:\t\t\n====\n%s\n====\n", aP.Total, aP.Page, aP.Result.String())
}