Skip to content

Commit 8e45c10

Browse files
authored
Merge pull request #74 from getamis/refactor-eth-handler
Refactor protocol manager
2 parents bf29b79 + 9bbfdb8 commit 8e45c10

File tree

11 files changed

+79
-816
lines changed

11 files changed

+79
-816
lines changed

consensus/consensus.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,8 @@ type PoW interface {
107107
type Istanbul interface {
108108
Engine
109109

110-
// Add a peer
111-
AddPeer(peerID string, publicKey *ecdsa.PublicKey) error
112-
113-
// Remove a peer
114-
RemovePeer(peerID string) error
115-
116110
// Handle a message from peer
117-
HandleMsg(peerID string, data []byte) error
111+
HandleMsg(pubKey *ecdsa.PublicKey, data []byte) error
118112

119113
// Receive new chain head block
120114
NewChainHead(block *types.Block)

consensus/istanbul/backends/simple/backend.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
func New(config *istanbul.Config, eventMux *event.TypeMux, privateKey *ecdsa.PrivateKey, db ethdb.Database) consensus.Istanbul {
3737
backend := &simpleBackend{
3838
config: config,
39-
peerSet: newPeerSet(),
4039
eventMux: eventMux,
4140
istanbulEventMux: new(event.TypeMux),
4241
privateKey: privateKey,
@@ -52,7 +51,6 @@ func New(config *istanbul.Config, eventMux *event.TypeMux, privateKey *ecdsa.Pri
5251

5352
type simpleBackend struct {
5453
config *istanbul.Config
55-
peerSet *peerSet
5654
valSet istanbul.ValidatorSet
5755
eventMux *event.TypeMux
5856
istanbulEventMux *event.TypeMux
@@ -83,33 +81,27 @@ func (sb *simpleBackend) Validators() istanbul.ValidatorSet {
8381
}
8482

8583
func (sb *simpleBackend) Send(payload []byte, target common.Address) error {
86-
peer := sb.peerSet.GetByAddress(target)
87-
if peer == nil {
88-
return errInvalidPeer
89-
}
90-
9184
go sb.eventMux.Post(istanbul.ConsensusDataEvent{
92-
PeerID: peer.ID(),
85+
Target: target,
9386
Data: payload,
9487
})
9588
return nil
9689
}
9790

9891
// Broadcast implements istanbul.Backend.Send
9992
func (sb *simpleBackend) Broadcast(payload []byte) error {
100-
istanbulMsg := istanbul.MessageEvent{
101-
Payload: payload,
102-
}
103-
104-
// send to self
105-
go sb.istanbulEventMux.Post(istanbulMsg)
106-
107-
// send to other peers
108-
for _, peer := range sb.peerSet.List() {
109-
go sb.eventMux.Post(istanbul.ConsensusDataEvent{
110-
PeerID: peer.ID(),
111-
Data: payload,
112-
})
93+
for _, val := range sb.valSet.List() {
94+
if val.Address() == sb.Address() {
95+
// send to self
96+
pbftMsg := istanbul.MessageEvent{
97+
Payload: payload,
98+
}
99+
go sb.istanbulEventMux.Post(pbftMsg)
100+
101+
} else {
102+
// send to other peers
103+
sb.Send(payload, val.Address())
104+
}
113105
}
114106
return nil
115107
}

consensus/istanbul/backends/simple/engine.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/consensus/istanbul/validator"
3131
"github.com/ethereum/go-ethereum/core/state"
3232
"github.com/ethereum/go-ethereum/core/types"
33+
"github.com/ethereum/go-ethereum/crypto"
3334
"github.com/ethereum/go-ethereum/crypto/sha3"
3435
"github.com/ethereum/go-ethereum/rlp"
3536
"github.com/ethereum/go-ethereum/rpc"
@@ -359,34 +360,12 @@ func (sb *simpleBackend) APIs(chain consensus.ChainReader) []rpc.API {
359360
}}
360361
}
361362

362-
// AddPeer implements consensus.Istanbul.AddPeer
363-
func (sb *simpleBackend) AddPeer(peerID string, publicKey *ecdsa.PublicKey) error {
364-
peer := newPeer(peerID, publicKey)
365-
// check is validator
366-
if _, val := sb.valSet.GetByAddress(peer.Address()); val != nil {
367-
// add to peer set
368-
sb.peerSet.Add(peer)
369-
}
370-
return nil
371-
}
372-
373-
// RemovePeer implements consensus.Istanbul.RemovePeer
374-
func (sb *simpleBackend) RemovePeer(peerID string) error {
375-
sb.peerSet.Remove(peerID)
376-
return nil
377-
}
378-
379363
// HandleMsg implements consensus.Istanbul.HandleMsg
380-
func (sb *simpleBackend) HandleMsg(peerID string, data []byte) error {
381-
peer := sb.peerSet.Get(peerID)
382-
if peer == nil {
383-
sb.logger.Error("Not in peer set", "peerID", peerID)
384-
return errInvalidPeer
385-
}
386-
387-
if _, val := sb.valSet.GetByAddress(peer.Address()); val == nil {
388-
sb.logger.Error("Not in validator set", "peerAddr", peer.Address())
389-
return errInvalidPeer
364+
func (sb *simpleBackend) HandleMsg(pubKey *ecdsa.PublicKey, data []byte) error {
365+
addr := crypto.PubkeyToAddress(*pubKey)
366+
if _, val := sb.valSet.GetByAddress(addr); val == nil {
367+
sb.logger.Error("Not in validator set", "addr", addr)
368+
return istanbul.ErrUnauthorizedAddress
390369
}
391370

392371
go sb.istanbulEventMux.Post(istanbul.MessageEvent{

consensus/istanbul/backends/simple/peer.go

Lines changed: 0 additions & 126 deletions
This file was deleted.

consensus/istanbul/backends/simple/peer_test.go

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)