2016-11-08 17:01:56 -08:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-10-13 20:51:29 -07: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-06-21 03:27:38 -07:00
|
|
|
"sync"
|
2016-10-13 20:51:29 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-17 06:39:55 -08:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-04-04 15:16:29 -07:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
2016-10-13 20:51:29 -07:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/filters"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/gasprice"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/internal/ethapi"
|
|
|
|
"github.com/ethereum/go-ethereum/light"
|
2017-02-22 04:10:07 -08:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-10-13 20:51:29 -07:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2017-06-21 03:27:38 -07:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2016-10-20 04:36:29 -07:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-10-13 20:51:29 -07:00
|
|
|
rpc "github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LightEthereum struct {
|
|
|
|
odr *LesOdr
|
|
|
|
relay *LesTxRelay
|
2016-10-20 04:36:29 -07:00
|
|
|
chainConfig *params.ChainConfig
|
2016-10-13 20:51:29 -07:00
|
|
|
// Channel for shutting down the service
|
|
|
|
shutdownChan chan bool
|
|
|
|
// Handlers
|
2017-06-21 03:27:38 -07:00
|
|
|
peers *peerSet
|
2016-10-13 20:51:29 -07:00
|
|
|
txPool *light.TxPool
|
|
|
|
blockchain *light.LightChain
|
|
|
|
protocolManager *ProtocolManager
|
2017-06-21 03:27:38 -07:00
|
|
|
serverPool *serverPool
|
|
|
|
reqDist *requestDistributor
|
|
|
|
retriever *retrieveManager
|
2016-10-13 20:51:29 -07:00
|
|
|
// DB interfaces
|
|
|
|
chainDb ethdb.Database // Block chain database
|
|
|
|
|
|
|
|
ApiBackend *LesApiBackend
|
|
|
|
|
|
|
|
eventMux *event.TypeMux
|
2017-04-04 15:16:29 -07:00
|
|
|
engine consensus.Engine
|
2016-10-13 20:51:29 -07:00
|
|
|
accountManager *accounts.Manager
|
|
|
|
|
2017-04-25 04:31:15 -07:00
|
|
|
networkId uint64
|
2016-10-13 20:51:29 -07:00
|
|
|
netRPCService *ethapi.PublicNetAPI
|
2017-06-21 03:27:38 -07:00
|
|
|
|
2017-08-08 09:31:08 -07:00
|
|
|
wg sync.WaitGroup
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
|
|
|
|
chainDb, err := eth.CreateDB(ctx, config, "lightchaindata")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-02 05:03:33 -08:00
|
|
|
chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis)
|
|
|
|
if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat {
|
|
|
|
return nil, genesisErr
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
2017-03-02 05:03:33 -08:00
|
|
|
log.Info("Initialised chain configuration", "config", chainConfig)
|
|
|
|
|
2017-06-21 03:27:38 -07:00
|
|
|
peers := newPeerSet()
|
|
|
|
quitSync := make(chan struct{})
|
|
|
|
|
2016-10-13 20:51:29 -07:00
|
|
|
eth := &LightEthereum{
|
2017-03-02 05:03:33 -08:00
|
|
|
chainConfig: chainConfig,
|
2017-06-21 03:27:38 -07:00
|
|
|
chainDb: chainDb,
|
2016-10-13 20:51:29 -07:00
|
|
|
eventMux: ctx.EventMux,
|
2017-06-21 03:27:38 -07:00
|
|
|
peers: peers,
|
|
|
|
reqDist: newRequestDistributor(peers, quitSync),
|
2016-10-13 20:51:29 -07:00
|
|
|
accountManager: ctx.AccountManager,
|
2017-04-04 15:16:29 -07:00
|
|
|
engine: eth.CreateConsensusEngine(ctx, config, chainConfig, chainDb),
|
2016-10-13 20:51:29 -07:00
|
|
|
shutdownChan: make(chan bool),
|
2017-04-25 04:31:15 -07:00
|
|
|
networkId: config.NetworkId,
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
2017-06-21 03:27:38 -07:00
|
|
|
|
|
|
|
eth.relay = NewLesTxRelay(peers, eth.reqDist)
|
|
|
|
eth.serverPool = newServerPool(chainDb, quitSync, ð.wg)
|
|
|
|
eth.retriever = newRetrieveManager(peers, eth.reqDist, eth.serverPool)
|
|
|
|
eth.odr = NewLesOdr(chainDb, eth.retriever)
|
|
|
|
if eth.blockchain, err = light.NewLightChain(eth.odr, eth.chainConfig, eth.engine, eth.eventMux); err != nil {
|
2016-10-13 20:51:29 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-02 05:03:33 -08: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)
|
|
|
|
eth.blockchain.SetHead(compat.RewindTo)
|
|
|
|
core.WriteChainConfig(chainDb, genesisHash, chainConfig)
|
|
|
|
}
|
2016-10-13 20:51:29 -07:00
|
|
|
|
|
|
|
eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay)
|
2017-06-21 03:27:38 -07:00
|
|
|
if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, true, config.NetworkId, eth.eventMux, eth.engine, eth.peers, eth.blockchain, nil, chainDb, eth.odr, eth.relay, quitSync, ð.wg); err != nil {
|
2016-10-13 20:51:29 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
eth.ApiBackend = &LesApiBackend{eth, nil}
|
2017-04-12 07:27:23 -07:00
|
|
|
gpoParams := config.GPO
|
|
|
|
if gpoParams.Default == nil {
|
|
|
|
gpoParams.Default = config.GasPrice
|
2017-04-06 07:20:42 -07:00
|
|
|
}
|
|
|
|
eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams)
|
2016-10-13 20:51:29 -07:00
|
|
|
return eth, nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 03:27:38 -07:00
|
|
|
func lesTopic(genesisHash common.Hash) discv5.Topic {
|
|
|
|
return discv5.Topic("LES@" + common.Bytes2Hex(genesisHash.Bytes()[0:8]))
|
|
|
|
}
|
|
|
|
|
2016-10-13 20:51:29 -07: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
|
2016-12-17 06:39:55 -08:00
|
|
|
func (s *LightDummyAPI) Hashrate() hexutil.Uint {
|
|
|
|
return 0
|
2016-10-13 20:51:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mining returns an indication if this node is currently mining.
|
|
|
|
func (s *LightDummyAPI) Mining() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns the collection of RPC services the ethereum package offers.
|
|
|
|
// NOTE, some of these services probably need to be moved to somewhere else.
|
|
|
|
func (s *LightEthereum) APIs() []rpc.API {
|
2017-04-12 14:04:14 -07:00
|
|
|
return append(ethapi.GetAPIs(s.ApiBackend), []rpc.API{
|
2016-10-13 20:51:29 -07:00
|
|
|
{
|
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: &LightDummyAPI{},
|
|
|
|
Public: true,
|
|
|
|
}, {
|
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux),
|
|
|
|
Public: true,
|
|
|
|
}, {
|
|
|
|
Namespace: "eth",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: filters.NewPublicFilterAPI(s.ApiBackend, true),
|
|
|
|
Public: true,
|
|
|
|
}, {
|
|
|
|
Namespace: "net",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: s.netRPCService,
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) {
|
|
|
|
s.blockchain.ResetWithGenesisBlock(gb)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain }
|
|
|
|
func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool }
|
2017-04-12 06:38:31 -07:00
|
|
|
func (s *LightEthereum) Engine() consensus.Engine { return s.engine }
|
2016-10-13 20:51:29 -07:00
|
|
|
func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) }
|
|
|
|
func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader }
|
2016-11-25 07:55:06 -08:00
|
|
|
func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux }
|
2016-10-13 20:51:29 -07:00
|
|
|
|
|
|
|
// Protocols implements node.Service, returning all the currently configured
|
|
|
|
// network protocols to start.
|
|
|
|
func (s *LightEthereum) Protocols() []p2p.Protocol {
|
|
|
|
return s.protocolManager.SubProtocols
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements node.Service, starting all internal goroutines needed by the
|
|
|
|
// Ethereum protocol implementation.
|
|
|
|
func (s *LightEthereum) Start(srvr *p2p.Server) error {
|
2017-03-03 01:41:52 -08:00
|
|
|
log.Warn("Light client mode is an experimental feature")
|
2017-04-25 04:31:15 -07:00
|
|
|
s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.networkId)
|
2017-06-21 03:27:38 -07:00
|
|
|
s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash()))
|
|
|
|
s.protocolManager.Start()
|
2016-10-13 20:51:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop implements node.Service, terminating all internal goroutines used by the
|
|
|
|
// Ethereum protocol.
|
|
|
|
func (s *LightEthereum) Stop() error {
|
|
|
|
s.odr.Stop()
|
|
|
|
s.blockchain.Stop()
|
|
|
|
s.protocolManager.Stop()
|
|
|
|
s.txPool.Stop()
|
|
|
|
|
|
|
|
s.eventMux.Stop()
|
|
|
|
|
|
|
|
time.Sleep(time.Millisecond * 200)
|
|
|
|
s.chainDb.Close()
|
|
|
|
close(s.shutdownChan)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|