Skip to content

Commit 97395bd

Browse files
authored
Merge pull request #1209 from jbhurat/qibft
QBFT Consensus Implementation
2 parents 4758c16 + e221434 commit 97395bd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+6330
-1373
lines changed

cmd/utils/flags.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2220,8 +2220,9 @@ func MakeChain(ctx *cli.Context, stack *node.Node, readOnly bool, useExist bool)
22202220
if config.Istanbul.Epoch != 0 {
22212221
istanbulConfig.Epoch = config.Istanbul.Epoch
22222222
}
2223-
istanbulConfig.ProposerPolicy = istanbul.ProposerPolicy(config.Istanbul.ProposerPolicy)
2223+
istanbulConfig.ProposerPolicy = istanbul.NewProposerPolicy(istanbul.ProposerPolicyId(config.Istanbul.ProposerPolicy))
22242224
istanbulConfig.Ceil2Nby3Block = config.Istanbul.Ceil2Nby3Block
2225+
istanbulConfig.TestQBFTBlock = config.Istanbul.TestQBFTBlock
22252226
engine = istanbulBackend.New(istanbulConfig, stack.GetNodeKey(), chainDb)
22262227
} else if config.IsQuorum {
22272228
// for Raft

consensus/istanbul/backend.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ type Backend interface {
3636
EventMux() *event.TypeMux
3737

3838
// Broadcast sends a message to all validators (include self)
39-
Broadcast(valSet ValidatorSet, payload []byte) error
39+
Broadcast(valSet ValidatorSet, code uint64, payload []byte) error
4040

4141
// Gossip sends a message to all validators (exclude self)
42-
Gossip(valSet ValidatorSet, payload []byte) error
42+
Gossip(valSet ValidatorSet, code uint64, payload []byte) error
4343

4444
// Commit delivers an approved proposal to backend.
4545
// The delivered proposal will be put into blockchain.
46-
Commit(proposal Proposal, seals [][]byte) error
46+
Commit(proposal Proposal, seals [][]byte, round *big.Int) error
4747

4848
// Verify verifies the proposal. If a consensus.ErrFutureBlock error is returned,
4949
// the time difference of the proposal and current time is also returned.
@@ -52,6 +52,9 @@ type Backend interface {
5252
// Sign signs input data with the backend's private key
5353
Sign([]byte) ([]byte, error)
5454

55+
// SignWithoutHashing sign input data with the backend's private key without hashing the input data
56+
SignWithoutHashing([]byte) ([]byte, error)
57+
5558
// CheckSignature verifies the signature by checking if it's signed by
5659
// the given validator
5760
CheckSignature(data []byte, addr common.Address, sig []byte) error
@@ -68,8 +71,14 @@ type Backend interface {
6871
// ParentValidators returns the validator set of the given proposal's parent block
6972
ParentValidators(proposal Proposal) ValidatorSet
7073

71-
// HasBadBlock returns whether the block with the hash is a bad block
74+
// HasBadProposal returns whether the block with the hash is a bad block
7275
HasBadProposal(hash common.Hash) bool
7376

7477
Close() error
78+
79+
// IsQBFTConsensus checks qbftBlock fork block and returns if it should be enabled
80+
IsQBFTConsensusAt(*big.Int) bool
81+
82+
// StartQBFTConsensus stops existing legacy ibft consensus and starts the new qbft consensus
83+
StartQBFTConsensus() error
7584
}

consensus/istanbul/backend/api.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import (
2121

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/consensus"
24+
istanbulcommon "github.com/ethereum/go-ethereum/consensus/istanbul/common"
2425
"github.com/ethereum/go-ethereum/core/types"
2526
"github.com/ethereum/go-ethereum/rpc"
2627
)
2728

2829
// API is a user facing RPC API to dump Istanbul state
2930
type API struct {
30-
chain consensus.ChainHeaderReader
31-
istanbul *backend
31+
chain consensus.ChainHeaderReader
32+
backend *Backend
3233
}
3334

3435
// BlockSigners is contains who created and who signed a particular block, denoted by its number and hash
@@ -46,7 +47,7 @@ type Status struct {
4647

4748
// NodeAddress returns the public address that is used to sign block headers in IBFT
4849
func (api *API) NodeAddress() common.Address {
49-
return api.istanbul.Address()
50+
return api.backend.Address()
5051
}
5152

5253
// GetSignersFromBlock returns the signers and minter for a given block number, or the
@@ -61,7 +62,7 @@ func (api *API) GetSignersFromBlock(number *rpc.BlockNumber) (*BlockSigners, err
6162
}
6263

6364
if header == nil {
64-
return nil, errUnknownBlock
65+
return nil, istanbulcommon.ErrUnknownBlock
6566
}
6667

6768
return api.signers(header)
@@ -71,19 +72,19 @@ func (api *API) GetSignersFromBlock(number *rpc.BlockNumber) (*BlockSigners, err
7172
func (api *API) GetSignersFromBlockByHash(hash common.Hash) (*BlockSigners, error) {
7273
header := api.chain.GetHeaderByHash(hash)
7374
if header == nil {
74-
return nil, errUnknownBlock
75+
return nil, istanbulcommon.ErrUnknownBlock
7576
}
7677

7778
return api.signers(header)
7879
}
7980

8081
func (api *API) signers(header *types.Header) (*BlockSigners, error) {
81-
author, err := api.istanbul.Author(header)
82+
author, err := api.backend.Author(header)
8283
if err != nil {
8384
return nil, err
8485
}
8586

86-
committers, err := api.istanbul.Signers(header)
87+
committers, err := api.backend.Signers(header)
8788
if err != nil {
8889
return nil, err
8990
}
@@ -107,18 +108,18 @@ func (api *API) GetSnapshot(number *rpc.BlockNumber) (*Snapshot, error) {
107108
}
108109
// Ensure we have an actually valid block and return its snapshot
109110
if header == nil {
110-
return nil, errUnknownBlock
111+
return nil, istanbulcommon.ErrUnknownBlock
111112
}
112-
return api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
113+
return api.backend.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
113114
}
114115

115116
// GetSnapshotAtHash retrieves the state snapshot at a given block.
116117
func (api *API) GetSnapshotAtHash(hash common.Hash) (*Snapshot, error) {
117118
header := api.chain.GetHeaderByHash(hash)
118119
if header == nil {
119-
return nil, errUnknownBlock
120+
return nil, istanbulcommon.ErrUnknownBlock
120121
}
121-
return api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
122+
return api.backend.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
122123
}
123124

124125
// GetValidators retrieves the list of authorized validators at the specified block.
@@ -132,9 +133,9 @@ func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error)
132133
}
133134
// Ensure we have an actually valid block and return the validators from its snapshot
134135
if header == nil {
135-
return nil, errUnknownBlock
136+
return nil, istanbulcommon.ErrUnknownBlock
136137
}
137-
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
138+
snap, err := api.backend.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
138139
if err != nil {
139140
return nil, err
140141
}
@@ -145,9 +146,9 @@ func (api *API) GetValidators(number *rpc.BlockNumber) ([]common.Address, error)
145146
func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error) {
146147
header := api.chain.GetHeaderByHash(hash)
147148
if header == nil {
148-
return nil, errUnknownBlock
149+
return nil, istanbulcommon.ErrUnknownBlock
149150
}
150-
snap, err := api.istanbul.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
151+
snap, err := api.backend.snapshot(api.chain, header.Number.Uint64(), header.Hash(), nil)
151152
if err != nil {
152153
return nil, err
153154
}
@@ -156,11 +157,11 @@ func (api *API) GetValidatorsAtHash(hash common.Hash) ([]common.Address, error)
156157

157158
// Candidates returns the current candidates the node tries to uphold and vote on.
158159
func (api *API) Candidates() map[common.Address]bool {
159-
api.istanbul.candidatesLock.RLock()
160-
defer api.istanbul.candidatesLock.RUnlock()
160+
api.backend.candidatesLock.RLock()
161+
defer api.backend.candidatesLock.RUnlock()
161162

162163
proposals := make(map[common.Address]bool)
163-
for address, auth := range api.istanbul.candidates {
164+
for address, auth := range api.backend.candidates {
164165
proposals[address] = auth
165166
}
166167
return proposals
@@ -169,19 +170,19 @@ func (api *API) Candidates() map[common.Address]bool {
169170
// Propose injects a new authorization candidate that the validator will attempt to
170171
// push through.
171172
func (api *API) Propose(address common.Address, auth bool) {
172-
api.istanbul.candidatesLock.Lock()
173-
defer api.istanbul.candidatesLock.Unlock()
173+
api.backend.candidatesLock.Lock()
174+
defer api.backend.candidatesLock.Unlock()
174175

175-
api.istanbul.candidates[address] = auth
176+
api.backend.candidates[address] = auth
176177
}
177178

178179
// Discard drops a currently running candidate, stopping the validator from casting
179180
// further votes (either for or against).
180181
func (api *API) Discard(address common.Address) {
181-
api.istanbul.candidatesLock.Lock()
182-
defer api.istanbul.candidatesLock.Unlock()
182+
api.backend.candidatesLock.Lock()
183+
defer api.backend.candidatesLock.Unlock()
183184

184-
delete(api.istanbul.candidates, address)
185+
delete(api.backend.candidates, address)
185186
}
186187

187188
func (api *API) Status(startBlockNum *rpc.BlockNumber, endBlockNum *rpc.BlockNumber) (*Status, error) {
@@ -264,7 +265,7 @@ func (api *API) IsValidator(blockNum *rpc.BlockNumber) (bool, error) {
264265
s, _ := api.GetValidators(&blockNumber)
265266

266267
for _, v := range s {
267-
if v == api.istanbul.address {
268+
if v == api.backend.address {
268269
return true, nil
269270
}
270271
}

0 commit comments

Comments
 (0)