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"
|
2017-08-04 16:14:17 +00:00
|
|
|
"sync"
|
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"
|
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"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
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/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2016-07-26 02:54:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
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"
|
2017-08-04 16:14:17 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2016-11-25 05:50:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-06-20 14:47:10 +00:00
|
|
|
rpc "github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
type LightEthereum struct {
|
2018-02-27 10:39:30 +00:00
|
|
|
config *eth.Config
|
|
|
|
|
2016-06-20 14:47:10 +00:00
|
|
|
odr *LesOdr
|
|
|
|
relay *LesTxRelay
|
2016-11-25 05:50:30 +00:00
|
|
|
chainConfig *params.ChainConfig
|
2016-06-20 14:47:10 +00:00
|
|
|
// Channel for shutting down the service
|
|
|
|
shutdownChan chan bool
|
|
|
|
// Handlers
|
2017-08-04 16:14:17 +00:00
|
|
|
peers *peerSet
|
2016-06-20 14:47:10 +00:00
|
|
|
txPool *light.TxPool
|
|
|
|
blockchain *light.LightChain
|
|
|
|
protocolManager *ProtocolManager
|
2017-08-04 16:14:17 +00:00
|
|
|
serverPool *serverPool
|
|
|
|
reqDist *requestDistributor
|
|
|
|
retriever *retrieveManager
|
2016-06-20 14:47:10 +00:00
|
|
|
// DB interfaces
|
|
|
|
chainDb ethdb.Database // Block chain database
|
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
|
|
|
|
bloomIndexer, chtIndexer, bloomTrieIndexer *core.ChainIndexer
|
|
|
|
|
2016-07-03 19:44:31 +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
|
|
|
|
|
2017-05-01 11:09:48 +00:00
|
|
|
networkId uint64
|
2016-06-20 14:47:10 +00:00
|
|
|
netRPCService *ethapi.PublicNetAPI
|
2016-07-27 11:47:41 +00:00
|
|
|
|
2017-10-10 09:38:49 +00:00
|
|
|
wg sync.WaitGroup
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
|
2016-09-08 09:45:12 +00:00
|
|
|
chainDb, err := eth.CreateDB(ctx, config, "lightchaindata")
|
2016-06-20 14:47:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 11:09:48 +00:00
|
|
|
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
|
|
|
|
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
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
peers := newPeerSet()
|
|
|
|
quitSync := make(chan struct{})
|
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
leth := &LightEthereum{
|
2018-02-27 10:39:30 +00:00
|
|
|
config: config,
|
2018-01-15 20:26:41 +00:00
|
|
|
chainConfig: chainConfig,
|
|
|
|
chainDb: chainDb,
|
|
|
|
eventMux: ctx.EventMux,
|
|
|
|
peers: peers,
|
|
|
|
reqDist: newRequestDistributor(peers, quitSync),
|
|
|
|
accountManager: ctx.AccountManager,
|
2018-02-27 10:39:30 +00:00
|
|
|
engine: eth.CreateConsensusEngine(ctx, &config.Ethash, chainConfig, chainDb),
|
2018-01-15 20:26:41 +00:00
|
|
|
shutdownChan: make(chan bool),
|
|
|
|
networkId: config.NetworkId,
|
|
|
|
bloomRequests: make(chan chan *bloombits.Retrieval),
|
|
|
|
bloomIndexer: eth.NewBloomIndexer(chainDb, light.BloomTrieFrequency),
|
|
|
|
chtIndexer: light.NewChtIndexer(chainDb, true),
|
|
|
|
bloomTrieIndexer: light.NewBloomTrieIndexer(chainDb, true),
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.relay = NewLesTxRelay(peers, leth.reqDist)
|
|
|
|
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg)
|
|
|
|
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool)
|
|
|
|
leth.odr = NewLesOdr(chainDb, leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer, leth.retriever)
|
|
|
|
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil {
|
2016-06-20 14:47:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.bloomIndexer.Start(leth.blockchain)
|
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
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay)
|
2018-06-20 14:51:55 +00:00
|
|
|
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 {
|
2016-06-20 14:47:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-16 14:40:37 +00:00
|
|
|
leth.ApiBackend = &LesApiBackend{leth, nil}
|
2017-05-01 11:09:48 +00:00
|
|
|
gpoParams := config.GPO
|
|
|
|
if gpoParams.Default == nil {
|
|
|
|
gpoParams.Default = config.GasPrice
|
|
|
|
}
|
2018-01-15 20:26:41 +00:00
|
|
|
leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams)
|
|
|
|
return leth, nil
|
2016-06-20 14:47:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 20:26:41 +00:00
|
|
|
func lesTopic(genesisHash common.Hash, protocolVersion uint) discv5.Topic {
|
|
|
|
var name string
|
|
|
|
switch protocolVersion {
|
|
|
|
case lpv1:
|
|
|
|
name = "LES"
|
|
|
|
case lpv2:
|
|
|
|
name = "LES2"
|
|
|
|
default:
|
|
|
|
panic(nil)
|
|
|
|
}
|
|
|
|
return discv5.Topic(name + "@" + common.Bytes2Hex(genesisHash.Bytes()[0:8]))
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return common.Address{}, fmt.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coinbase is the address that mining rewards will be send to (alias for Etherbase)
|
|
|
|
func (s *LightDummyAPI) Coinbase() (common.Address, error) {
|
|
|
|
return common.Address{}, fmt.Errorf("not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-05-01 11:09:48 +00:00
|
|
|
return append(ethapi.GetAPIs(s.ApiBackend), []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",
|
|
|
|
Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
|
|
|
|
Public: true,
|
2016-07-03 19:44:31 +00:00
|
|
|
}, {
|
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
2016-11-25 05:50:30 +00:00
|
|
|
Service: filters.NewPublicFilterAPI(s.ApiBackend, true),
|
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,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
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 }
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
|
|
|
func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.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
|
|
|
|
|
|
|
// Protocols implements node.Service, returning all the currently configured
|
|
|
|
// network protocols to start.
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) Protocols() []p2p.Protocol {
|
2016-06-20 14:47:10 +00:00
|
|
|
return s.protocolManager.SubProtocols
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements node.Service, starting all internal goroutines needed by the
|
|
|
|
// Ethereum protocol implementation.
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) Start(srvr *p2p.Server) error {
|
2018-01-15 20:26:41 +00:00
|
|
|
s.startBloomHandlers()
|
2017-05-01 11:09:48 +00:00
|
|
|
log.Warn("Light client mode is an experimental feature")
|
|
|
|
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.networkId)
|
2018-02-27 10:39:30 +00:00
|
|
|
// clients are searching for the first advertised protocol in the list
|
|
|
|
protocolVersion := AdvertiseProtocolVersions[0]
|
2018-01-15 20:26:41 +00:00
|
|
|
s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash(), protocolVersion))
|
2018-02-27 10:39:30 +00:00
|
|
|
s.protocolManager.Start(s.config.LightPeers)
|
2016-06-20 14:47:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop implements node.Service, terminating all internal goroutines used by the
|
|
|
|
// Ethereum protocol.
|
2016-07-26 02:54:35 +00:00
|
|
|
func (s *LightEthereum) Stop() error {
|
2016-06-20 14:47:10 +00:00
|
|
|
s.odr.Stop()
|
2018-01-15 20:26:41 +00:00
|
|
|
if s.bloomIndexer != nil {
|
|
|
|
s.bloomIndexer.Close()
|
|
|
|
}
|
|
|
|
if s.chtIndexer != nil {
|
|
|
|
s.chtIndexer.Close()
|
|
|
|
}
|
|
|
|
if s.bloomTrieIndexer != nil {
|
|
|
|
s.bloomTrieIndexer.Close()
|
|
|
|
}
|
2016-06-20 14:47:10 +00:00
|
|
|
s.blockchain.Stop()
|
|
|
|
s.protocolManager.Stop()
|
|
|
|
s.txPool.Stop()
|
|
|
|
|
|
|
|
s.eventMux.Stop()
|
|
|
|
|
|
|
|
s.chainDb.Close()
|
|
|
|
close(s.shutdownChan)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|