/
tribe.go
803 lines (728 loc) · 27.4 KB
/
tribe.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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
// Copyright 2018 The Spectrum Authors
package tribe
import (
"bytes"
"encoding/hex"
"errors"
"math/big"
"math/rand"
"time"
"crypto/ecdsa"
"fmt"
"github.com/SmartMeshFoundation/Spectrum/accounts"
"github.com/SmartMeshFoundation/Spectrum/accounts/abi/bind"
"github.com/SmartMeshFoundation/Spectrum/common"
"github.com/SmartMeshFoundation/Spectrum/common/math"
"github.com/SmartMeshFoundation/Spectrum/consensus"
"github.com/SmartMeshFoundation/Spectrum/consensus/misc"
"github.com/SmartMeshFoundation/Spectrum/core/state"
"github.com/SmartMeshFoundation/Spectrum/core/types"
"github.com/SmartMeshFoundation/Spectrum/crypto"
"github.com/SmartMeshFoundation/Spectrum/crypto/sha3"
"github.com/SmartMeshFoundation/Spectrum/ethdb"
"github.com/SmartMeshFoundation/Spectrum/log"
"github.com/SmartMeshFoundation/Spectrum/params"
"github.com/SmartMeshFoundation/Spectrum/rlp"
"github.com/SmartMeshFoundation/Spectrum/rpc"
lru "github.com/hashicorp/golang-lru"
)
// sigHash returns the hash which is used as input for the proof-of-authority
// signing. It is the hash of the entire header apart from the 65 byte signature
// contained at the end of the extra data.
//
// Note, the method requires the extra data to be at least 65 bytes, otherwise it
// panics. This is done to avoid accidentally using both forms (signature present
// or not), which could be abused to produce different hashes for the same header.
func sigHash(header *types.Header) (hash common.Hash) {
hasher := sha3.NewKeccak256()
err := rlp.Encode(hasher, []interface{}{
header.ParentHash,
header.UncleHash,
header.Coinbase,
header.Root,
header.TxHash,
header.ReceiptHash,
header.Bloom,
header.Difficulty,
header.Number,
header.GasLimit,
header.GasUsed,
header.Time,
header.Extra[:len(header.Extra)-65], // Yes, this will panic if extra is too short
header.MixDigest,
header.Nonce,
})
if err != nil {
panic(err)
}
hasher.Sum(hash[:0])
return hash
}
func ecrecoverPubkey(header *types.Header, signature []byte) ([]byte, error) {
pubkey, err := crypto.Ecrecover(sigHash(header).Bytes(), signature)
return pubkey, err
}
// ecrecover extracts the Ethereum account address from a signed header.
func ecrecover(header *types.Header, t *Tribe) (common.Address, error) {
// XXXX : 掐头去尾 ,约定创世区块只能指定一个签名人,因为第一个块要部署合约
extraVanity := extraVanityFn(header.Number)
if header.Number.Uint64() == 0 {
signer := common.Address{}
copy(signer[:], header.Extra[extraVanity:])
t.Status.loadSigners([]*Signer{{signer, 3}})
return signer, nil
}
sigcache := t.sigcache
// If the signature's already cached, return that
hash := header.Hash()
if sigcache != nil {
if address, known := sigcache.Get(hash); known {
return address.(common.Address), nil
}
}
// Retrieve the signature from the header extra-data
if len(header.Extra) < extraSeal {
return common.Address{}, errMissingSignature
}
// Recover the public key and the Ethereum address
pubkey, err := ecrecoverPubkey(header, header.Extra[len(header.Extra)-extraSeal:])
//pubkey, err := crypto.Ecrecover(sigHash(header).Bytes(), signature)
if err != nil {
return common.Address{}, err
}
var signer common.Address
copy(signer[:], crypto.Keccak256(pubkey[1:])[12:])
if sigcache != nil {
sigcache.Add(hash, signer)
}
return signer, nil
}
// signers set to the ones provided by the user.
func New(accman *accounts.Manager, config *params.TribeConfig, _ ethdb.Database) *Tribe {
status := NewTribeStatus()
sigcache, err := lru.NewARC(historyLimit)
if err != nil {
panic(err)
}
conf := *config
if conf.Period <= 0 {
conf.Period = blockPeriod
}
tribe := &Tribe{
accman: accman,
config: &conf,
Status: status,
sigcache: sigcache,
}
status.SetTribe(tribe)
return tribe
}
func (t *Tribe) Init() {
go func() {
t.lock.Lock()
defer t.lock.Unlock()
if t.isInit {
return
}
<-params.InitTribeStatus
rtn := params.SendToMsgBox("GetNodeKey")
success := <-rtn
t.Status.nodeKey = success.Entity.(*ecdsa.PrivateKey)
if params.InitTribe != nil {
close(params.InitTribe)
}
t.isInit = true
log.Info("init tribe.status success.")
}()
}
func (t *Tribe) GetConfig() *params.TribeConfig {
return t.config
}
func (t *Tribe) SetConfig(config *params.TribeConfig) {
t.config = config
}
// Author implements consensus.Engine, returning the Ethereum address recovered
// from the signature in the header's extra-data section.
func (t *Tribe) Author(header *types.Header) (common.Address, error) {
var (
coinbase common.Address
err error
)
if header.Coinbase == coinbase {
coinbase, err = ecrecover(header, t)
} else {
coinbase = header.Coinbase
}
log.Debug("<<Tribe.Author>>", "num", header.Number, "coinbase", coinbase.Hex())
return coinbase, err
}
// VerifyHeader checks whether a header conforms to the consensus rules.
func (t *Tribe) VerifyHeader(chain consensus.ChainReader, header *types.Header, seal bool) error {
err := t.verifyHeader(chain, header, nil)
if err == nil {
//p := chain.GetHeaderByHash(header.ParentHash)
//t.Status.LoadSignersFromChief(p.Hash(), p.Number)
}
return err
}
// VerifyHeaders is similar to VerifyHeader, but verifies a batch of headers. The
// method returns a quit channel to abort the operations and a results channel to
// retrieve the async verifications (the order is that of the input slice).
func (t *Tribe) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, seals []bool) (chan<- struct{}, <-chan error) {
abort := make(chan struct{})
results := make(chan error)
log.Debug("==> VerifyHeaders ", "currentNum", chain.CurrentHeader().Number.Int64(), "headers.len", len(headers))
go func() {
for i, header := range headers {
err := t.verifyHeader(chain, header, headers[:i])
select {
case <-abort:
return
case results <- err:
}
}
}()
return abort, results
}
// verifyHeader checks whether a header conforms to the consensus rules.The
// caller may optionally pass in a batch of parents (ascending order) to avoid
// looking those up from the database. This is useful for concurrently verifying
// a batch of new headers.
func (t *Tribe) verifyHeader(chain consensus.ChainReader, header *types.Header, parents []*types.Header) (err error) {
defer func() {
if err != nil {
log.Info(fmt.Sprintf("verifyHeader err return number=%s,err=%s", header.Number, err))
}
}()
if header.Number == nil {
return errUnknownBlock
}
extraVanity := extraVanityFn(header.Number)
number := header.Number.Uint64()
// Don't waste time checking blocks from the future
if header.Time.Cmp(big.NewInt(time.Now().Unix())) > 0 {
return consensus.ErrFutureBlock
}
// Nonces must be 0x00..0 or 0xff..f, zeroes enforced on checkpoints
if !bytes.Equal(header.Nonce[:], nonceSync) && !bytes.Equal(header.Nonce[:], nonceAsync) {
return errInvalidNonce
}
// Check that the extra-data contains both the vanity and signature
if len(header.Extra) < extraVanity {
return errMissingVanity
}
if len(header.Extra) < extraVanity+extraSeal {
return errMissingSignature
}
// Ensure that the mix digest is zero as we don't have fork protection currently
if header.MixDigest != (common.Hash{}) {
return errInvalidMixDigest
}
// Ensure that the block doesn't contain any uncles which are meaningless in PoA
if header.UncleHash != uncleHash {
return errInvalidUncleHash
}
// Ensure that the block's difficulty is meaningful (may not be correct at this point)
if number > 0 && !params.IsBeforeChief100block(header.Number) {
if ci := params.GetChiefInfo(header.Number); ci != nil {
switch ci.Version {
case "1.0.0":
//TODO max value is a var ???
if header.Difficulty == nil || header.Difficulty.Cmp(big.NewInt(6)) > 0 || header.Difficulty.Cmp(diffNoTurn) < 0 {
log.Error("** verifyHeader ERROR CHIEF.v1.0.0 **", "diff", header.Difficulty.String(), "err", errInvalidDifficulty)
return errInvalidDifficulty
}
default:
if header.Difficulty == nil || (header.Difficulty.Cmp(diffInTurnMain) != 0 && header.Difficulty.Cmp(diffInTurn) != 0 && header.Difficulty.Cmp(diffNoTurn) != 0) {
log.Error("** verifyHeader ERROR **", "diff", header.Difficulty.String(), "err", errInvalidDifficulty)
return errInvalidDifficulty
}
}
}
}
// If all checks passed, validate any special fields for hard forks
if err := misc.VerifyForkHashes(chain.Config(), header, false); err != nil {
return err
}
// All basic checks passed, verify cascading fields
err = t.verifyCascadingFields(chain, header, parents)
if err != nil {
log.Error("verifyCascadingFields", "num", header.Number.Int64(), "err", err)
}
return err
}
// verifyCascadingFields verifies all the header fields that are not standalone,
// rather depend on a batch of previous headers. The caller may optionally pass
// in a batch of parents (ascending order) to avoid looking those up from the
// database. This is useful for concurrently verifying a batch of new headers.
func (t *Tribe) verifyCascadingFields(chain consensus.ChainReader, header *types.Header, parents []*types.Header) (err error) {
// The genesis block is the always valid dead-end
number := header.Number.Uint64()
if number == 0 {
return nil
}
// Ensure that the block's timestamp isn't too close to it's parent
var parent *types.Header
verifyTime := func() error {
if params.IsSIP002Block(header.Number) {
// first verification
// second verification block time in validateSigner function
// the min limit period is config.Period - 1
if parent.Time.Uint64()+(t.config.Period-1) > header.Time.Uint64() {
return ErrInvalidTimestampSIP002
}
} else {
if parent.Time.Uint64()+t.config.Period > header.Time.Uint64() {
return ErrInvalidTimestamp
}
}
return nil
}
if len(parents) > 0 {
parent = parents[len(parents)-1]
if err := verifyTime(); err != nil {
return err
}
} else {
parent = chain.GetHeader(header.ParentHash, number-1)
if parent == nil || parent.Time == nil {
e := errors.New(fmt.Sprintf("nil_parent_current_num=%d", header.Number.Int64()))
log.Error("-->bad_block-->", "err", e)
return e
}
if err := verifyTime(); err != nil {
return err
}
}
if parent == nil || parent.Number.Uint64() != number-1 || parent.Hash() != header.ParentHash {
return consensus.ErrUnknownAncestor
}
/* TODO timestamp unit second , how to do this logic ?
if header.Difficulty.Cmp(diffNoTurn) == 0 && parent.Time.Uint64()+t.config.Period == header.Time.Uint64() {
return ErrInvalidTimestamp
}
*/
// Verify that the gas limit is <= 2^63-1
if header.GasLimit.Cmp(math.MaxBig63) > 0 {
return fmt.Errorf("invalid gasLimit: have %v, max %v", header.GasLimit, math.MaxBig63)
}
// Verify that the gasUsed is <= gasLimit
if header.GasUsed.Cmp(header.GasLimit) > 0 {
return fmt.Errorf("invalid gasUsed: have %v, gasLimit %v", header.GasUsed, header.GasLimit)
}
// Verify that the gas limit remains within allowed bounds
diff := new(big.Int).Set(parent.GasLimit)
diff = diff.Sub(diff, header.GasLimit)
diff.Abs(diff)
limit := new(big.Int).Set(parent.GasLimit)
limit = limit.Div(limit, params.GasLimitBoundDivisor)
minGasLimit := params.MinGasLimit
/*
//sip004区块硬分叉开始,提升区块最小的gaslimit
sip004Block := params.MainnetChainConfig.Sip004Block
if params.IsTestnet() {
sip004Block = params.TestnetChainConfig.Sip004Block
} else if params.IsDevnet() {
sip004Block = params.DevnetChainConfig.Sip004Block
}
if header.Number.Cmp(sip004Block) >= 0 {
minGasLimit = params.Sip004GasLimit
}
if header.Number.Cmp(sip004Block) != 0 && (diff.Cmp(limit) >= 0 || header.GasLimit.Cmp(minGasLimit) < 0) {
return fmt.Errorf("invalid gas limit: have %v, want %v += %v", header.GasLimit, parent.GasLimit, limit)
}
*/
if params.IsSIP004Block(header.Number) {
minGasLimit = params.Sip004GasLimit
}
if !params.EqualSIP004Block(header.Number) && (diff.Cmp(limit) >= 0 || header.GasLimit.Cmp(minGasLimit) < 0) {
return fmt.Errorf("invalid gas limit: have %v, want %v += %v", header.GasLimit, parent.GasLimit, limit)
}
// Verify that the block number is parent's +1
if diff := new(big.Int).Sub(header.Number, parent.Number); diff.Cmp(big.NewInt(1)) != 0 {
return consensus.ErrInvalidNumber
}
return
}
// VerifyUncles implements consensus.Engine, always returning an error for any
// uncles as this consensus mechanism doesn't permit uncles.
func (t *Tribe) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
if len(block.Uncles()) > 0 {
return errors.New("uncles not allowed")
}
return nil
}
// VerifySeal implements consensus.Engine, checking whether the signature contained
// in the header satisfies the consensus protocol requirements.
// don't support remote miner agent, these code never reached
func (t *Tribe) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
panic("never reach")
e := t.verifySeal(chain, header, nil)
if e != nil {
log.Error("Tribe.VerifySeal", "err", e)
}
return e
}
func (t *Tribe) verifySeal(chain consensus.ChainReader, header *types.Header, parents []*types.Header) error {
// Verifying the genesis block is not supported
number := header.Number.Int64()
if number == 0 {
return errUnknownBlock
}
// Resolve the authorization key and check against signers
signer, err := ecrecover(header, t)
if err != nil {
return err
}
log.Debug("verifySeal", "number", number, "signer", signer.Hex())
if !t.Status.validateSigner(chain.GetHeaderByHash(header.ParentHash), header, signer) {
return errUnauthorized
}
if number > CHIEF_NUMBER && !params.IsBeforeChief100block(header.Number) {
difficulty := t.Status.InTurnForVerifyDiffculty(number, header.ParentHash, signer)
if difficulty.Cmp(header.Difficulty) != 0 {
log.Error("** verifySeal ERROR **", "diff", header.Difficulty.String(), "err", errInvalidDifficulty)
return errInvalidDifficulty
}
}
return nil
}
// Prepare implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top.
func (t *Tribe) Prepare(chain consensus.ChainReader, header *types.Header) error {
//t.Status.LoadStatusFromChief(header.ParentHash, header.Number)
err := t.Status.LoadStatusFromChief(header.ParentHash, header.Number)
if err != nil {
return err
}
number := header.Number.Uint64()
if f, _, err := params.AnmapBindInfo(t.Status.GetMinerAddress(), chain.CurrentHeader().Hash()); err == nil && f != common.HexToAddress("0x") {
header.Coinbase = f
} else {
header.Coinbase = t.Status.GetMinerAddress()
}
header.Nonce = types.BlockNonce{}
copy(header.Nonce[:], nonceAsync)
// Extra : append sig to last 65 bytes >>>>
if params.IsSIP100Block(header.Number) {
header.Extra = make([]byte, _extraVrf+extraSeal)
// append vrf to header.Extra before sign
parentHeader := chain.GetHeaderByHash(header.ParentHash)
msg := append(parentHeader.Number.Bytes(), parentHeader.Extra[:32]...)
vrfnp, err := crypto.SimpleVRF2Bytes(t.Status.nodeKey, msg)
log.Debug("Tribe.Prepare --> params.GetVRFByHash", "err", err, "hash", header.ParentHash.Hex(), "vrfn", hex.EncodeToString(vrfnp[:32]))
//vr, err := crypto.SimpleVRF2Bytes(t.Status.nodeKey, header.ParentHash.Bytes())
if err != nil {
panic(err)
}
copy(header.Extra[:len(header.Extra)-extraSeal], vrfnp)
log.Debug("prepare_vrf", "num", header.Number, "parent_hash", header.ParentHash.Hex())
log.Debug("prepare_vrf", "num", header.Number, "miner", crypto.PubkeyToAddress(t.Status.nodeKey.PublicKey).Hex())
log.Debug("prepare_vrf", "num", header.Number, "err", err, "vrf", hex.EncodeToString(vrfnp))
log.Debug("prepare_vrf", "num", header.Number, "extra", hex.EncodeToString(header.Extra))
} else {
extraVanity := extraVanityFn(header.Number)
log.Debug("fix extra", "extra-len", len(header.Extra), "extraVanity", extraVanity)
if len(header.Extra) < extraVanity {
header.Extra = append(header.Extra, bytes.Repeat([]byte{0x00}, extraVanity-len(header.Extra))...)
}
header.Extra = header.Extra[:extraVanity]
header.Extra = append(header.Extra, make([]byte, extraSeal)...)
}
// Extra : append sig to last 65 bytes <<<<
// Mix digest is reserved for now, set to empty
header.MixDigest = common.Hash{}
// Ensure the timestamp has the correct delay
parent := chain.GetHeader(header.ParentHash, number-1)
if parent == nil {
return consensus.ErrUnknownAncestor
}
// Set the correct difficulty
header.Difficulty = t.CalcDifficulty(chain, header.Time.Uint64(), parent)
if params.IsSIP002Block(header.Number) {
//modify by liangc : change period rule
header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(t.GetPeriod(header, nil)))
} else {
header.Time = new(big.Int).Add(parent.Time, new(big.Int).SetUint64(t.config.Period))
}
if header.Time.Int64() < time.Now().Unix() {
header.Time = big.NewInt(time.Now().Unix())
}
return nil
}
/*
这个数值是经验书数值,统计了过去一万块后得到的,
1. 在chief100以后此数值不起作用,
2. 在chief100之前,最好通过estimateGas来估算
*/
var chiefGasLimit = big.NewInt(4712388)
var chiefGasPrice = big.NewInt(18000000000)
/*
获取要插入的chief Tx
1.0之后是通过vrf来选择下一轮出块人
1.0之前因为没有奖励,所以是现有signer任意指定候选人
*/
func (t *Tribe) GetChiefUpdateTx(chain consensus.ChainReader, header *types.Header, state *state.StateDB) *types.Transaction {
if header.Number.Cmp(big.NewInt(CHIEF_NUMBER)) <= 0 {
return nil
}
parentHash := header.ParentHash
parentNumber := new(big.Int).Set(header.Number)
parentNumber.Sub(parentNumber, big.NewInt(1))
var vrf *big.Int
if params.IsSIP100Block(parentNumber) {
vrf = new(big.Int).SetBytes(header.Extra[:32])
}
nextRoundSigner := params.Chief100GetNextRoundSigner(parentHash, parentNumber, vrf)
nonce := state.GetNonce(t.Status.GetMinerAddress())
txData, err := hex.DecodeString("1c1b8772000000000000000000000000") //这个是4字节chiefUpdate函数标识以及12字节的0
if err != nil {
panic(err)
}
txData = append(txData, nextRoundSigner[:]...)
rawTx := types.NewTransaction(nonce, params.GetChiefInfo(header.Number).Addr, big.NewInt(0), chiefGasLimit, chiefGasPrice, txData)
auth := bind.NewKeyedTransactor(t.Status.getNodekey())
signedTx, err := auth.Signer(types.NewEIP155Signer(chain.Config().ChainId), auth.From, rawTx)
if err != nil {
panic(fmt.Sprintf("sign tx:%s", err))
}
return signedTx
}
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given, and returns the final block.
func (t *Tribe) Finalize(chain consensus.ChainReader, header *types.Header, state *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
// Accumulate any block and uncle rewards and commit the final state root
accumulateRewards(chain.Config(), state, header)
//destroy SmartMeshFoundation 12% balance
destroySmartMeshFoundation12Balance(chain.Config(), state, header)
header.Root = state.IntermediateRoot(chain.Config().IsEIP158(header.Number))
//there is no uncle in triple
header.UncleHash = types.CalcUncleHash(nil)
return types.NewBlock(header, txs, nil, receipts), nil
}
// Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials.
func (t *Tribe) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) {
if block.Number().Cmp(big.NewInt(CHIEF_NUMBER)) <= 0 {
return nil, errors.New("never mining block before #3")
}
if err := t.Status.ValidateBlock(chain.GetBlock(block.ParentHash(), block.NumberU64()-1), block, false); err != nil {
log.Error("Tribe_Seal", "number", block.Number().Int64(), "err", err)
//log.Error("Tribe_Seal", "retry", atomic.LoadUint32(&t.SealErrorCounter), "number", block.Number().Int64(), "err", err)
return nil, err
}
//atomic.StoreUint32(&t.SealErrorCounter, 0)
header := block.Header()
// Sealing the genesis block is not supported
number := header.Number.Int64()
// For 0-period chains, refuse to seal empty blocks (no reward but would spin sealing)
if len(block.Transactions()) == 0 {
panic("at least one chief update tx")
}
if !t.Status.validateSigner(chain.GetHeaderByHash(block.ParentHash()), block.Header(), t.Status.GetMinerAddress()) {
return nil, errUnauthorized
}
now := time.Now()
delay := time.Unix(header.Time.Int64(), 0).Sub(now)
//fmt.Println(" ---->", "diff", header.Difficulty, "header.time=", header.Time.Int64(), "now=", now.Unix(), "delay=", delay)
log.Info(fmt.Sprintf("Seal -> num=%d, diff=%d, miner=%s, delay=%d", number, header.Difficulty, header.Coinbase.Hex(), delay))
if !params.IsSIP100Block(header.Number) && header.Difficulty.Cmp(diffNoTurn) == 0 {
wiggle := time.Duration(len(t.Status.Signers)/2+1) * wiggleTime
delay += time.Duration(rand.Int63n(int64(wiggle)))
}
select {
case <-stop:
log.Warn(fmt.Sprintf("🐦 cancel -> num=%d, diff=%d, miner=%s, delay=%d", number, header.Difficulty, header.Coinbase.Hex(), delay))
return nil, nil
case <-time.After(delay):
}
// Sign all the things!
hash := sigHash(header).Bytes()
sighash, err := crypto.Sign(hash, t.Status.nodeKey)
if err != nil {
return nil, err
}
copy(header.Extra[len(header.Extra)-extraSeal:], sighash)
blk := block.WithSeal(header)
return blk, nil
}
// CalcDifficulty is the difficulty adjustment algorithm. It returns the difficulty
// that a new block should have based on the previous blocks in the chain and the
// current signer.
func (t *Tribe) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
log.Debug("CalcDifficulty", "ParentNumber", parent.Number.Int64(), "CurrentNumber:", chain.CurrentHeader().Number.Int64())
currentNumber := new(big.Int).Add(parent.Number, big.NewInt(1))
if ci := params.GetChiefInfo(currentNumber); ci != nil {
switch ci.Version {
case "1.0.0":
return t.Status.InTurnForCalcDiffcultyChief100(t.Status.GetMinerAddress(), parent)
}
}
return t.Status.InTurnForCalcDifficulty(t.Status.GetMinerAddress(), parent)
}
// APIs implements consensus.Engine, returning the user facing RPC API to allow
// controlling the signer voting.
func (t *Tribe) APIs(chain consensus.ChainReader) []rpc.API {
return []rpc.API{{
Namespace: "tribe",
Version: "0.0.1",
Service: &API{accman: t.accman, chain: chain, tribe: t},
Public: false,
}}
}
/*
GetPeriodChief100: 计算出块节点对应的延时时间
依据:
普通节点替补出块规则调整为当前常委会出块节点替补出块,当前常委会出块节点替补出块规则调整为常委会节点列表顺序替补出块,出块难度依次递减。
当普通节点出块时,难度为6->5->4->3->2->1;当常委会节点出块时,难度为6->5->4->3->2。每次替补出块时间都顺延4秒,
下一个区块将在当前区块产生以后的14秒+ 以后产生。
总共只有5个常委会节点,其排序固定
signers:[0,...,16] 0号对应的是常委会节点,1-16对应的是普通出块节点
场景1:
假设`header`中的块该signers[3]出,
如果signers[3]出,则延时14秒,否则只能由其他常委会节点替代
假设当前signers[0]对应的是常委2,那么如果常委2出则延时18,常委3出则延时22,...,如果常委1出则延时34
场景2:
假设`header`中的块该signers[0]出,
如果signers[0]出,则延时14秒,否则只能由其他常委会节点替代
假设当前signers[0]对应的是常委2,那么常委3出则延时18,常委4出则延时22,...常委1出则延时30
替补节点最多5个
*/
func (t *Tribe) GetPeriodChief100(header *types.Header, signers []*Signer) (p uint64) {
var (
// 14 , 18 , 22 , 26 , 30 , 34 , 38(无出块资格的节点)
signature = header.Extra[len(header.Extra)-extraSeal:]
err error
miner common.Address
empty = make([]byte, len(signature))
Main, Subs, Other, Else = t.config.Period - 1, t.config.Period + 3, t.config.Period + 7, t.config.Period - 1 + 4*6
number = header.Number
)
p = Else
if bytes.Equal(signature, empty) {
miner = t.Status.GetMinerAddress()
} else {
miner, err = ecrecover(header, t)
if err != nil {
log.Error("ecrecover on getPeriod", "header", header, "err", err)
return
}
}
if signers == nil {
signers = t.Status.Signers
}
sl := len(signers)
if sl == 0 {
log.Error("GetPeriod_signers_cannot_empty")
return
}
// normal
idx_m := number.Int64() % int64(sl)
if miner == signers[idx_m].Address {
p = Main
return
}
// first leader
if miner == signers[0].Address {
p = Subs
return
}
// other leader
if leaders, err := leaderSort(signers[0].Address, t.Status.Leaders); err == nil {
for i, leader := range leaders {
if miner == leader && number.Int64()%int64(sl) == 0 {
p = Subs + uint64(i)*(Subs-Main)
return
} else if miner == leader {
p = Other + uint64(i)*(Subs-Main)
return
}
}
}
return
}
func (t *Tribe) GetPeriod(header *types.Header, signers []*Signer) (p uint64) {
if ci := params.GetChiefInfo(header.Number); ci != nil {
switch ci.Version {
case "1.0.0":
p = t.GetPeriodChief100(header, signers)
log.Debug("<<GetPeriodSIP005>>", "num", header.Number, "period", p)
return
}
}
// 14 , 18 , 22(random add 0~4.5s)
signature := header.Extra[len(header.Extra)-extraSeal:]
var (
err error
miner common.Address
empty = make([]byte, len(signature))
/*
替补出块按照顺序依次增加4秒的延迟
*/
Main, Subs, Other = t.config.Period - 1, t.config.Period + 3, t.config.Period + 7
number = header.Number
)
p = Other
if bytes.Equal(signature, empty) {
miner = t.Status.GetMinerAddress()
} else {
miner, err = ecrecover(header, t)
if err != nil {
log.Error("ecrecover on getPeriod", "header", header, "err", err)
return
}
}
if number.Int64() <= 3 {
p = Subs
return
}
if signers == nil {
signers = t.Status.Signers
}
sl := len(signers)
if sl == 0 {
log.Error("GetPeriod_signers_cannot_empty")
p = Other
return
}
idx_m, idx_s := number.Int64()%int64(sl), (number.Int64()+1)%int64(sl)
if miner == signers[idx_m].Address {
p = Main
return
}
if miner == signers[idx_s].Address {
p = Subs
return
}
return
}
// AccumulateRewards credits the coinbase of the given block with the mining
// reward. The total reward consists of the static block reward and rewards for
// included uncles. The coinbase of each uncle block is also rewarded.
// add by liangc : no reward
func accumulateRewards(config *params.ChainConfig, state *state.StateDB, header *types.Header) {
if config.Chief100Block == nil || header.Number.Cmp(config.Chief100Block) < 0 {
return
}
// Select the correct block reward based on chain progression
blockReward := new(big.Int).Set(Chief100BlockReward)
// Accumulate the rewards for the miner and any included uncles
number := new(big.Int).Set(header.Number)
number = number.Sub(number, config.Chief100Block)
number = number.Div(number, big.NewInt(int64(BlockRewardReducedInterval)))
blockReward = blockReward.Rsh(blockReward, uint(number.Int64()))
state.AddBalance(header.Coinbase, blockReward)
}
//
//销毁基金会账户12% token
//Destroy 12% token of Foundation Account
func destroySmartMeshFoundation12Balance(config *params.ChainConfig, state *state.StateDB, header *types.Header) {
if !params.IsDevnet() && !params.IsTestnet() && config.Chief100Block != nil && header.Number.Cmp(config.Chief100Block) == 0 {
ob := state.GetBalance(SmartMeshFoundationAccount)
if ob.Cmp(SmartMeshFoundationAccountDestroyBalance) >= 0 {
state.SubBalance(SmartMeshFoundationAccount, SmartMeshFoundationAccountDestroyBalance)
} else {
state.SubBalance(SmartMeshFoundationAccount, ob)
}
}
}