github.com/myafeier/go-ethereum@v1.6.8-0.20170719123245-3e0dbe0eaa72/les/backend.go (about) 1 // Copyright 2016 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 implements the Light Ethereum Subprotocol. 18 package les 19 20 import ( 21 "fmt" 22 "sync" 23 "time" 24 25 "github.com/ethereum/go-ethereum/accounts" 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/common/hexutil" 28 "github.com/ethereum/go-ethereum/consensus" 29 "github.com/ethereum/go-ethereum/core" 30 "github.com/ethereum/go-ethereum/core/types" 31 "github.com/ethereum/go-ethereum/eth" 32 "github.com/ethereum/go-ethereum/eth/downloader" 33 "github.com/ethereum/go-ethereum/eth/filters" 34 "github.com/ethereum/go-ethereum/eth/gasprice" 35 "github.com/ethereum/go-ethereum/ethdb" 36 "github.com/ethereum/go-ethereum/event" 37 "github.com/ethereum/go-ethereum/internal/ethapi" 38 "github.com/ethereum/go-ethereum/light" 39 "github.com/ethereum/go-ethereum/log" 40 "github.com/ethereum/go-ethereum/node" 41 "github.com/ethereum/go-ethereum/p2p" 42 "github.com/ethereum/go-ethereum/p2p/discv5" 43 "github.com/ethereum/go-ethereum/params" 44 rpc "github.com/ethereum/go-ethereum/rpc" 45 ) 46 47 type LightEthereum struct { 48 odr *LesOdr 49 relay *LesTxRelay 50 chainConfig *params.ChainConfig 51 // Channel for shutting down the service 52 shutdownChan chan bool 53 // Handlers 54 peers *peerSet 55 txPool *light.TxPool 56 blockchain *light.LightChain 57 protocolManager *ProtocolManager 58 serverPool *serverPool 59 reqDist *requestDistributor 60 retriever *retrieveManager 61 // DB interfaces 62 chainDb ethdb.Database // Block chain database 63 64 ApiBackend *LesApiBackend 65 66 eventMux *event.TypeMux 67 engine consensus.Engine 68 accountManager *accounts.Manager 69 70 networkId uint64 71 netRPCService *ethapi.PublicNetAPI 72 73 quitSync chan struct{} 74 wg sync.WaitGroup 75 } 76 77 func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { 78 chainDb, err := eth.CreateDB(ctx, config, "lightchaindata") 79 if err != nil { 80 return nil, err 81 } 82 chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis) 83 if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat { 84 return nil, genesisErr 85 } 86 log.Info("Initialised chain configuration", "config", chainConfig) 87 88 peers := newPeerSet() 89 quitSync := make(chan struct{}) 90 91 eth := &LightEthereum{ 92 chainConfig: chainConfig, 93 chainDb: chainDb, 94 eventMux: ctx.EventMux, 95 peers: peers, 96 reqDist: newRequestDistributor(peers, quitSync), 97 accountManager: ctx.AccountManager, 98 engine: eth.CreateConsensusEngine(ctx, config, chainConfig, chainDb), 99 shutdownChan: make(chan bool), 100 networkId: config.NetworkId, 101 } 102 103 eth.relay = NewLesTxRelay(peers, eth.reqDist) 104 eth.serverPool = newServerPool(chainDb, quitSync, ð.wg) 105 eth.retriever = newRetrieveManager(peers, eth.reqDist, eth.serverPool) 106 eth.odr = NewLesOdr(chainDb, eth.retriever) 107 if eth.blockchain, err = light.NewLightChain(eth.odr, eth.chainConfig, eth.engine, eth.eventMux); err != nil { 108 return nil, err 109 } 110 // Rewind the chain in case of an incompatible config upgrade. 111 if compat, ok := genesisErr.(*params.ConfigCompatError); ok { 112 log.Warn("Rewinding chain to upgrade configuration", "err", compat) 113 eth.blockchain.SetHead(compat.RewindTo) 114 core.WriteChainConfig(chainDb, genesisHash, chainConfig) 115 } 116 117 eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay) 118 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 { 119 return nil, err 120 } 121 eth.ApiBackend = &LesApiBackend{eth, nil} 122 gpoParams := config.GPO 123 if gpoParams.Default == nil { 124 gpoParams.Default = config.GasPrice 125 } 126 eth.ApiBackend.gpo = gasprice.NewOracle(eth.ApiBackend, gpoParams) 127 return eth, nil 128 } 129 130 func lesTopic(genesisHash common.Hash) discv5.Topic { 131 return discv5.Topic("LES@" + common.Bytes2Hex(genesisHash.Bytes()[0:8])) 132 } 133 134 type LightDummyAPI struct{} 135 136 // Etherbase is the address that mining rewards will be send to 137 func (s *LightDummyAPI) Etherbase() (common.Address, error) { 138 return common.Address{}, fmt.Errorf("not supported") 139 } 140 141 // Coinbase is the address that mining rewards will be send to (alias for Etherbase) 142 func (s *LightDummyAPI) Coinbase() (common.Address, error) { 143 return common.Address{}, fmt.Errorf("not supported") 144 } 145 146 // Hashrate returns the POW hashrate 147 func (s *LightDummyAPI) Hashrate() hexutil.Uint { 148 return 0 149 } 150 151 // Mining returns an indication if this node is currently mining. 152 func (s *LightDummyAPI) Mining() bool { 153 return false 154 } 155 156 // APIs returns the collection of RPC services the ethereum package offers. 157 // NOTE, some of these services probably need to be moved to somewhere else. 158 func (s *LightEthereum) APIs() []rpc.API { 159 return append(ethapi.GetAPIs(s.ApiBackend), []rpc.API{ 160 { 161 Namespace: "eth", 162 Version: "1.0", 163 Service: &LightDummyAPI{}, 164 Public: true, 165 }, { 166 Namespace: "eth", 167 Version: "1.0", 168 Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux), 169 Public: true, 170 }, { 171 Namespace: "eth", 172 Version: "1.0", 173 Service: filters.NewPublicFilterAPI(s.ApiBackend, true), 174 Public: true, 175 }, { 176 Namespace: "net", 177 Version: "1.0", 178 Service: s.netRPCService, 179 Public: true, 180 }, 181 }...) 182 } 183 184 func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) { 185 s.blockchain.ResetWithGenesisBlock(gb) 186 } 187 188 func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain } 189 func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool } 190 func (s *LightEthereum) Engine() consensus.Engine { return s.engine } 191 func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } 192 func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader } 193 func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux } 194 195 // Protocols implements node.Service, returning all the currently configured 196 // network protocols to start. 197 func (s *LightEthereum) Protocols() []p2p.Protocol { 198 return s.protocolManager.SubProtocols 199 } 200 201 // Start implements node.Service, starting all internal goroutines needed by the 202 // Ethereum protocol implementation. 203 func (s *LightEthereum) Start(srvr *p2p.Server) error { 204 log.Warn("Light client mode is an experimental feature") 205 s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.networkId) 206 s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash())) 207 s.protocolManager.Start() 208 return nil 209 } 210 211 // Stop implements node.Service, terminating all internal goroutines used by the 212 // Ethereum protocol. 213 func (s *LightEthereum) Stop() error { 214 s.odr.Stop() 215 s.blockchain.Stop() 216 s.protocolManager.Stop() 217 s.txPool.Stop() 218 219 s.eventMux.Stop() 220 221 time.Sleep(time.Millisecond * 200) 222 s.chainDb.Close() 223 close(s.shutdownChan) 224 225 return nil 226 }