forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathXDPoS.go
More file actions
621 lines (555 loc) · 21 KB
/
Copy pathXDPoS.go
File metadata and controls
621 lines (555 loc) · 21 KB
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
// Copyright (c) 2021 XDPoSChain
//
// This program 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.
//
// 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package XDPoS is the adaptor for different consensus engine.
package XDPoS
import (
"errors"
"math/big"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/lru"
"github.com/XinFinOrg/XDPoSChain/consensus"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/engines/engine_v1"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/engines/engine_v2"
"github.com/XinFinOrg/XDPoSChain/consensus/XDPoS/utils"
"github.com/XinFinOrg/XDPoSChain/consensus/clique"
"github.com/XinFinOrg/XDPoSChain/core/state"
"github.com/XinFinOrg/XDPoSChain/core/types"
"github.com/XinFinOrg/XDPoSChain/core/vm"
"github.com/XinFinOrg/XDPoSChain/ethdb"
"github.com/XinFinOrg/XDPoSChain/event"
"github.com/XinFinOrg/XDPoSChain/log"
"github.com/XinFinOrg/XDPoSChain/params"
"github.com/XinFinOrg/XDPoSChain/rpc"
)
const (
ExtraFieldCheck = true
SkipExtraFieldCheck = false
newRoundChanSize = 1
)
func (x *XDPoS) SigHash(header *types.Header) (hash common.Hash) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.SignHash(header)
default: // Default "v1"
return x.EngineV1.SigHash(header)
}
}
// XDPoS is the delegated-proof-of-stake consensus engine proposed to support the
// Ethereum testnet following the Ropsten attacks.
type XDPoS struct {
config *params.XDPoSConfig // Consensus engine configuration parameters
db ethdb.Database // Database to store and retrieve snapshot checkpoints
// Transaction cache, only make sense for adaptor level
signingTxsCache *lru.Cache[common.Hash, []*types.Transaction]
// Share Channel
MinePeriodCh chan int // Miner wait Period Channel
NewRoundCh chan types.Round // Miner use this channel to trigger worker to commitNewWork
// Trading and lending service
GetXDCXService func() utils.TradingService
GetLendingService func() utils.LendingService
// The exact consensus engine with different versions
EngineV1 *engine_v1.XDPoS_v1
EngineV2 *engine_v2.XDPoS_v2
}
// Subscribe to consensus engines forensics events. Currently only exist for engine v2
func (x *XDPoS) SubscribeForensicsEvent(ch chan<- types.ForensicsEvent) event.Subscription {
return x.EngineV2.ForensicsProcessor.SubscribeForensicsEvent(ch)
}
// New creates a XDPoS delegated-proof-of-stake consensus engine with the initial
// signers set to the ones provided by the user.
func New(chainConfig *params.ChainConfig, db ethdb.Database) (*XDPoS, error) {
log.Info("[New] initialise consensus engines")
if chainConfig == nil {
return nil, errors.New("missing chain config")
}
config := chainConfig.XDPoS
// Set any missing consensus parameters to their defaults
if config != nil && config.Epoch == 0 {
config.Epoch = utils.EpochLength
}
if err := chainConfig.CheckConfigForkOrder(); err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("missing XDPoS config")
}
minePeriodCh := make(chan int)
newRoundCh := make(chan types.Round, newRoundChanSize)
engineV2, err := engine_v2.New(chainConfig, db, minePeriodCh, newRoundCh)
if err != nil {
return nil, err
}
return &XDPoS{
config: config,
db: db,
MinePeriodCh: minePeriodCh,
NewRoundCh: newRoundCh,
GetXDCXService: func() utils.TradingService { return nil },
GetLendingService: func() utils.LendingService {
return nil
},
signingTxsCache: lru.NewCache[common.Hash, []*types.Transaction](utils.BlockSignersCacheLimit),
EngineV1: engine_v1.New(chainConfig, db),
EngineV2: engineV2,
}, nil
}
// Stop stops the consensus engine:
// - close chanel MinePeriodCh
// - close chanel NewRoundCh
func (x *XDPoS) Stop() {
close(x.MinePeriodCh)
close(x.NewRoundCh)
}
// NewFaker creates an XDPoS consensus engine with a full fake scheme that
// accepts all blocks as valid without enforcing consensus rules.
func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS {
var fakeEngine *XDPoS
// Set any missing consensus parameters to their defaults
fakeChainConfig := params.TestXDPoSMockChainConfig
if chainConfig != nil {
fakeChainConfig = chainConfig
}
if err := fakeChainConfig.CheckConfigForkOrder(); err != nil {
return nil
}
conf := fakeChainConfig.XDPoS
minePeriodCh := make(chan int)
newRoundCh := make(chan types.Round, newRoundChanSize)
engineV2, err := engine_v2.New(fakeChainConfig, db, minePeriodCh, newRoundCh)
if err != nil {
return nil
}
fakeEngine = &XDPoS{
config: conf,
db: db,
MinePeriodCh: minePeriodCh,
NewRoundCh: newRoundCh,
GetXDCXService: func() utils.TradingService { return nil },
GetLendingService: func() utils.LendingService { return nil },
signingTxsCache: lru.NewCache[common.Hash, []*types.Transaction](utils.BlockSignersCacheLimit),
EngineV1: engine_v1.NewFaker(db, fakeChainConfig),
EngineV2: engineV2,
}
return fakeEngine
}
// Reset parameters after checkpoint due to config may change
func (x *XDPoS) UpdateParams(header *types.Header) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
x.EngineV2.UpdateParams(header)
return
default: // Default "v1"
return
}
}
func (x *XDPoS) Initial(chain consensus.ChainReader, header *types.Header) error {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.Initial(chain, header)
default: // Default "v1"
return nil
}
}
/*
Eth Consensus engine interface implementation
*/
// APIs implements consensus.Engine, returning the user facing RPC API to allow
// controlling the signer voting.
func (x *XDPoS) APIs(chain consensus.ChainReader) []rpc.API {
return []rpc.API{{
Namespace: "XDPoS",
Service: &API{chain: chain, XDPoS: x},
}}
}
// Author implements consensus.Engine, returning the Ethereum address recovered
// from the signature in the header's extra-data section.
func (x *XDPoS) Author(header *types.Header) (common.Address, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.Author(header)
default: // Default "v1"
return x.EngineV1.Author(header)
}
}
// VerifyHeader checks whether a header conforms to the consensus rules.
func (x *XDPoS) VerifyHeader(chain consensus.ChainReader, header *types.Header, fullVerify bool) error {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.VerifyHeader(chain, header, fullVerify)
default: // Default "v1"
return x.EngineV1.VerifyHeader(chain, header, fullVerify)
}
}
// 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. For mixed v1/v2 inputs, results are emitted
// in deterministic consensus order: all v1 results first, then all v2 results.
func (x *XDPoS) VerifyHeaders(chain consensus.ChainReader, headers []*types.Header, fullVerifies []bool) (chan<- struct{}, <-chan error) {
abort := make(chan struct{})
results := make(chan error, len(headers))
verifyChain := NewVerifyHeadersChainReader(chain, headers, nil)
// Split the headers list into v1 and v2 buckets
var v1Headers []*types.Header
var v2Headers []*types.Header
v1FullVerifies := make([]bool, 0, len(headers))
v2FullVerifies := make([]bool, 0, len(headers))
for i, header := range headers {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
v2Headers = append(v2Headers, header)
v2FullVerifies = append(v2FullVerifies, fullVerifies[i])
default: // Default "v1"
v1Headers = append(v1Headers, header)
v1FullVerifies = append(v1FullVerifies, fullVerifies[i])
}
}
v1Count := len(v1Headers)
v2Count := len(v2Headers)
if v1Count != 0 && v2Count == 0 {
x.EngineV1.VerifyHeaders(verifyChain, v1Headers, v1FullVerifies, abort, results)
} else if v1Count == 0 && v2Count != 0 {
x.EngineV2.VerifyHeaders(verifyChain, v2Headers, v2FullVerifies, abort, results)
} else if v1Count != 0 && v2Count != 0 {
v1Results := make(chan error, v1Count)
v2Results := make(chan error, v2Count)
x.EngineV1.VerifyHeaders(verifyChain, v1Headers, v1FullVerifies, abort, v1Results)
x.EngineV2.VerifyHeaders(verifyChain, v2Headers, v2FullVerifies, abort, v2Results)
go func() {
for range v1Count {
select {
case <-abort:
return
case err := <-v1Results:
select {
case <-abort:
return
case results <- err:
}
}
}
for range v2Count {
select {
case <-abort:
return
case err := <-v2Results:
select {
case <-abort:
return
case results <- err:
}
}
}
}()
}
return abort, results
}
// VerifyUncles implements consensus.Engine, always returning an error for any
// uncles as this consensus mechanism doesn't permit uncles.
func (x *XDPoS) VerifyUncles(chain consensus.ChainReader, block *types.Block) error {
switch x.config.BlockConsensusVersion(block.Number()) {
case params.ConsensusEngineVersion2:
return x.EngineV2.VerifyUncles(chain, block)
default: // Default "v1"
return x.EngineV1.VerifyUncles(chain, block)
}
}
// VerifySeal implements consensus.Engine, checking whether the signature contained
// in the header satisfies the consensus protocol requirements.
func (x *XDPoS) VerifySeal(chain consensus.ChainReader, header *types.Header) error {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return nil
default: // Default "v1"
return x.EngineV1.VerifySeal(chain, header)
}
}
// Prepare implements consensus.Engine, preparing all the consensus fields of the
// header for running the transactions on top.
func (x *XDPoS) Prepare(chain consensus.ChainReader, header *types.Header) error {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.Prepare(chain, header)
default: // Default "v1"
return x.EngineV1.Prepare(chain, header)
}
}
// Finalize implements consensus.Engine, ensuring no uncles are set, nor block
// rewards given, and returns the final block.
func (x *XDPoS) Finalize(chain consensus.ChainReader, header *types.Header, state vm.StateDB, parentState *state.StateDB, txs []*types.Transaction, uncles []*types.Header, receipts []*types.Receipt) (*types.Block, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.Finalize(chain, header, state, parentState, txs, uncles, receipts)
default: // Default "v1"
return x.EngineV1.Finalize(chain, header, state, parentState, txs, uncles, receipts)
}
}
// Seal implements consensus.Engine, attempting to create a sealed block using
// the local signing credentials.
func (x *XDPoS) Seal(chain consensus.ChainReader, block *types.Block, stop <-chan struct{}) (*types.Block, error) {
switch x.config.BlockConsensusVersion(block.Number()) {
case params.ConsensusEngineVersion2:
return x.EngineV2.Seal(chain, block, stop)
default: // Default "v1"
return x.EngineV1.Seal(chain, block, stop)
}
}
// 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 (x *XDPoS) CalcDifficulty(chain consensus.ChainReader, time uint64, parent *types.Header) *big.Int {
switch x.config.BlockConsensusVersion(parent.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.CalcDifficulty(chain, time, parent)
default: // Default "v1"
return x.EngineV1.CalcDifficulty(chain, time, parent)
}
}
func (x *XDPoS) HandleProposedBlock(chain consensus.ChainReader, header *types.Header) error {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.ProposedBlockHandler(chain, header)
default: // Default "v1"
return nil
}
}
/*
XDC specific methods
*/
// Authorize injects a private key into the consensus engine to mint new blocks
// with.
func (x *XDPoS) Authorize(signer common.Address, signFn clique.SignerFn) {
// Authorize each consensus individually
x.EngineV1.Authorize(signer, signFn)
x.EngineV2.Authorize(signer, signFn)
}
func (x *XDPoS) GetPeriod() uint64 {
return x.config.Period
}
func (x *XDPoS) IsAuthorisedAddress(chain consensus.ChainReader, header *types.Header, address common.Address) bool {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.IsAuthorisedAddress(chain, header, address)
default: // Default "v1"
return x.EngineV1.IsAuthorisedAddress(chain, header, address)
}
}
func (x *XDPoS) GetMasternodes(chain consensus.ChainReader, header *types.Header) []common.Address {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.GetMasternodes(chain, header)
default: // Default "v1"
return x.EngineV1.GetMasternodes(chain, header)
}
}
func (x *XDPoS) GetMasternodesByNumber(chain consensus.ChainReader, blockNumber uint64) []common.Address {
blockHeader := chain.GetHeaderByNumber(blockNumber)
if blockHeader == nil {
log.Error("[GetMasternodesByNumber] Unable to find block", "Num", blockNumber)
return []common.Address{}
}
switch x.config.BlockConsensusVersion(big.NewInt(int64(blockNumber))) {
case params.ConsensusEngineVersion2:
return x.EngineV2.GetMasternodes(chain, blockHeader)
default: // Default "v1"
return x.EngineV1.GetMasternodes(chain, blockHeader)
}
}
func (x *XDPoS) YourTurn(chain consensus.ChainReader, parent *types.Header, signer common.Address) (bool, error) {
switch x.config.BlockConsensusVersion(big.NewInt(parent.Number.Int64() + 1)) {
case params.ConsensusEngineVersion2:
return x.EngineV2.YourTurn(chain, parent, signer)
default: // Default "v1"
return x.EngineV1.YourTurn(chain, parent, signer)
}
}
func (x *XDPoS) GetValidator(creator common.Address, chain consensus.ChainReader, header *types.Header) (common.Address, error) {
switch x.config.BlockConsensusVersion(header.Number) {
default: // Default "v1", v2 does not need this function
return x.EngineV1.GetValidator(creator, chain, header)
}
}
func (x *XDPoS) UpdateMasternodes(chain consensus.ChainReader, header *types.Header, ms []utils.Masternode) error {
// fmt.Println("UpdateMasternodes")
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.UpdateMasternodes(chain, header, ms)
default: // Default "v1"
return x.EngineV1.UpdateMasternodes(chain, header, ms)
}
}
func (x *XDPoS) RecoverSigner(header *types.Header) (common.Address, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return common.Address{}, nil
default: // Default "v1"
return x.EngineV1.RecoverSigner(header)
}
}
func (x *XDPoS) RecoverValidator(header *types.Header) (common.Address, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return common.Address{}, nil
default: // Default "v1"
return x.EngineV1.RecoverValidator(header)
}
}
// Get master nodes over extra data of previous checkpoint block.
func (x *XDPoS) GetMasternodesFromCheckpointHeader(checkpointHeader *types.Header) []common.Address {
switch x.config.BlockConsensusVersion(checkpointHeader.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.GetMasternodesFromEpochSwitchHeader(checkpointHeader)
default: // Default "v1"
return x.EngineV1.GetMasternodesFromCheckpointHeader(checkpointHeader)
}
}
// Check is epoch switch (checkpoint) block
func (x *XDPoS) IsEpochSwitch(header *types.Header) (bool, uint64, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.IsEpochSwitch(header)
default: // Default "v1"
return x.EngineV1.IsEpochSwitch(header)
}
}
func (x *XDPoS) GetCurrentEpochSwitchBlock(chain consensus.ChainReader, blockNumber *big.Int) (uint64, uint64, error) {
switch x.config.BlockConsensusVersion(blockNumber) {
case params.ConsensusEngineVersion2:
return x.EngineV2.GetCurrentEpochSwitchBlock(chain, blockNumber)
default: // Default "v1"
return x.EngineV1.GetCurrentEpochSwitchBlock(blockNumber)
}
}
func (x *XDPoS) CalculateMissingRounds(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiMissedRoundsMetadata, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.CalculateMissingRounds(chain, header)
default: // Default "v1"
return nil, errors.New("not supported in the v1 consensus")
}
}
// Same DB across all consensus engines
func (x *XDPoS) GetDb() ethdb.Database {
return x.db
}
func (x *XDPoS) GetSnapshot(chain consensus.ChainReader, header *types.Header) (*utils.PublicApiSnapshot, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
sp, err := x.EngineV2.GetSnapshot(chain, header)
if err != nil {
return nil, err
}
return &utils.PublicApiSnapshot{
Number: sp.Number,
Hash: sp.Hash,
Signers: sp.GetMappedCandidates(),
}, err
default: // Default "v1"
sp, err := x.EngineV1.GetSnapshot(chain, header)
if err != nil {
return nil, err
}
// Convert to a standard PublicApiSnapshot type, otherwise it's a breaking change to API
return &utils.PublicApiSnapshot{
Number: sp.Number,
Hash: sp.Hash,
Signers: sp.Signers,
Recents: sp.Recents,
Votes: sp.Votes,
Tally: sp.Tally,
}, err
}
}
func (x *XDPoS) GetAuthorisedSignersFromSnapshot(chain consensus.ChainReader, header *types.Header) ([]common.Address, error) {
switch x.config.BlockConsensusVersion(header.Number) {
case params.ConsensusEngineVersion2:
return x.EngineV2.GetSignersFromSnapshot(chain, header)
default: // Default "v1"
return x.EngineV1.GetAuthorisedSignersFromSnapshot(chain, header)
}
}
func (x *XDPoS) FindParentBlockToAssign(chain consensus.ChainReader, currentBlock *types.Header) *types.Block {
var parent *types.Block = nil
if x.config.BlockConsensusVersion(currentBlock.Number) == params.ConsensusEngineVersion2 {
parent = x.EngineV2.FindParentBlockToAssign(chain)
}
if parent == nil {
parent = chain.GetBlock(currentBlock.Hash(), currentBlock.Number.Uint64())
}
return parent
}
/**
Caching
*/
// Cache signing transaction data into BlockSingers cache object
func (x *XDPoS) CacheNoneTIPSigningTxs(header *types.Header, txs []*types.Transaction, receipts []*types.Receipt) []*types.Transaction {
signTxs := []*types.Transaction{}
for txIndex, tx := range txs {
if tx.IsSigningTransaction() {
receipt := findTransactionReceipt(txIndex, tx.Hash(), receipts)
if receipt == nil {
continue
}
status := receipt.Status
if len(receipt.PostState) > 0 {
status = types.ReceiptStatusSuccessful
}
if status == types.ReceiptStatusFailed {
continue
}
signTxs = append(signTxs, tx)
}
}
log.Debug("Save tx signers to cache", "hash", header.Hash(), "number", header.Number, "len(txs)", len(signTxs))
x.signingTxsCache.Add(header.Hash(), signTxs)
return signTxs
}
func findTransactionReceipt(txIndex int, txHash common.Hash, receipts []*types.Receipt) *types.Receipt {
if txIndex < len(receipts) {
receipt := receipts[txIndex]
if receipt != nil && (receipt.TxHash == (common.Hash{}) || receipt.TxHash == txHash) {
return receipt
}
}
for _, receipt := range receipts {
if receipt != nil && receipt.TxHash == txHash {
return receipt
}
}
return nil
}
// Cache
func (x *XDPoS) CacheSigningTxs(hash common.Hash, txs []*types.Transaction) []*types.Transaction {
signTxs := []*types.Transaction{}
for _, tx := range txs {
if tx.IsSigningTransaction() {
signTxs = append(signTxs, tx)
}
}
log.Debug("Save tx signers to cache", "hash", hash, "len(txs)", len(signTxs))
x.signingTxsCache.Add(hash, signTxs)
return signTxs
}
func (x *XDPoS) GetCachedSigningTxs(hash common.Hash) ([]*types.Transaction, bool) {
return x.signingTxsCache.Get(hash)
}
func (x *XDPoS) GetEpochSwitchInfoBetween(chain consensus.ChainReader, begin, end *types.Header) ([]*types.EpochSwitchInfo, error) {
beginBlockVersion := x.config.BlockConsensusVersion(begin.Number)
endBlockVersion := x.config.BlockConsensusVersion(end.Number)
if beginBlockVersion == params.ConsensusEngineVersion2 && endBlockVersion == params.ConsensusEngineVersion2 {
return x.EngineV2.GetEpochSwitchInfoBetween(chain, begin, end)
}
// Default "v1"
return nil, errors.New("not supported in the v1 consensus")
}