From d1c492d21da4942471f4d3b898cae675bd3affe4 Mon Sep 17 00:00:00 2001 From: buddh0 Date: Tue, 13 Aug 2024 17:59:52 +0800 Subject: [PATCH] api: support enable/disable voting via rpc --- core/vote/vote_manager.go | 6 ++++++ core/vote/vote_pool_test.go | 1 + eth/api_miner.go | 5 +++++ eth/backend.go | 1 + miner/gen_mb_config.go | 6 ++++++ miner/malicious_behaviour.go | 3 +++ miner/miner.go | 8 ++++++++ 7 files changed, 30 insertions(+) diff --git a/core/vote/vote_manager.go b/core/vote/vote_manager.go index 891785482b..35b3412f47 100644 --- a/core/vote/vote_manager.go +++ b/core/vote/vote_manager.go @@ -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 } @@ -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) diff --git a/core/vote/vote_pool_test.go b/core/vote/vote_pool_test.go index bb8374e90f..68ee4e8ae7 100644 --- a/core/vote/vote_pool_test.go +++ b/core/vote/vote_pool_test.go @@ -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 } func (b *testBackend) EventMux() *event.TypeMux { return b.eventMux } func (p *mockPOSA) GetJustifiedNumberAndHash(chain consensus.ChainHeaderReader, headers []*types.Header) (uint64, common.Hash, error) { diff --git a/eth/api_miner.go b/eth/api_miner.go index d713006509..4e83eaabb5 100644 --- a/eth/api_miner.go +++ b/eth/api_miner.go @@ -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() diff --git a/eth/backend.go b/eth/backend.go index 0d434a9130..686a2b3460 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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() } func (s *Ethereum) Miner() *miner.Miner { return s.miner } func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager } diff --git a/miner/gen_mb_config.go b/miner/gen_mb_config.go index cd2c8158b6..32e2a1464f 100644 --- a/miner/gen_mb_config.go +++ b/miner/gen_mb_config.go @@ -6,12 +6,14 @@ package miner func (m MBConfig) MarshalTOML() (interface{}, error) { type MBConfig struct { DoubleSign bool + VoteDisable bool SkipOffsetInturn *uint64 `toml:",omitempty"` BroadcastDelayBlocks uint64 LastBlockMiningTime uint64 } var enc MBConfig enc.DoubleSign = m.DoubleSign + enc.VoteDisable = m.VoteDisable enc.SkipOffsetInturn = m.SkipOffsetInturn enc.BroadcastDelayBlocks = m.BroadcastDelayBlocks enc.LastBlockMiningTime = m.LastBlockMiningTime @@ -22,6 +24,7 @@ func (m MBConfig) MarshalTOML() (interface{}, error) { func (m *MBConfig) UnmarshalTOML(unmarshal func(interface{}) error) error { type MBConfig struct { DoubleSign *bool + VoteDisable *bool SkipOffsetInturn *uint64 `toml:",omitempty"` BroadcastDelayBlocks *uint64 LastBlockMiningTime *uint64 @@ -33,6 +36,9 @@ func (m *MBConfig) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.DoubleSign != nil { m.DoubleSign = *dec.DoubleSign } + if dec.VoteDisable != nil { + m.VoteDisable = *dec.VoteDisable + } if dec.SkipOffsetInturn != nil { m.SkipOffsetInturn = dec.SkipOffsetInturn } diff --git a/miner/malicious_behaviour.go b/miner/malicious_behaviour.go index 0fb7e4fc8f..13aad5542b 100644 --- a/miner/malicious_behaviour.go +++ b/miner/malicious_behaviour.go @@ -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 @@ -14,6 +16,7 @@ type MBConfig struct { var DefaultMBConfig = MBConfig{ DoubleSign: false, + VoteDisable: false, BroadcastDelayBlocks: 0, LastBlockMiningTime: 0, } diff --git a/miner/miner.go b/miner/miner.go index a4b01724e7..a8c8d0a526 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -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() } @@ -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 }