-
Notifications
You must be signed in to change notification settings - Fork 47
/
worker.go
823 lines (710 loc) · 22.4 KB
/
worker.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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// Copyright 2022 The AmazeChain Authors
// This file is part of the AmazeChain library.
//
// The AmazeChain library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The AmazeChain library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
package miner
import (
"context"
"errors"
"fmt"
"github.com/amazechain/amc/core"
"github.com/amazechain/amc/internal/api"
"github.com/amazechain/amc/internal/consensus/misc"
"github.com/amazechain/amc/internal/metrics/prometheus"
"github.com/holiman/uint256"
"sort"
"sync"
"github.com/amazechain/amc/modules/rawdb"
"github.com/amazechain/amc/internal"
vm2 "github.com/amazechain/amc/internal/vm"
"github.com/amazechain/amc/modules/state"
"sync/atomic"
"time"
"github.com/amazechain/amc/common"
"github.com/amazechain/amc/common/block"
"github.com/amazechain/amc/common/transaction"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/conf"
"github.com/amazechain/amc/internal/consensus"
"github.com/amazechain/amc/log"
event "github.com/amazechain/amc/modules/event/v2"
"github.com/amazechain/amc/params"
mapset "github.com/deckarep/golang-set"
"golang.org/x/sync/errgroup"
)
var (
blockSignGauge = prometheus.GetOrCreateCounter("block_sign_counter", true)
)
type task struct {
receipts []*block.Receipt
state *state.IntraBlockState
block block.IBlock
createdAt time.Time
nopay map[types.Address]*uint256.Int
}
type newWorkReq struct {
interrupt *atomic.Int32
noempty bool
timestamp int64
}
type generateParams struct {
timestamp uint64 // The timstamp for sealing task
parentHash types.Hash // Parent block hash, empty means the latest chain head
coinbase types.Address // The fee recipient address for including transaction
random types.Hash // The randomness generated by beacon chain, empty before the merge
noTxs bool // Flag whether an empty block without any transaction is expected
}
type environment struct {
//signer types.Signer
//state *state.IntraBlockState
ancestors mapset.Set // ancestor set (used for checking uncle parent validity)
family mapset.Set // family set (used for checking uncle invalidity)
tcount int // tx count in cycle
gasPool *common.GasPool // available gas used to pack transactions
coinbase types.Address
header *block.Header
txs []*transaction.Transaction
receipts []*block.Receipt
}
func (env *environment) copy() *environment {
cpy := &environment{
ancestors: env.ancestors.Clone(),
family: env.family.Clone(),
tcount: env.tcount,
coinbase: env.coinbase,
header: block.CopyHeader(env.header),
receipts: env.receipts,
}
if env.gasPool != nil {
gasPool := *env.gasPool
cpy.gasPool = &gasPool
}
cpy.txs = make([]*transaction.Transaction, len(env.txs))
copy(cpy.txs, env.txs)
return cpy
}
const (
commitInterruptNone int32 = iota
commitInterruptNewHead
commitInterruptResubmit
commitInterruptTimeout
)
const (
minPeriodInterval = 1 * time.Second // 1s
staleThreshold = 7
resubmitAdjustChanSize = 10
// maxRecommitInterval is the maximum time interval to recreate the sealing block with
// any newly arrived transactions.
maxRecommitInterval = 12 * time.Second
intervalAdjustRatio = 0.1
intervalAdjustBias = 200 * 1000.0 * 1000.0
)
var (
errBlockInterruptedByNewHead = errors.New("new head arrived while building block")
errBlockInterruptedByRecommit = errors.New("recommit interrupt while building block")
errBlockInterruptedByTimeout = errors.New("timeout while building block")
)
// intervalAdjust represents a resubmitting interval adjustment.
type intervalAdjust struct {
ratio float64
inc bool
}
type worker struct {
minerConf conf.MinerConfig
engine consensus.Engine
chain common.IBlockChain
txsPool common.ITxsPool
coinbase types.Address
chainConfig *params.ChainConfig
isLocalBlock func(header *block.Header) bool
pendingTasks map[types.Hash]*task
wg sync.WaitGroup
mu sync.RWMutex
startCh chan struct{}
newWorkCh chan *newWorkReq
resultCh chan block.IBlock
taskCh chan *task
resubmitAdjustCh chan *intervalAdjust
running int32
newTxs int32
group *errgroup.Group
ctx context.Context
cancel context.CancelFunc
//current *environment
newTaskHook func(*task)
snapshotMu sync.RWMutex // The lock used to protect the snapshots below
snapshotBlock block.IBlock
snapshotReceipts block.Receipts
}
func newWorker(ctx context.Context, group *errgroup.Group, chainConfig *params.ChainConfig, engine consensus.Engine, bc common.IBlockChain, txsPool common.ITxsPool, isLocalBlock func(header *block.Header) bool, init bool, minerConf conf.MinerConfig) *worker {
c, cancel := context.WithCancel(ctx)
worker := &worker{
engine: engine,
chain: bc,
txsPool: txsPool,
chainConfig: chainConfig,
mu: sync.RWMutex{},
startCh: make(chan struct{}, 1),
group: group,
isLocalBlock: isLocalBlock,
ctx: c,
cancel: cancel,
taskCh: make(chan *task),
newWorkCh: make(chan *newWorkReq),
resultCh: make(chan block.IBlock),
pendingTasks: make(map[types.Hash]*task),
minerConf: minerConf,
resubmitAdjustCh: make(chan *intervalAdjust, resubmitAdjustChanSize),
}
recommit := worker.minerConf.Recommit
if recommit < minPeriodInterval {
recommit = minPeriodInterval
}
// machine verify
group.Go(func() error {
return api.MachineVerify(ctx)
})
group.Go(func() error {
return worker.workLoop(recommit)
})
group.Go(func() error {
return worker.runLoop()
})
group.Go(func() error {
return worker.taskLoop()
})
group.Go(func() error {
return worker.resultLoop()
})
if init {
worker.startCh <- struct{}{}
}
return worker
}
func (w *worker) start() {
atomic.StoreInt32(&w.running, 1)
w.startCh <- struct{}{}
}
func (w *worker) stop() {
atomic.StoreInt32(&w.running, 0)
}
func (w *worker) close() {
}
func (w *worker) isRunning() bool {
return atomic.LoadInt32(&w.running) == 1
}
func (w *worker) setCoinbase(addr types.Address) {
w.mu.Lock()
defer w.mu.Unlock()
w.coinbase = addr
}
func (w *worker) runLoop() error {
defer w.cancel()
defer w.stop()
for {
select {
case <-w.ctx.Done():
return w.ctx.Err()
case req := <-w.newWorkCh:
err := w.commitWork(req.interrupt, req.noempty, req.timestamp)
if err != nil {
log.Error("runLoop err:", err.Error())
//w.startCh <- struct{}{}
}
}
}
}
func (w *worker) resultLoop() error {
defer w.cancel()
defer w.stop()
for {
select {
case <-w.ctx.Done():
return w.ctx.Err()
case blk := <-w.resultCh:
if blk == nil {
continue
}
// Short circuit when receiving duplicate result caused by resubmitting.
if w.chain.HasBlock(blk.Hash(), blk.Number64().Uint64()) {
continue
}
var (
sealhash = w.engine.SealHash(blk.Header())
hash = blk.Hash()
)
w.mu.RLock()
task, exist := w.pendingTasks[sealhash]
w.mu.RUnlock()
if !exist {
log.Error("Block found but no relative pending task", "number", blk.Number64().Uint64(), "sealhash", sealhash, "hash", hash)
//w.startCh <- struct{}{}
continue
}
// Different block could share same sealhash, deep copy here to prevent write-write conflict.
var (
receipts = make([]*block.Receipt, len(task.receipts))
logs []*block.Log
)
for i, taskReceipt := range task.receipts {
receipt := new(block.Receipt)
receipts[i] = receipt
*receipt = *taskReceipt
// add block location fields
receipt.BlockHash = hash
receipt.BlockNumber = blk.Number64()
receipt.TransactionIndex = uint(i)
// Update the block hash in all logs since it is now available and not when the
// receipt/log of individual transactions were created.
receipt.Logs = make([]*block.Log, len(taskReceipt.Logs))
for i, taskLog := range taskReceipt.Logs {
log := new(block.Log)
receipt.Logs[i] = log
*log = *taskLog
log.BlockHash = hash
}
logs = append(logs, receipt.Logs...)
}
// Commit block and state to database.
err := w.chain.WriteBlockWithState(blk, receipts, task.state, task.nopay)
if err != nil {
log.Error("Failed writing block to chain", "err", err)
continue
}
blockSignGauge.Set(uint64(len(blk.Body().Verifier())))
if len(logs) > 0 {
event.GlobalEvent.Send(common.NewLogsEvent{Logs: logs})
}
log.Info("🔨 Successfully sealed new block",
"sealhash", sealhash,
"hash", hash,
"number", blk.Number64().Uint64(),
"used gas", blk.GasUsed(),
"diff", blk.Difficulty().Uint64(),
"headerTime", time.Unix(int64(blk.Time()), 0).Format(time.RFC3339),
"verifierCount", len(blk.Body().Verifier()),
"rewardCount", len(blk.Body().Reward()),
"elapsed", common.PrettyDuration(time.Since(task.createdAt)),
"txs", len(blk.Transactions()))
if err = w.chain.SealedBlock(blk); err != nil {
log.Error("Failed Broadcast block to p2p network", "err", err)
continue
}
event.GlobalEvent.Send(common.ChainHighestBlock{Block: *blk.(*block.Block), Inserted: true})
}
}
}
func (w *worker) taskLoop() error {
defer w.cancel()
defer w.stop()
var (
stopCh chan struct{}
prev types.Hash
)
interrupt := func() {
if stopCh != nil {
close(stopCh)
stopCh = nil
}
}
for {
select {
case <-w.ctx.Done():
return w.ctx.Err()
case task := <-w.taskCh:
if w.newTaskHook != nil {
w.newTaskHook(task)
}
sealHash := w.engine.SealHash(task.block.Header())
hash := task.block.Hash()
stateRoot := task.block.StateRoot()
if sealHash == prev {
continue
}
interrupt()
stopCh, prev = make(chan struct{}), sealHash
w.mu.Lock()
w.pendingTasks[sealHash] = task
w.mu.Unlock()
if err := w.engine.Seal(w.chain, task.block, w.resultCh, stopCh); err != nil {
w.mu.Lock()
delete(w.pendingTasks, sealHash)
w.mu.Unlock()
log.Warn("delete task", "sealHash", sealHash, "hash", hash, "stateRoot", stateRoot, "err", err)
if errors.Is(err, consensus.ErrNotEnoughSign) {
time.Sleep(1 * time.Second)
w.startCh <- struct{}{}
}
} else {
log.Debug("send task", "sealHash", sealHash, "hash", hash, "stateRoot", stateRoot)
}
}
}
}
func (w *worker) commitWork(interrupt *atomic.Int32, noempty bool, timestamp int64) error {
start := time.Now()
if w.isRunning() {
if w.coinbase == (types.Address{}) {
return fmt.Errorf("coinbase is empty")
}
}
current, err := w.prepareWork(&generateParams{timestamp: uint64(timestamp), coinbase: w.coinbase})
if err != nil {
log.Error("cannot prepare work", "err", err)
return err
}
tx, err := w.chain.DB().BeginRo(w.ctx)
if nil != err {
log.Error("work.commitWork failed", err)
return err
}
defer tx.Rollback()
stateReader := state.NewPlainStateReader(tx)
stateWriter := state.NewNoopWriter()
ibs := state.New(stateReader)
// generate state for mobile verify
ibs.BeginWriteSnapshot()
ibs.BeginWriteCodes()
headers := make([]*block.Header, 0)
getHeader := func(hash types.Hash, number uint64) *block.Header {
h := rawdb.ReadHeader(tx, hash, number)
if nil != h {
headers = append(headers, h)
}
return h
}
err = w.fillTransactions(interrupt, current, ibs, getHeader)
switch {
case err == nil:
w.resubmitAdjustCh <- &intervalAdjust{inc: false}
case errors.Is(err, errBlockInterruptedByRecommit):
gaslimit := current.header.GasLimit
ratio := float64(gaslimit-current.gasPool.Gas()) / float64(gaslimit)
if ratio < 0.1 {
ratio = 0.1
}
w.resubmitAdjustCh <- &intervalAdjust{
ratio: ratio,
inc: true,
}
}
//var rewards []*block.Reward
//if w.chainConfig.IsBeijing(current.header.Number.Uint64()) {
// rewards, err = w.engine.Rewards(tx, block.CopyHeader(current.header), ibs, false)
// if err != nil {
// return err
// }
//}
if err = w.commit(current, stateWriter, ibs, start, headers); nil != err {
log.Errorf("w.commit failed, error %v\n", err)
return err
}
return nil
}
// recalcRecommit recalculates the resubmitting interval upon feedback.
func recalcRecommit(minRecommit, prev time.Duration, target float64, inc bool) time.Duration {
var (
prevF = float64(prev.Nanoseconds())
next float64
)
if inc {
next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target+intervalAdjustBias)
max := float64(maxRecommitInterval.Nanoseconds())
if next > max {
next = max
}
} else {
next = prevF*(1-intervalAdjustRatio) + intervalAdjustRatio*(target-intervalAdjustBias)
min := float64(minRecommit.Nanoseconds())
if next < min {
next = min
}
}
return time.Duration(int64(next))
}
func (w *worker) workLoop(recommit time.Duration) error {
defer w.cancel()
defer w.stop()
var (
interrupt *atomic.Int32
minRecommit = recommit // minimal resubmit interval specified by user.
timestamp int64 // timestamp for each round of sealing.
)
newBlockCh := make(chan common.ChainHighestBlock)
defer close(newBlockCh)
newBlockSub := event.GlobalEvent.Subscribe(newBlockCh)
defer newBlockSub.Unsubscribe()
timer := time.NewTimer(0)
defer timer.Stop()
<-timer.C // discard the initial tick
commit := func(noempty bool, s int32) {
if interrupt != nil {
interrupt.Store(s)
}
interrupt = new(atomic.Int32)
select {
case w.newWorkCh <- &newWorkReq{interrupt: interrupt, noempty: noempty, timestamp: timestamp}:
case <-w.ctx.Done():
return
}
timer.Reset(recommit)
//atomic.StoreInt32(&w.newTxs, 0)
}
clearPending := func(number *uint256.Int) {
w.mu.Lock()
for h, t := range w.pendingTasks {
if number.Cmp(uint256.NewInt(0).Add(t.block.Number64(), uint256.NewInt(staleThreshold))) < 1 {
delete(w.pendingTasks, h)
}
}
w.mu.Unlock()
}
for {
select {
case <-w.ctx.Done():
return w.ctx.Err()
case <-w.startCh:
clearPending(w.chain.CurrentBlock().Number64())
timestamp = time.Now().Unix()
commit(false, commitInterruptNewHead)
case blockEvent := <-newBlockCh:
clearPending(blockEvent.Block.Number64())
timestamp = time.Now().Unix()
commit(false, commitInterruptNewHead)
case err := <-newBlockSub.Err():
return err
case <-timer.C:
// If sealing is running resubmit a new work cycle periodically to pull in
// higher priced transactions. Disable this overhead for pending blocks.
//if w.isRunning() && (w.chainConfig.Apos == nil && w.chainConfig.Clique == nil) {
// continue
// commit(false, commitInterruptResubmit)
//}
case adjust := <-w.resubmitAdjustCh:
// Adjust resubmit interval by feedback.
if adjust.inc {
before := recommit
target := float64(recommit.Nanoseconds()) / adjust.ratio
recommit = recalcRecommit(minRecommit, recommit, target, true)
log.Trace("Increase miner recommit interval", "from", before, "to", recommit)
} else {
before := recommit
recommit = recalcRecommit(minRecommit, recommit, float64(minRecommit.Nanoseconds()), false)
log.Trace("Decrease miner recommit interval", "from", before, "to", recommit)
}
}
}
}
func (w *worker) fillTransactions(interrupt *atomic.Int32, env *environment, ibs *state.IntraBlockState, getHeader func(hash types.Hash, number uint64) *block.Header) error {
// todo fillTx
env.txs = []*transaction.Transaction{}
txs, err := w.txsPool.GetTransaction()
if err != nil {
log.Warn("get transaction error", "err", err)
return err
}
header := env.header
noop := state.NewNoopWriter()
var miningCommitTx = func(txn *transaction.Transaction, coinbase types.Address, vmConfig *vm2.Config, chainConfig *params.ChainConfig, ibs *state.IntraBlockState, current *environment) ([]*block.Log, error) {
ibs.Prepare(txn.Hash(), types.Hash{}, env.tcount)
gasSnap := current.gasPool.Gas()
snap := ibs.Snapshot()
log.Debug("addTransactionsToMiningBlock", "txn hash", txn.Hash())
receipt, _, err := internal.ApplyTransaction(chainConfig, internal.GetHashFn(header, getHeader), w.engine, &coinbase, env.gasPool, ibs, noop, current.header, txn, &header.GasUsed, *vmConfig)
if err != nil {
ibs.RevertToSnapshot(snap)
env.gasPool = new(common.GasPool).AddGas(gasSnap) // restore gasPool as well as ibs
return nil, err
}
current.txs = append(current.txs, txn)
current.receipts = append(current.receipts, receipt)
return receipt.Logs, nil
}
log.Tracef("fillTransactions txs len:%d", len(txs))
for _, tx := range txs {
// Check interruption signal and abort building if it's fired.
if interrupt != nil {
if signal := interrupt.Load(); signal != commitInterruptNone {
return signalToErr(signal)
}
}
if env.gasPool.Gas() < params.TxGas {
log.Trace("Not enough gas for further transactions", "have", env.gasPool, "want", params.TxGas)
break
}
// Start executing the transaction
_, err := miningCommitTx(tx, env.coinbase, &vm2.Config{}, w.chainConfig, ibs, env)
switch {
case errors.Is(err, core.ErrGasLimitReached):
continue
case errors.Is(err, core.ErrNonceTooHigh):
continue
case errors.Is(err, core.ErrNonceTooLow):
continue
case errors.Is(err, nil):
env.tcount++
continue
default:
log.Error("miningCommitTx failed ", "error", err)
}
}
return nil
}
func (w *worker) prepareWork(param *generateParams) (*environment, error) {
w.mu.RLock()
defer w.mu.RUnlock()
timestamp := param.timestamp
parent := w.chain.CurrentBlock().Header().(*block.Header)
if param.parentHash != (types.Hash{}) {
b, _ := w.chain.GetBlockByHash(param.parentHash)
if b == nil {
return nil, fmt.Errorf("missing parent")
}
parent = b.Header().(*block.Header)
}
if parent.Time >= param.timestamp {
timestamp = parent.Time + 1
}
header := &block.Header{
//Root: parent.StateRoot(),
ParentHash: parent.Hash(),
Coinbase: param.coinbase,
Number: uint256.NewInt(0).Add(parent.Number64(), uint256.NewInt(1)),
GasLimit: CalcGasLimit(parent.GasLimit, w.minerConf.GasCeil),
Time: uint64(timestamp),
Difficulty: uint256.NewInt(0),
// just for now
BaseFee: uint256.NewInt(0),
}
// Set baseFee and GasLimit if we are on an EIP-1559 chain
if w.chainConfig.IsLondon(header.Number.Uint64()) {
header.BaseFee, _ = uint256.FromBig(misc.CalcBaseFee(w.chainConfig, parent))
if !w.chainConfig.IsLondon(parent.Number64().Uint64()) {
parentGasLimit := parent.GasLimit * params.ElasticityMultiplier
header.GasLimit = CalcGasLimit(parentGasLimit, w.minerConf.GasCeil)
}
}
if err := w.engine.Prepare(w.chain, header); err != nil {
return nil, err
}
return w.makeEnv(parent, header, param.coinbase), nil
}
func (w *worker) makeEnv(parent *block.Header, header *block.Header, coinbase types.Address) *environment {
//rtx, err := w.chain.DB().BeginRo(context.Background())
//if nil != err {
// return nil
//}
//defer rtx.Rollback()
env := &environment{
ancestors: mapset.NewSet(),
family: mapset.NewSet(),
coinbase: coinbase,
header: header,
//state: ibs,
gasPool: new(common.GasPool),
tcount: 0,
}
env.gasPool = new(common.GasPool).AddGas(header.GasLimit)
//}
for _, ancestor := range w.chain.GetBlocksFromHash(parent.ParentHash, 3) {
env.family.Add(ancestor.(*block.Block).Hash())
env.ancestors.Add(ancestor.Hash())
}
return env
}
func (w *worker) commit(env *environment, writer state.WriterWithChangeSets, ibs *state.IntraBlockState, start time.Time, needHeaders []*block.Header) error {
if w.isRunning() {
env := env.copy()
iblock, rewards, unpay, err := w.engine.FinalizeAndAssemble(w.chain, env.header, ibs, env.txs, nil, env.receipts)
if nil != err {
return err
}
if w.chainConfig.IsBeijing(env.header.Number.Uint64()) {
txs := make([][]byte, len(env.txs))
for i, tx := range env.txs {
var err error
txs[i], err = tx.Marshal()
if nil != err {
panic(err)
}
}
entri := state.Entire{Header: iblock.Header().(*block.Header), Uncles: nil, Transactions: txs, Senders: nil, Snap: ibs.Snap(), Proof: types.Hash{}}
cs := ibs.CodeHashes()
hs := make(state.HashCodes, 0, len(cs))
for k, v := range cs {
hs = append(hs, &state.HashCode{Hash: k, Code: v})
}
sort.Sort(hs)
event.GlobalEvent.Send(common.MinedEntireEvent{Entire: state.EntireCode{Codes: hs, Headers: needHeaders, Entire: entri, Rewards: rewards, CoinBase: env.coinbase}})
}
//
w.updateSnapshot(env, rewards)
select {
case w.taskCh <- &task{receipts: env.receipts, block: iblock, createdAt: time.Now(), state: ibs, nopay: unpay}:
log.Debug("Commit new sealing work",
"number", iblock.Header().Number64().Uint64(),
"sealhash", w.engine.SealHash(iblock.Header()),
"txs", env.tcount,
"gas", iblock.GasUsed(),
"elapsed", common.PrettyDuration(time.Since(start)),
"headerTime", time.Unix(int64(iblock.Time()), 0).Format(time.RFC3339),
"rewardCount", len(iblock.Body().Reward()),
)
case <-w.ctx.Done():
return w.ctx.Err()
}
}
return nil
}
// copyReceipts makes a deep copy of the given receipts.
func copyReceipts(receipts []*block.Receipt) []*block.Receipt {
result := make([]*block.Receipt, len(receipts))
for i, l := range receipts {
cpy := *l
result[i] = &cpy
}
return result
}
// pendingBlockAndReceipts returns pending block and corresponding receipts.
func (w *worker) pendingBlockAndReceipts() (block.IBlock, block.Receipts) {
// return a snapshot to avoid contention on currentMu mutex
w.snapshotMu.RLock()
defer w.snapshotMu.RUnlock()
return w.snapshotBlock, w.snapshotReceipts
}
// updateSnapshot updates pending snapshot block, receipts and state.
func (w *worker) updateSnapshot(env *environment, rewards []*block.Reward) {
w.snapshotMu.Lock()
defer w.snapshotMu.Unlock()
w.snapshotBlock = block.NewBlockFromReceipt(
env.header,
env.txs,
nil,
env.receipts,
rewards,
)
w.snapshotReceipts = copyReceipts(env.receipts)
}
func signalToErr(signal int32) error {
switch signal {
case commitInterruptNewHead:
return errBlockInterruptedByNewHead
case commitInterruptResubmit:
return errBlockInterruptedByRecommit
case commitInterruptTimeout:
return errBlockInterruptedByTimeout
default:
panic(fmt.Errorf("undefined signal %d", signal))
}
}