2016-11-25 05:50:30 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-06-20 14:47:10 +00:00
|
|
|
// 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Package les implements the Light Ethereum Subprotocol.
|
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-28 06:53:50 +00:00
|
|
|
"time"
|
2016-06-20 14:47:10 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2016-11-25 05:50:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-02-23 00:22:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2017-05-01 11:09:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
2016-06-20 14:47:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-01-15 20:26:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/bloombits"
|
2018-06-20 14:51:55 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-06-20 14:47:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2021-06-28 06:53:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
2016-07-03 19:44:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/filters"
|
2016-06-20 14:47:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/gasprice"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2016-07-26 02:54:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
2021-06-28 06:53:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les/vflux"
|
|
|
|
vfc "github.com/ethereum/go-ethereum/les/vflux/client"
|
2016-06-20 14:47:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/light"
|
2017-05-01 11:09:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-06-20 14:47:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2021-06-28 06:53:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2016-11-25 05:50:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2021-06-28 06:53:50 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2019-10-04 15:21:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-06-20 14:47:10 +00:00
|
|
|
)
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
type LightEthereum struct {
|
2018-08-27 08:22:21 +00:00
|
|
|
lesCommons
|
2018-02-27 10:39:30 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
peers *serverPeerSet
|
|
|
|
reqDist *requestDistributor
|
|
|
|
retriever *retrieveManager
|
|
|
|
odr *LesOdr
|
|
|
|
relay *lesTxRelay
|
|
|
|
handler *clientHandler
|
|
|
|
txPool *light.TxPool
|
|
|
|
blockchain *light.LightChain
|
|
|
|
serverPool *vfc.ServerPool
|
|
|
|
serverPoolIterator enode.Iterator
|
|
|
|
pruner *pruner
|
2018-08-27 08:22:21 +00:00
|
|
|
|
|
|
|
bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
|
2019-10-04 15:21:24 +00:00
|
|
|
bloomIndexer *core.ChainIndexer // Bloom indexer operating during block imports
|
2016-06-20 14:47:10 +00:00
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
ApiBackend *LesApiBackend
|
2016-06-20 14:47:10 +00:00
|
|
|
eventMux *event.TypeMux
|
2017-05-01 11:09:48 +00:00
|
|
|
engine consensus.Engine
|
2016-06-20 14:47:10 +00:00
|
|
|
accountManager *accounts.Manager
|
2019-10-04 15:21:24 +00:00
|
|
|
netRPCService *ethapi.PublicNetAPI
|
2021-06-28 06:53:50 +00:00
|
|
|
|
|
|
|
p2pServer *p2p.Server
|
|
|
|
p2pConfig *p2p.Config
|
|
|
|
udpEnabled bool
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// New creates an instance of the light client.
|
|
|
|
func New(stack *node.Node, config *ethconfig.Config) (*LightEthereum, error) {
|
|
|
|
chainDb, err := stack.OpenDatabase("lightchaindata", config.DatabaseCache, config.DatabaseHandles, "eth/db/chaindata/", false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lesDb, err := stack.OpenDatabase("les.client", 0, 0, "eth/db/lesclient/", false)
|
2016-06-20 14:47:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-28 06:53:50 +00:00
|
|
|
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlockWithOverride(chainDb, config.Genesis, config.OverrideLondon)
|
2017-05-01 11:09:48 +00:00
|
|
|
if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
|
|
|
|
return nil, genesisErr
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
2017-05-01 11:09:48 +00:00
|
|
|
log.Info("Initialised chain configuration", "config", chainConfig)
|
2016-06-20 14:47:10 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
peers := newServerPeerSet()
|
2018-01-15 20:26:41 +00:00
|
|
|
leth := &LightEthereum{
|
2018-08-27 08:22:21 +00:00
|
|
|
lesCommons: lesCommons{
|
2019-10-04 15:21:24 +00:00
|
|
|
genesis: genesisHash,
|
|
|
|
config: config,
|
|
|
|
chainConfig: chainConfig,
|
|
|
|
iConfig: light.DefaultClientIndexerConfig,
|
|
|
|
chainDb: chainDb,
|
2021-06-28 06:53:50 +00:00
|
|
|
lesDb: lesDb,
|
2019-10-04 15:21:24 +00:00
|
|
|
closeCh: make(chan struct{}),
|
2018-08-27 08:22:21 +00:00
|
|
|
},
|
2021-06-28 06:53:50 +00:00
|
|
|
peers: peers,
|
|
|
|
eventMux: stack.EventMux(),
|
2019-10-04 15:21:24 +00:00
|
|
|
reqDist: newRequestDistributor(peers, &mclock.System{}),
|
2021-06-28 06:53:50 +00:00
|
|
|
accountManager: stack.AccountManager(),
|
|
|
|
engine: ethconfig.CreateConsensusEngine(stack, chainConfig, &config.Ethash, nil, false, chainDb),
|
2018-08-27 08:22:21 +00:00
|
|
|
bloomRequests: make(chan chan *bloombits.Retrieval),
|
2021-06-28 06:53:50 +00:00
|
|
|
bloomIndexer: core.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations),
|
|
|
|
p2pServer: stack.Server(),
|
|
|
|
p2pConfig: &stack.Config().P2P,
|
|
|
|
udpEnabled: stack.Config().P2P.DiscoveryV5,
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
2021-06-28 06:53:50 +00:00
|
|
|
|
|
|
|
var prenegQuery vfc.QueryFunc
|
|
|
|
if leth.udpEnabled {
|
|
|
|
prenegQuery = leth.prenegQuery
|
|
|
|
}
|
|
|
|
leth.serverPool, leth.serverPoolIterator = vfc.NewServerPool(lesDb, []byte("serverpool:"), time.Second, prenegQuery, &mclock.System{}, config.UltraLightServers, requestList)
|
|
|
|
leth.serverPool.AddMetrics(suggestedTimeoutGauge, totalValueGauge, serverSelectableGauge, serverConnectedGauge, sessionValueMeter, serverDialedMeter)
|
|
|
|
|
|
|
|
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool.GetTimeout)
|
2019-10-04 15:21:24 +00:00
|
|
|
leth.relay = newLesTxRelay(peers, leth.retriever)
|
2018-08-27 08:22:21 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
leth.odr = NewLesOdr(chainDb, light.DefaultClientIndexerConfig, leth.peers, leth.retriever)
|
|
|
|
leth.chtIndexer = light.NewChtIndexer(chainDb, leth.odr, params.CHTFrequency, params.HelperTrieConfirmations, config.LightNoPrune)
|
|
|
|
leth.bloomTrieIndexer = light.NewBloomTrieIndexer(chainDb, leth.odr, params.BloomBitsBlocksClient, params.BloomTrieFrequency, config.LightNoPrune)
|
2018-08-27 08:22:21 +00:00
|
|
|
leth.odr.SetIndexers(leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer)
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
checkpoint := config.Checkpoint
|
|
|
|
if checkpoint == nil {
|
|
|
|
checkpoint = params.TrustedCheckpoints[genesisHash]
|
|
|
|
}
|
2018-08-27 08:22:21 +00:00
|
|
|
// Note: NewLightChain adds the trusted checkpoint so it needs an ODR with
|
|
|
|
// indexers already set but not started yet
|
2019-10-04 15:21:24 +00:00
|
|
|
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine, checkpoint); err != nil {
|
2016-06-20 14:47:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-04 15:21:24 +00:00
|
|
|
leth.chainReader = leth.blockchain
|
|
|
|
leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay)
|
|
|
|
|
|
|
|
// Set up checkpoint oracle.
|
2021-06-28 06:53:50 +00:00
|
|
|
leth.oracle = leth.setupOracle(stack, genesisHash, config)
|
2019-10-04 15:21:24 +00:00
|
|
|
|
2018-08-27 08:22:21 +00:00
|
|
|
// Note: AddChildIndexer starts the update process for the child
|
|
|
|
leth.bloomIndexer.AddChildIndexer(leth.bloomTrieIndexer)
|
|
|
|
leth.chtIndexer.Start(leth.blockchain)
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.bloomIndexer.Start(leth.blockchain)
|
2018-08-27 08:22:21 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// Start a light chain pruner to delete useless historical data.
|
|
|
|
leth.pruner = newPruner(chainDb, leth.chtIndexer, leth.bloomTrieIndexer)
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
// Rewind the chain in case of an incompatible config upgrade.
|
|
|
|
if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
|
|
|
|
log.Warn("Rewinding chain to upgrade configuration", "err", compat)
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.blockchain.SetHead(compat.RewindTo)
|
2018-06-20 14:51:55 +00:00
|
|
|
rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig)
|
2017-05-01 11:09:48 +00:00
|
|
|
}
|
2016-06-20 14:47:10 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
leth.ApiBackend = &LesApiBackend{stack.Config().ExtRPCEnabled(), stack.Config().AllowUnprotectedTxs, leth, nil}
|
2017-05-01 11:09:48 +00:00
|
|
|
gpoParams := config.GPO
|
|
|
|
if gpoParams.Default == nil {
|
2019-10-04 15:21:24 +00:00
|
|
|
gpoParams.Default = config.Miner.GasPrice
|
2017-05-01 11:09:48 +00:00
|
|
|
}
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams)
|
2016-06-20 14:47:10 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
leth.handler = newClientHandler(config.UltraLightServers, config.UltraLightFraction, checkpoint, leth)
|
|
|
|
if leth.handler.ulc != nil {
|
|
|
|
log.Warn("Ultra light client is enabled", "trustedNodes", len(leth.handler.ulc.keys), "minTrustedFraction", leth.handler.ulc.fraction)
|
|
|
|
leth.blockchain.DisableCheckFreq()
|
|
|
|
}
|
|
|
|
|
|
|
|
leth.netRPCService = ethapi.NewPublicNetAPI(leth.p2pServer, leth.config.NetworkId)
|
|
|
|
|
|
|
|
// Register the backend on the node
|
|
|
|
stack.RegisterAPIs(leth.APIs())
|
|
|
|
stack.RegisterProtocols(leth.Protocols())
|
|
|
|
stack.RegisterLifecycle(leth)
|
|
|
|
|
|
|
|
// Check for unclean shutdown
|
|
|
|
if uncleanShutdowns, discards, err := rawdb.PushUncleanShutdownMarker(chainDb); err != nil {
|
|
|
|
log.Error("Could not update unclean-shutdown-marker list", "error", err)
|
|
|
|
} else {
|
|
|
|
if discards > 0 {
|
|
|
|
log.Warn("Old unclean shutdowns found", "count", discards)
|
|
|
|
}
|
|
|
|
for _, tstamp := range uncleanShutdowns {
|
|
|
|
t := time.Unix(int64(tstamp), 0)
|
|
|
|
log.Warn("Unclean shutdown detected", "booted", t,
|
|
|
|
"age", common.PrettyAge(t))
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 15:21:24 +00:00
|
|
|
return leth, nil
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// VfluxRequest sends a batch of requests to the given node through discv5 UDP TalkRequest and returns the responses
|
|
|
|
func (s *LightEthereum) VfluxRequest(n *enode.Node, reqs vflux.Requests) vflux.Replies {
|
|
|
|
if !s.udpEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
reqsEnc, _ := rlp.EncodeToBytes(&reqs)
|
|
|
|
repliesEnc, _ := s.p2pServer.DiscV5.TalkRequest(s.serverPool.DialNode(n), "vfx", reqsEnc)
|
|
|
|
var replies vflux.Replies
|
|
|
|
if len(repliesEnc) == 0 || rlp.DecodeBytes(repliesEnc, &replies) != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return replies
|
|
|
|
}
|
|
|
|
|
|
|
|
// vfxVersion returns the version number of the "les" service subdomain of the vflux UDP
|
|
|
|
// service, as advertised in the ENR record
|
|
|
|
func (s *LightEthereum) vfxVersion(n *enode.Node) uint {
|
|
|
|
if n.Seq() == 0 {
|
|
|
|
var err error
|
|
|
|
if !s.udpEnabled {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if n, err = s.p2pServer.DiscV5.RequestENR(n); n != nil && err == nil && n.Seq() != 0 {
|
|
|
|
s.serverPool.Persist(n)
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var les []rlp.RawValue
|
|
|
|
if err := n.Load(enr.WithEntry("les", &les)); err != nil || len(les) < 1 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
var version uint
|
|
|
|
rlp.DecodeBytes(les[0], &version) // Ignore additional fields (for forward compatibility).
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
// prenegQuery sends a capacity query to the given server node to determine whether
|
|
|
|
// a connection slot is immediately available
|
|
|
|
func (s *LightEthereum) prenegQuery(n *enode.Node) int {
|
|
|
|
if s.vfxVersion(n) < 1 {
|
|
|
|
// UDP query not supported, always try TCP connection
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var requests vflux.Requests
|
|
|
|
requests.Add("les", vflux.CapacityQueryName, vflux.CapacityQueryReq{
|
|
|
|
Bias: 180,
|
|
|
|
AddTokens: []vflux.IntOrInf{{}},
|
|
|
|
})
|
|
|
|
replies := s.VfluxRequest(n, requests)
|
|
|
|
var cqr vflux.CapacityQueryReply
|
|
|
|
if replies.Get(0, &cqr) != nil || len(cqr) != 1 { // Note: Get returns an error if replies is nil
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if cqr[0] > 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-11-25 05:50:30 +00:00
|
|
|
type LightDummyAPI struct{}
|
|
|
|
|
|
|
|
// Etherbase is the address that mining rewards will be send to
|
|
|
|
func (s *LightDummyAPI) Etherbase() (common.Address, error) {
|
2019-10-04 15:21:24 +00:00
|
|
|
return common.Address{}, fmt.Errorf("mining is not supported in light mode")
|
2016-11-25 05:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Coinbase is the address that mining rewards will be send to (alias for Etherbase)
|
|
|
|
func (s *LightDummyAPI) Coinbase() (common.Address, error) {
|
2019-10-04 15:21:24 +00:00
|
|
|
return common.Address{}, fmt.Errorf("mining is not supported in light mode")
|
2016-11-25 05:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hashrate returns the POW hashrate
|
2017-02-23 00:22:43 +00:00
|
|
|
func (s *LightDummyAPI) Hashrate() hexutil.Uint {
|
|
|
|
return 0
|
2016-11-25 05:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mining returns an indication if this node is currently mining.
|
|
|
|
func (s *LightDummyAPI) Mining() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-06-20 14:47:10 +00:00
|
|
|
// APIs returns the collection of RPC services the ethereum package offers.
|
|
|
|
// NOTE, some of these services probably need to be moved to somewhere else.
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) APIs() []rpc.API {
|
2021-06-28 06:53:50 +00:00
|
|
|
apis := ethapi.GetAPIs(s.ApiBackend)
|
|
|
|
apis = append(apis, s.engine.APIs(s.BlockChain().HeaderChain())...)
|
|
|
|
return append(apis, []rpc.API{
|
2016-06-20 14:47:10 +00:00
|
|
|
{
|
2016-11-25 05:50:30 +00:00
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: &LightDummyAPI{},
|
|
|
|
Public: true,
|
|
|
|
}, {
|
2016-06-20 14:47:10 +00:00
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
2019-10-04 15:21:24 +00:00
|
|
|
Service: downloader.NewPublicDownloaderAPI(s.handler.downloader, s.eventMux),
|
2016-06-20 14:47:10 +00:00
|
|
|
Public: true,
|
2016-07-03 19:44:31 +00:00
|
|
|
}, {
|
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
2021-06-28 06:53:50 +00:00
|
|
|
Service: filters.NewPublicFilterAPI(s.ApiBackend, true, 5*time.Minute),
|
2016-07-03 19:44:31 +00:00
|
|
|
Public: true,
|
2016-06-20 14:47:10 +00:00
|
|
|
}, {
|
|
|
|
Namespace: "net",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: s.netRPCService,
|
|
|
|
Public: true,
|
2019-10-04 15:21:24 +00:00
|
|
|
}, {
|
|
|
|
Namespace: "les",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: NewPrivateLightAPI(&s.lesCommons),
|
|
|
|
Public: false,
|
2021-06-28 06:53:50 +00:00
|
|
|
}, {
|
|
|
|
Namespace: "vflux",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: s.serverPool.API(),
|
|
|
|
Public: false,
|
2016-06-20 14:47:10 +00:00
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) {
|
2016-06-20 14:47:10 +00:00
|
|
|
s.blockchain.ResetWithGenesisBlock(gb)
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain }
|
|
|
|
func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool }
|
2017-05-01 11:09:48 +00:00
|
|
|
func (s *LightEthereum) Engine() consensus.Engine { return s.engine }
|
2018-08-27 08:22:21 +00:00
|
|
|
func (s *LightEthereum) LesVersion() int { return int(ClientProtocolVersions[0]) }
|
2019-10-04 15:21:24 +00:00
|
|
|
func (s *LightEthereum) Downloader() *downloader.Downloader { return s.handler.downloader }
|
2016-12-07 20:54:59 +00:00
|
|
|
func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux }
|
2016-06-20 14:47:10 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// Protocols returns all the currently configured network protocols to start.
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) Protocols() []p2p.Protocol {
|
2019-10-04 15:21:24 +00:00
|
|
|
return s.makeProtocols(ClientProtocolVersions, s.handler.runPeer, func(id enode.ID) interface{} {
|
2021-06-28 06:53:50 +00:00
|
|
|
if p := s.peers.peer(id.String()); p != nil {
|
2019-10-04 15:21:24 +00:00
|
|
|
return p.Info()
|
|
|
|
}
|
|
|
|
return nil
|
2021-06-28 06:53:50 +00:00
|
|
|
}, s.serverPoolIterator)
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// Start implements node.Lifecycle, starting all internal goroutines needed by the
|
2019-10-04 15:21:24 +00:00
|
|
|
// light ethereum protocol implementation.
|
2021-06-28 06:53:50 +00:00
|
|
|
func (s *LightEthereum) Start() error {
|
2017-05-01 11:09:48 +00:00
|
|
|
log.Warn("Light client mode is an experimental feature")
|
2019-10-04 15:21:24 +00:00
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
if s.udpEnabled && s.p2pServer.DiscV5 == nil {
|
|
|
|
s.udpEnabled = false
|
|
|
|
log.Error("Discovery v5 is not initialized")
|
|
|
|
}
|
|
|
|
discovery, err := s.setupDiscovery()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.serverPool.AddSource(discovery)
|
|
|
|
s.serverPool.Start()
|
2019-10-04 15:21:24 +00:00
|
|
|
// Start bloom request workers.
|
|
|
|
s.wg.Add(bloomServiceThreads)
|
2018-09-25 08:55:54 +00:00
|
|
|
s.startBloomHandlers(params.BloomBitsBlocksClient)
|
2021-06-28 06:53:50 +00:00
|
|
|
s.handler.start()
|
2019-10-04 15:21:24 +00:00
|
|
|
|
2016-06-20 14:47:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-28 06:53:50 +00:00
|
|
|
// Stop implements node.Lifecycle, terminating all internal goroutines used by the
|
2016-06-20 14:47:10 +00:00
|
|
|
// Ethereum protocol.
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) Stop() error {
|
2019-10-04 15:21:24 +00:00
|
|
|
close(s.closeCh)
|
2021-06-28 06:53:50 +00:00
|
|
|
s.serverPool.Stop()
|
|
|
|
s.peers.close()
|
2019-10-04 15:21:24 +00:00
|
|
|
s.reqDist.close()
|
2016-06-20 14:47:10 +00:00
|
|
|
s.odr.Stop()
|
2019-10-04 15:21:24 +00:00
|
|
|
s.relay.Stop()
|
2018-08-27 08:22:21 +00:00
|
|
|
s.bloomIndexer.Close()
|
|
|
|
s.chtIndexer.Close()
|
2016-06-20 14:47:10 +00:00
|
|
|
s.blockchain.Stop()
|
2019-10-04 15:21:24 +00:00
|
|
|
s.handler.stop()
|
2016-06-20 14:47:10 +00:00
|
|
|
s.txPool.Stop()
|
2018-08-27 08:22:21 +00:00
|
|
|
s.engine.Close()
|
2021-06-28 06:53:50 +00:00
|
|
|
s.pruner.close()
|
2016-06-20 14:47:10 +00:00
|
|
|
s.eventMux.Stop()
|
2021-06-28 06:53:50 +00:00
|
|
|
rawdb.PopUncleanShutdownMarker(s.chainDb)
|
2016-06-20 14:47:10 +00:00
|
|
|
s.chainDb.Close()
|
2021-06-28 06:53:50 +00:00
|
|
|
s.lesDb.Close()
|
2019-10-04 15:21:24 +00:00
|
|
|
s.wg.Wait()
|
|
|
|
log.Info("Light ethereum stopped")
|
2016-06-20 14:47:10 +00:00
|
|
|
return nil
|
|
|
|
}
|