mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-19 19:30:44 +00:00
feat: add a new round chan between consensus and miner
This commit is contained in:
parent
8556473e69
commit
1b89654663
3 changed files with 26 additions and 3 deletions
|
|
@ -41,6 +41,7 @@ import (
|
||||||
const (
|
const (
|
||||||
ExtraFieldCheck = true
|
ExtraFieldCheck = true
|
||||||
SkipExtraFieldCheck = false
|
SkipExtraFieldCheck = false
|
||||||
|
newRoundChanSize = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
func (x *XDPoS) SigHash(header *types.Header) (hash common.Hash) {
|
func (x *XDPoS) SigHash(header *types.Header) (hash common.Hash) {
|
||||||
|
|
@ -64,6 +65,8 @@ type XDPoS struct {
|
||||||
// Share Channel
|
// Share Channel
|
||||||
MinePeriodCh chan int // Miner wait Period 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
|
// Trading and lending service
|
||||||
GetXDCXService func() utils.TradingService
|
GetXDCXService func() utils.TradingService
|
||||||
GetLendingService func() utils.LendingService
|
GetLendingService func() utils.LendingService
|
||||||
|
|
@ -104,6 +107,7 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database) *XDPoS {
|
||||||
log.Info("xdc config loading", "v2 config", config.V2)
|
log.Info("xdc config loading", "v2 config", config.V2)
|
||||||
|
|
||||||
minePeriodCh := make(chan int)
|
minePeriodCh := make(chan int)
|
||||||
|
newRoundCh := make(chan types.Round, newRoundChanSize)
|
||||||
|
|
||||||
// Allocate the snapshot caches and create the engine
|
// Allocate the snapshot caches and create the engine
|
||||||
signingTxsCache, _ := lru.New(utils.BlockSignersCacheLimit)
|
signingTxsCache, _ := lru.New(utils.BlockSignersCacheLimit)
|
||||||
|
|
@ -113,10 +117,11 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database) *XDPoS {
|
||||||
db: db,
|
db: db,
|
||||||
|
|
||||||
MinePeriodCh: minePeriodCh,
|
MinePeriodCh: minePeriodCh,
|
||||||
|
NewRoundCh: newRoundCh,
|
||||||
|
|
||||||
signingTxsCache: signingTxsCache,
|
signingTxsCache: signingTxsCache,
|
||||||
EngineV1: engine_v1.New(chainConfig, db),
|
EngineV1: engine_v1.New(chainConfig, db),
|
||||||
EngineV2: engine_v2.New(chainConfig, db, minePeriodCh),
|
EngineV2: engine_v2.New(chainConfig, db, minePeriodCh, newRoundCh),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -131,6 +136,7 @@ func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS {
|
||||||
}
|
}
|
||||||
|
|
||||||
minePeriodCh := make(chan int)
|
minePeriodCh := make(chan int)
|
||||||
|
newRoundCh := make(chan types.Round, newRoundChanSize)
|
||||||
|
|
||||||
// Allocate the snapshot caches and create the engine
|
// Allocate the snapshot caches and create the engine
|
||||||
signingTxsCache, _ := lru.New(utils.BlockSignersCacheLimit)
|
signingTxsCache, _ := lru.New(utils.BlockSignersCacheLimit)
|
||||||
|
|
@ -140,13 +146,14 @@ func NewFaker(db ethdb.Database, chainConfig *params.ChainConfig) *XDPoS {
|
||||||
db: db,
|
db: db,
|
||||||
|
|
||||||
MinePeriodCh: minePeriodCh,
|
MinePeriodCh: minePeriodCh,
|
||||||
|
NewRoundCh: newRoundCh,
|
||||||
|
|
||||||
GetXDCXService: func() utils.TradingService { return nil },
|
GetXDCXService: func() utils.TradingService { return nil },
|
||||||
GetLendingService: func() utils.LendingService { return nil },
|
GetLendingService: func() utils.LendingService { return nil },
|
||||||
|
|
||||||
signingTxsCache: signingTxsCache,
|
signingTxsCache: signingTxsCache,
|
||||||
EngineV1: engine_v1.NewFaker(db, chainConfig),
|
EngineV1: engine_v1.NewFaker(db, chainConfig),
|
||||||
EngineV2: engine_v2.New(chainConfig, db, minePeriodCh),
|
EngineV2: engine_v2.New(chainConfig, db, minePeriodCh, newRoundCh),
|
||||||
}
|
}
|
||||||
return fakeEngine
|
return fakeEngine
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,6 +48,7 @@ type XDPoS_v2 struct {
|
||||||
|
|
||||||
BroadcastCh chan interface{}
|
BroadcastCh chan interface{}
|
||||||
minePeriodCh chan int
|
minePeriodCh chan int
|
||||||
|
newRoundCh chan types.Round
|
||||||
|
|
||||||
timeoutWorker *countdown.CountdownTimer // Timer to generate broadcast timeout msg if threashold reached
|
timeoutWorker *countdown.CountdownTimer // Timer to generate broadcast timeout msg if threashold reached
|
||||||
timeoutCount int // number of timeout being sent
|
timeoutCount int // number of timeout being sent
|
||||||
|
|
@ -71,7 +72,7 @@ type XDPoS_v2 struct {
|
||||||
votePoolCollectionTime time.Time
|
votePoolCollectionTime time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(chainConfig *params.ChainConfig, db ethdb.Database, minePeriodCh chan int) *XDPoS_v2 {
|
func New(chainConfig *params.ChainConfig, db ethdb.Database, minePeriodCh chan int, newRoundCh chan types.Round) *XDPoS_v2 {
|
||||||
config := chainConfig.XDPoS
|
config := chainConfig.XDPoS
|
||||||
// Setup timeoutTimer
|
// Setup timeoutTimer
|
||||||
duration := time.Duration(config.V2.CurrentConfig.TimeoutPeriod) * time.Second
|
duration := time.Duration(config.V2.CurrentConfig.TimeoutPeriod) * time.Second
|
||||||
|
|
@ -100,6 +101,7 @@ func New(chainConfig *params.ChainConfig, db ethdb.Database, minePeriodCh chan i
|
||||||
timeoutWorker: timeoutTimer,
|
timeoutWorker: timeoutTimer,
|
||||||
BroadcastCh: make(chan interface{}),
|
BroadcastCh: make(chan interface{}),
|
||||||
minePeriodCh: minePeriodCh,
|
minePeriodCh: minePeriodCh,
|
||||||
|
newRoundCh: newRoundCh,
|
||||||
|
|
||||||
round2epochBlockInfo: round2epochBlockInfo,
|
round2epochBlockInfo: round2epochBlockInfo,
|
||||||
|
|
||||||
|
|
@ -902,6 +904,7 @@ func (x *XDPoS_v2) processQC(blockChainReader consensus.ChainReader, incomingQuo
|
||||||
1. Set currentRound = QC round + 1 (or TC round +1)
|
1. Set currentRound = QC round + 1 (or TC round +1)
|
||||||
2. Reset timer
|
2. Reset timer
|
||||||
3. Reset vote and timeout Pools
|
3. Reset vote and timeout Pools
|
||||||
|
4. Send signal to miner
|
||||||
*/
|
*/
|
||||||
func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainReader, round types.Round) {
|
func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainReader, round types.Round) {
|
||||||
log.Info("[setNewRound] new round and reset pools and workers", "round", round)
|
log.Info("[setNewRound] new round and reset pools and workers", "round", round)
|
||||||
|
|
@ -911,6 +914,12 @@ func (x *XDPoS_v2) setNewRound(blockChainReader consensus.ChainReader, round typ
|
||||||
x.timeoutPool.Clear()
|
x.timeoutPool.Clear()
|
||||||
// don't need to clean vote pool, we have other process to clean and it's not good to clean here, some edge case may break
|
// don't need to clean vote pool, we have other process to clean and it's not good to clean here, some edge case may break
|
||||||
// for example round gets bump during collecting vote, so we have to keep vote.
|
// for example round gets bump during collecting vote, so we have to keep vote.
|
||||||
|
|
||||||
|
// send signal to newRoundCh, but if full don't send
|
||||||
|
select {
|
||||||
|
case x.newRoundCh <- round:
|
||||||
|
default:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *XDPoS_v2) broadcastToBftChannel(msg interface{}) {
|
func (x *XDPoS_v2) broadcastToBftChannel(msg interface{}) {
|
||||||
|
|
|
||||||
|
|
@ -273,6 +273,7 @@ func (self *worker) update() {
|
||||||
minePeriod := 2
|
minePeriod := 2
|
||||||
MinePeriodCh := self.engine.(*XDPoS.XDPoS).MinePeriodCh
|
MinePeriodCh := self.engine.(*XDPoS.XDPoS).MinePeriodCh
|
||||||
defer close(MinePeriodCh)
|
defer close(MinePeriodCh)
|
||||||
|
NewRoundCh := self.engine.(*XDPoS.XDPoS).NewRoundCh
|
||||||
|
|
||||||
timeout := time.NewTimer(time.Duration(minePeriod) * time.Second)
|
timeout := time.NewTimer(time.Duration(minePeriod) * time.Second)
|
||||||
c := make(chan struct{})
|
c := make(chan struct{})
|
||||||
|
|
@ -312,6 +313,12 @@ func (self *worker) update() {
|
||||||
resetTime := getResetTime(self.chain, minePeriod, &prevReset0TimeMillisec)
|
resetTime := getResetTime(self.chain, minePeriod, &prevReset0TimeMillisec)
|
||||||
timeout.Reset(resetTime)
|
timeout.Reset(resetTime)
|
||||||
|
|
||||||
|
// Handle new round
|
||||||
|
case <-NewRoundCh:
|
||||||
|
self.commitNewWork()
|
||||||
|
resetTime := getResetTime(self.chain, minePeriod, &prevReset0TimeMillisec)
|
||||||
|
timeout.Reset(resetTime)
|
||||||
|
|
||||||
// Handle ChainSideEvent
|
// Handle ChainSideEvent
|
||||||
case <-self.chainSideCh:
|
case <-self.chainSideCh:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue