Skip to content

Commit 80258ff

Browse files
committed
miner: add malicious miner feature for test
1 parent 9b0130f commit 80258ff

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
@@ -1559,6 +1559,23 @@ func (p *Parlia) Delay(chain consensus.ChainReader, header *types.Header, leftOv
15591559
return &delay
15601560
}
15611561

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

2138+
for i := uint64(0); i < uint64(n); i++ {
2139+
log.Debug("backOffTime", "Number", header.Number, "val", validators[i], "delay", delay+backOffSteps[i]*wiggleTime)
2140+
}
2141+
21212142
delay += backOffSteps[idx] * wiggleTime
21222143
return delay
21232144
}

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
@@ -535,6 +535,7 @@ func (s *Ethereum) StopMining() {
535535
}
536536

537537
func (s *Ethereum) IsMining() bool { return s.miner.Mining() }
538+
func (s *Ethereum) VoteEnabled() bool { return s.miner.VoteEnabled() }
538539
func (s *Ethereum) Miner() *miner.Miner { return s.miner }
539540

540541
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
}
@@ -225,6 +229,34 @@ func (miner *Miner) SetGasCeil(ceil uint64) {
225229
miner.worker.setGasCeil(ceil)
226230
}
227231

232+
func (miner *Miner) MBConfig() minerconfig.MBConfig {
233+
return miner.worker.config.MB
234+
}
235+
236+
func (miner *Miner) ResetMaliciousBehavior() {
237+
miner.worker.config.MB = minerconfig.DefaultMBConfig
238+
}
239+
240+
func (miner *Miner) SetDoubleSign(on bool) {
241+
miner.worker.config.MB.DoubleSign = on
242+
}
243+
244+
func (miner *Miner) SetVoteDisable(on bool) {
245+
miner.worker.config.MB.VoteDisable = on
246+
}
247+
248+
func (miner *Miner) SetSkipOffsetInturn(offset uint64) {
249+
miner.worker.config.MB.SkipOffsetInturn = &offset
250+
}
251+
252+
func (miner *Miner) SetBroadcastDelayBlocks(num uint64) {
253+
miner.worker.config.MB.BroadcastDelayBlocks = num
254+
}
255+
256+
func (miner *Miner) SetLastBlockMiningTime(time uint64) {
257+
miner.worker.config.MB.LastBlockMiningTime = time
258+
}
259+
228260
// SubscribePendingLogs starts delivering logs from pending transactions
229261
// to the given channel.
230262
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
@@ -40,6 +40,7 @@ type Config struct {
4040
DisableVoteAttestation bool // Whether to skip assembling vote attestation
4141

4242
Mev MevConfig // Mev configuration
43+
MB MBConfig // Malicious behavior configuration
4344
}
4445

4546
// DefaultConfig contains default settings for miner.
@@ -55,6 +56,7 @@ var DefaultConfig = Config{
5556
DelayLeftOver: 50 * time.Millisecond,
5657

5758
Mev: DefaultMevConfig,
59+
MB: DefaultMBConfig,
5860
}
5961

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

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)