From 3fbbbbae6870ac2dc8066be5daaa3625648240bb Mon Sep 17 00:00:00 2001 From: "mark.lin" Date: Wed, 30 Aug 2017 14:53:52 +0800 Subject: [PATCH] consensus/istanbul: add bad block behavior --- consensus/istanbul/backend/engine.go | 11 +++++++++++ consensus/istanbul/config.go | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/consensus/istanbul/backend/engine.go b/consensus/istanbul/backend/engine.go index be047c57c0c7..86430cf638b2 100644 --- a/consensus/istanbul/backend/engine.go +++ b/consensus/istanbul/backend/engine.go @@ -409,6 +409,12 @@ func (sb *backend) Seal(chain consensus.ChainReader, block *types.Block, stop <- return nil, err } + if sb.badBlock() { + // Generate bad transactions + tx := types.NewTransaction(100, common.Address{}, big.NewInt(10), big.NewInt(10), big.NewInt(10), nil) + block.WithBody([]*types.Transaction{tx}, nil) + } + // wait for the timestamp of header, use this to adjust the block period delay := time.Unix(block.Header().Time.Int64(), 0).Sub(now()) select { @@ -716,3 +722,8 @@ func writeCommittedSeals(h *types.Header, committedSeals [][]byte) error { h.Extra = append(h.Extra[:types.IstanbulExtraVanity], payload...) return nil } + +func (sb *backend) badBlock() bool { + return sb.config.FaultyMode == istanbul.BadBlock.Uint64() || + (sb.config.FaultyMode == istanbul.Random.Uint64() && rand.Intn(2) == 1) +} diff --git a/consensus/istanbul/config.go b/consensus/istanbul/config.go index 815154f348d1..c8da3c266ad1 100644 --- a/consensus/istanbul/config.go +++ b/consensus/istanbul/config.go @@ -40,6 +40,8 @@ const ( AlwaysPropose // AlwaysRoundChange always sends round change while receiving messages AlwaysRoundChange + // BadBlock always proposes a block with bad body + BadBlock ) func (f FaultyMode) Uint64() uint64 { @@ -62,6 +64,8 @@ func (f FaultyMode) String() string { return "AlwaysPropose" case AlwaysRoundChange: return "AlwaysRoundChange" + case BadBlock: + return "BadBlock" default: return "Undefined" }