-
Notifications
You must be signed in to change notification settings - Fork 9
/
miner_base.go
642 lines (565 loc) · 18.4 KB
/
miner_base.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// Copyright (C) 2019 ZVChain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package core
import (
"bytes"
"fmt"
"github.com/vmihailenco/msgpack"
"github.com/zvchain/zvchain/common"
"github.com/zvchain/zvchain/log"
"github.com/zvchain/zvchain/middleware/types"
"github.com/zvchain/zvchain/storage/account"
)
const (
MinMinerStake = 500 * common.ZVC // minimal token of miner can stake
initMaxMinerStakeAddAmount = 1000000 * common.ZVC //init stake adjust amount of token
maxMinerStakeAddAdjustPeriod = 30000000 //
initMaxMinerStake = 2500000 * common.ZVC
MaxMinerStakeAdjustPeriod = 10000000 // maximal token of miner can stake
stakeAdjustTimes = 12 // stake adjust times
initMinerPoolTickets = 8 // init miner pool need tickets
minMinerPoolTickets = 1 // minimal miner pool need tickets
minerPoolReduceCount = 2 // every reduce tickets count
)
type FundGuardType byte
const (
normalNodeType FundGuardType = iota
fullStakeGuardNodeType
fundGuardNodeType
)
// minimumStake shows miner can stake the min value
func minimumStake() uint64 {
return MinMinerStake
}
// maximumStake shows miner can stake the max value
func maximumStake(height uint64) uint64 {
canStake := uint64(initMaxMinerStake)
period := height / MaxMinerStakeAdjustPeriod
if height > stakeAdjustTimes*MaxMinerStakeAdjustPeriod {
period = stakeAdjustTimes
height = stakeAdjustTimes * MaxMinerStakeAdjustPeriod
}
for i := uint64(0); i < period; i++ {
canStake += initMaxMinerStakeAddAmount >> (i * MaxMinerStakeAdjustPeriod / maxMinerStakeAddAdjustPeriod)
}
return canStake
}
// miner pool valid tickets
func getValidTicketsByHeight(height uint64) uint64 {
reduce := height / (adjustWeightPeriod * 3)
reduceTickets := reduce * minerPoolReduceCount
if initMinerPoolTickets <= reduceTickets {
return minMinerPoolTickets
}
return initMinerPoolTickets - reduceTickets
}
type fundGuardNode struct {
Type FundGuardType
Height uint64 // Operation height
FundModeType common.FundModeType //default 6+6
}
type fundGuardNodeDetail struct {
Address common.Address
*fundGuardNode
}
type stakeDetail struct {
Value uint64 // Stake operation amount
Height uint64 // Operation height
DisMissHeight uint64 // Stake end height
MarkNotFullHeight uint64 // Mark the height when stake is not full
}
type voteInfo struct {
Target common.Address // vote target addr,default empty adress
Height uint64 // Operation height
}
func NewFundGuardNode() *fundGuardNode {
return &fundGuardNode{
Type: fundGuardNodeType,
Height: 0,
FundModeType: common.SIXAddSix,
}
}
func (f *fundGuardNode) isSixAddSix() bool {
return f.FundModeType == common.SIXAddSix
}
func (f *fundGuardNode) isSixAddFive() bool {
return f.FundModeType == common.SIXAddFive
}
func (f *fundGuardNode) isFundGuard() bool {
return f.Type == fundGuardNodeType
}
// if type == normalNodeType,means this fund guard node is invalid,if 6+5 or 6+6 expired will set this value
// if type == fullStakeGuardNodeType means this fund guard becomes full stake fund node before invalid
// if type == fundGuardNodeType means this fund guard node is init status
func (f *fundGuardNode) isGuardNode() bool {
return f.Type != normalNodeType
}
func (f *fundGuardNode) isNormal() bool {
return f.Type == normalNodeType
}
func (f *fundGuardNode) isFullStakeGuardNode() bool {
return f.Type == fullStakeGuardNodeType
}
func checkCanVote(voteHeight, currentHeight uint64) bool {
if voteHeight == 0 {
return true
}
voteRound := voteHeight / (adjustWeightPeriod / 2)
currentRound := currentHeight / (adjustWeightPeriod / 2)
return currentRound > voteRound
}
func newVoteInfo() *voteInfo {
return &voteInfo{
Target: common.Address{},
Height: 0,
}
}
func getDetailKey(address common.Address, typ types.MinerType, status types.StakeStatus) []byte {
buf := bytes.NewBuffer([]byte{})
buf.Write(common.PrefixDetail)
buf.Write(address.Bytes())
buf.WriteByte(byte(typ))
buf.WriteByte(byte(status))
return buf.Bytes()
}
func parseDetailKey(key []byte) (common.Address, types.MinerType, types.StakeStatus) {
reader := bytes.NewReader(key)
detail := make([]byte, len(common.PrefixDetail))
n, err := reader.Read(detail)
if err != nil || n != len(common.PrefixDetail) {
panic(fmt.Errorf("parse detail key error:%v", err))
}
addrBytes := make([]byte, len(common.Address{}))
n, err = reader.Read(addrBytes)
if err != nil || n != len(addrBytes) {
panic(fmt.Errorf("parse detail key error:%v", err))
}
mtByte, err := reader.ReadByte()
if err != nil {
panic(fmt.Errorf("parse detail key error:%v", err))
}
stByte, err := reader.ReadByte()
if err != nil {
panic(fmt.Errorf("parse detail key error:%v", err))
}
return common.BytesToAddress(addrBytes), types.MinerType(mtByte), types.StakeStatus(stByte)
}
func setPks(miner *types.Miner, pks *types.MinerPks) *types.Miner {
if len(pks.Pk) > 0 {
miner.PublicKey = pks.Pk
}
if len(pks.VrfPk) > 0 {
miner.VrfPublicKey = pks.VrfPk
}
return miner
}
// checkCanActivate if status can be set to types.MinerStatusActive
func checkCanActivate(miner *types.Miner) bool {
// pks not completed
if !miner.PksCompleted() {
return false
}
// If the stake up to the lower bound, then activate the miner
return checkLowerBound(miner)
}
func checkUpperBound(miner *types.Miner, height uint64) bool {
return miner.Stake <= maximumStake(height)
}
func isFullStake(stake, height uint64) bool {
return stake == maximumStake(height)
}
func checkMinerPoolUpperBound(miner *types.Miner, height uint64) bool {
return miner.Stake <= getFullMinerPoolStake(height)
}
func getFullMinerPoolStake(height uint64) uint64 {
return maximumStake(height) * getValidTicketsByHeight(height)
}
func checkLowerBound(miner *types.Miner) bool {
return miner.Stake >= minimumStake()
}
func getMinerKey(typ types.MinerType) []byte {
buf := bytes.NewBuffer(common.PrefixMiner)
buf.WriteByte(byte(typ))
return buf.Bytes()
}
func getPoolKey(prefix []byte, address common.Address) []byte {
buf := bytes.NewBuffer(prefix)
buf.Write(address.Bytes())
return buf.Bytes()
}
func getFundGuardKey(prefix []byte, address common.Address) []byte {
buf := bytes.NewBuffer(prefix)
buf.Write(address.Bytes())
return buf.Bytes()
}
func getMinerFromStateObject(db account.AccountDatabase, stateObject account.AccAccesser, mType types.MinerType) (*types.Miner, error) {
data := stateObject.GetData(db, getMinerKey(mType))
if data != nil && len(data) > 0 {
var miner types.Miner
err := msgpack.Unmarshal(data, &miner)
if err != nil {
return nil, err
}
return &miner, nil
}
return nil, nil
}
func getMiner(db types.AccountDB, address common.Address, mType types.MinerType) (*types.Miner, error) {
data := db.GetData(address, getMinerKey(mType))
if data != nil && len(data) > 0 {
var miner types.Miner
err := msgpack.Unmarshal(data, &miner)
if err != nil {
return nil, err
}
return &miner, nil
}
return nil, nil
}
func setMiner(db types.AccountDB, miner *types.Miner) error {
bs, err := msgpack.Marshal(miner)
if err != nil {
return err
}
db.SetData(common.BytesToAddress(miner.ID), getMinerKey(miner.Type), bs)
return nil
}
func parseDetail(value []byte) (*stakeDetail, error) {
var detail stakeDetail
err := msgpack.Unmarshal(value, &detail)
if err != nil {
return nil, err
}
return &detail, nil
}
func getDetail(db types.AccountDB, address common.Address, detailKey []byte) (*stakeDetail, error) {
data := db.GetData(address, detailKey)
if data != nil && len(data) > 0 {
return parseDetail(data)
}
return nil, nil
}
func setDetail(db types.AccountDB, address common.Address, detailKey []byte, sd *stakeDetail) error {
bs, err := msgpack.Marshal(sd)
if err != nil {
return err
}
db.SetData(address, detailKey, bs)
return nil
}
func getTotalTickets(db types.AccountDB, key []byte) uint64 {
totalTicketsBytes := db.GetData(common.MinerPoolTicketsAddr, key)
totalTickets := uint64(0)
if len(totalTicketsBytes) > 0 {
totalTickets = common.ByteToUInt64(totalTicketsBytes)
}
return totalTickets
}
func getTicketsKey(address common.Address) []byte {
buf := bytes.NewBuffer([]byte{})
buf.Write(common.KeyTickets)
buf.Write(address.Bytes())
return buf.Bytes()
}
func getProposalTotalStake(db types.AccountDB) uint64 {
totalStakeBytes := db.GetData(common.MinerPoolAddr, common.KeyPoolProposalTotalStake)
totalStake := uint64(0)
if len(totalStakeBytes) > 0 {
totalStake = common.ByteToUInt64(totalStakeBytes)
}
return totalStake
}
func setFundGuardNode(db types.AccountDB, address common.Address, fn *fundGuardNode) error {
bs, err := msgpack.Marshal(fn)
if err != nil {
return err
}
key := getFundGuardKey(common.KeyGuardNodes, address)
db.SetData(common.FundGuardNodeAddr, key, bs)
return nil
}
func addFundGuardPool(db types.AccountDB, address common.Address) error {
fg := NewFundGuardNode()
err := setFundGuardNode(db, address, fg)
return err
}
func getFundGuardNode(db types.AccountDB, address common.Address) (*fundGuardNode, error) {
key := getFundGuardKey(common.KeyGuardNodes, address)
bts := db.GetData(common.FundGuardNodeAddr, key)
if bts == nil {
return nil, nil
}
var fn fundGuardNode
err := msgpack.Unmarshal(bts, &fn)
if err != nil {
return nil, err
}
return &fn, nil
}
func hasScanedSixAddFiveFundGuards(db types.AccountDB) bool {
bts := db.GetData(common.ScanAllFundGuardStatusAddr, common.KeyScanSixAddFiveNodes)
if bts == nil {
return false
}
return true
}
func markScanedSixAddFiveFundGuards(db types.AccountDB) {
db.SetData(common.ScanAllFundGuardStatusAddr, common.KeyScanSixAddFiveNodes, []byte{1})
}
func hasScanedSixAddSixFundGuards(db types.AccountDB) bool {
bts := db.GetData(common.ScanAllFundGuardStatusAddr, common.KeyScanSixAddSixNodes)
if bts == nil {
return false
}
return true
}
func markScanedSixAddSixFundGuards(db types.AccountDB) {
db.SetData(common.ScanAllFundGuardStatusAddr, common.KeyScanSixAddSixNodes, []byte{1})
}
func updateFundGuardMode(db types.AccountDB, fn *fundGuardNode, address common.Address, mode common.FundModeType, height uint64) error {
fn.Height = height
fn.FundModeType = mode
err := setFundGuardNode(db, address, fn)
return err
}
func updateFundGuardPoolStatus(db types.AccountDB, address common.Address, fnType FundGuardType, height uint64) error {
fn, err := getFundGuardNode(db, address)
if err != nil {
return nil
}
if fn == nil {
return fmt.Errorf("fund guard info is nil,addr is %s", address.String())
}
fn.Height = height
fn.Type = fnType
err = setFundGuardNode(db, address, fn)
return err
}
func addFullStakeGuardPool(db types.AccountDB, address common.Address) {
key := getFundGuardKey(common.KeyGuardNodes, address)
db.SetData(common.FullStakeGuardNodeAddr, key, []byte{1})
}
func isInFullStakeGuardNode(db types.AccountDB, address common.Address) bool {
key := getFundGuardKey(common.KeyGuardNodes, address)
return db.GetData(common.FullStakeGuardNodeAddr, key) != nil
}
func removeFullStakeGuardNodeFromPool(db types.AccountDB, address common.Address) {
key := getFundGuardKey(common.KeyGuardNodes, address)
db.RemoveData(common.FullStakeGuardNodeAddr, key)
}
func setVoteInfo(db types.AccountDB, address common.Address, vf *voteInfo) error {
bs, err := msgpack.Marshal(vf)
if err != nil {
return err
}
db.SetData(address, common.KeyVote, bs)
return nil
}
func delVoteInfo(db types.AccountDB, address common.Address) {
db.RemoveData(address, common.KeyVote)
}
func initVoteInfo(db types.AccountDB, address common.Address) (*voteInfo, error) {
vote := newVoteInfo()
bs, err := msgpack.Marshal(vote)
if err != nil {
return nil, err
}
db.SetData(address, common.KeyVote, bs)
return vote, nil
}
func getVoteInfo(db types.AccountDB, address common.Address) (*voteInfo, error) {
data := db.GetData(address, common.KeyVote)
if data == nil {
return nil, nil
}
var vf voteInfo
err := msgpack.Unmarshal(data, &vf)
if err != nil {
return nil, err
}
return &vf, nil
}
func guardNodeExpired(db types.AccountDB, address common.Address, height uint64, isFundGuardNode bool) error {
miner, err := getMiner(db, address, types.MinerTypeProposal)
if err != nil {
return err
}
if miner == nil {
return fmt.Errorf("guard invalid find miner is nil,addr is %s", address.String())
}
miner.UpdateIdentity(types.MinerNormal, height)
err = setMiner(db, miner)
if err != nil {
return err
}
// fund guard node is not in this pool,only full stake node in this pool
if !isFundGuardNode {
removeFullStakeGuardNodeFromPool(db, address)
log.CoreLogger.Infof("full stake expired,addr is %s,height = %v", address.String(), height)
} else {
log.CoreLogger.Infof("fund stake expired,addr is %s,height = %v", address.String(), height)
}
vf, err := getVoteInfo(db, address)
if err != nil {
return err
}
if vf != nil {
delVoteInfo(db, address)
var empty = common.Address{}
if vf.Target != empty {
mop := newReduceTicketsOp(db, vf.Target, height)
ret := mop.Transition()
if ret.err != nil {
return ret.err
}
}
}
return nil
}
func subProposalTotalStake(db types.AccountDB, subStake uint64) {
totalStake := getProposalTotalStake(db)
// Must not happen
if totalStake < subStake {
panic("total stake less than sub stake")
}
newTotalStake := totalStake - subStake
db.SetData(common.MinerPoolAddr, common.KeyPoolProposalTotalStake, common.Uint64ToByte(newTotalStake))
log.CoreLogger.Infof("total stake changed,reduce total stake,total stake is %v", newTotalStake)
}
func removeFromPool(db types.AccountDB, minerType types.MinerType, address common.Address, stake uint64) {
var key []byte
if types.IsProposalRole(minerType) {
key = getPoolKey(common.PrefixPoolProposal, address)
totalStakeBytes := db.GetData(common.MinerPoolAddr, common.KeyPoolProposalTotalStake)
totalStake := uint64(0)
if len(totalStakeBytes) > 0 {
totalStake = common.ByteToUInt64(totalStakeBytes)
}
if totalStake < stake {
panic(fmt.Errorf("totalStake less than stake: %v %v", totalStake, stake))
}
newTotalStake := totalStake - stake
db.SetData(common.MinerPoolAddr, common.KeyPoolProposalTotalStake, common.Uint64ToByte(newTotalStake))
log.CoreLogger.Infof("total stake changed,remove from pool,total stake is %v", newTotalStake)
} else if types.IsVerifyRole(minerType) {
key = getPoolKey(common.PrefixPoolVerifier, address)
}
log.CoreLogger.Infof("remove from pool,addr is %s,type is %d", address.String(), minerType)
db.RemoveData(common.MinerPoolAddr, key)
}
func addProposalTotalStake(db types.AccountDB, addStake uint64) {
totalStake := getProposalTotalStake(db)
// Must not happen
if addStake+totalStake < totalStake {
panic(fmt.Errorf("total stake overflow:%v %v", addStake, totalStake))
}
newTotalStake := addStake + totalStake
db.SetData(common.MinerPoolAddr, common.KeyPoolProposalTotalStake, common.Uint64ToByte(newTotalStake))
log.CoreLogger.Infof("total stake changed,add stake,current total stake is %v", newTotalStake)
}
func removeDetail(db types.AccountDB, address common.Address, detailKey []byte) {
db.RemoveData(address, detailKey)
}
func initMiner(op *stakeAddOp) *types.Miner {
miner := &types.Miner{
ID: op.addTarget.Bytes(),
Stake: op.value,
ApplyHeight: op.height,
Type: op.minerType,
Status: types.MinerStatusPrepare,
}
return miner
}
func addToPool(db types.AccountDB, minerType types.MinerType, address common.Address, addStake uint64) {
var key []byte
if types.IsProposalRole(minerType) {
key = getPoolKey(common.PrefixPoolProposal, address)
addProposalTotalStake(db, addStake)
} else if types.IsVerifyRole(minerType) {
key = getPoolKey(common.PrefixPoolVerifier, address)
}
db.SetData(common.MinerPoolAddr, key, []byte{1})
}
func addTicket(db types.AccountDB, address common.Address) uint64 {
key := getTicketsKey(address)
totalTickets := getTotalTickets(db, key)
totalTickets += 1
db.SetData(common.MinerPoolTicketsAddr, key, common.Uint64ToByte(totalTickets))
return totalTickets
}
func subTicket(db types.AccountDB, address common.Address) uint64 {
key := getTicketsKey(address)
totalTickets := getTotalTickets(db, key)
if totalTickets < 1 {
totalTickets = 0
} else {
totalTickets -= 1
}
db.SetData(common.MinerPoolTicketsAddr, key, common.Uint64ToByte(totalTickets))
return totalTickets
}
func getTickets(db types.AccountDB, address common.Address) uint64 {
key := getTicketsKey(address)
return getTotalTickets(db, key)
}
func processVote(op *voteMinerPoolOp, vf *voteInfo) (error, bool, types.ReceiptStatus) {
var err error
if vf == nil {
vf, err = initVoteInfo(op.accountDB, op.source)
if err != nil {
return err, false, types.RSFail
}
}
oldTarget := vf.Target
// set vote false
err = voteMinerPool(op.accountDB, vf, op.source, op.targetAddr, op.height)
if err != nil {
return err, false, types.RSFail
}
var totalTickets uint64 = 0
var empty = common.Address{}
// vote target is old target
if oldTarget == op.targetAddr && oldTarget != empty {
totalTickets = getTickets(op.accountDB, op.targetAddr)
} else {
if oldTarget != empty {
//reduce ticket first
mop := newReduceTicketsOp(op.accountDB, oldTarget, op.height)
ret := mop.Transition()
if ret.err != nil {
return ret.err, false, ret.transitionStatus
}
}
// add tickets count
totalTickets = addTicket(op.accountDB, op.targetAddr)
}
log.CoreLogger.Infof("vote success,source = %s,target =%s,current tickets = %d,height = %v", op.source, op.targetAddr, totalTickets, op.height)
isFull := isFullTickets(totalTickets, op.height)
return nil, isFull, types.RSSuccess
}
func isFullTickets(totalTickets uint64, height uint64) bool {
needTickets := getValidTicketsByHeight(height)
return totalTickets >= needTickets
}
func voteMinerPool(db types.AccountDB, vf *voteInfo, source, targetAddress common.Address, height uint64) error {
vf.Height = height
vf.Target = targetAddress
bs, err := msgpack.Marshal(vf)
if err != nil {
return err
}
db.SetData(source, common.KeyVote, bs)
return nil
}