github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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 "errors" 22 "fmt" 23 "time" 24 25 "github.com/atheioschain/go-atheios/accounts" 26 "github.com/atheioschain/go-atheios/common" 27 "github.com/atheioschain/go-atheios/common/compiler" 28 "github.com/atheioschain/go-atheios/common/hexutil" 29 "github.com/atheioschain/go-atheios/core" 30 "github.com/atheioschain/go-atheios/core/types" 31 "github.com/atheioschain/go-atheios/eth" 32 "github.com/atheioschain/go-atheios/eth/downloader" 33 "github.com/atheioschain/go-atheios/eth/filters" 34 "github.com/atheioschain/go-atheios/eth/gasprice" 35 "github.com/atheioschain/go-atheios/ethdb" 36 "github.com/atheioschain/go-atheios/event" 37 "github.com/atheioschain/go-atheios/internal/ethapi" 38 "github.com/atheioschain/go-atheios/light" 39 "github.com/atheioschain/go-atheios/logger" 40 "github.com/atheioschain/go-atheios/logger/glog" 41 "github.com/atheioschain/go-atheios/node" 42 "github.com/atheioschain/go-atheios/p2p" 43 "github.com/atheioschain/go-atheios/params" 44 "github.com/atheioschain/go-atheios/pow" 45 rpc "github.com/atheioschain/go-atheios/rpc" 46 ) 47 48 type LightEthereum struct { 49 odr *LesOdr 50 relay *LesTxRelay 51 chainConfig *params.ChainConfig 52 // Channel for shutting down the service 53 shutdownChan chan bool 54 // Handlers 55 txPool *light.TxPool 56 blockchain *light.LightChain 57 protocolManager *ProtocolManager 58 // DB interfaces 59 chainDb ethdb.Database // Block chain database 60 61 ApiBackend *LesApiBackend 62 63 eventMux *event.TypeMux 64 pow pow.PoW 65 accountManager *accounts.Manager 66 solcPath string 67 solc *compiler.Solidity 68 69 netVersionId int 70 netRPCService *ethapi.PublicNetAPI 71 } 72 73 func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { 74 chainDb, err := eth.CreateDB(ctx, config, "lightchaindata") 75 if err != nil { 76 return nil, err 77 } 78 if err := eth.SetupGenesisBlock(&chainDb, config); err != nil { 79 return nil, err 80 } 81 pow, err := eth.CreatePoW(config) 82 if err != nil { 83 return nil, err 84 } 85 86 odr := NewLesOdr(chainDb) 87 relay := NewLesTxRelay() 88 eth := &LightEthereum{ 89 odr: odr, 90 relay: relay, 91 chainDb: chainDb, 92 eventMux: ctx.EventMux, 93 accountManager: ctx.AccountManager, 94 pow: pow, 95 shutdownChan: make(chan bool), 96 netVersionId: config.NetworkId, 97 solcPath: config.SolcPath, 98 } 99 100 if config.ChainConfig == nil { 101 return nil, errors.New("missing chain config") 102 } 103 eth.chainConfig = config.ChainConfig 104 eth.blockchain, err = light.NewLightChain(odr, eth.chainConfig, eth.pow, eth.eventMux) 105 if err != nil { 106 if err == core.ErrNoGenesis { 107 return nil, fmt.Errorf(`Genesis block not found. Please supply a genesis block with the "--genesis /path/to/file" argument`) 108 } 109 return nil, err 110 } 111 112 eth.txPool = light.NewTxPool(eth.chainConfig, eth.eventMux, eth.blockchain, eth.relay) 113 if eth.protocolManager, err = NewProtocolManager(eth.chainConfig, config.LightMode, config.NetworkId, eth.eventMux, eth.pow, eth.blockchain, nil, chainDb, odr, relay); err != nil { 114 return nil, err 115 } 116 117 eth.ApiBackend = &LesApiBackend{eth, nil} 118 eth.ApiBackend.gpo = gasprice.NewLightPriceOracle(eth.ApiBackend) 119 return eth, nil 120 } 121 122 type LightDummyAPI struct{} 123 124 // Etherbase is the address that mining rewards will be send to 125 func (s *LightDummyAPI) Etherbase() (common.Address, error) { 126 return common.Address{}, fmt.Errorf("not supported") 127 } 128 129 // Coinbase is the address that mining rewards will be send to (alias for Etherbase) 130 func (s *LightDummyAPI) Coinbase() (common.Address, error) { 131 return common.Address{}, fmt.Errorf("not supported") 132 } 133 134 // Hashrate returns the POW hashrate 135 func (s *LightDummyAPI) Hashrate() hexutil.Uint { 136 return 0 137 } 138 139 // Mining returns an indication if this node is currently mining. 140 func (s *LightDummyAPI) Mining() bool { 141 return false 142 } 143 144 // APIs returns the collection of RPC services the ethereum package offers. 145 // NOTE, some of these services probably need to be moved to somewhere else. 146 func (s *LightEthereum) APIs() []rpc.API { 147 return append(ethapi.GetAPIs(s.ApiBackend, s.solcPath), []rpc.API{ 148 { 149 Namespace: "eth", 150 Version: "1.0", 151 Service: &LightDummyAPI{}, 152 Public: true, 153 }, { 154 Namespace: "eth", 155 Version: "1.0", 156 Service: downloader.NewPublicDownloaderAPI(s.protocolManager.downloader, s.eventMux), 157 Public: true, 158 }, { 159 Namespace: "eth", 160 Version: "1.0", 161 Service: filters.NewPublicFilterAPI(s.ApiBackend, true), 162 Public: true, 163 }, { 164 Namespace: "net", 165 Version: "1.0", 166 Service: s.netRPCService, 167 Public: true, 168 }, 169 }...) 170 } 171 172 func (s *LightEthereum) ResetWithGenesisBlock(gb *types.Block) { 173 s.blockchain.ResetWithGenesisBlock(gb) 174 } 175 176 func (s *LightEthereum) BlockChain() *light.LightChain { return s.blockchain } 177 func (s *LightEthereum) TxPool() *light.TxPool { return s.txPool } 178 func (s *LightEthereum) LesVersion() int { return int(s.protocolManager.SubProtocols[0].Version) } 179 func (s *LightEthereum) Downloader() *downloader.Downloader { return s.protocolManager.downloader } 180 func (s *LightEthereum) EventMux() *event.TypeMux { return s.eventMux } 181 182 // Protocols implements node.Service, returning all the currently configured 183 // network protocols to start. 184 func (s *LightEthereum) Protocols() []p2p.Protocol { 185 return s.protocolManager.SubProtocols 186 } 187 188 // Start implements node.Service, starting all internal goroutines needed by the 189 // Ethereum protocol implementation. 190 func (s *LightEthereum) Start(srvr *p2p.Server) error { 191 glog.V(logger.Info).Infof("WARNING: light client mode is an experimental feature") 192 s.netRPCService = ethapi.NewPublicNetAPI(srvr, s.netVersionId) 193 s.protocolManager.Start(srvr) 194 return nil 195 } 196 197 // Stop implements node.Service, terminating all internal goroutines used by the 198 // Ethereum protocol. 199 func (s *LightEthereum) Stop() error { 200 s.odr.Stop() 201 s.blockchain.Stop() 202 s.protocolManager.Stop() 203 s.txPool.Stop() 204 205 s.eventMux.Stop() 206 207 time.Sleep(time.Millisecond * 200) 208 s.chainDb.Close() 209 close(s.shutdownChan) 210 211 return nil 212 }