6666type Config struct {
6767 ChainConfig * core.ChainConfig // chain configuration
6868
69- NetworkId int // Network ID to use for selecting peers to connect to
70- Genesis string // Genesis JSON to seed the chain database with
71- FastSync bool // Enables the state download based fast synchronisation algorithm
69+ NetworkId int // Network ID to use for selecting peers to connect to
70+ Genesis string // Genesis JSON to seed the chain database with
71+ FastSync bool // Enables the state download based fast synchronisation algorithm
72+ LightMode bool // Running in light client mode
73+ NoDefSrv bool // No default LES server
74+ LightServ int // Maximum percentage of time allowed for serving LES requests
75+ LightPeers int // Maximum number of LES client peers
76+ MaxPeers int // Maximum number of global peers
7277
7378 SkipBcVersionCheck bool // e.g. blockchain export
7479 DatabaseCache int
@@ -100,6 +105,12 @@ type Config struct {
100105 TestGenesisState ethdb.Database // Genesis state to seed the database with (testing only!)
101106}
102107
108+ type LesServer interface {
109+ Start ()
110+ Stop ()
111+ Protocols () []p2p.Protocol
112+ }
113+
103114// Ethereum implements the Ethereum full node service.
104115type Ethereum struct {
105116 chainConfig * core.ChainConfig
@@ -111,6 +122,7 @@ type Ethereum struct {
111122 txMu sync.Mutex
112123 blockchain * core.BlockChain
113124 protocolManager * ProtocolManager
125+ lesServer LesServer
114126 // DB interfaces
115127 chainDb ethdb.Database // Block chain database
116128
@@ -119,7 +131,7 @@ type Ethereum struct {
119131 httpclient * httpclient.HTTPClient
120132 accountManager * accounts.Manager
121133
122- apiBackend * EthApiBackend
134+ ApiBackend * EthApiBackend
123135
124136 miner * miner.Miner
125137 Mining bool
@@ -135,10 +147,14 @@ type Ethereum struct {
135147 netRPCService * ethapi.PublicNetAPI
136148}
137149
150+ func (s * Ethereum ) AddLesServer (ls LesServer ) {
151+ s .lesServer = ls
152+ }
153+
138154// New creates a new Ethereum object (including the
139155// initialisation of the common Ethereum object)
140156func New (ctx * node.ServiceContext , config * Config ) (* Ethereum , error ) {
141- chainDb , err := createDB (ctx , config )
157+ chainDb , err := CreateDB (ctx , config , "chaindata" )
142158 if err != nil {
143159 return nil , err
144160 }
@@ -217,7 +233,18 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
217233 newPool := core .NewTxPool (eth .chainConfig , eth .EventMux (), eth .blockchain .State , eth .blockchain .GasLimit )
218234 eth .txPool = newPool
219235
220- if eth .protocolManager , err = NewProtocolManager (eth .chainConfig , config .FastSync , config .NetworkId , eth .eventMux , eth .txPool , eth .pow , eth .blockchain , chainDb ); err != nil {
236+ maxPeers := config .MaxPeers
237+ if config .LightServ > 0 {
238+ // if we are running a light server, limit the number of ETH peers so that we reserve some space for incoming LES connections
239+ // temporary solution until the new peer connectivity API is finished
240+ halfPeers := maxPeers / 2
241+ maxPeers -= config .LightPeers
242+ if maxPeers < halfPeers {
243+ maxPeers = halfPeers
244+ }
245+ }
246+
247+ if eth .protocolManager , err = NewProtocolManager (eth .chainConfig , config .FastSync , config .NetworkId , maxPeers , eth .eventMux , eth .txPool , eth .pow , eth .blockchain , chainDb ); err != nil {
221248 return nil , err
222249 }
223250 eth .miner = miner .New (eth , eth .chainConfig , eth .EventMux (), eth .pow )
@@ -233,14 +260,14 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
233260 GpobaseCorrectionFactor : config .GpobaseCorrectionFactor ,
234261 }
235262 gpo := gasprice .NewGasPriceOracle (eth .blockchain , chainDb , eth .eventMux , gpoParams )
236- eth .apiBackend = & EthApiBackend {eth , gpo }
263+ eth .ApiBackend = & EthApiBackend {eth , gpo }
237264
238265 return eth , nil
239266}
240267
241- // createDB creates the chain database.
242- func createDB (ctx * node.ServiceContext , config * Config ) (ethdb.Database , error ) {
243- db , err := ctx .OpenDatabase ("chaindata" , config .DatabaseCache , config .DatabaseHandles )
268+ // CreateDB creates the chain database.
269+ func CreateDB (ctx * node.ServiceContext , config * Config , name string ) (ethdb.Database , error ) {
270+ db , err := ctx .OpenDatabase (name , config .DatabaseCache , config .DatabaseHandles )
244271 if db , ok := db .(* ethdb.LDBDatabase ); ok {
245272 db .Meter ("eth/db/chaindata/" )
246273 }
@@ -288,7 +315,7 @@ func CreatePoW(config *Config) (*ethash.Ethash, error) {
288315// APIs returns the collection of RPC services the ethereum package offers.
289316// NOTE, some of these services probably need to be moved to somewhere else.
290317func (s * Ethereum ) APIs () []rpc.API {
291- return append (ethapi .GetAPIs (s .apiBackend , s .solcPath ), []rpc.API {
318+ return append (ethapi .GetAPIs (s .ApiBackend , s .solcPath ), []rpc.API {
292319 {
293320 Namespace : "eth" ,
294321 Version : "1.0" ,
@@ -312,7 +339,7 @@ func (s *Ethereum) APIs() []rpc.API {
312339 }, {
313340 Namespace : "eth" ,
314341 Version : "1.0" ,
315- Service : filters .NewPublicFilterAPI (s .chainDb , s . eventMux ),
342+ Service : filters .NewPublicFilterAPI (s .ApiBackend , true ),
316343 Public : true ,
317344 }, {
318345 Namespace : "admin" ,
@@ -380,7 +407,11 @@ func (s *Ethereum) Downloader() *downloader.Downloader { return s.protocolManage
380407// Protocols implements node.Service, returning all the currently configured
381408// network protocols to start.
382409func (s * Ethereum ) Protocols () []p2p.Protocol {
383- return s .protocolManager .SubProtocols
410+ if s .lesServer == nil {
411+ return s .protocolManager .SubProtocols
412+ } else {
413+ return append (s .protocolManager .SubProtocols , s .lesServer .Protocols ()... )
414+ }
384415}
385416
386417// Start implements node.Service, starting all internal goroutines needed by the
@@ -391,6 +422,9 @@ func (s *Ethereum) Start(srvr *p2p.Server) error {
391422 s .StartAutoDAG ()
392423 }
393424 s .protocolManager .Start ()
425+ if s .lesServer != nil {
426+ s .lesServer .Start ()
427+ }
394428 return nil
395429}
396430
@@ -402,6 +436,9 @@ func (s *Ethereum) Stop() error {
402436 }
403437 s .blockchain .Stop ()
404438 s .protocolManager .Stop ()
439+ if s .lesServer != nil {
440+ s .lesServer .Stop ()
441+ }
405442 s .txPool .Stop ()
406443 s .miner .Stop ()
407444 s .eventMux .Stop ()
0 commit comments