core/vote: wait some blocks beforing voting since mining begin

This commit is contained in:
buddh0 2023-12-08 18:43:13 +08:00 committed by buddho
parent 4b107c5303
commit b3df096358
2 changed files with 10 additions and 1 deletions

@ -15,6 +15,8 @@ import (
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
) )
const blocksNumberSinceMining = 5 // the number of blocks need to wait before voting, counting from the validator begin to mine
var votesManagerCounter = metrics.NewRegisteredCounter("votesManager/local", nil) var votesManagerCounter = metrics.NewRegisteredCounter("votesManager/local", nil)
// Backend wraps all methods required for voting. // Backend wraps all methods required for voting.
@ -95,6 +97,7 @@ func (voteManager *VoteManager) loop() {
dlEventCh := events.Chan() dlEventCh := events.Chan()
startVote := true startVote := true
blockCountSinceMining := 0
var once sync.Once var once sync.Once
for { for {
select { select {
@ -120,9 +123,15 @@ func (voteManager *VoteManager) loop() {
continue continue
} }
if !voteManager.eth.IsMining() { if !voteManager.eth.IsMining() {
blockCountSinceMining = 0
log.Debug("skip voting because mining is disabled, continue") log.Debug("skip voting because mining is disabled, continue")
continue continue
} }
blockCountSinceMining++
if blockCountSinceMining <= blocksNumberSinceMining {
log.Debug("skip voting", "blockCountSinceMining", blockCountSinceMining, "blocksNumberSinceMining", blocksNumberSinceMining)
continue
}
if cHead.Block == nil { if cHead.Block == nil {
log.Debug("cHead.Block is nil, continue") log.Debug("cHead.Block is nil, continue")

@ -190,7 +190,7 @@ func testVotePool(t *testing.T, isValidRules bool) {
if _, err := chain.InsertChain(bs); err != nil { if _, err := chain.InsertChain(bs); err != nil {
panic(err) panic(err)
} }
for i := 0; i < 10; i++ { for i := 0; i < 10+blocksNumberSinceMining; i++ {
bs, _ = core.GenerateChain(params.TestChainConfig, bs[len(bs)-1], ethash.NewFaker(), db, 1, nil) bs, _ = core.GenerateChain(params.TestChainConfig, bs[len(bs)-1], ethash.NewFaker(), db, 1, nil)
if _, err := chain.InsertChain(bs); err != nil { if _, err := chain.InsertChain(bs); err != nil {
panic(err) panic(err)