@@ -19,6 +19,7 @@ package les
1919
2020import (
2121 "fmt"
22+ "sync"
2223 "time"
2324
2425 "github.com/ethereum/go-ethereum/accounts"
@@ -38,6 +39,7 @@ import (
3839 "github.com/ethereum/go-ethereum/log"
3940 "github.com/ethereum/go-ethereum/node"
4041 "github.com/ethereum/go-ethereum/p2p"
42+ "github.com/ethereum/go-ethereum/p2p/discv5"
4143 "github.com/ethereum/go-ethereum/params"
4244 rpc "github.com/ethereum/go-ethereum/rpc"
4345)
@@ -49,9 +51,13 @@ type LightEthereum struct {
4951 // Channel for shutting down the service
5052 shutdownChan chan bool
5153 // Handlers
54+ peers * peerSet
5255 txPool * light.TxPool
5356 blockchain * light.LightChain
5457 protocolManager * ProtocolManager
58+ serverPool * serverPool
59+ reqDist * requestDistributor
60+ retriever * retrieveManager
5561 // DB interfaces
5662 chainDb ethdb.Database // Block chain database
5763
@@ -63,6 +69,9 @@ type LightEthereum struct {
6369
6470 networkId uint64
6571 netRPCService * ethapi.PublicNetAPI
72+
73+ quitSync chan struct {}
74+ wg sync.WaitGroup
6675}
6776
6877func New (ctx * node.ServiceContext , config * eth.Config ) (* LightEthereum , error ) {
@@ -76,20 +85,26 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
7685 }
7786 log .Info ("Initialised chain configuration" , "config" , chainConfig )
7887
79- odr := NewLesOdr (chainDb )
80- relay := NewLesTxRelay ()
88+ peers := newPeerSet ()
89+ quitSync := make (chan struct {})
90+
8191 eth := & LightEthereum {
82- odr : odr ,
83- relay : relay ,
84- chainDb : chainDb ,
8592 chainConfig : chainConfig ,
93+ chainDb : chainDb ,
8694 eventMux : ctx .EventMux ,
95+ peers : peers ,
96+ reqDist : newRequestDistributor (peers , quitSync ),
8797 accountManager : ctx .AccountManager ,
8898 engine : eth .CreateConsensusEngine (ctx , config , chainConfig , chainDb ),
8999 shutdownChan : make (chan bool ),
90100 networkId : config .NetworkId ,
91101 }
92- if eth .blockchain , err = light .NewLightChain (odr , eth .chainConfig , eth .engine , eth .eventMux ); err != nil {
102+
103+ eth .relay = NewLesTxRelay (peers , eth .reqDist )
104+ eth .serverPool = newServerPool (chainDb , quitSync , & eth .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 {
93108 return nil , err
94109 }
95110 // Rewind the chain in case of an incompatible config upgrade.
@@ -100,13 +115,9 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
100115 }
101116
102117 eth .txPool = light .NewTxPool (eth .chainConfig , eth .eventMux , eth .blockchain , eth .relay )
103- lightSync := config .SyncMode == downloader .LightSync
104- if eth .protocolManager , err = NewProtocolManager (eth .chainConfig , lightSync , config .NetworkId , eth .eventMux , eth .engine , eth .blockchain , nil , chainDb , odr , relay ); err != nil {
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 , & eth .wg ); err != nil {
105119 return nil , err
106120 }
107- relay .ps = eth .protocolManager .peers
108- relay .reqDist = eth .protocolManager .reqDist
109-
110121 eth .ApiBackend = & LesApiBackend {eth , nil }
111122 gpoParams := config .GPO
112123 if gpoParams .Default == nil {
@@ -116,6 +127,10 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
116127 return eth , nil
117128}
118129
130+ func lesTopic (genesisHash common.Hash ) discv5.Topic {
131+ return discv5 .Topic ("LES@" + common .Bytes2Hex (genesisHash .Bytes ()[0 :8 ]))
132+ }
133+
119134type LightDummyAPI struct {}
120135
121136// Etherbase is the address that mining rewards will be send to
@@ -188,7 +203,8 @@ func (s *LightEthereum) Protocols() []p2p.Protocol {
188203func (s * LightEthereum ) Start (srvr * p2p.Server ) error {
189204 log .Warn ("Light client mode is an experimental feature" )
190205 s .netRPCService = ethapi .NewPublicNetAPI (srvr , s .networkId )
191- s .protocolManager .Start (srvr )
206+ s .serverPool .start (srvr , lesTopic (s .blockchain .Genesis ().Hash ()))
207+ s .protocolManager .Start ()
192208 return nil
193209}
194210
0 commit comments