Skip to content

Commit 2695fa2

Browse files
fjlkaralabe
authored andcommitted
les: fix crasher in NodeInfo when running as server (#17419)
* les: fix crasher in NodeInfo when running as server The ProtocolManager computes CHT and Bloom trie roots by asking the indexers for their current head. It tried to get the indexers from LesOdr, but no LesOdr instance is created in server mode. Attempt to fix this by moving the indexers, protocol creation and NodeInfo to a new lesCommons struct which is embedded into both server and client. All this setup code should really be cleaned up, but this is just a hotfix so we have to do that some other time. * les: fix commons protocol maker
1 parent f44046a commit 2695fa2

5 files changed

Lines changed: 178 additions & 152 deletions

File tree

‎les/backend.go‎

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/ethereum/go-ethereum/eth/downloader"
3535
"github.com/ethereum/go-ethereum/eth/filters"
3636
"github.com/ethereum/go-ethereum/eth/gasprice"
37-
"github.com/ethereum/go-ethereum/ethdb"
3837
"github.com/ethereum/go-ethereum/event"
3938
"github.com/ethereum/go-ethereum/internal/ethapi"
4039
"github.com/ethereum/go-ethereum/light"
@@ -47,26 +46,24 @@ import (
4746
)
4847

4948
type LightEthereum struct {
50-
config *eth.Config
49+
lesCommons
5150

5251
odr *LesOdr
5352
relay *LesTxRelay
5453
chainConfig *params.ChainConfig
5554
// Channel for shutting down the service
5655
shutdownChan chan bool
56+
5757
// Handlers
58-
peers *peerSet
59-
txPool *light.TxPool
60-
blockchain *light.LightChain
61-
protocolManager *ProtocolManager
62-
serverPool *serverPool
63-
reqDist *requestDistributor
64-
retriever *retrieveManager
65-
// DB interfaces
66-
chainDb ethdb.Database // Block chain database
67-
68-
bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
69-
bloomIndexer, chtIndexer, bloomTrieIndexer *core.ChainIndexer
58+
peers *peerSet
59+
txPool *light.TxPool
60+
blockchain *light.LightChain
61+
serverPool *serverPool
62+
reqDist *requestDistributor
63+
retriever *retrieveManager
64+
65+
bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
66+
bloomIndexer *core.ChainIndexer
7067

7168
ApiBackend *LesApiBackend
7269

@@ -95,9 +92,11 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
9592
quitSync := make(chan struct{})
9693

9794
leth := &LightEthereum{
98-
config: config,
95+
lesCommons: lesCommons{
96+
chainDb: chainDb,
97+
config: config,
98+
},
9999
chainConfig: chainConfig,
100-
chainDb: chainDb,
101100
eventMux: ctx.EventMux,
102101
peers: peers,
103102
reqDist: newRequestDistributor(peers, quitSync),
@@ -112,10 +111,12 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
112111
leth.relay = NewLesTxRelay(peers, leth.reqDist)
113112
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg)
114113
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool)
114+
115115
leth.odr = NewLesOdr(chainDb, leth.retriever)
116116
leth.chtIndexer = light.NewChtIndexer(chainDb, true, leth.odr)
117117
leth.bloomTrieIndexer = light.NewBloomTrieIndexer(chainDb, true, leth.odr)
118118
leth.odr.SetIndexers(leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer)
119+
119120
// Note: NewLightChain adds the trusted checkpoint so it needs an ODR with
120121
// indexers already set but not started yet
121122
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil {
@@ -125,6 +126,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
125126
leth.bloomIndexer.AddChildIndexer(leth.bloomTrieIndexer)
126127
leth.chtIndexer.Start(leth.blockchain)
127128
leth.bloomIndexer.Start(leth.blockchain)
129+
128130
// Rewind the chain in case of an incompatible config upgrade.
129131
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
130132
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
@@ -133,7 +135,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
133135
}
134136

135137
leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay)
136-
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, true, ClientProtocolVersions, config.NetworkId, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, leth.serverPool, quitSync, &leth.wg); err != nil {
138+
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, true, config.NetworkId, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, leth.serverPool, quitSync, &leth.wg); err != nil {
137139
return nil, err
138140
}
139141
leth.ApiBackend = &LesApiBackend{leth, nil}
@@ -215,14 +217,14 @@ func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) {
215217
func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain }
216218
func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool }
217219
func (s *LightEthereum) Engine() consensus.Engine { return s.engine }
218-
func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
220+
func (s *LightEthereum) LesVersion() int { return int(ClientProtocolVersions[0]) }
219221
func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
220222
func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux }
221223

222224
// Protocols implements node.Service, returning all the currently configured
223225
// network protocols to start.
224226
func (s *LightEthereum) Protocols() []p2p.Protocol {
225-
return s.protocolManager.SubProtocols
227+
return s.makeProtocols(ClientProtocolVersions)
226228
}
227229

228230
// Start implements node.Service, starting all internal goroutines needed by the
@@ -242,12 +244,8 @@ func (s *LightEthereum) Start(srvr *p2p.Server) error {
242244
// Ethereum protocol.
243245
func (s *LightEthereum) Stop() error {
244246
s.odr.Stop()
245-
if s.bloomIndexer != nil {
246-
s.bloomIndexer.Close()
247-
}
248-
if s.chtIndexer != nil {
249-
s.chtIndexer.Close()
250-
}
247+
s.bloomIndexer.Close()
248+
s.chtIndexer.Close()
251249
s.blockchain.Stop()
252250
s.protocolManager.Stop()
253251
s.txPool.Stop()

‎les/commons.go‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2018 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package les
18+
19+
import (
20+
"fmt"
21+
"math/big"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core"
25+
"github.com/ethereum/go-ethereum/eth"
26+
"github.com/ethereum/go-ethereum/ethdb"
27+
"github.com/ethereum/go-ethereum/light"
28+
"github.com/ethereum/go-ethereum/p2p"
29+
"github.com/ethereum/go-ethereum/p2p/discover"
30+
"github.com/ethereum/go-ethereum/params"
31+
)
32+
33+
// lesCommons contains fields needed by both server and client.
34+
type lesCommons struct {
35+
config *eth.Config
36+
chainDb ethdb.Database
37+
protocolManager *ProtocolManager
38+
chtIndexer, bloomTrieIndexer *core.ChainIndexer
39+
}
40+
41+
// NodeInfo represents a short summary of the Ethereum sub-protocol metadata
42+
// known about the host peer.
43+
type NodeInfo struct {
44+
Network uint64 `json:"network"` // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
45+
Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
46+
Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
47+
Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules
48+
Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
49+
CHT light.TrustedCheckpoint `json:"cht"` // Trused CHT checkpoint for fast catchup
50+
}
51+
52+
// makeProtocols creates protocol descriptors for the given LES versions.
53+
func (c *lesCommons) makeProtocols(versions []uint) []p2p.Protocol {
54+
protos := make([]p2p.Protocol, len(versions))
55+
for i, version := range versions {
56+
version := version
57+
protos[i] = p2p.Protocol{
58+
Name: "les",
59+
Version: version,
60+
Length: ProtocolLengths[version],
61+
NodeInfo: c.nodeInfo,
62+
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
63+
return c.protocolManager.runPeer(version, p, rw)
64+
},
65+
PeerInfo: func(id discover.NodeID) interface{} {
66+
if p := c.protocolManager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
67+
return p.Info()
68+
}
69+
return nil
70+
},
71+
}
72+
}
73+
return protos
74+
}
75+
76+
// nodeInfo retrieves some protocol metadata about the running host node.
77+
func (c *lesCommons) nodeInfo() interface{} {
78+
var cht light.TrustedCheckpoint
79+
sections, _, sectionHead := c.chtIndexer.Sections()
80+
sections2, _, sectionHead2 := c.bloomTrieIndexer.Sections()
81+
if sections2 < sections {
82+
sections = sections2
83+
sectionHead = sectionHead2
84+
}
85+
if sections > 0 {
86+
sectionIndex := sections - 1
87+
cht = light.TrustedCheckpoint{
88+
SectionIdx: sectionIndex,
89+
SectionHead: sectionHead,
90+
CHTRoot: light.GetChtRoot(c.chainDb, sectionIndex, sectionHead),
91+
BloomRoot: light.GetBloomTrieRoot(c.chainDb, sectionIndex, sectionHead),
92+
}
93+
}
94+
95+
chain := c.protocolManager.blockchain
96+
head := chain.CurrentHeader()
97+
hash := head.Hash()
98+
return &NodeInfo{
99+
Network: c.config.NetworkId,
100+
Difficulty: chain.GetTd(hash, head.Number.Uint64()),
101+
Genesis: chain.Genesis().Hash(),
102+
Config: chain.Config(),
103+
Head: chain.CurrentHeader().Hash(),
104+
CHT: cht,
105+
}
106+
}

0 commit comments

Comments
 (0)