/
state_pov.go
261 lines (219 loc) · 6.35 KB
/
state_pov.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
260
261
package types
import (
"fmt"
"strings"
)
//type PovStateSerdeser interface {
// Serialize() ([]byte, error)
// Deserialize(text []byte) error
//}
//go:generate msgp
type PovAccountState struct {
Account Address `msg:"a,extension" json:"account"`
Balance Balance `msg:"b,extension" json:"balance"`
Vote Balance `msg:"v,extension" json:"vote"`
Network Balance `msg:"n,extension" json:"network"`
Storage Balance `msg:"s,extension" json:"storage"`
Oracle Balance `msg:"o,extension" json:"oracle"`
TokenStates []*PovTokenState `msg:"ts" json:"tokenStates"`
}
func NewPovAccountState() *PovAccountState {
return &PovAccountState{
Balance: NewBalance(0),
Vote: NewBalance(0),
Network: NewBalance(0),
Storage: NewBalance(0),
Oracle: NewBalance(0),
}
}
func (as *PovAccountState) GetTokenState(tokenType Hash) *PovTokenState {
for _, token := range as.TokenStates {
if token.Type == tokenType {
return token
}
}
return nil
}
func (as *PovAccountState) TotalBalance() Balance {
return as.Balance.Add(as.Vote).Add(as.Storage).Add(as.Network).Add(as.Oracle)
}
func (as *PovAccountState) Serialize() ([]byte, error) {
return as.MarshalMsg(nil)
}
func (as *PovAccountState) Deserialize(text []byte) error {
_, err := as.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (as *PovAccountState) Clone() *PovAccountState {
newAs := *as
newAs.TokenStates = nil
for _, ts := range as.TokenStates {
newTs := ts.Clone()
newAs.TokenStates = append(newAs.TokenStates, newTs)
}
return &newAs
}
func (as *PovAccountState) String() string {
sb := strings.Builder{}
sb.WriteString("{")
asInfo := fmt.Sprintf("Balance:%s, Vote:%s, Network:%s, Storage:%s, Oracle:%s",
as.Balance, as.Vote, as.Network, as.Storage, as.Oracle)
sb.WriteString(asInfo)
sb.WriteString(", TokenState:[")
for _, ts := range as.TokenStates {
sb.WriteString(ts.String())
}
sb.WriteString("]")
sb.WriteString("}")
return sb.String()
}
type PovTokenState struct {
Type Hash `msg:"t,extension" json:"type"`
Hash Hash `msg:"h,extension" json:"hash"`
Representative Address `msg:"r,extension" json:"representative"`
Balance Balance `msg:"b,extension" json:"balance"`
}
func NewPovTokenState(token Hash) *PovTokenState {
return &PovTokenState{
Type: token,
Balance: NewBalance(0),
}
}
func (ts *PovTokenState) Serialize() ([]byte, error) {
return ts.MarshalMsg(nil)
}
func (ts *PovTokenState) Deserialize(text []byte) error {
_, err := ts.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (ts *PovTokenState) Clone() *PovTokenState {
newTs := *ts
return &newTs
}
func (ts *PovTokenState) String() string {
return fmt.Sprintf("{Type:%s, Hash:%s, Rep:%s, Balance:%s}",
ts.Type, ts.Hash, ts.Representative, ts.Balance)
}
type PovRepState struct {
Account Address `msg:"a,extension" json:"account"`
Balance Balance `msg:"b,extension" json:"balance"`
Vote Balance `msg:"v,extension" json:"vote"`
Network Balance `msg:"n,extension" json:"network"`
Storage Balance `msg:"s,extension" json:"storage"`
Oracle Balance `msg:"o,extension" json:"oracle"`
Total Balance `msg:"t,extension" json:"total"`
Status uint32 `msg:"st" json:"status"`
Height uint64 `msg:"he" json:"height"`
}
func NewPovRepState() *PovRepState {
return &PovRepState{
Balance: NewBalance(0),
Vote: NewBalance(0),
Network: NewBalance(0),
Storage: NewBalance(0),
Oracle: NewBalance(0),
Total: NewBalance(0),
}
}
func (rs *PovRepState) Serialize() ([]byte, error) {
return rs.MarshalMsg(nil)
}
func (rs *PovRepState) Deserialize(text []byte) error {
_, err := rs.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (rs *PovRepState) Clone() *PovRepState {
newRs := *rs
newRs.Balance = rs.Balance.Copy()
newRs.Vote = rs.Vote.Copy()
newRs.Network = rs.Network.Copy()
newRs.Storage = rs.Storage.Copy()
newRs.Oracle = rs.Oracle.Copy()
newRs.Total = rs.Total.Copy()
return &newRs
}
func (rs *PovRepState) CalcTotal() Balance {
return rs.Balance.Add(rs.Vote).Add(rs.Network).Add(rs.Oracle).Add(rs.Storage)
}
func (rs *PovRepState) String() string {
return fmt.Sprintf("{Account:%s, Balance:%s, Vote:%s, Network:%s, Storage:%s, Oracle:%s, Total:%s, Status:%d, Height:%d}",
rs.Account, rs.Balance, rs.Vote, rs.Network, rs.Storage, rs.Oracle, rs.Total, rs.Status, rs.Height)
}
// Common Contract State, key value in trie for each contract
// key = contract address
type PovContractState struct {
StateHash Hash `msg:"sh,extension" json:"stateHash"`
CodeHash Hash `msg:"ch,extension" json:"codeHash"`
}
func NewPovContractState() *PovContractState {
cs := new(PovContractState)
return cs
}
func (cs *PovContractState) Serialize() ([]byte, error) {
return cs.MarshalMsg(nil)
}
func (cs *PovContractState) Deserialize(text []byte) error {
_, err := cs.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
const (
PovPublishStatusInit = 0
PovPublishStatusVerified = 1
)
// key = type + id + pubkey + sendBlockHash
type PovPublishState struct {
OracleAccounts []Address `msg:"oas" json:"oracleAccounts"`
PublishHeight uint64 `msg:"ph" json:"publishHeight"`
VerifiedHeight uint64 `msg:"vh" json:"verifiedHeight"`
VerifiedStatus int8 `msg:"vs" json:"verifiedStatus"`
BonusFee *BigNum `msg:"bf,extension" json:"bonusFee"`
}
func NewPovPublishState() *PovPublishState {
ps := new(PovPublishState)
ps.VerifiedStatus = PovPublishStatusInit
return ps
}
func (ps *PovPublishState) Serialize() ([]byte, error) {
return ps.MarshalMsg(nil)
}
func (ps *PovPublishState) Deserialize(text []byte) error {
_, err := ps.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
// key = address
type PovVerifierState struct {
TotalVerify uint64 `msg:"tv" json:"totalVerify"`
TotalReward *BigNum `msg:"tr,extension" json:"totalReward"`
ActiveHeight map[string]uint64 `msg:"ah" json:"activeHeight"`
}
func NewPovVerifierState() *PovVerifierState {
vs := new(PovVerifierState)
vs.TotalReward = NewBigNumFromInt(0)
vs.ActiveHeight = make(map[string]uint64)
return vs
}
func (vs *PovVerifierState) Serialize() ([]byte, error) {
return vs.MarshalMsg(nil)
}
func (vs *PovVerifierState) Deserialize(text []byte) error {
_, err := vs.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}