2016-11-09 01:01:56 +00:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-10-14 03:51:29 +00: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 13:19:09 +00:00
|
|
|
"crypto/ecdsa"
|
2016-10-14 03:51:29 +00:00
|
|
|
"sync"
|
2019-05-30 18:51:13 +00:00
|
|
|
"time"
|
2016-10-14 03:51:29 +00:00
|
|
|
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-02-26 11:32:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/mclock"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2018-05-07 11:35:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2016-10-14 03:51:29 +00: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 12:10:07 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-10-14 03:51:29 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2017-06-21 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2019-08-03 12:36:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-08-28 07:08:16 +00:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2019-02-26 11:32:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2016-10-14 03:51:29 +00:00
|
|
|
)
|
|
|
|
|
2019-02-26 11:32:48 +00:00
|
|
|
const bufLimitRatio = 6000 // fixed bufLimit/MRR ratio
|
|
|
|
|
2016-10-14 03:51:29 +00:00
|
|
|
type LesServer struct {
|
2018-08-17 10:21:53 +00:00
|
|
|
lesCommons
|
2017-10-24 13:19:09 +00:00
|
|
|
|
2019-06-11 07:40:32 +00:00
|
|
|
archiveMode bool // Flag whether the ethereum node runs in archive mode.
|
|
|
|
|
2019-01-24 11:18:26 +00:00
|
|
|
fcManager *flowcontrol.ClientManager // nil if our node is client only
|
2019-02-26 11:32:48 +00:00
|
|
|
costTracker *costTracker
|
2019-05-30 18:51:13 +00:00
|
|
|
testCost uint64
|
2019-02-26 11:32:48 +00:00
|
|
|
defParams flowcontrol.ServerParams
|
2019-01-24 11:18:26 +00:00
|
|
|
lesTopics []discv5.Topic
|
|
|
|
privateKey *ecdsa.PrivateKey
|
|
|
|
quitSync chan struct{}
|
|
|
|
onlyAnnounce bool
|
2019-02-26 11:32:48 +00:00
|
|
|
|
|
|
|
thcNormal, thcBlockProcessing int // serving thread count for normal operation and block processing mode
|
|
|
|
|
2019-08-03 12:36:10 +00:00
|
|
|
maxPeers int
|
|
|
|
minCapacity, maxCapacity, freeClientCap uint64
|
|
|
|
clientPool *clientPool
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) {
|
2018-01-22 12:38:34 +00:00
|
|
|
lesTopics := make([]discv5.Topic, len(AdvertiseProtocolVersions))
|
|
|
|
for i, pv := range AdvertiseProtocolVersions {
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
lesTopics[i] = lesTopic(e.BlockChain().Genesis().Hash(), pv)
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
quitSync := make(chan struct{})
|
2017-06-21 10:27:38 +00:00
|
|
|
srv := &LesServer{
|
2018-08-17 10:21:53 +00:00
|
|
|
lesCommons: lesCommons{
|
|
|
|
config: config,
|
2018-08-28 07:08:16 +00:00
|
|
|
iConfig: light.DefaultServerIndexerConfig,
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
chainDb: e.ChainDb(),
|
|
|
|
chtIndexer: light.NewChtIndexer(e.ChainDb(), nil, params.CHTFrequency, params.HelperTrieProcessConfirmations),
|
|
|
|
bloomTrieIndexer: light.NewBloomTrieIndexer(e.ChainDb(), nil, params.BloomBitsBlocks, params.BloomTrieFrequency),
|
2018-08-17 10:21:53 +00:00
|
|
|
},
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
archiveMode: e.ArchiveMode(),
|
2019-01-24 11:18:26 +00:00
|
|
|
quitSync: quitSync,
|
|
|
|
lesTopics: lesTopics,
|
2019-07-09 17:30:24 +00:00
|
|
|
onlyAnnounce: config.UltraLightOnlyAnnounce,
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
2019-07-03 18:23:06 +00:00
|
|
|
srv.costTracker, srv.minCapacity = newCostTracker(e.ChainDb(), config)
|
2018-08-17 10:21:53 +00:00
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
logger := log.New()
|
2019-02-26 11:32:48 +00:00
|
|
|
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 13:19:09 +00:00
|
|
|
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
checkpoint := srv.latestLocalCheckpoint()
|
|
|
|
if !checkpoint.Empty() {
|
|
|
|
logger.Info("Loaded latest checkpoint", "section", checkpoint.SectionIndex, "head", checkpoint.SectionHead,
|
|
|
|
"chtroot", checkpoint.CHTRoot, "bloomroot", checkpoint.BloomRoot)
|
2017-10-24 13:19:09 +00:00
|
|
|
}
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
|
|
|
|
srv.chtIndexer.Start(e.BlockChain())
|
|
|
|
|
|
|
|
oracle := config.CheckpointOracle
|
|
|
|
if oracle == nil {
|
|
|
|
oracle = params.CheckpointOracles[e.BlockChain().Genesis().Hash()]
|
2017-06-21 10:27:38 +00:00
|
|
|
}
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
registrar := newCheckpointOracle(oracle, srv.getLocalCheckpoint)
|
|
|
|
// TODO(rjl493456442) Checkpoint is useless for les server, separate handler for client and server.
|
2019-07-09 17:30:24 +00:00
|
|
|
pm, err := NewProtocolManager(e.BlockChain().Config(), nil, light.DefaultServerIndexerConfig, config.UltraLightServers, config.UltraLightFraction, false, config.NetworkId, e.EventMux(), newPeerSet(), e.BlockChain(), e.TxPool(), e.ChainDb(), nil, nil, registrar, quitSync, new(sync.WaitGroup), e.Synced)
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv.protocolManager = pm
|
2019-07-03 18:23:06 +00:00
|
|
|
pm.servingQueue = newServingQueue(int64(time.Millisecond*10), float64(config.LightServ)/100)
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
pm.server = srv
|
2017-10-24 13:19:09 +00:00
|
|
|
|
2019-02-26 11:32:48 +00:00
|
|
|
return srv, nil
|
|
|
|
}
|
2016-10-14 03:51:29 +00:00
|
|
|
|
2019-02-26 11:32:48 +00:00
|
|
|
func (s *LesServer) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
{
|
|
|
|
Namespace: "les",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: NewPrivateLightAPI(&s.lesCommons, s.protocolManager.reg),
|
|
|
|
Public: false,
|
|
|
|
},
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
2019-02-26 11:32:48 +00: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)
|
|
|
|
|
2019-07-03 18:23:06 +00:00
|
|
|
var (
|
|
|
|
processing, procLast bool
|
|
|
|
procStarted time.Time
|
|
|
|
)
|
2019-02-26 11:32:48 +00:00
|
|
|
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 {
|
2019-05-30 18:51:13 +00:00
|
|
|
if !procLast {
|
2019-07-03 18:23:06 +00:00
|
|
|
procStarted = time.Now()
|
2019-05-30 18:51:13 +00:00
|
|
|
}
|
2019-02-26 11:32:48 +00:00
|
|
|
s.protocolManager.servingQueue.setThreads(s.thcBlockProcessing)
|
|
|
|
s.fcManager.SetRechargeCurve(flowcontrol.PieceWiseLinear{{0, 0}, {totalRecharge, totalRecharge}})
|
|
|
|
} else {
|
2019-05-30 18:51:13 +00:00
|
|
|
if procLast {
|
2019-07-03 18:23:06 +00:00
|
|
|
blockProcessingTimer.UpdateSince(procStarted)
|
2019-05-30 18:51:13 +00:00
|
|
|
}
|
2019-02-26 11:32:48 +00:00
|
|
|
s.protocolManager.servingQueue.setThreads(s.thcNormal)
|
2019-05-30 18:51:13 +00:00
|
|
|
s.fcManager.SetRechargeCurve(flowcontrol.PieceWiseLinear{{0, 0}, {totalRecharge / 16, totalRecharge / 2}, {totalRecharge / 2, totalRecharge / 2}, {totalRecharge, totalRecharge}})
|
2019-02-26 11:32:48 +00:00
|
|
|
}
|
2019-05-30 18:51:13 +00:00
|
|
|
procLast = processing
|
2019-02-26 11:32:48 +00:00
|
|
|
}
|
|
|
|
updateRecharge()
|
|
|
|
totalCapacity := s.fcManager.SubscribeTotalCapacity(totalCapacityCh)
|
2019-08-03 12:36:10 +00:00
|
|
|
s.clientPool.setLimits(s.maxPeers, totalCapacity)
|
2019-02-26 11:32:48 +00:00
|
|
|
|
2019-05-30 18:51:13 +00:00
|
|
|
var maxFreePeers uint64
|
2019-02-26 11:32:48 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case processing = <-blockProcFeed:
|
|
|
|
updateRecharge()
|
|
|
|
case totalRecharge = <-totalRechargeCh:
|
|
|
|
updateRecharge()
|
|
|
|
case totalCapacity = <-totalCapacityCh:
|
2019-07-03 18:23:06 +00:00
|
|
|
totalCapacityGauge.Update(int64(totalCapacity))
|
2019-05-30 18:51:13 +00:00
|
|
|
newFreePeers := totalCapacity / s.freeClientCap
|
|
|
|
if newFreePeers < maxFreePeers && newFreePeers < uint64(s.maxPeers) {
|
|
|
|
log.Warn("Reduced total capacity", "maxFreePeers", newFreePeers)
|
|
|
|
}
|
|
|
|
maxFreePeers = newFreePeers
|
2019-08-03 12:36:10 +00:00
|
|
|
s.clientPool.setLimits(s.maxPeers, totalCapacity)
|
2019-02-26 11:32:48 +00:00
|
|
|
case <-s.protocolManager.quitSync:
|
|
|
|
s.protocolManager.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *LesServer) Protocols() []p2p.Protocol {
|
2018-08-17 10:21:53 +00:00
|
|
|
return s.makeProtocols(ServerProtocolVersions)
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 01:35:46 +00:00
|
|
|
// Start starts the LES server
|
2016-10-19 11:04:55 +00:00
|
|
|
func (s *LesServer) Start(srvr *p2p.Server) {
|
2019-02-26 11:32:48 +00:00
|
|
|
s.maxPeers = s.config.LightPeers
|
|
|
|
totalRecharge := s.costTracker.totalRecharge()
|
|
|
|
if s.maxPeers > 0 {
|
2019-05-30 18:51:13 +00:00
|
|
|
s.freeClientCap = s.minCapacity //totalRecharge / uint64(s.maxPeers)
|
|
|
|
if s.freeClientCap < s.minCapacity {
|
|
|
|
s.freeClientCap = s.minCapacity
|
2019-02-26 11:32:48 +00:00
|
|
|
}
|
|
|
|
if s.freeClientCap > 0 {
|
|
|
|
s.defParams = flowcontrol.ServerParams{
|
|
|
|
BufLimit: s.freeClientCap * bufLimitRatio,
|
|
|
|
MinRecharge: s.freeClientCap,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 12:36:10 +00:00
|
|
|
s.maxCapacity = s.freeClientCap * uint64(s.maxPeers)
|
|
|
|
if totalRecharge > s.maxCapacity {
|
|
|
|
s.maxCapacity = totalRecharge
|
2019-05-30 18:51:13 +00:00
|
|
|
}
|
2019-08-03 12:36:10 +00:00
|
|
|
s.fcManager.SetCapacityLimits(s.freeClientCap, s.maxCapacity, s.freeClientCap*2)
|
|
|
|
s.clientPool = newClientPool(s.chainDb, s.freeClientCap, 10000, mclock.System{}, func(id enode.ID) { go s.protocolManager.removePeer(peerIdToString(id)) })
|
|
|
|
s.clientPool.setPriceFactors(priceFactors{0, 1, 1}, priceFactors{0, 1, 1})
|
|
|
|
s.protocolManager.peers.notify(s.clientPool)
|
2019-02-26 11:32:48 +00:00
|
|
|
s.startEventLoop()
|
2018-02-05 13:41:53 +00:00
|
|
|
s.protocolManager.Start(s.config.LightPeers)
|
2018-02-10 12:33:52 +00: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 13:19:09 +00:00
|
|
|
}
|
|
|
|
s.privateKey = srvr.PrivateKey
|
|
|
|
s.protocolManager.blockLoop()
|
|
|
|
}
|
2017-06-21 10:27:38 +00:00
|
|
|
|
2017-10-24 13:19:09 +00:00
|
|
|
func (s *LesServer) SetBloomBitsIndexer(bloomIndexer *core.ChainIndexer) {
|
|
|
|
bloomIndexer.AddChildIndexer(s.bloomTrieIndexer)
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
all: on-chain oracle checkpoint syncing (#19543)
* all: implement simple checkpoint syncing
cmd, les, node: remove callback mechanism
cmd, node: remove callback definition
les: simplify the registrar
les: expose checkpoint rpc services in the light client
les, light: don't store untrusted receipt
cmd, contracts, les: discard stale checkpoint
cmd, contracts/registrar: loose restriction of registeration
cmd, contracts: add replay-protection
all: off-chain multi-signature contract
params: deploy checkpoint contract for rinkeby
cmd/registrar: add raw signing mode for registrar
cmd/registrar, contracts/registrar, les: fixed messages
* cmd/registrar, contracts/registrar: fix lints
* accounts/abi/bind, les: address comments
* cmd, contracts, les, light, params: minor checkpoint sync cleanups
* cmd, eth, les, light: move checkpoint config to config file
* cmd, eth, les, params: address comments
* eth, les, params: address comments
* cmd: polish up the checkpoint admin CLI
* cmd, contracts, params: deploy new version contract
* cmd/checkpoint-admin: add another flag for clef mode signing
* cmd, contracts, les: rename and regen checkpoint oracle with abigen
2019-06-28 07:34:02 +00:00
|
|
|
// SetClient sets the rpc client and starts running checkpoint contract if it is not yet watched.
|
|
|
|
func (s *LesServer) SetContractBackend(backend bind.ContractBackend) {
|
|
|
|
if s.protocolManager.reg != nil {
|
|
|
|
s.protocolManager.reg.start(backend)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 08:03:22 +00:00
|
|
|
// Stop stops the LES service
|
2016-10-14 03:51:29 +00:00
|
|
|
func (s *LesServer) Stop() {
|
2019-05-30 18:51:13 +00:00
|
|
|
s.fcManager.Stop()
|
2017-10-24 13:19:09 +00:00
|
|
|
s.chtIndexer.Close()
|
|
|
|
// bloom trie indexer is closed by parent bloombits indexer
|
2016-10-14 03:51:29 +00:00
|
|
|
go func() {
|
|
|
|
<-s.protocolManager.noMorePeers
|
|
|
|
}()
|
2019-08-03 12:36:10 +00:00
|
|
|
s.clientPool.stop()
|
2019-02-26 11:32:48 +00:00
|
|
|
s.costTracker.stop()
|
2016-10-14 03:51:29 +00:00
|
|
|
s.protocolManager.Stop()
|
|
|
|
}
|
|
|
|
|
2019-05-26 16:15:05 +00:00
|
|
|
// todo(rjl493456442) separate client and server implementation.
|
2016-10-14 03:51:29 +00:00
|
|
|
func (pm *ProtocolManager) blockLoop() {
|
|
|
|
pm.wg.Add(1)
|
2017-08-18 10:58:36 +00:00
|
|
|
headCh := make(chan core.ChainHeadEvent, 10)
|
|
|
|
headSub := pm.blockchain.SubscribeChainHeadEvent(headCh)
|
2016-10-14 03:51:29 +00:00
|
|
|
go func() {
|
|
|
|
var lastHead *types.Header
|
|
|
|
lastBroadcastTd := common.Big0
|
|
|
|
for {
|
|
|
|
select {
|
2017-08-18 10:58:36 +00:00
|
|
|
case ev := <-headCh:
|
2016-10-14 03:51:29 +00:00
|
|
|
peers := pm.peers.AllPeers()
|
|
|
|
if len(peers) > 0 {
|
2017-08-18 10:58:36 +00:00
|
|
|
header := ev.Block.Header()
|
2016-10-14 03:51:29 +00:00
|
|
|
hash := header.Hash()
|
2016-11-09 00:20:49 +00:00
|
|
|
number := header.Number.Uint64()
|
2018-05-07 11:35:06 +00:00
|
|
|
td := rawdb.ReadTd(pm.chainDb, hash, number)
|
2016-10-14 03:51:29 +00:00
|
|
|
if td != nil && td.Cmp(lastBroadcastTd) > 0 {
|
|
|
|
var reorg uint64
|
|
|
|
if lastHead != nil {
|
2018-05-07 11:35:06 +00:00
|
|
|
reorg = lastHead.Number.Uint64() - rawdb.FindCommonAncestor(pm.chainDb, header, lastHead).Number.Uint64()
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
lastHead = header
|
|
|
|
lastBroadcastTd = td
|
2016-11-13 11:34:50 +00:00
|
|
|
|
2017-03-03 09:41:52 +00:00
|
|
|
log.Debug("Announcing block to peers", "number", number, "hash", hash, "td", td, "reorg", reorg)
|
2016-11-13 11:34:50 +00:00
|
|
|
|
2016-10-14 03:51:29 +00:00
|
|
|
announce := announceData{Hash: hash, Number: number, Td: td, ReorgDepth: reorg}
|
2017-10-24 13:19:09 +00:00
|
|
|
var (
|
|
|
|
signed bool
|
|
|
|
signedAnnounce announceData
|
|
|
|
)
|
|
|
|
|
2016-10-14 03:51:29 +00:00
|
|
|
for _, p := range peers {
|
2019-03-25 07:17:55 +00:00
|
|
|
p := p
|
2017-10-24 13:19:09 +00:00
|
|
|
switch p.announceType {
|
|
|
|
case announceTypeSimple:
|
2019-02-26 11:32:48 +00:00
|
|
|
p.queueSend(func() { p.SendAnnounce(announce) })
|
2017-10-24 13:19:09 +00:00
|
|
|
case announceTypeSigned:
|
|
|
|
if !signed {
|
|
|
|
signedAnnounce = announce
|
|
|
|
signedAnnounce.sign(pm.server.privateKey)
|
|
|
|
signed = true
|
|
|
|
}
|
2019-02-26 11:32:48 +00:00
|
|
|
p.queueSend(func() { p.SendAnnounce(signedAnnounce) })
|
2016-10-14 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-pm.quitSync:
|
2017-08-18 10:58:36 +00:00
|
|
|
headSub.Unsubscribe()
|
2016-10-14 03:51:29 +00:00
|
|
|
pm.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|