Skip to content

Commit

Permalink
api: support enable/disable voting via rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
buddh0 committed Aug 13, 2024
1 parent 5c0fe4f commit d1c492d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/vote/vote_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var notContinuousJustified = metrics.NewRegisteredCounter("votesManager/notConti
// Backend wraps all methods required for voting.
type Backend interface {
IsMining() bool
VoteEnabled() bool
EventMux() *event.TypeMux
}

Expand Down Expand Up @@ -136,6 +137,11 @@ func (voteManager *VoteManager) loop() {
log.Debug("skip voting because mining is disabled, continue")
continue
}
if !voteManager.eth.VoteEnabled() {
log.Debug("skip voting because voting is disabled, continue")
continue
}

blockCountSinceMining++
if blockCountSinceMining <= blocksNumberSinceMining {
log.Debug("skip voting", "blockCountSinceMining", blockCountSinceMining, "blocksNumberSinceMining", blocksNumberSinceMining)
Expand Down
1 change: 1 addition & 0 deletions core/vote/vote_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func newTestBackend() *testBackend {
return &testBackend{eventMux: new(event.TypeMux)}
}
func (b *testBackend) IsMining() bool { return true }
func (b *testBackend) VoteEnabled() bool { return true }

Check failure on line 80 in core/vote/vote_pool_test.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

File is not `goimports`-ed (goimports)
func (b *testBackend) EventMux() *event.TypeMux { return b.eventMux }

func (p *mockPOSA) GetJustifiedNumberAndHash(chain consensus.ChainHeaderReader, headers []*types.Header) (uint64, common.Hash, error) {
Expand Down
5 changes: 5 additions & 0 deletions eth/api_miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func (api *MinerAPI) SetDoubleSign(on bool) miner.MBConfig {
return api.e.Miner().MBConfig()
}

func (api *MinerAPI) SetVoteDisable(on bool) miner.MBConfig {
api.e.Miner().SetVoteDisable(on)
return api.e.Miner().MBConfig()
}

func (api *MinerAPI) SetSkipOffsetInturn(offset uint64) miner.MBConfig {
api.e.Miner().SetSkipOffsetInturn(offset)
return api.e.Miner().MBConfig()
Expand Down
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ func (s *Ethereum) StopMining() {
}

func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
func (s *Ethereum) VoteEnabled() bool { return s.miner.VoteEnabled() }

Check failure on line 624 in eth/backend.go

View workflow job for this annotation

GitHub Actions / golang-lint (1.21.x, ubuntu-latest)

File is not `goimports`-ed (goimports)
func (s *Ethereum) Miner() *miner.Miner { return s.miner }

func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager }
Expand Down
6 changes: 6 additions & 0 deletions miner/gen_mb_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions miner/malicious_behaviour.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package miner
type MBConfig struct {
// Generate two consecutive blocks for the same parent block
DoubleSign bool
// Disable voting for Fast Finality
VoteDisable bool
// Skip block production for in-turn validators at a specified offset
SkipOffsetInturn *uint64 `toml:",omitempty"`
// Delay broadcasting mined blocks by a specified number of blocks, only for in turn validators
Expand All @@ -14,6 +16,7 @@ type MBConfig struct {

var DefaultMBConfig = MBConfig{
DoubleSign: false,
VoteDisable: false,
BroadcastDelayBlocks: 0,
LastBlockMiningTime: 0,
}
8 changes: 8 additions & 0 deletions miner/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func (miner *Miner) Mining() bool {
return miner.worker.isRunning()
}

func (miner *Miner) VoteEnabled() bool {
return miner.worker.config.VoteEnable && !miner.worker.config.MB.VoteDisable
}

func (miner *Miner) InTurn() bool {
return miner.worker.inTurn()
}
Expand Down Expand Up @@ -299,6 +303,10 @@ func (miner *Miner) SetDoubleSign(on bool) {
miner.worker.config.MB.DoubleSign = on
}

func (miner *Miner) SetVoteDisable(on bool) {
miner.worker.config.MB.VoteDisable = on
}

func (miner *Miner) SetSkipOffsetInturn(offset uint64) {
miner.worker.config.MB.SkipOffsetInturn = &offset
}
Expand Down

0 comments on commit d1c492d

Please sign in to comment.