2016-11-09 04:01:56 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-10-14 06:51:29 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
2017-10-24 16:19:09 +03:00
|
|
|
"crypto/ecdsa"
|
2016-10-14 06:51:29 +03:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-26 14:32:48 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 14:35:06 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/les/flowcontrol"
|
|
|
|
"github.com/ethereum/go-ethereum/light"
|
2017-02-22 15:10:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2017-06-21 13:27:38 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-08-28 10:08:16 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-02-26 14:32:48 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-10-14 06:51:29 +03:00
|
|
|
)
|
|
|
|
|
2019-02-26 14:32:48 +03:00
|
|
|
const bufLimitRatio = 6000 // fixed bufLimit/MRR ratio
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
type LesServer struct {
|
2018-08-17 13:21:53 +03:00
|
|
|
lesCommons
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2019-01-24 14:18:26 +03:00
|
|
|
fcManager *flowcontrol.ClientManager // nil if our node is client only
|
2019-02-26 14:32:48 +03:00
|
|
|
costTracker *costTracker
|
|
|
|
defParams flowcontrol.ServerParams
|
2019-01-24 14:18:26 +03:00
|
|
|
lesTopics []discv5.Topic
|
|
|
|
privateKey *ecdsa.PrivateKey
|
|
|
|
quitSync chan struct{}
|
|
|
|
onlyAnnounce bool
|
2019-02-26 14:32:48 +03:00
|
|
|
|
|
|
|
thcNormal, thcBlockProcessing int // serving thread count for normal operation and block processing mode
|
|
|
|
|
|
|
|
maxPeers int
|
|
|
|
freeClientCap uint64
|
|
|
|
freeClientPool *freeClientPool
|
|
|
|
priorityClientPool *priorityClientPool
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
|
2017-06-21 13:27:38 +03:00
|
|
|
quitSync := make(chan struct{})
|
2019-01-24 14:18:26 +03:00
|
|
|
pm, err := NewProtocolManager(
|
|
|
|
eth.BlockChain().Config(),
|
|
|
|
light.DefaultServerIndexerConfig,
|
|
|
|
false,
|
|
|
|
config.NetworkId,
|
|
|
|
eth.EventMux(),
|
|
|
|
eth.Engine(),
|
|
|
|
newPeerSet(),
|
|
|
|
eth.BlockChain(),
|
|
|
|
eth.TxPool(),
|
|
|
|
eth.ChainDb(),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
quitSync,
|
|
|
|
new(sync.WaitGroup),
|
|
|
|
config.ULC)
|
2016-10-14 06:51:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2018-01-22 15:38:34 +03:00
|
|
|
lesTopics := make([]discv5.Topic, len(AdvertiseProtocolVersions))
|
|
|
|
for i, pv := range AdvertiseProtocolVersions {
|
2017-10-24 16:19:09 +03:00
|
|
|
lesTopics[i] = lesTopic(eth.BlockChain().Genesis().Hash(), pv)
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
|
2017-06-21 13:27:38 +03:00
|
|
|
srv := &LesServer{
|
2018-08-17 13:21:53 +03:00
|
|
|
lesCommons: lesCommons{
|
|
|
|
config: config,
|
|
|
|
chainDb: eth.ChainDb(),
|
2018-08-28 10:08:16 +03:00
|
|
|
iConfig: light.DefaultServerIndexerConfig,
|
|
|
|
chtIndexer: light.NewChtIndexer(eth.ChainDb(), nil, params.CHTFrequencyServer, params.HelperTrieProcessConfirmations),
|
|
|
|
bloomTrieIndexer: light.NewBloomTrieIndexer(eth.ChainDb(), nil, params.BloomBitsBlocks, params.BloomTrieFrequency),
|
2018-08-17 13:21:53 +03:00
|
|
|
protocolManager: pm,
|
|
|
|
},
|
2019-02-26 14:32:48 +03:00
|
|
|
costTracker: newCostTracker(eth.ChainDb(), config),
|
2019-01-24 14:18:26 +03:00
|
|
|
quitSync: quitSync,
|
|
|
|
lesTopics: lesTopics,
|
|
|
|
onlyAnnounce: config.OnlyAnnounce,
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
2018-08-17 13:21:53 +03:00
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
logger := log.New()
|
2019-02-26 14:32:48 +03:00
|
|
|
pm.server = srv
|
|
|
|
srv.thcNormal = config.LightServ * 4 / 100
|
|
|
|
if srv.thcNormal < 4 {
|
|
|
|
srv.thcNormal = 4
|
|
|
|
}
|
|
|
|
srv.thcBlockProcessing = config.LightServ/100 + 1
|
|
|
|
srv.fcManager = flowcontrol.NewClientManager(nil, &mclock.System{})
|
2017-10-24 16:19:09 +03:00
|
|
|
|
|
|
|
chtV1SectionCount, _, _ := srv.chtIndexer.Sections() // indexer still uses LES/1 4k section size for backwards server compatibility
|
2018-08-28 10:08:16 +03:00
|
|
|
chtV2SectionCount := chtV1SectionCount / (params.CHTFrequencyClient / params.CHTFrequencyServer)
|
2017-10-24 16:19:09 +03:00
|
|
|
if chtV2SectionCount != 0 {
|
|
|
|
// convert to LES/2 section
|
|
|
|
chtLastSection := chtV2SectionCount - 1
|
|
|
|
// convert last LES/2 section index back to LES/1 index for chtIndexer.SectionHead
|
2018-08-28 10:08:16 +03:00
|
|
|
chtLastSectionV1 := (chtLastSection+1)*(params.CHTFrequencyClient/params.CHTFrequencyServer) - 1
|
2017-10-24 16:19:09 +03:00
|
|
|
chtSectionHead := srv.chtIndexer.SectionHead(chtLastSectionV1)
|
2018-08-28 10:08:16 +03:00
|
|
|
chtRoot := light.GetChtRoot(pm.chainDb, chtLastSectionV1, chtSectionHead)
|
2018-02-11 15:57:46 +03:00
|
|
|
logger.Info("Loaded CHT", "section", chtLastSection, "head", chtSectionHead, "root", chtRoot)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
bloomTrieSectionCount, _, _ := srv.bloomTrieIndexer.Sections()
|
|
|
|
if bloomTrieSectionCount != 0 {
|
|
|
|
bloomTrieLastSection := bloomTrieSectionCount - 1
|
|
|
|
bloomTrieSectionHead := srv.bloomTrieIndexer.SectionHead(bloomTrieLastSection)
|
|
|
|
bloomTrieRoot := light.GetBloomTrieRoot(pm.chainDb, bloomTrieLastSection, bloomTrieSectionHead)
|
2018-02-11 15:57:46 +03:00
|
|
|
logger.Info("Loaded bloom trie", "section", bloomTrieLastSection, "head", bloomTrieSectionHead, "root", bloomTrieRoot)
|
2017-06-21 13:27:38 +03:00
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
|
|
|
|
srv.chtIndexer.Start(eth.BlockChain())
|
2019-02-26 14:32:48 +03:00
|
|
|
return srv, nil
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
|
2019-02-26 14:32:48 +03:00
|
|
|
func (s *LesServer) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "les",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: NewPrivateLightServerAPI(s),
|
|
|
|
Public: false,
|
|
|
|
},
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
2019-02-26 14:32:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// startEventLoop starts an event handler loop that updates the recharge curve of
|
|
|
|
// the client manager and adjusts the client pool's size according to the total
|
|
|
|
// capacity updates coming from the client manager
|
|
|
|
func (s *LesServer) startEventLoop() {
|
|
|
|
s.protocolManager.wg.Add(1)
|
|
|
|
|
|
|
|
var processing bool
|
|
|
|
blockProcFeed := make(chan bool, 100)
|
|
|
|
s.protocolManager.blockchain.(*core.BlockChain).SubscribeBlockProcessingEvent(blockProcFeed)
|
|
|
|
totalRechargeCh := make(chan uint64, 100)
|
|
|
|
totalRecharge := s.costTracker.subscribeTotalRecharge(totalRechargeCh)
|
|
|
|
totalCapacityCh := make(chan uint64, 100)
|
|
|
|
updateRecharge := func() {
|
|
|
|
if processing {
|
|
|
|
s.protocolManager.servingQueue.setThreads(s.thcBlockProcessing)
|
|
|
|
s.fcManager.SetRechargeCurve(flowcontrol.PieceWiseLinear{{0, 0}, {totalRecharge, totalRecharge}})
|
|
|
|
} else {
|
|
|
|
s.protocolManager.servingQueue.setThreads(s.thcNormal)
|
|
|
|
s.fcManager.SetRechargeCurve(flowcontrol.PieceWiseLinear{{0, 0}, {totalRecharge / 10, totalRecharge}, {totalRecharge, totalRecharge}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateRecharge()
|
|
|
|
totalCapacity := s.fcManager.SubscribeTotalCapacity(totalCapacityCh)
|
|
|
|
s.priorityClientPool.setLimits(s.maxPeers, totalCapacity)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case processing = <-blockProcFeed:
|
|
|
|
updateRecharge()
|
|
|
|
case totalRecharge = <-totalRechargeCh:
|
|
|
|
updateRecharge()
|
|
|
|
case totalCapacity = <-totalCapacityCh:
|
|
|
|
s.priorityClientPool.setLimits(s.maxPeers, totalCapacity)
|
|
|
|
case <-s.protocolManager.quitSync:
|
|
|
|
s.protocolManager.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LesServer) Protocols() []p2p.Protocol {
|
2018-08-17 13:21:53 +03:00
|
|
|
return s.makeProtocols(ServerProtocolVersions)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
2017-01-23 04:35:46 +03:00
|
|
|
// Start starts the LES server
|
2016-10-19 14:04:55 +03:00
|
|
|
func (s *LesServer) Start(srvr *p2p.Server) {
|
2019-02-26 14:32:48 +03:00
|
|
|
s.maxPeers = s.config.LightPeers
|
|
|
|
totalRecharge := s.costTracker.totalRecharge()
|
|
|
|
if s.maxPeers > 0 {
|
|
|
|
s.freeClientCap = minCapacity //totalRecharge / uint64(s.maxPeers)
|
|
|
|
if s.freeClientCap < minCapacity {
|
|
|
|
s.freeClientCap = minCapacity
|
|
|
|
}
|
|
|
|
if s.freeClientCap > 0 {
|
|
|
|
s.defParams = flowcontrol.ServerParams{
|
|
|
|
BufLimit: s.freeClientCap * bufLimitRatio,
|
|
|
|
MinRecharge: s.freeClientCap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freePeers := int(totalRecharge / s.freeClientCap)
|
|
|
|
if freePeers < s.maxPeers {
|
|
|
|
log.Warn("Light peer count limited", "specified", s.maxPeers, "allowed", freePeers)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.freeClientPool = newFreeClientPool(s.chainDb, s.freeClientCap, 10000, mclock.System{}, func(id string) { go s.protocolManager.removePeer(id) })
|
|
|
|
s.priorityClientPool = newPriorityClientPool(s.freeClientCap, s.protocolManager.peers, s.freeClientPool)
|
|
|
|
|
|
|
|
s.protocolManager.peers.notify(s.priorityClientPool)
|
|
|
|
s.startEventLoop()
|
2018-02-05 16:41:53 +03:00
|
|
|
s.protocolManager.Start(s.config.LightPeers)
|
2018-02-10 15:33:52 +03:00
|
|
|
if srvr.DiscV5 != nil {
|
|
|
|
for _, topic := range s.lesTopics {
|
|
|
|
topic := topic
|
|
|
|
go func() {
|
|
|
|
logger := log.New("topic", topic)
|
|
|
|
logger.Info("Starting topic registration")
|
|
|
|
defer logger.Info("Terminated topic registration")
|
|
|
|
|
|
|
|
srvr.DiscV5.RegisterTopic(topic, s.quitSync)
|
|
|
|
}()
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
s.privateKey = srvr.PrivateKey
|
|
|
|
s.protocolManager.blockLoop()
|
|
|
|
}
|
2017-06-21 13:27:38 +03:00
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
func (s *LesServer) SetBloomBitsIndexer(bloomIndexer *core.ChainIndexer) {
|
|
|
|
bloomIndexer.AddChildIndexer(s.bloomTrieIndexer)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
2016-12-09 11:03:22 +03:00
|
|
|
// Stop stops the LES service
|
2016-10-14 06:51:29 +03:00
|
|
|
func (s *LesServer) Stop() {
|
2017-10-24 16:19:09 +03:00
|
|
|
s.chtIndexer.Close()
|
|
|
|
// bloom trie indexer is closed by parent bloombits indexer
|
2016-10-14 06:51:29 +03:00
|
|
|
go func() {
|
|
|
|
<-s.protocolManager.noMorePeers
|
|
|
|
}()
|
2019-02-26 14:32:48 +03:00
|
|
|
s.freeClientPool.stop()
|
|
|
|
s.costTracker.stop()
|
2016-10-14 06:51:29 +03:00
|
|
|
s.protocolManager.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *ProtocolManager) blockLoop() {
|
|
|
|
pm.wg.Add(1)
|
2017-08-18 13:58:36 +03:00
|
|
|
headCh := make(chan core.ChainHeadEvent, 10)
|
|
|
|
headSub := pm.blockchain.SubscribeChainHeadEvent(headCh)
|
2016-10-14 06:51:29 +03:00
|
|
|
go func() {
|
|
|
|
var lastHead *types.Header
|
|
|
|
lastBroadcastTd := common.Big0
|
|
|
|
for {
|
|
|
|
select {
|
2017-08-18 13:58:36 +03:00
|
|
|
case ev := <-headCh:
|
2016-10-14 06:51:29 +03:00
|
|
|
peers := pm.peers.AllPeers()
|
|
|
|
if len(peers) > 0 {
|
2017-08-18 13:58:36 +03:00
|
|
|
header := ev.Block.Header()
|
2016-10-14 06:51:29 +03:00
|
|
|
hash := header.Hash()
|
2016-11-09 03:20:49 +03:00
|
|
|
number := header.Number.Uint64()
|
2018-05-07 14:35:06 +03:00
|
|
|
td := rawdb.ReadTd(pm.chainDb, hash, number)
|
2016-10-14 06:51:29 +03:00
|
|
|
if td != nil && td.Cmp(lastBroadcastTd) > 0 {
|
|
|
|
var reorg uint64
|
|
|
|
if lastHead != nil {
|
2018-05-07 14:35:06 +03:00
|
|
|
reorg = lastHead.Number.Uint64() - rawdb.FindCommonAncestor(pm.chainDb, header, lastHead).Number.Uint64()
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
lastHead = header
|
|
|
|
lastBroadcastTd = td
|
2016-11-13 14:34:50 +03:00
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
log.Debug("Announcing block to peers", "number", number, "hash", hash, "td", td, "reorg", reorg)
|
2016-11-13 14:34:50 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
announce := announceData{Hash: hash, Number: number, Td: td, ReorgDepth: reorg}
|
2017-10-24 16:19:09 +03:00
|
|
|
var (
|
|
|
|
signed bool
|
|
|
|
signedAnnounce announceData
|
|
|
|
)
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
for _, p := range peers {
|
2017-10-24 16:19:09 +03:00
|
|
|
switch p.announceType {
|
|
|
|
|
|
|
|
case announceTypeSimple:
|
2019-02-26 14:32:48 +03:00
|
|
|
p.queueSend(func() { p.SendAnnounce(announce) })
|
2017-10-24 16:19:09 +03:00
|
|
|
case announceTypeSigned:
|
|
|
|
if !signed {
|
|
|
|
signedAnnounce = announce
|
|
|
|
signedAnnounce.sign(pm.server.privateKey)
|
|
|
|
signed = true
|
|
|
|
}
|
|
|
|
|
2019-02-26 14:32:48 +03:00
|
|
|
p.queueSend(func() { p.SendAnnounce(signedAnnounce) })
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-pm.quitSync:
|
2017-08-18 13:58:36 +03:00
|
|
|
headSub.Unsubscribe()
|
2016-10-14 06:51:29 +03:00
|
|
|
pm.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|