Skip to content

Commit e6134a4

Browse files
NathanBSCbuddh0
authored andcommitted
miner: add malicious miner feature for test
1 parent d41d601 commit e6134a4

10 files changed

Lines changed: 301 additions & 1 deletion

File tree

consensus/parlia/parlia.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,23 @@ func (p *Parlia) Delay(chain consensus.ChainReader, header *types.Header, leftOv
15601560
return &delay
15611561
}
15621562

1563+
// AssembleSignature assemble the signature for block header
1564+
func (p *Parlia) AssembleSignature(block *types.Block) (*types.Block, error) {
1565+
header := block.Header()
1566+
// Don't hold the val fields for the entire sealing procedure
1567+
p.lock.RLock()
1568+
val, signFn := p.val, p.signFn
1569+
p.lock.RUnlock()
1570+
sig, err := signFn(accounts.Account{Address: val}, accounts.MimetypeParlia, ParliaRLP(header, p.chainConfig.ChainID))
1571+
if err != nil {
1572+
log.Error("Sign for the block header failed when sealing", "err", err)
1573+
return nil, err
1574+
}
1575+
copy(header.Extra[len(header.Extra)-extraSeal:], sig)
1576+
block = block.WithSeal(header)
1577+
return block, nil
1578+
}
1579+
15631580
// Seal implements consensus.Engine, attempting to create a sealed block using
15641581
// the local signing credentials.
15651582
func (p *Parlia) Seal(chain consensus.ChainHeaderReader, block *types.Block, results chan<- *types.Block, stop <-chan struct{}) error {
@@ -2154,6 +2171,10 @@ func (p *Parlia) backOffTime(snap *Snapshot, header *types.Header, val common.Ad
21542171
backOffSteps[i], backOffSteps[j] = backOffSteps[j], backOffSteps[i]
21552172
})
21562173

2174+
for i := uint64(0); i < uint64(n); i++ {
2175+
log.Debug("backOffTime", "Number", header.Number, "val", validators[i], "delay", delay+backOffSteps[i]*wiggleTime)
2176+
}
2177+
21572178
delay += backOffSteps[idx] * wiggleTime
21582179
return delay
21592180
}

core/types/block.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package types
2020
import (
2121
"crypto/sha256"
2222
"encoding/binary"
23+
"encoding/json"
2324
"fmt"
2425
"io"
2526
"math/big"
@@ -604,6 +605,14 @@ func (b *Block) WithWitness(witness *ExecutionWitness) *Block {
604605
}
605606
}
606607

608+
func (b *Block) DeepCopySidecars(sidecars BlobSidecars) {
609+
b.sidecars = make(BlobSidecars, len(sidecars))
610+
if len(sidecars) != 0 {
611+
buffer, _ := json.Marshal(sidecars)
612+
json.Unmarshal(buffer, &b.sidecars)
613+
}
614+
}
615+
607616
// Hash returns the keccak256 hash of b's header.
608617
// The hash is computed on the first call and cached thereafter.
609618
func (b *Block) Hash() common.Hash {

core/vote/vote_manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var notContinuousJustified = metrics.NewRegisteredCounter("votesManager/notConti
3030
// Backend wraps all methods required for voting.
3131
type Backend interface {
3232
IsMining() bool
33+
VoteEnabled() bool
3334
EventMux() *event.TypeMux
3435
}
3536

@@ -135,6 +136,11 @@ func (voteManager *VoteManager) loop() {
135136
log.Debug("skip voting because mining is disabled, continue")
136137
continue
137138
}
139+
if !voteManager.eth.VoteEnabled() {
140+
log.Debug("skip voting because voting is disabled, continue")
141+
continue
142+
}
143+
138144
blockCountSinceMining++
139145
if blockCountSinceMining <= blocksNumberSinceMining {
140146
log.Debug("skip voting", "blockCountSinceMining", blockCountSinceMining, "blocksNumberSinceMining", blocksNumberSinceMining)

core/vote/vote_pool_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func newTestBackend() *testBackend {
7777
return &testBackend{eventMux: new(event.TypeMux)}
7878
}
7979
func (b *testBackend) IsMining() bool { return true }
80+
func (b *testBackend) VoteEnabled() bool { return true }
8081
func (b *testBackend) EventMux() *event.TypeMux { return b.eventMux }
8182

8283
func (mp *mockPOSA) GetJustifiedNumberAndHash(chain consensus.ChainHeaderReader, headers []*types.Header) (uint64, common.Hash, error) {

eth/api_miner.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"math/big"
2121
"time"
2222

23+
"github.com/ethereum/go-ethereum/miner/minerconfig"
2324
"github.com/ethereum/go-ethereum/params"
2425

2526
"github.com/ethereum/go-ethereum/common"
@@ -117,3 +118,37 @@ func (api *MinerAPI) AddBuilder(builder common.Address, url string) error {
117118
func (api *MinerAPI) RemoveBuilder(builder common.Address) error {
118119
return api.e.APIBackend.RemoveBuilder(builder)
119120
}
121+
122+
func (api *MinerAPI) MBConfig() minerconfig.MBConfig {
123+
return api.e.Miner().MBConfig()
124+
}
125+
126+
func (api *MinerAPI) ResetMaliciousBehavior() minerconfig.MBConfig {
127+
api.e.Miner().ResetMaliciousBehavior()
128+
return api.e.Miner().MBConfig()
129+
}
130+
131+
func (api *MinerAPI) SetDoubleSign(on bool) minerconfig.MBConfig {
132+
api.e.Miner().SetDoubleSign(on)
133+
return api.e.Miner().MBConfig()
134+
}
135+
136+
func (api *MinerAPI) SetVoteDisable(on bool) minerconfig.MBConfig {
137+
api.e.Miner().SetVoteDisable(on)
138+
return api.e.Miner().MBConfig()
139+
}
140+
141+
func (api *MinerAPI) SetSkipOffsetInturn(offset uint64) minerconfig.MBConfig {
142+
api.e.Miner().SetSkipOffsetInturn(offset)
143+
return api.e.Miner().MBConfig()
144+
}
145+
146+
func (api *MinerAPI) SetBroadcastDelayBlocks(num uint64) minerconfig.MBConfig {
147+
api.e.Miner().SetBroadcastDelayBlocks(num)
148+
return api.e.Miner().MBConfig()
149+
}
150+
151+
func (api *MinerAPI) SetLastBlockMiningTime(time uint64) minerconfig.MBConfig {
152+
api.e.Miner().SetLastBlockMiningTime(time)
153+
return api.e.Miner().MBConfig()
154+
}

eth/backend.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ func (s *Ethereum) StopMining() {
532532
}
533533

534534
func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
535+
func (s *Ethereum) VoteEnabled() bool { return s.miner.VoteEnabled() }
535536
func (s *Ethereum) Miner() *miner.Miner { return s.miner }
536537

537538
func (s *Ethereum) AccountManager() *accounts.Manager { return s.accountManager }

miner/miner.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ func (miner *Miner) Mining() bool {
170170
return miner.worker.isRunning()
171171
}
172172

173+
func (miner *Miner) VoteEnabled() bool {
174+
return miner.worker.config.VoteEnable && !miner.worker.config.MB.VoteDisable
175+
}
176+
173177
func (miner *Miner) InTurn() bool {
174178
return miner.worker.inTurn()
175179
}
@@ -229,6 +233,34 @@ func (miner *Miner) SetGasCeil(ceil uint64) {
229233
miner.worker.setGasCeil(ceil)
230234
}
231235

236+
func (miner *Miner) MBConfig() minerconfig.MBConfig {
237+
return miner.worker.config.MB
238+
}
239+
240+
func (miner *Miner) ResetMaliciousBehavior() {
241+
miner.worker.config.MB = minerconfig.DefaultMBConfig
242+
}
243+
244+
func (miner *Miner) SetDoubleSign(on bool) {
245+
miner.worker.config.MB.DoubleSign = on
246+
}
247+
248+
func (miner *Miner) SetVoteDisable(on bool) {
249+
miner.worker.config.MB.VoteDisable = on
250+
}
251+
252+
func (miner *Miner) SetSkipOffsetInturn(offset uint64) {
253+
miner.worker.config.MB.SkipOffsetInturn = &offset
254+
}
255+
256+
func (miner *Miner) SetBroadcastDelayBlocks(num uint64) {
257+
miner.worker.config.MB.BroadcastDelayBlocks = num
258+
}
259+
260+
func (miner *Miner) SetLastBlockMiningTime(time uint64) {
261+
miner.worker.config.MB.LastBlockMiningTime = time
262+
}
263+
232264
// SubscribePendingLogs starts delivering logs from pending transactions
233265
// to the given channel.
234266
func (miner *Miner) SubscribePendingLogs(ch chan<- []*types.Log) event.Subscription {

miner/minerconfig/config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Config struct {
4141
DisableVoteAttestation bool // Whether to skip assembling vote attestation
4242

4343
Mev MevConfig // Mev configuration
44+
MB MBConfig // Malicious behavior configuration
4445
}
4546

4647
// DefaultConfig contains default settings for miner.
@@ -60,6 +61,7 @@ var DefaultConfig = Config{
6061
MaxWaitProposalInSecs: 30,
6162

6263
Mev: DefaultMevConfig,
64+
MB: DefaultMBConfig,
6365
}
6466

6567
type BuilderConfig struct {
@@ -84,3 +86,24 @@ var DefaultMevConfig = MevConfig{
8486
ValidatorCommission: 100,
8587
BidSimulationLeftOver: 50 * time.Millisecond,
8688
}
89+
90+
//go:generate go run github.com/fjl/gencodec -type MBConfig -formats toml -out gen_mb_config.go
91+
type MBConfig struct {
92+
// Generate two consecutive blocks for the same parent block
93+
DoubleSign bool
94+
// Disable voting for Fast Finality
95+
VoteDisable bool
96+
// Skip block production for in-turn validators at a specified offset
97+
SkipOffsetInturn *uint64 `toml:",omitempty"`
98+
// Delay broadcasting mined blocks by a specified number of blocks, only for in turn validators
99+
BroadcastDelayBlocks uint64
100+
// Mining time (milliseconds) for the last block in every turn
101+
LastBlockMiningTime uint64
102+
}
103+
104+
var DefaultMBConfig = MBConfig{
105+
DoubleSign: false,
106+
VoteDisable: false,
107+
BroadcastDelayBlocks: 0,
108+
LastBlockMiningTime: 0,
109+
}

miner/minerconfig/gen_mb_config.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)