github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/accounts" 26 "github.com/luckypickle/go-ethereum-vet/common" 27 "github.com/luckypickle/go-ethereum-vet/common/hexutil" 28 "github.com/luckypickle/go-ethereum-vet/consensus" 29 "github.com/luckypickle/go-ethereum-vet/core" 30 "github.com/luckypickle/go-ethereum-vet/core/bloombits" 31 "github.com/luckypickle/go-ethereum-vet/core/rawdb" 32 "github.com/luckypickle/go-ethereum-vet/core/types" 33 "github.com/luckypickle/go-ethereum-vet/eth" 34 "github.com/luckypickle/go-ethereum-vet/eth/downloader" 35 "github.com/luckypickle/go-ethereum-vet/eth/filters" 36 "github.com/luckypickle/go-ethereum-vet/eth/gasprice" 37 "github.com/luckypickle/go-ethereum-vet/event" 38 "github.com/luckypickle/go-ethereum-vet/internal/ethapi" 39 "github.com/luckypickle/go-ethereum-vet/light" 40 "github.com/luckypickle/go-ethereum-vet/log" 41 "github.com/luckypickle/go-ethereum-vet/node" 42 "github.com/luckypickle/go-ethereum-vet/p2p" 43 "github.com/luckypickle/go-ethereum-vet/p2p/discv5" 44 "github.com/luckypickle/go-ethereum-vet/params" 45 rpc "github.com/luckypickle/go-ethereum-vet/rpc" 46 ) 47 48 type LightEthereum struct { 49 lesCommons 50 51 odr *LesOdr 52 relay *LesTxRelay 53 chainConfig *params.ChainConfig 54 // Channel for shutting down the service 55 shutdownChan chan bool 56 57 // Handlers 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 67 68 ApiBackend *LesApiBackend 69 70 eventMux *event.TypeMux 71 engine consensus.Engine 72 accountManager *accounts.Manager 73 74 networkId uint64 75 netRPCService *ethapi.PublicNetAPI 76 77 wg sync.WaitGroup 78 } 79 80 func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { 81 chainDb, err := eth.CreateDB(ctx, config, "lightchaindata") 82 if err != nil { 83 return nil, err 84 } 85 chainConfig, genesisHash, genesisErr := core.SetupGenesisBlock(chainDb, config.Genesis) 86 if _, isCompat := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !isCompat { 87 return nil, genesisErr 88 } 89 log.Info("Initialised chain configuration", "config", chainConfig) 90 91 peers := newPeerSet() 92 quitSync := make(chan struct{}) 93 94 leth := &LightEthereum{ 95 lesCommons: lesCommons{ 96 chainDb: chainDb, 97 config: config, 98 }, 99 chainConfig: chainConfig, 100 eventMux: ctx.EventMux, 101 peers: peers, 102 reqDist: newRequestDistributor(peers, quitSync), 103 accountManager: ctx.AccountManager, 104 engine: eth.CreateConsensusEngine(ctx, chainConfig, &config.Ethash, nil, chainDb), 105 shutdownChan: make(chan bool), 106 networkId: config.NetworkId, 107 bloomRequests: make(chan chan *bloombits.Retrieval), 108 bloomIndexer: eth.NewBloomIndexer(chainDb, light.BloomTrieFrequency, light.HelperTrieConfirmations), 109 } 110 111 leth.relay = NewLesTxRelay(peers, leth.reqDist) 112 leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg) 113 leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool) 114 115 leth.odr = NewLesOdr(chainDb, leth.retriever) 116 leth.chtIndexer = light.NewChtIndexer(chainDb, true, leth.odr) 117 leth.bloomTrieIndexer = light.NewBloomTrieIndexer(chainDb, true, leth.odr) 118 leth.odr.SetIndexers(leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer) 119 120 // Note: NewLightChain adds the trusted checkpoint so it needs an ODR with 121 // indexers already set but not started yet 122 if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil { 123 return nil, err 124 } 125 // Note: AddChildIndexer starts the update process for the child 126 leth.bloomIndexer.AddChildIndexer(leth.bloomTrieIndexer) 127 leth.chtIndexer.Start(leth.blockchain) 128 leth.bloomIndexer.Start(leth.blockchain) 129 130 // Rewind the chain in case of an incompatible config upgrade. 131 if compat, ok := genesisErr.(*params.ConfigCompatError); ok { 132 log.Warn("Rewinding chain to upgrade configuration", "err", compat) 133 leth.blockchain.SetHead(compat.RewindTo) 134 rawdb.WriteChainConfig(chainDb, genesisHash, chainConfig) 135 } 136 137 leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay) 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 { 139 return nil, err 140 } 141 leth.ApiBackend = &LesApiBackend{leth, nil} 142 gpoParams := config.GPO 143 if gpoParams.Default == nil { 144 gpoParams.Default = config.MinerGasPrice 145 } 146 leth.ApiBackend.gpo = gasprice.NewOracle(leth.ApiBackend, gpoParams) 147 return leth, nil 148 } 149 150 func lesTopic(genesisHash common.Hash, protocolVersion uint) discv5.Topic { 151 var name string 152 switch protocolVersion { 153 case lpv1: 154 name = "LES" 155 case lpv2: 156 name = "LES2" 157 default: 158 panic(nil) 159 } 160 return discv5.Topic(name + "@" + common.Bytes2Hex(genesisHash.Bytes()[0:8])) 161 } 162 163 type LightDummyAPI struct{} 164 165 // Etherbase is the address that mining rewards will be send to 166 func (s *LightDummyAPI) Etherbase() (common.Address, error) { 167 return common.Address{}, fmt.Errorf("not supported") 168 } 169 170 // Coinbase is the address that mining rewards will be send to (alias for Etherbase) 171 func (s *LightDummyAPI) Coinbase() (common.Address, error) { 172 return common.Address{}, fmt.Errorf("not supported") 173 } 174 175 // Hashrate returns the POW hashrate 176 func (s *LightDummyAPI) Hashrate() hexutil.Uint { 177 return 0 178 } 179 180 // Mining returns an indication if this node is currently mining. 181 func (s *LightDummyAPI) Mining() bool { 182 return false 183 } 184 185 // APIs returns the collection of RPC services the ethereum package offers. 186 // NOTE, some of these services probably need to be moved to somewhere else. 187 func (s *LightEthereum) APIs() []rpc.API { 188 return append(ethapi.GetAPIs(s.ApiBackend), []rpc.API{ 189 { 190 Namespace: "eth", 191 Version: "1.0", 192 Service: &LightDummyAPI{}, 193 Public: true, 194 }, { 195 Namespace: "eth", 196 Version: "1.0", 197 Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux), 198 Public: true, 199 }, { 200 Namespace: "eth", 201 Version: "1.0", 202 Service: filters.NewPublicFilterAPI(s.ApiBackend, true), 203 Public: true, 204 }, { 205 Namespace: "net", 206 Version: "1.0", 207 Service: s.netRPCService, 208 Public: true, 209 }, 210 }...) 211 } 212 213 func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) { 214 s.blockchain.ResetWithGenesisBlock(gb) 215 } 216 217 func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain } 218 func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool } 219 func (s *LightEthereum) Engine() consensus.Engine { return s.engine } 220 func (s *LightEthereum) LesVersion() int { return int(ClientProtocolVersions[0]) } 221 func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader } 222 func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux } 223 224 // Protocols implements node.Service, returning all the currently configured 225 // network protocols to start. 226 func (s *LightEthereum) Protocols() []p2p.Protocol { 227 return s.makeProtocols(ClientProtocolVersions) 228 } 229 230 // Start implements node.Service, starting all internal goroutines needed by the 231 // Ethereum protocol implementation. 232 func (s *LightEthereum) Start(srvr *p2p.Server) error { 233 s.startBloomHandlers() 234 log.Warn("Light client mode is an experimental feature") 235 s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.networkId) 236 // clients are searching for the first advertised protocol in the list 237 protocolVersion := AdvertiseProtocolVersions[0] 238 s.serverPool.start(srvr, lesTopic(s.blockchain.Genesis().Hash(), protocolVersion)) 239 s.protocolManager.Start(s.config.LightPeers) 240 return nil 241 } 242 243 // Stop implements node.Service, terminating all internal goroutines used by the 244 // Ethereum protocol. 245 func (s *LightEthereum) Stop() error { 246 s.odr.Stop() 247 s.bloomIndexer.Close() 248 s.chtIndexer.Close() 249 s.blockchain.Stop() 250 s.protocolManager.Stop() 251 s.txPool.Stop() 252 s.engine.Close() 253 254 s.eventMux.Stop() 255 256 time.Sleep(time.Millisecond * 200) 257 s.chainDb.Close() 258 close(s.shutdownChan) 259 260 return nil 261 }