From 983f92368bdd79c65082ac2c98cbc0d58b28b22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20Szil=C3=A1gyi?= Date: Mon, 8 Jul 2019 18:53:47 +0300 Subject: core/forkid: implement the forkid EIP, announce via ENR (#19738) * eth: chain config (genesis + fork) ENR entry * core/forkid, eth: protocol independent fork ID, update to CRC32 spec * core/forkid, eth: make forkid a struct, next uint64, enr struct, RLP * core/forkid: change forkhash rlp encoding from int to [4]byte * eth: fixup eth entry a bit and update it every block * eth: fix lint * eth: fix crash in ethclient tests --- eth/backend.go | 20 +++++++++--- eth/enr_entry.go | 61 +++++++++++++++++++++++++++++++++++ eth/handler.go | 91 +++++++++++++++++++++++------------------------------ eth/handler_test.go | 29 ----------------- eth/peer.go | 4 +-- eth/protocol.go | 12 +++---- 6 files changed, 123 insertions(+), 94 deletions(-) create mode 100644 eth/enr_entry.go (limited to 'eth') diff --git a/eth/backend.go b/eth/backend.go index a3275caca..dc4ff8ade 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -47,6 +47,7 @@ import ( "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" + "github.com/ethereum/go-ethereum/p2p/enr" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" @@ -66,7 +67,9 @@ type Ethereum struct { config *Config // Channel for shutting down the service - shutdownChan chan bool // Channel for shutting down the Ethereum + shutdownChan chan bool + + server *p2p.Server // Handlers txPool *core.TxPool @@ -496,7 +499,7 @@ func (s *Ethereum) EventMux() *event.TypeMux { return s.eventMux } func (s *Ethereum) Engine() consensus.Engine { return s.engine } func (s *Ethereum) ChainDb() ethdb.Database { return s.chainDb } func (s *Ethereum) IsListening() bool { return true } // Always listening -func (s *Ethereum) EthVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } +func (s *Ethereum) EthVersion() int { return int(ProtocolVersions[0]) } func (s *Ethereum) NetVersion() uint64 { return s.networkID } func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader } func (s *Ethereum) Synced() bool { return atomic.LoadUint32(&s.protocolManager.acceptTxs) == 1 } @@ -505,15 +508,22 @@ func (s *Ethereum) ArchiveMode() bool { return s.config.NoPruni // Protocols implements node.Service, returning all the currently configured // network protocols to start. func (s *Ethereum) Protocols() []p2p.Protocol { - if s.lesServer == nil { - return s.protocolManager.SubProtocols + protos := make([]p2p.Protocol, len(ProtocolVersions)) + for i, vsn := range ProtocolVersions { + protos[i] = s.protocolManager.makeProtocol(vsn) + protos[i].Attributes = []enr.Entry{s.currentEthEntry()} + } + if s.lesServer != nil { + protos = append(protos, s.lesServer.Protocols()...) } - return append(s.protocolManager.SubProtocols, s.lesServer.Protocols()...) + return protos } // Start implements node.Service, starting all internal goroutines needed by the // Ethereum protocol implementation. func (s *Ethereum) Start(srvr *p2p.Server) error { + s.startEthEntryUpdate(srvr.LocalNode()) + // Start the bloom bits servicing goroutines s.startBloomHandlers(params.BloomBitsBlocks) diff --git a/eth/enr_entry.go b/eth/enr_entry.go new file mode 100644 index 000000000..d9e7b9578 --- /dev/null +++ b/eth/enr_entry.go @@ -0,0 +1,61 @@ +// Copyright 2019 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package eth + +import ( + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/forkid" + "github.com/ethereum/go-ethereum/p2p/enode" + "github.com/ethereum/go-ethereum/rlp" +) + +// ethEntry is the "eth" ENR entry which advertises eth protocol +// on the discovery network. +type ethEntry struct { + ForkID forkid.ID // Fork identifier per EIP-2124 + + // Ignore additional fields (for forward compatibility). + Rest []rlp.RawValue `rlp:"tail"` +} + +// ENRKey implements enr.Entry. +func (e ethEntry) ENRKey() string { + return "eth" +} + +func (eth *Ethereum) startEthEntryUpdate(ln *enode.LocalNode) { + var newHead = make(chan core.ChainHeadEvent, 10) + sub := eth.blockchain.SubscribeChainHeadEvent(newHead) + + go func() { + defer sub.Unsubscribe() + for { + select { + case <-newHead: + ln.Set(eth.currentEthEntry()) + case <-sub.Err(): + // Would be nice to sync with eth.Stop, but there is no + // good way to do that. + return + } + } + }() +} + +func (eth *Ethereum) currentEthEntry() *ethEntry { + return ðEntry{ForkID: forkid.NewID(eth.blockchain)} +} diff --git a/eth/handler.go b/eth/handler.go index fe9f8b53b..4ce2d1c82 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -58,10 +58,6 @@ var ( syncChallengeTimeout = 15 * time.Second // Time allowance for a node to reply to the sync progress challenge ) -// errIncompatibleConfig is returned if the requested protocols and configs are -// not compatible (low protocol version restrictions and high requirements). -var errIncompatibleConfig = errors.New("incompatible configuration") - func errResp(code errCode, format string, v ...interface{}) error { return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...)) } @@ -75,17 +71,14 @@ type ProtocolManager struct { checkpointNumber uint64 // Block number for the sync progress validator to cross reference checkpointHash common.Hash // Block hash for the sync progress validator to cross reference - txpool txPool - blockchain *core.BlockChain - chainconfig *params.ChainConfig - maxPeers int + txpool txPool + blockchain *core.BlockChain + maxPeers int downloader *downloader.Downloader fetcher *fetcher.Fetcher peers *peerSet - SubProtocols []p2p.Protocol - eventMux *event.TypeMux txsCh chan core.NewTxsEvent txsSub event.Subscription @@ -113,7 +106,6 @@ func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCh eventMux: mux, txpool: txpool, blockchain: blockchain, - chainconfig: config, peers: newPeerSet(), whitelist: whitelist, newPeerCh: make(chan *peer), @@ -149,45 +141,7 @@ func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCh manager.checkpointNumber = (checkpoint.SectionIndex+1)*params.CHTFrequency - 1 manager.checkpointHash = checkpoint.SectionHead } - // Initiate a sub-protocol for every implemented version we can handle - manager.SubProtocols = make([]p2p.Protocol, 0, len(ProtocolVersions)) - for i, version := range ProtocolVersions { - // Skip protocol version if incompatible with the mode of operation - // TODO(karalabe): hard-drop eth/62 from the code base - if atomic.LoadUint32(&manager.fastSync) == 1 && version < eth63 { - continue - } - // Compatible; initialise the sub-protocol - version := version // Closure for the run - manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{ - Name: ProtocolName, - Version: version, - Length: ProtocolLengths[i], - Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { - peer := manager.newPeer(int(version), p, rw) - select { - case manager.newPeerCh <- peer: - manager.wg.Add(1) - defer manager.wg.Done() - return manager.handle(peer) - case <-manager.quitSync: - return p2p.DiscQuitting - } - }, - NodeInfo: func() interface{} { - return manager.NodeInfo() - }, - PeerInfo: func(id enode.ID) interface{} { - if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil { - return p.Info() - } - return nil - }, - }) - } - if len(manager.SubProtocols) == 0 { - return nil, errIncompatibleConfig - } + // Construct the downloader (long sync) and its backing state bloom if fast // sync is requested. The downloader is responsible for deallocating the state // bloom when it's done. @@ -235,6 +189,39 @@ func NewProtocolManager(config *params.ChainConfig, checkpoint *params.TrustedCh return manager, nil } +func (pm *ProtocolManager) makeProtocol(version uint) p2p.Protocol { + length, ok := protocolLengths[version] + if !ok { + panic("makeProtocol for unknown version") + } + + return p2p.Protocol{ + Name: protocolName, + Version: version, + Length: length, + Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error { + peer := pm.newPeer(int(version), p, rw) + select { + case pm.newPeerCh <- peer: + pm.wg.Add(1) + defer pm.wg.Done() + return pm.handle(peer) + case <-pm.quitSync: + return p2p.DiscQuitting + } + }, + NodeInfo: func() interface{} { + return pm.NodeInfo() + }, + PeerInfo: func(id enode.ID) interface{} { + if p := pm.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil { + return p.Info() + } + return nil + }, + } +} + func (pm *ProtocolManager) removePeer(id string) { // Short circuit if the peer was already removed peer := pm.peers.Peer(id) @@ -381,8 +368,8 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { if err != nil { return err } - if msg.Size > ProtocolMaxMsgSize { - return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) + if msg.Size > protocolMaxMsgSize { + return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize) } defer msg.Discard() diff --git a/eth/handler_test.go b/eth/handler_test.go index 445d52afc..0f1672fd4 100644 --- a/eth/handler_test.go +++ b/eth/handler_test.go @@ -38,35 +38,6 @@ import ( "github.com/ethereum/go-ethereum/params" ) -// Tests that protocol versions and modes of operations are matched up properly. -func TestProtocolCompatibility(t *testing.T) { - // Define the compatibility chart - tests := []struct { - version uint - mode downloader.SyncMode - compatible bool - }{ - {61, downloader.FullSync, true}, {62, downloader.FullSync, true}, {63, downloader.FullSync, true}, - {61, downloader.FastSync, false}, {62, downloader.FastSync, false}, {63, downloader.FastSync, true}, - } - // Make sure anything we screw up is restored - backup := ProtocolVersions - defer func() { ProtocolVersions = backup }() - - // Try all available compatibility configs and check for errors - for i, tt := range tests { - ProtocolVersions = []uint{tt.version} - - pm, _, err := newTestProtocolManager(tt.mode, 0, nil, nil) - if pm != nil { - defer pm.Stop() - } - if (err == nil && !tt.compatible) || (err != nil && tt.compatible) { - t.Errorf("test %d: compatibility mismatch: have error %v, want compatibility %v", i, err, tt.compatible) - } - } -} - // Tests that block headers can be retrieved from a remote chain based on user queries. func TestGetBlockHeaders62(t *testing.T) { testGetBlockHeaders(t, 62) } func TestGetBlockHeaders63(t *testing.T) { testGetBlockHeaders(t, 63) } diff --git a/eth/peer.go b/eth/peer.go index 208badc5e..814c787b8 100644 --- a/eth/peer.go +++ b/eth/peer.go @@ -394,8 +394,8 @@ func (p *peer) readStatus(network uint64, status *statusData, genesis common.Has if msg.Code != StatusMsg { return errResp(ErrNoStatusMsg, "first msg has code %x (!= %x)", msg.Code, StatusMsg) } - if msg.Size > ProtocolMaxMsgSize { - return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize) + if msg.Size > protocolMaxMsgSize { + return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, protocolMaxMsgSize) } // Decode the handshake and make sure everything matches if err := msg.Decode(&status); err != nil { diff --git a/eth/protocol.go b/eth/protocol.go index 5beb562f8..de0c979d8 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -34,16 +34,16 @@ const ( eth63 = 63 ) -// ProtocolName is the official short name of the protocol used during capability negotiation. -var ProtocolName = "eth" +// protocolName is the official short name of the protocol used during capability negotiation. +const protocolName = "eth" // ProtocolVersions are the supported versions of the eth protocol (first is primary). -var ProtocolVersions = []uint{eth63, eth62} +var ProtocolVersions = []uint{eth63} -// ProtocolLengths are the number of implemented message corresponding to different protocol versions. -var ProtocolLengths = []uint64{17, 8} +// protocolLengths are the number of implemented message corresponding to different protocol versions. +var protocolLengths = map[uint]uint64{eth63: 17, eth62: 8} -const ProtocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message +const protocolMaxMsgSize = 10 * 1024 * 1024 // Maximum cap on the size of a protocol message // eth protocol message codes const ( -- cgit v1.2.3