eth/protocols/eth/handler.go¶
| Type | MOD |
| Upstream Lines | 239 |
| Changed | +12 -4 |
Register PoW message handlers via versionHandlers registry
diff --git a/eth/protocols/eth/handler.go b/eth/protocols/eth/handler.go
index f7d25bd8c..dc52a786f 100644
--- a/eth/protocols/eth/handler.go
+++ b/eth/protocols/eth/handler.go
@@ -102,9 +102,9 @@ type TxPool interface {
}
// MakeProtocols constructs the P2P protocol definitions for `eth`.
-func MakeProtocols(backend Backend, network uint64, disc enode.Iterator) []p2p.Protocol {
- protocols := make([]p2p.Protocol, 0, len(ProtocolVersions))
- for _, version := range ProtocolVersions {
+func MakeProtocols(backend Backend, network uint64, disc enode.Iterator, versions []uint) []p2p.Protocol {
+ protocols := make([]p2p.Protocol, 0, len(versions))
+ for _, version := range versions {
protocols = append(protocols, p2p.Protocol{
Name: ProtocolName,
Version: version,
@@ -169,6 +169,10 @@ type Decoder interface {
Decode(val interface{}) error
}
+// versionHandlers maps protocol versions to their message handler maps.
+// PoW protocol versions (e.g. ETH68) are registered via init() in _pow.go files.
+var versionHandlers = map[uint]map[uint64]msgHandler{}
+
var eth69 = map[uint64]msgHandler{
TransactionsMsg: handleTransactions,
NewPooledTransactionHashesMsg: handleNewPooledTransactionHashes,
@@ -217,7 +221,11 @@ func handleMessage(backend Backend, peer *Peer) error {
case ETH70:
handlers = eth70
default:
- return fmt.Errorf("unknown eth protocol version: %v", peer.version)
+ if h, ok := versionHandlers[peer.version]; ok {
+ handlers = h
+ } else {
+ return fmt.Errorf("unknown eth protocol version: %v", peer.version)
+ }
}
// Track the amount of time it takes to serve the request and run the handler