-
Notifications
You must be signed in to change notification settings - Fork 0
/
extimpawn.go
453 lines (416 loc) · 11.3 KB
/
extimpawn.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package vm
import (
"encoding/json"
"errors"
"io"
"math/big"
"github.com/iceming123/go-ice/common"
"github.com/iceming123/go-ice/common/hexutil"
"github.com/iceming123/go-ice/core/types"
"github.com/iceming123/go-ice/params"
"github.com/iceming123/go-ice/rlp"
)
// "external" ImpawnImpl encoding. used for pos staking.
type extImpawnImpl struct {
Accounts []SAImpawns
CurEpochID uint64
Array []uint64
LastReward uint64
}
func (i *ImpawnImpl) DecodeRLP(s *rlp.Stream) error {
var ei extImpawnImpl
if err := s.Decode(&ei); err != nil {
return err
}
accounts := make(map[uint64]SAImpawns)
for i, account := range ei.Accounts {
accounts[ei.Array[i]] = account
}
i.curEpochID, i.accounts, i.lastReward = ei.CurEpochID, accounts, ei.LastReward
return nil
}
// EncodeRLP serializes b into the icechain RLP ImpawnImpl format.
func (i *ImpawnImpl) EncodeRLP(w io.Writer) error {
var accounts []SAImpawns
var order []uint64
for i, _ := range i.accounts {
order = append(order, i)
}
for m := 0; m < len(order)-1; m++ {
for n := 0; n < len(order)-1-m; n++ {
if order[n] > order[n+1] {
order[n], order[n+1] = order[n+1], order[n]
}
}
}
for _, epoch := range order {
accounts = append(accounts, i.accounts[epoch])
}
return rlp.Encode(w, extImpawnImpl{
CurEpochID: i.curEpochID,
Accounts: accounts,
Array: order,
LastReward: i.lastReward,
})
}
func (i *ImpawnImpl) GetAllStakingAccountRPC(height uint64) map[string]interface{} {
sas := i.GetAllStakingAccount()
sasRPC := make(map[string]interface{}, len(sas))
var attrs []map[string]interface{}
count := 0
countCommittee := 0
for index, sa := range sas {
attr := make(map[string]interface{})
attr["id"] = index
attr["unit"] = unitDisplay(sa.Unit)
attr["votePubKey"] = hexutil.Bytes(sa.Votepubkey)
attr["fee"] = sa.Fee.Uint64()
if countCommittee <= params.CountInEpoch && isCommitteeMember(i, sa.Unit.Address) {
attr["committee"] = true
countCommittee++
} else {
attr["committee"] = false
}
attr["delegation"] = daSDisplay(sa.Delegation, height)
if sa.Modify != nil {
ai := make(map[string]interface{})
if sa.Modify.Fee != nil {
ai["fee"] = sa.Modify.Fee.Uint64()
}
if sa.Modify.VotePubkey != nil {
ai["votePubKey"] = hexutil.Bytes(sa.Modify.VotePubkey)
}
attr["modify"] = ai
}
attr["staking"] = weitoCoin(sa.getAllStaking(height))
attr["validStaking"] = weitoCoin(sa.getValidStaking(height))
attrs = append(attrs, attr)
count = count + len(sa.Delegation)
}
sasRPC["stakers"] = attrs
sasRPC["stakerCount"] = len(sas)
sasRPC["delegateCount"] = count
return sasRPC
}
func (i *ImpawnImpl) GetStakingAssetRPC(addr common.Address) []StakingAsset {
msv := i.GetStakingAsset(addr)
var attrs []StakingAsset
for key, value := range msv {
attr := StakingAsset{
StakingValue: stakingValueDisplay(value),
Address: key,
}
attrs = append(attrs, attr)
}
return attrs
}
type StakingAsset struct {
StakingValue []*StakingValue `json:"stakingValue"`
Address common.Address `json:"address"`
}
type StakingValue struct {
Height uint64
Amount string
}
// MarshalJSON marshals as JSON.
func (s StakingValue) MarshalJSON() ([]byte, error) {
type StakingValue struct {
Height hexutil.Uint64 `json:"height"`
Amount string `json:"amount"`
}
var enc StakingValue
enc.Height = hexutil.Uint64(s.Height)
enc.Amount = s.Amount
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (s *StakingValue) UnmarshalJSON(input []byte) error {
type StakingValue struct {
Height *hexutil.Uint64 `json:"height"`
Amount *string `json:"amount"`
}
var dec StakingValue
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.Height != nil {
s.Height = uint64(*dec.Height)
}
if dec.Amount != nil {
s.Amount = *dec.Amount
}
return nil
}
// MarshalJSON marshals as JSON.
func (s StakingAsset) MarshalJSON() ([]byte, error) {
type StakingAsset struct {
StakingValue []*StakingValue `json:"stakingValue"`
Address common.Address `json:"address"`
}
var enc StakingAsset
enc.StakingValue = s.StakingValue
enc.Address = s.Address
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (s *StakingAsset) UnmarshalJSON(input []byte) error {
type StakingAsset struct {
StakingValue []*StakingValue `json:"stakingValue"`
Address *common.Address `json:"address"`
}
var dec StakingAsset
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.StakingValue == nil {
return errors.New("missing required field 'stakingValue' for LockedAsset")
}
s.StakingValue = dec.StakingValue
if dec.Address != nil {
s.Address = *dec.Address
}
return nil
}
func (i *ImpawnImpl) GetLockedAssetRPC(addr common.Address, height uint64) []LockedAsset {
ls := i.GetLockedAsset2(addr, height)
var attrs []LockedAsset
for key, value := range ls {
attr := LockedAsset{
LockValue: lockValueDisplay(value),
Address: key,
}
attrs = append(attrs, attr)
}
return attrs
}
type LockedAsset struct {
LockValue []*LockValue `json:"lockValue"`
Address common.Address `json:"address"`
}
type LockValue struct {
EpochID uint64
Amount string
Height *big.Int
Locked bool
}
// MarshalJSON marshals as JSON.
func (l LockValue) MarshalJSON() ([]byte, error) {
type LockValue struct {
EpochID hexutil.Uint64 `json:"epochID"`
Amount string `json:"amount"`
Height *hexutil.Big `json:"height"`
Locked bool `json:"locked"`
}
var enc LockValue
enc.EpochID = hexutil.Uint64(l.EpochID)
enc.Amount = l.Amount
enc.Height = (*hexutil.Big)(l.Height)
enc.Locked = l.Locked
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (l *LockValue) UnmarshalJSON(input []byte) error {
type LockValue struct {
EpochID *hexutil.Uint64 `json:"epochID"`
Amount *string `json:"amount"`
Height *hexutil.Big `json:"height"`
Locked *bool `json:"locked"`
}
var dec LockValue
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.EpochID != nil {
l.EpochID = uint64(*dec.EpochID)
}
if dec.Amount != nil {
l.Amount = *dec.Amount
}
if dec.Height != nil {
l.Height = (*big.Int)(dec.Height)
}
if dec.Locked != nil {
l.Locked = *dec.Locked
}
return nil
}
// MarshalJSON marshals as JSON.
func (l LockedAsset) MarshalJSON() ([]byte, error) {
type LockedAsset struct {
LockValue []*LockValue `json:"lockValue"`
Address common.Address `json:"address"`
}
var enc LockedAsset
enc.LockValue = l.LockValue
enc.Address = l.Address
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (l *LockedAsset) UnmarshalJSON(input []byte) error {
type LockedAsset struct {
LockValue []*LockValue `json:"lockValue"`
Address *common.Address `json:"address"`
}
var dec LockedAsset
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.LockValue == nil {
return errors.New("missing required field 'lockValue' for LockedAsset")
}
l.LockValue = dec.LockValue
if dec.Address != nil {
l.Address = *dec.Address
}
return nil
}
func (i *ImpawnImpl) GetAllCancelableAssetRPC(addr common.Address) []CancelableAsset {
assets := i.GetAllCancelableAsset(addr)
var attrs []CancelableAsset
for key, value := range assets {
attr := CancelableAsset{Value: weitoCoin(value), Address: key}
attrs = append(attrs, attr)
}
return attrs
}
type CancelableAsset struct {
Value string
Address common.Address
}
// MarshalJSON marshals as JSON.
func (c CancelableAsset) MarshalJSON() ([]byte, error) {
type CancelableAsset struct {
Value string `json:"value"`
Address common.Address `json:"address"`
}
var enc CancelableAsset
enc.Value = c.Value
enc.Address = c.Address
return json.Marshal(&enc)
}
// UnmarshalJSON unmarshals from JSON.
func (c *CancelableAsset) UnmarshalJSON(input []byte) error {
type CancelableAsset struct {
Value *string `json:"value"`
Address *common.Address `json:"address"`
}
var dec CancelableAsset
if err := json.Unmarshal(input, &dec); err != nil {
return err
}
if dec.Value != nil {
c.Value = *dec.Value
}
if dec.Address != nil {
c.Address = *dec.Address
}
return nil
}
func (i *ImpawnImpl) GetStakingAccountRPC(height uint64, address common.Address) map[string]interface{} {
sas := i.GetAllStakingAccount()
sa := sas.getSA(address)
attr := make(map[string]interface{})
if sa == nil {
return nil
}
attr["unit"] = unitDisplay(sa.Unit)
attr["votePubKey"] = hexutil.Bytes(sa.Votepubkey)
attr["fee"] = sa.Fee.Uint64()
attr["committee"] = isCommitteeMember(i, sa.Unit.Address)
attr["delegation"] = daSDisplay(sa.Delegation, height)
if sa.Modify != nil {
ai := make(map[string]interface{})
if sa.Modify.Fee != nil {
ai["fee"] = sa.Modify.Fee.Uint64()
}
if sa.Modify.VotePubkey != nil {
ai["votePubKey"] = hexutil.Bytes(sa.Modify.VotePubkey)
}
attr["modify"] = ai
}
attr["staking"] = weitoCoin(sa.getAllStaking(height))
attr["validStaking"] = weitoCoin(sa.getValidStaking(height))
return attr
}
func isCommitteeMember(i *ImpawnImpl, address common.Address) bool {
sas := i.getElections3(i.curEpochID)
if sas == nil {
return false
}
impawns := SAImpawns(sas)
sa := impawns.getSA(address)
if sa == nil {
return false
}
return true
}
func daSDisplay(das []*DelegationAccount, height uint64) []map[string]interface{} {
var attrs []map[string]interface{}
for _, da := range das {
attr := make(map[string]interface{})
attr["saAddress"] = da.SaAddress.String()
attr["delegate"] = weitoCoin(da.getAllStaking(height))
attr["validDelegate"] = weitoCoin(da.getValidStaking(height))
attr["unit"] = unitDisplay(da.Unit)
attrs = append(attrs, attr)
}
return attrs
}
func unitDisplay(uint *impawnUnit) map[string]interface{} {
attr := make(map[string]interface{})
attr["address"] = uint.Address.String()
attr["value"] = pvSDisplay(uint.Value)
attr["redeemInfo"] = riSDisplay(uint.RedeemInof)
return attr
}
func pvSDisplay(pvs []*PairstakingValue) []map[string]interface{} {
var attrs []map[string]interface{}
for _, pv := range pvs {
attr := make(map[string]interface{})
attr["amount"] = weitoCoin(pv.Amount)
attr["height"] = pv.Height
attr["state"] = uint64(pv.State)
attrs = append(attrs, attr)
}
return attrs
}
func riSDisplay(ris []*RedeemItem) []map[string]interface{} {
var attrs []map[string]interface{}
for _, ri := range ris {
attr := make(map[string]interface{})
attr["amount"] = weitoCoin(ri.Amount)
attr["epochID"] = ri.EpochID
attr["state"] = uint64(ri.State)
attrs = append(attrs, attr)
}
return attrs
}
func lockValueDisplay(lv *types.LockedValue) []*LockValue {
attrs := make([]*LockValue, 0)
for epoch, value := range lv.Value {
attrs = append(attrs, &LockValue{
EpochID: epoch,
Amount: weitoCoin(value.Amount),
Height: new(big.Int).SetUint64(types.MinCalcRedeemHeight(epoch)),
Locked: value.Locked,
})
}
return attrs
}
func stakingValueDisplay(sv *types.StakingValue) []*StakingValue {
attrs := make([]*StakingValue, 0)
for height, value := range sv.Value {
attrs = append(attrs, &StakingValue{
Height: height,
Amount: weitoCoin(value),
})
}
return attrs
}
var (
baseUnit = new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
fbaseUnit = new(big.Float).SetFloat64(float64(baseUnit.Int64()))
)
func weitoCoin(val *big.Int) string {
return new(big.Float).Quo(new(big.Float).SetInt(val), fbaseUnit).Text('f', 8)
}