Skip to content

Commit

Permalink
core/vote: wait some blocks beforing voting since mining begin
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Dec 13, 2023
1 parent 4b107c5 commit b3df096
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions core/vote/vote_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"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)

// Backend wraps all methods required for voting.
Expand Down Expand Up @@ -95,6 +97,7 @@ func (voteManager *VoteManager) loop() {
dlEventCh := events.Chan()

startVote := true
blockCountSinceMining := 0
var once sync.Once
for {
select {
Expand All @@ -120,9 +123,15 @@ func (voteManager *VoteManager) loop() {
continue
}
if !voteManager.eth.IsMining() {
blockCountSinceMining = 0
log.Debug("skip voting because mining is disabled, continue")
continue
}
blockCountSinceMining++
if blockCountSinceMining <= blocksNumberSinceMining {
log.Debug("skip voting", "blockCountSinceMining", blockCountSinceMining, "blocksNumberSinceMining", blocksNumberSinceMining)
continue
}

if cHead.Block == nil {
log.Debug("cHead.Block is nil, continue")
Expand Down
2 changes: 1 addition & 1 deletion core/vote/vote_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func testVotePool(t *testing.T, isValidRules bool) {
if _, err := chain.InsertChain(bs); err != nil {
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)
if _, err := chain.InsertChain(bs); err != nil {
panic(err)
Expand Down

0 comments on commit b3df096

Please sign in to comment.