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 implements the Light Ethereum Subprotocol.
|
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2016-11-17 17:54:24 +03:00
|
|
|
"net"
|
2016-10-14 06:51:29 +03:00
|
|
|
"sync"
|
2016-12-15 13:13:52 +03:00
|
|
|
"time"
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-04-05 01:16:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
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/state"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2017-10-24 16:19:09 +03:00
|
|
|
"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"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2016-10-19 14:04:55 +03:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2016-10-20 14:36:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
softResponseLimit = 2 * 1024 * 1024 // Target maximum size of returned blocks, headers or node data.
|
|
|
|
estHeaderRlpSize = 500 // Approximate size of an RLP encoded block header
|
|
|
|
|
|
|
|
ethVersion = 63 // equivalent eth version for the downloader
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
MaxHeaderFetch = 192 // Amount of block headers to be fetched per retrieval request
|
|
|
|
MaxBodyFetch = 32 // Amount of block bodies to be fetched per retrieval request
|
|
|
|
MaxReceiptFetch = 128 // Amount of transaction receipts to allow fetching per request
|
|
|
|
MaxCodeFetch = 64 // Amount of contract codes to allow fetching per request
|
|
|
|
MaxProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request
|
|
|
|
MaxHelperTrieProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request
|
|
|
|
MaxTxSend = 64 // Amount of transactions to be send per request
|
|
|
|
MaxTxStatus = 256 // Amount of transactions to queried per request
|
2016-10-14 06:51:29 +03:00
|
|
|
|
2016-11-30 08:02:08 +03:00
|
|
|
disableClientRemovePeer = false
|
2016-10-14 06:51:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// errIncompatibleConfig is returned if the requested protocols and configs are
|
|
|
|
// not compatible (low protocol version restrictions and high requirements).
|
|
|
|
var errIncompatibleConfig = errors.New("incompatible configuration")
|
|
|
|
|
|
|
|
func errResp(code errCode, format string, v ...interface{}) error {
|
|
|
|
return fmt.Errorf("%v - %v", code, fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
|
|
|
type BlockChain interface {
|
2017-12-28 16:18:34 +03:00
|
|
|
Config() *params.ChainConfig
|
2017-09-09 19:03:07 +03:00
|
|
|
HasHeader(hash common.Hash, number uint64) bool
|
2016-10-14 06:51:29 +03:00
|
|
|
GetHeader(hash common.Hash, number uint64) *types.Header
|
|
|
|
GetHeaderByHash(hash common.Hash) *types.Header
|
|
|
|
CurrentHeader() *types.Header
|
2018-01-30 19:39:32 +03:00
|
|
|
GetTd(hash common.Hash, number uint64) *big.Int
|
2018-02-05 19:40:32 +03:00
|
|
|
State() (*state.StateDB, error)
|
2016-10-14 06:51:29 +03:00
|
|
|
InsertHeaderChain(chain []*types.Header, checkFreq int) (int, error)
|
|
|
|
Rollback(chain []common.Hash)
|
|
|
|
GetHeaderByNumber(number uint64) *types.Header
|
|
|
|
GetBlockHashesFromHash(hash common.Hash, max uint64) []common.Hash
|
|
|
|
Genesis() *types.Block
|
2017-08-18 13:58:36 +03:00
|
|
|
SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type txPool interface {
|
2017-10-25 12:18:44 +03:00
|
|
|
AddRemotes(txs []*types.Transaction) []error
|
|
|
|
Status(hashes []common.Hash) []core.TxStatus
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProtocolManager struct {
|
|
|
|
lightSync bool
|
|
|
|
txpool txPool
|
|
|
|
txrelay *LesTxRelay
|
2017-04-25 14:31:15 +03:00
|
|
|
networkId uint64
|
2016-10-20 14:36:29 +03:00
|
|
|
chainConfig *params.ChainConfig
|
2016-10-14 06:51:29 +03:00
|
|
|
blockchain BlockChain
|
|
|
|
chainDb ethdb.Database
|
|
|
|
odr *LesOdr
|
|
|
|
server *LesServer
|
2016-11-17 17:54:24 +03:00
|
|
|
serverPool *serverPool
|
2017-06-21 13:27:38 +03:00
|
|
|
lesTopic discv5.Topic
|
2017-03-22 22:44:22 +03:00
|
|
|
reqDist *requestDistributor
|
2017-06-21 13:27:38 +03:00
|
|
|
retriever *retrieveManager
|
2016-10-19 14:04:55 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
downloader *downloader.Downloader
|
|
|
|
fetcher *lightFetcher
|
|
|
|
peers *peerSet
|
2018-02-05 16:41:53 +03:00
|
|
|
maxPeers int
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
SubProtocols []p2p.Protocol
|
|
|
|
|
|
|
|
eventMux *event.TypeMux
|
|
|
|
|
|
|
|
// channels for fetcher, syncer, txsyncLoop
|
|
|
|
newPeerCh chan *peer
|
|
|
|
quitSync chan struct{}
|
|
|
|
noMorePeers chan struct{}
|
|
|
|
|
|
|
|
// wait group is used for graceful shutdowns during downloading
|
|
|
|
// and processing
|
2017-06-21 13:27:38 +03:00
|
|
|
wg *sync.WaitGroup
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
|
|
|
// with the ethereum network.
|
2017-10-24 16:19:09 +03:00
|
|
|
func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, protocolVersions []uint, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, quitSync chan struct{}, wg *sync.WaitGroup) (*ProtocolManager, error) {
|
2016-10-14 06:51:29 +03:00
|
|
|
// Create the protocol manager with the base fields
|
|
|
|
manager := &ProtocolManager{
|
|
|
|
lightSync: lightSync,
|
|
|
|
eventMux: mux,
|
|
|
|
blockchain: blockchain,
|
|
|
|
chainConfig: chainConfig,
|
|
|
|
chainDb: chainDb,
|
2017-06-21 13:27:38 +03:00
|
|
|
odr: odr,
|
2016-10-14 06:51:29 +03:00
|
|
|
networkId: networkId,
|
|
|
|
txpool: txpool,
|
|
|
|
txrelay: txrelay,
|
2017-06-21 13:27:38 +03:00
|
|
|
peers: peers,
|
2016-10-14 06:51:29 +03:00
|
|
|
newPeerCh: make(chan *peer),
|
2017-06-21 13:27:38 +03:00
|
|
|
quitSync: quitSync,
|
|
|
|
wg: wg,
|
2016-10-14 06:51:29 +03:00
|
|
|
noMorePeers: make(chan struct{}),
|
|
|
|
}
|
2017-06-21 13:27:38 +03:00
|
|
|
if odr != nil {
|
|
|
|
manager.retriever = odr.retriever
|
|
|
|
manager.reqDist = odr.retriever.dist
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
// Initiate a sub-protocol for every implemented version we can handle
|
2017-10-24 16:19:09 +03:00
|
|
|
manager.SubProtocols = make([]p2p.Protocol, 0, len(protocolVersions))
|
|
|
|
for _, version := range protocolVersions {
|
2016-10-14 06:51:29 +03:00
|
|
|
// Compatible, initialize the sub-protocol
|
|
|
|
version := version // Closure for the run
|
|
|
|
manager.SubProtocols = append(manager.SubProtocols, p2p.Protocol{
|
|
|
|
Name: "les",
|
|
|
|
Version: version,
|
2017-10-24 16:19:09 +03:00
|
|
|
Length: ProtocolLengths[version],
|
2016-10-14 06:51:29 +03:00
|
|
|
Run: func(p *p2p.Peer, rw p2p.MsgReadWriter) error {
|
2016-11-17 17:54:24 +03:00
|
|
|
var entry *poolEntry
|
2016-10-14 06:51:29 +03:00
|
|
|
peer := manager.newPeer(int(version), networkId, p, rw)
|
2016-11-17 17:54:24 +03:00
|
|
|
if manager.serverPool != nil {
|
|
|
|
addr := p.RemoteAddr().(*net.TCPAddr)
|
2016-11-30 08:02:08 +03:00
|
|
|
entry = manager.serverPool.connect(peer, addr.IP, uint16(addr.Port))
|
2016-11-17 17:54:24 +03:00
|
|
|
}
|
|
|
|
peer.poolEntry = entry
|
2016-10-14 06:51:29 +03:00
|
|
|
select {
|
|
|
|
case manager.newPeerCh <- peer:
|
|
|
|
manager.wg.Add(1)
|
|
|
|
defer manager.wg.Done()
|
2016-11-17 17:54:24 +03:00
|
|
|
err := manager.handle(peer)
|
|
|
|
if entry != nil {
|
2016-11-30 08:02:08 +03:00
|
|
|
manager.serverPool.disconnect(entry)
|
2016-11-17 17:54:24 +03:00
|
|
|
}
|
|
|
|
return err
|
2016-10-14 06:51:29 +03:00
|
|
|
case <-manager.quitSync:
|
2016-11-17 17:54:24 +03:00
|
|
|
if entry != nil {
|
2016-11-30 08:02:08 +03:00
|
|
|
manager.serverPool.disconnect(entry)
|
2016-11-17 17:54:24 +03:00
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
return p2p.DiscQuitting
|
|
|
|
}
|
|
|
|
},
|
|
|
|
NodeInfo: func() interface{} {
|
|
|
|
return manager.NodeInfo()
|
|
|
|
},
|
|
|
|
PeerInfo: func(id discover.NodeID) interface{} {
|
|
|
|
if p := manager.peers.Peer(fmt.Sprintf("%x", id[:8])); p != nil {
|
|
|
|
return p.Info()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(manager.SubProtocols) == 0 {
|
|
|
|
return nil, errIncompatibleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
removePeer := manager.removePeer
|
|
|
|
if disableClientRemovePeer {
|
|
|
|
removePeer = func(id string) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if lightSync {
|
2017-06-27 18:15:29 +03:00
|
|
|
manager.downloader = downloader.New(downloader.LightSync, chainDb, manager.eventMux, nil, blockchain, removePeer)
|
2017-06-21 13:27:38 +03:00
|
|
|
manager.peers.notify((*downloaderPeerNotify)(manager))
|
|
|
|
manager.fetcher = newLightFetcher(manager)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return manager, nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 13:27:38 +03:00
|
|
|
// removePeer initiates disconnection from a peer by removing it from the peer set
|
2016-10-14 06:51:29 +03:00
|
|
|
func (pm *ProtocolManager) removePeer(id string) {
|
2017-06-21 13:27:38 +03:00
|
|
|
pm.peers.Unregister(id)
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
2018-02-05 16:41:53 +03:00
|
|
|
func (pm *ProtocolManager) Start(maxPeers int) {
|
|
|
|
pm.maxPeers = maxPeers
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
if pm.lightSync {
|
|
|
|
go pm.syncer()
|
|
|
|
} else {
|
|
|
|
go func() {
|
|
|
|
for range pm.newPeerCh {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pm *ProtocolManager) Stop() {
|
|
|
|
// Showing a log message. During download / process this could actually
|
|
|
|
// take between 5 to 10 seconds and therefor feedback is required.
|
2017-03-03 12:41:52 +03:00
|
|
|
log.Info("Stopping light Ethereum protocol")
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
// Quit the sync loop.
|
|
|
|
// After this send has completed, no new peers will be accepted.
|
|
|
|
pm.noMorePeers <- struct{}{}
|
|
|
|
|
|
|
|
close(pm.quitSync) // quits syncer, fetcher
|
|
|
|
|
|
|
|
// Disconnect existing sessions.
|
|
|
|
// This also closes the gate for any new registrations on the peer set.
|
|
|
|
// sessions which are already established but not added to pm.peers yet
|
|
|
|
// will exit when they try to register.
|
|
|
|
pm.peers.Close()
|
|
|
|
|
|
|
|
// Wait for any process action
|
|
|
|
pm.wg.Wait()
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
log.Info("Light Ethereum protocol stopped")
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
2017-04-25 14:31:15 +03:00
|
|
|
func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
|
2016-10-14 06:51:29 +03:00
|
|
|
return newPeer(pv, nv, p, newMeteredMsgWriter(rw))
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle is the callback invoked to manage the life cycle of a les peer. When
|
|
|
|
// this function terminates, the peer is disconnected.
|
|
|
|
func (pm *ProtocolManager) handle(p *peer) error {
|
2018-02-27 13:52:59 +03:00
|
|
|
// Ignore maxPeers if this is a trusted peer
|
|
|
|
if pm.peers.Len() >= pm.maxPeers && !p.Peer.Info().Network.Trusted {
|
2018-02-05 16:41:53 +03:00
|
|
|
return p2p.DiscTooManyPeers
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Debug("Light Ethereum peer connected", "name", p.Name())
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
// Execute the LES handshake
|
2018-01-30 19:39:32 +03:00
|
|
|
var (
|
|
|
|
genesis = pm.blockchain.Genesis()
|
|
|
|
head = pm.blockchain.CurrentHeader()
|
|
|
|
hash = head.Hash()
|
|
|
|
number = head.Number.Uint64()
|
|
|
|
td = pm.blockchain.GetTd(hash, number)
|
|
|
|
)
|
|
|
|
if err := p.Handshake(td, hash, number, genesis.Hash(), pm.server); err != nil {
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Debug("Light Ethereum handshake failed", "err", err)
|
2016-10-14 06:51:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
|
|
|
|
rw.Init(p.version)
|
|
|
|
}
|
|
|
|
// Register the peer locally
|
|
|
|
if err := pm.peers.Register(p); err != nil {
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Error("Light Ethereum peer registration failed", "err", err)
|
2016-10-14 06:51:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if pm.server != nil && pm.server.fcManager != nil && p.fcClient != nil {
|
|
|
|
p.fcClient.Remove(pm.server.fcManager)
|
|
|
|
}
|
|
|
|
pm.removePeer(p.id)
|
|
|
|
}()
|
|
|
|
// Register the peer in the downloader. If the downloader considers it banned, we disconnect
|
|
|
|
if pm.lightSync {
|
2016-11-30 08:02:08 +03:00
|
|
|
p.lock.Lock()
|
|
|
|
head := p.headInfo
|
|
|
|
p.lock.Unlock()
|
|
|
|
if pm.fetcher != nil {
|
|
|
|
pm.fetcher.announce(p, head)
|
|
|
|
}
|
2016-11-17 17:54:24 +03:00
|
|
|
|
|
|
|
if p.poolEntry != nil {
|
|
|
|
pm.serverPool.registered(p.poolEntry)
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
defer close(stop)
|
|
|
|
go func() {
|
|
|
|
// new block announce loop
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case announce := <-p.announceChn:
|
|
|
|
p.SendAnnounce(announce)
|
|
|
|
case <-stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// main loop. handle incoming messages.
|
|
|
|
for {
|
|
|
|
if err := pm.handleMsg(p); err != nil {
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Debug("Light Ethereum message handling failed", "err", err)
|
2016-10-14 06:51:29 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetHelperTrieProofsMsg}
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
// handleMsg is invoked whenever an inbound message is received from a remote
|
|
|
|
// peer. The remote connection is torn down upon returning any error.
|
|
|
|
func (pm *ProtocolManager) handleMsg(p *peer) error {
|
|
|
|
// Read the next message from the remote peer, and ensure it's fully consumed
|
|
|
|
msg, err := p.rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Light Ethereum message arrived", "code", msg.Code, "bytes", msg.Size)
|
2016-12-15 13:13:52 +03:00
|
|
|
|
|
|
|
costs := p.fcCosts[msg.Code]
|
|
|
|
reject := func(reqCnt, maxCnt uint64) bool {
|
|
|
|
if p.fcClient == nil || reqCnt > maxCnt {
|
|
|
|
return true
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
2016-12-15 13:13:52 +03:00
|
|
|
bufValue, _ := p.fcClient.AcceptRequest()
|
|
|
|
cost := costs.baseCost + reqCnt*costs.reqCost
|
|
|
|
if cost > pm.server.defParams.BufLimit {
|
|
|
|
cost = pm.server.defParams.BufLimit
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
2016-12-15 13:13:52 +03:00
|
|
|
if cost > bufValue {
|
2017-03-03 12:41:52 +03:00
|
|
|
recharge := time.Duration((cost - bufValue) * 1000000 / pm.server.defParams.MinRecharge)
|
|
|
|
p.Log().Error("Request came too early", "recharge", common.PrettyDuration(recharge))
|
2016-12-15 13:13:52 +03:00
|
|
|
return true
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
2016-12-15 13:13:52 +03:00
|
|
|
return false
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Size > ProtocolMaxMsgSize {
|
|
|
|
return errResp(ErrMsgTooLarge, "%v > %v", msg.Size, ProtocolMaxMsgSize)
|
|
|
|
}
|
|
|
|
defer msg.Discard()
|
|
|
|
|
|
|
|
var deliverMsg *Msg
|
|
|
|
|
|
|
|
// Handle the message depending on its contents
|
|
|
|
switch msg.Code {
|
|
|
|
case StatusMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received status message")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Status messages should never arrive after the handshake
|
|
|
|
return errResp(ErrExtraStatusMsg, "uncontrolled status message")
|
|
|
|
|
|
|
|
// Block header query, collect the requested headers and reply
|
|
|
|
case AnnounceMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received announce message")
|
2017-10-24 16:19:09 +03:00
|
|
|
if p.requestAnnounceType == announceTypeNone {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
2016-11-13 14:34:50 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
var req announceData
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "%v: %v", msg, err)
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
|
|
|
|
if p.requestAnnounceType == announceTypeSigned {
|
|
|
|
if err := req.checkSignature(p.pubKey); err != nil {
|
|
|
|
p.Log().Trace("Invalid announcement signature", "err", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.Log().Trace("Valid announcement signature")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Announce message content", "number", req.Number, "hash", req.Hash, "td", req.Td, "reorg", req.ReorgDepth)
|
2016-11-30 08:02:08 +03:00
|
|
|
if pm.fetcher != nil {
|
2016-12-15 13:13:52 +03:00
|
|
|
pm.fetcher.announce(p, &req)
|
2016-11-30 08:02:08 +03:00
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
case GetBlockHeadersMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received block header request")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Decode the complex header query
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Query getBlockHeadersData
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "%v: %v", msg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := req.Query
|
2016-12-15 13:13:52 +03:00
|
|
|
if reject(query.Amount, MaxHeaderFetch) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
hashMode := query.Origin.Hash != (common.Hash{})
|
|
|
|
|
|
|
|
// Gather headers until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
bytes common.StorageSize
|
|
|
|
headers []*types.Header
|
|
|
|
unknown bool
|
|
|
|
)
|
|
|
|
for !unknown && len(headers) < int(query.Amount) && bytes < softResponseLimit {
|
|
|
|
// Retrieve the next header satisfying the query
|
|
|
|
var origin *types.Header
|
|
|
|
if hashMode {
|
|
|
|
origin = pm.blockchain.GetHeaderByHash(query.Origin.Hash)
|
|
|
|
} else {
|
|
|
|
origin = pm.blockchain.GetHeaderByNumber(query.Origin.Number)
|
|
|
|
}
|
|
|
|
if origin == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
number := origin.Number.Uint64()
|
|
|
|
headers = append(headers, origin)
|
|
|
|
bytes += estHeaderRlpSize
|
|
|
|
|
|
|
|
// Advance to the next header of the query
|
|
|
|
switch {
|
|
|
|
case query.Origin.Hash != (common.Hash{}) && query.Reverse:
|
|
|
|
// Hash based traversal towards the genesis block
|
|
|
|
for i := 0; i < int(query.Skip)+1; i++ {
|
|
|
|
if header := pm.blockchain.GetHeader(query.Origin.Hash, number); header != nil {
|
|
|
|
query.Origin.Hash = header.ParentHash
|
|
|
|
number--
|
|
|
|
} else {
|
|
|
|
unknown = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case query.Origin.Hash != (common.Hash{}) && !query.Reverse:
|
|
|
|
// Hash based traversal towards the leaf block
|
|
|
|
if header := pm.blockchain.GetHeaderByNumber(origin.Number.Uint64() + query.Skip + 1); header != nil {
|
|
|
|
if pm.blockchain.GetBlockHashesFromHash(header.Hash(), query.Skip+1)[query.Skip] == query.Origin.Hash {
|
|
|
|
query.Origin.Hash = header.Hash()
|
|
|
|
} else {
|
|
|
|
unknown = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unknown = true
|
|
|
|
}
|
|
|
|
case query.Reverse:
|
|
|
|
// Number based traversal towards the genesis block
|
|
|
|
if query.Origin.Number >= query.Skip+1 {
|
2018-01-03 15:14:47 +03:00
|
|
|
query.Origin.Number -= query.Skip + 1
|
2016-10-14 06:51:29 +03:00
|
|
|
} else {
|
|
|
|
unknown = true
|
|
|
|
}
|
|
|
|
|
|
|
|
case !query.Reverse:
|
|
|
|
// Number based traversal towards the leaf block
|
2018-01-03 15:14:47 +03:00
|
|
|
query.Origin.Number += query.Skip + 1
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + query.Amount*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, query.Amount, rcost)
|
|
|
|
return p.SendBlockHeaders(req.ReqID, bv, headers)
|
|
|
|
|
|
|
|
case BlockHeadersMsg:
|
|
|
|
if pm.downloader == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received block header response message")
|
2016-10-14 06:51:29 +03:00
|
|
|
// A batch of headers arrived to one of our previous requests
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Headers []*types.Header
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
2016-11-30 08:02:08 +03:00
|
|
|
if pm.fetcher != nil && pm.fetcher.requestedID(resp.ReqID) {
|
2016-11-30 01:55:35 +03:00
|
|
|
pm.fetcher.deliverHeaders(p, resp.ReqID, resp.Headers)
|
2016-10-14 06:51:29 +03:00
|
|
|
} else {
|
|
|
|
err := pm.downloader.DeliverHeaders(p.id, resp.Headers)
|
|
|
|
if err != nil {
|
2017-02-22 15:10:07 +03:00
|
|
|
log.Debug(fmt.Sprint(err))
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case GetBlockBodiesMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received block bodies request")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Hashes []common.Hash
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather blocks until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
bytes int
|
|
|
|
bodies []rlp.RawValue
|
|
|
|
)
|
2016-12-15 13:13:52 +03:00
|
|
|
reqCnt := len(req.Hashes)
|
|
|
|
if reject(uint64(reqCnt), MaxBodyFetch) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
for _, hash := range req.Hashes {
|
|
|
|
if bytes >= softResponseLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Retrieve the requested block body, stopping if enough was found
|
2018-05-07 14:35:06 +03:00
|
|
|
if number := rawdb.ReadHeaderNumber(pm.chainDb, hash); number != nil {
|
|
|
|
if data := rawdb.ReadBodyRLP(pm.chainDb, hash, *number); len(data) != 0 {
|
|
|
|
bodies = append(bodies, data)
|
|
|
|
bytes += len(data)
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
|
|
|
return p.SendBlockBodiesRLP(req.ReqID, bv, bodies)
|
|
|
|
|
|
|
|
case BlockBodiesMsg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received block bodies response")
|
2016-10-14 06:51:29 +03:00
|
|
|
// A batch of block bodies arrived to one of our previous requests
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Data []*types.Body
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
|
|
|
MsgType: MsgBlockBodies,
|
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
case GetCodeMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received code request")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Reqs []CodeReq
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather state data until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
bytes int
|
|
|
|
data [][]byte
|
|
|
|
)
|
2016-12-15 13:13:52 +03:00
|
|
|
reqCnt := len(req.Reqs)
|
|
|
|
if reject(uint64(reqCnt), MaxCodeFetch) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
for _, req := range req.Reqs {
|
|
|
|
// Retrieve the requested state entry, stopping if enough was found
|
2018-05-07 14:35:06 +03:00
|
|
|
if number := rawdb.ReadHeaderNumber(pm.chainDb, req.BHash); number != nil {
|
|
|
|
if header := rawdb.ReadHeader(pm.chainDb, req.BHash, *number); header != nil {
|
|
|
|
statedb, err := pm.blockchain.State()
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
account, err := pm.getAccount(statedb, header.Root, common.BytesToHash(req.AccKey))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
code, _ := statedb.Database().TrieDB().Node(common.BytesToHash(account.CodeHash))
|
2018-02-05 19:40:32 +03:00
|
|
|
|
2018-05-07 14:35:06 +03:00
|
|
|
data = append(data, code)
|
|
|
|
if bytes += len(code); bytes >= softResponseLimit {
|
|
|
|
break
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
|
|
|
return p.SendCode(req.ReqID, bv, data)
|
|
|
|
|
|
|
|
case CodeMsg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received code response")
|
2016-10-14 06:51:29 +03:00
|
|
|
// A batch of node state data arrived to one of our previous requests
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Data [][]byte
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
|
|
|
MsgType: MsgCode,
|
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
case GetReceiptsMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received receipts request")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Hashes []common.Hash
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather state data until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
bytes int
|
|
|
|
receipts []rlp.RawValue
|
|
|
|
)
|
2016-12-15 13:13:52 +03:00
|
|
|
reqCnt := len(req.Hashes)
|
|
|
|
if reject(uint64(reqCnt), MaxReceiptFetch) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
for _, hash := range req.Hashes {
|
|
|
|
if bytes >= softResponseLimit {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Retrieve the requested block's receipts, skipping if unknown to us
|
2018-05-07 14:35:06 +03:00
|
|
|
var results types.Receipts
|
|
|
|
if number := rawdb.ReadHeaderNumber(pm.chainDb, hash); number != nil {
|
|
|
|
results = rawdb.ReadReceipts(pm.chainDb, hash, *number)
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
if results == nil {
|
|
|
|
if header := pm.blockchain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If known, encode and queue for response packet
|
|
|
|
if encoded, err := rlp.EncodeToBytes(results); err != nil {
|
2017-03-03 12:41:52 +03:00
|
|
|
log.Error("Failed to encode receipt", "err", err)
|
2016-10-14 06:51:29 +03:00
|
|
|
} else {
|
|
|
|
receipts = append(receipts, encoded)
|
|
|
|
bytes += len(encoded)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
|
|
|
return p.SendReceiptsRLP(req.ReqID, bv, receipts)
|
|
|
|
|
|
|
|
case ReceiptsMsg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received receipts response")
|
2016-10-14 06:51:29 +03:00
|
|
|
// A batch of receipts arrived to one of our previous requests
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Receipts []types.Receipts
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
|
|
|
MsgType: MsgReceipts,
|
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Receipts,
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
case GetProofsV1Msg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received proofs request")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Reqs []ProofReq
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather state data until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
bytes int
|
|
|
|
proofs proofsData
|
|
|
|
)
|
2016-12-15 13:13:52 +03:00
|
|
|
reqCnt := len(req.Reqs)
|
|
|
|
if reject(uint64(reqCnt), MaxProofsFetch) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
for _, req := range req.Reqs {
|
|
|
|
// Retrieve the requested state entry, stopping if enough was found
|
2018-05-07 14:35:06 +03:00
|
|
|
if number := rawdb.ReadHeaderNumber(pm.chainDb, req.BHash); number != nil {
|
|
|
|
if header := rawdb.ReadHeader(pm.chainDb, req.BHash, *number); header != nil {
|
|
|
|
statedb, err := pm.blockchain.State()
|
2018-02-05 19:40:32 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
2018-05-07 14:35:06 +03:00
|
|
|
var trie state.Trie
|
|
|
|
if len(req.AccKey) > 0 {
|
|
|
|
account, err := pm.getAccount(statedb, header.Root, common.BytesToHash(req.AccKey))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
trie, _ = statedb.Database().OpenStorageTrie(common.BytesToHash(req.AccKey), account.Root)
|
|
|
|
} else {
|
|
|
|
trie, _ = statedb.Database().OpenTrie(header.Root)
|
|
|
|
}
|
|
|
|
if trie != nil {
|
|
|
|
var proof light.NodeList
|
|
|
|
trie.Prove(req.Key, 0, &proof)
|
|
|
|
|
|
|
|
proofs = append(proofs, proof)
|
|
|
|
if bytes += proof.DataSize(); bytes >= softResponseLimit {
|
|
|
|
break
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
|
|
|
return p.SendProofs(req.ReqID, bv, proofs)
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
case GetProofsV2Msg:
|
|
|
|
p.Log().Trace("Received les/2 proofs request")
|
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Reqs []ProofReq
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather state data until the fetch or network limits is reached
|
|
|
|
var (
|
2018-02-05 19:40:32 +03:00
|
|
|
lastBHash common.Hash
|
|
|
|
statedb *state.StateDB
|
|
|
|
root common.Hash
|
2017-10-24 16:19:09 +03:00
|
|
|
)
|
|
|
|
reqCnt := len(req.Reqs)
|
|
|
|
if reject(uint64(reqCnt), MaxProofsFetch) {
|
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := light.NewNodeSet()
|
|
|
|
|
|
|
|
for _, req := range req.Reqs {
|
2018-02-05 19:40:32 +03:00
|
|
|
// Look up the state belonging to the request
|
|
|
|
if statedb == nil || req.BHash != lastBHash {
|
|
|
|
statedb, root, lastBHash = nil, common.Hash{}, req.BHash
|
|
|
|
|
2018-05-07 14:35:06 +03:00
|
|
|
if number := rawdb.ReadHeaderNumber(pm.chainDb, req.BHash); number != nil {
|
|
|
|
if header := rawdb.ReadHeader(pm.chainDb, req.BHash, *number); header != nil {
|
|
|
|
statedb, _ = pm.blockchain.State()
|
|
|
|
root = header.Root
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-05 19:40:32 +03:00
|
|
|
if statedb == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Pull the account or storage trie of the request
|
|
|
|
var trie state.Trie
|
|
|
|
if len(req.AccKey) > 0 {
|
|
|
|
account, err := pm.getAccount(statedb, root, common.BytesToHash(req.AccKey))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
2018-02-05 19:40:32 +03:00
|
|
|
trie, _ = statedb.Database().OpenStorageTrie(common.BytesToHash(req.AccKey), account.Root)
|
|
|
|
} else {
|
|
|
|
trie, _ = statedb.Database().OpenTrie(root)
|
|
|
|
}
|
|
|
|
if trie == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Prove the user's request from the account or stroage trie
|
|
|
|
trie.Prove(req.Key, req.FromLevel, nodes)
|
|
|
|
if nodes.DataSize() >= softResponseLimit {
|
|
|
|
break
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
2018-02-11 15:57:46 +03:00
|
|
|
return p.SendProofsV2(req.ReqID, bv, nodes.NodeList())
|
2017-10-24 16:19:09 +03:00
|
|
|
|
|
|
|
case ProofsV1Msg:
|
2016-10-14 06:51:29 +03:00
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received proofs response")
|
2016-10-14 06:51:29 +03:00
|
|
|
// A batch of merkle proofs arrived to one of our previous requests
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
2017-10-24 16:19:09 +03:00
|
|
|
Data []light.NodeList
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
2017-10-24 16:19:09 +03:00
|
|
|
MsgType: MsgProofsV1,
|
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
case ProofsV2Msg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Log().Trace("Received les/2 proofs response")
|
|
|
|
// A batch of merkle proofs arrived to one of our previous requests
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Data light.NodeList
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
|
|
|
MsgType: MsgProofsV2,
|
2016-10-14 06:51:29 +03:00
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Data,
|
|
|
|
}
|
|
|
|
|
|
|
|
case GetHeaderProofsMsg:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received headers proof request")
|
2016-10-14 06:51:29 +03:00
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Reqs []ChtReq
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather state data until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
bytes int
|
|
|
|
proofs []ChtResp
|
|
|
|
)
|
2016-12-15 13:13:52 +03:00
|
|
|
reqCnt := len(req.Reqs)
|
2017-10-24 16:19:09 +03:00
|
|
|
if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
2018-02-11 15:57:46 +03:00
|
|
|
trieDb := trie.NewDatabase(ethdb.NewTable(pm.chainDb, light.ChtTablePrefix))
|
2016-10-14 06:51:29 +03:00
|
|
|
for _, req := range req.Reqs {
|
|
|
|
if header := pm.blockchain.GetHeaderByNumber(req.BlockNum); header != nil {
|
2018-05-07 14:35:06 +03:00
|
|
|
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, req.ChtNum*light.CHTFrequencyServer-1)
|
2018-01-09 13:41:59 +03:00
|
|
|
if root := light.GetChtRoot(pm.chainDb, req.ChtNum-1, sectionHead); root != (common.Hash{}) {
|
2018-02-11 15:57:46 +03:00
|
|
|
trie, err := trie.New(root, trieDb)
|
2018-02-05 19:40:32 +03:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var encNumber [8]byte
|
|
|
|
binary.BigEndian.PutUint64(encNumber[:], req.BlockNum)
|
|
|
|
|
|
|
|
var proof light.NodeList
|
|
|
|
trie.Prove(encNumber[:], 0, &proof)
|
|
|
|
|
|
|
|
proofs = append(proofs, ChtResp{Header: header, Proof: proof})
|
|
|
|
if bytes += proof.DataSize() + estHeaderRlpSize; bytes >= softResponseLimit {
|
|
|
|
break
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
|
|
|
return p.SendHeaderProofs(req.ReqID, bv, proofs)
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
case GetHelperTrieProofsMsg:
|
|
|
|
p.Log().Trace("Received helper trie proof request")
|
|
|
|
// Decode the retrieval message
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Reqs []HelperTrieReq
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
// Gather state data until the fetch or network limits is reached
|
|
|
|
var (
|
|
|
|
auxBytes int
|
|
|
|
auxData [][]byte
|
|
|
|
)
|
|
|
|
reqCnt := len(req.Reqs)
|
|
|
|
if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) {
|
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
lastIdx uint64
|
|
|
|
lastType uint
|
|
|
|
root common.Hash
|
2018-02-11 15:57:46 +03:00
|
|
|
auxTrie *trie.Trie
|
2017-10-24 16:19:09 +03:00
|
|
|
)
|
|
|
|
nodes := light.NewNodeSet()
|
|
|
|
for _, req := range req.Reqs {
|
2018-02-11 15:57:46 +03:00
|
|
|
if auxTrie == nil || req.Type != lastType || req.TrieIdx != lastIdx {
|
|
|
|
auxTrie, lastType, lastIdx = nil, req.Type, req.TrieIdx
|
2018-02-05 19:40:32 +03:00
|
|
|
|
2018-02-11 15:57:46 +03:00
|
|
|
var prefix string
|
|
|
|
if root, prefix = pm.getHelperTrie(req.Type, req.TrieIdx); root != (common.Hash{}) {
|
|
|
|
auxTrie, _ = trie.New(root, trie.NewDatabase(ethdb.NewTable(pm.chainDb, prefix)))
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if req.AuxReq == auxRoot {
|
|
|
|
var data []byte
|
|
|
|
if root != (common.Hash{}) {
|
|
|
|
data = root[:]
|
|
|
|
}
|
|
|
|
auxData = append(auxData, data)
|
|
|
|
auxBytes += len(data)
|
|
|
|
} else {
|
2018-02-11 15:57:46 +03:00
|
|
|
if auxTrie != nil {
|
|
|
|
auxTrie.Prove(req.Key, req.FromLevel, nodes)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
if req.AuxReq != 0 {
|
|
|
|
data := pm.getHelperTrieAuxData(req)
|
|
|
|
auxData = append(auxData, data)
|
|
|
|
auxBytes += len(data)
|
|
|
|
}
|
|
|
|
}
|
2018-02-05 19:40:32 +03:00
|
|
|
if nodes.DataSize()+auxBytes >= softResponseLimit {
|
|
|
|
break
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
2018-02-11 15:57:46 +03:00
|
|
|
return p.SendHelperTrieProofs(req.ReqID, bv, HelperTrieResps{Proofs: nodes.NodeList(), AuxData: auxData})
|
2017-10-24 16:19:09 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
case HeaderProofsMsg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received headers proof response")
|
2016-10-14 06:51:29 +03:00
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Data []ChtResp
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
|
|
|
MsgType: MsgHeaderProofs,
|
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Data,
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
case HelperTrieProofsMsg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Log().Trace("Received helper trie proof response")
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
|
|
|
Data HelperTrieResps
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
deliverMsg = &Msg{
|
|
|
|
MsgType: MsgHelperTrieProofs,
|
|
|
|
ReqID: resp.ReqID,
|
|
|
|
Obj: resp.Data,
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
case SendTxMsg:
|
|
|
|
if pm.txpool == nil {
|
2017-10-24 16:19:09 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
// Transactions arrived, parse all of them and deliver to the pool
|
|
|
|
var txs []*types.Transaction
|
|
|
|
if err := msg.Decode(&txs); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
2016-12-15 13:13:52 +03:00
|
|
|
reqCnt := len(txs)
|
|
|
|
if reject(uint64(reqCnt), MaxTxSend) {
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
2017-10-25 12:18:44 +03:00
|
|
|
pm.txpool.AddRemotes(txs)
|
2016-12-11 01:54:58 +03:00
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
_, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
case SendTxV2Msg:
|
|
|
|
if pm.txpool == nil {
|
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
// Transactions arrived, parse all of them and deliver to the pool
|
|
|
|
var req struct {
|
|
|
|
ReqID uint64
|
|
|
|
Txs []*types.Transaction
|
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
reqCnt := len(req.Txs)
|
|
|
|
if reject(uint64(reqCnt), MaxTxSend) {
|
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:18:44 +03:00
|
|
|
hashes := make([]common.Hash, len(req.Txs))
|
2017-10-24 16:19:09 +03:00
|
|
|
for i, tx := range req.Txs {
|
2017-10-25 12:18:44 +03:00
|
|
|
hashes[i] = tx.Hash()
|
|
|
|
}
|
|
|
|
stats := pm.txStatus(hashes)
|
|
|
|
for i, stat := range stats {
|
|
|
|
if stat.Status == core.TxStatusUnknown {
|
|
|
|
if errs := pm.txpool.AddRemotes([]*types.Transaction{req.Txs[i]}); errs[0] != nil {
|
2018-01-26 20:30:45 +03:00
|
|
|
stats[i].Error = errs[0].Error()
|
2017-10-25 12:18:44 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stats[i] = pm.txStatus([]common.Hash{hashes[i]})[0]
|
|
|
|
}
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
2017-10-25 12:18:44 +03:00
|
|
|
|
|
|
|
return p.SendTxStatus(req.ReqID, bv, stats)
|
2017-10-24 16:19:09 +03:00
|
|
|
|
|
|
|
case GetTxStatusMsg:
|
|
|
|
if pm.txpool == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
// Transactions arrived, parse all of them and deliver to the pool
|
|
|
|
var req struct {
|
2017-10-25 12:18:44 +03:00
|
|
|
ReqID uint64
|
|
|
|
Hashes []common.Hash
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
if err := msg.Decode(&req); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
2017-10-25 12:18:44 +03:00
|
|
|
reqCnt := len(req.Hashes)
|
2017-10-24 16:19:09 +03:00
|
|
|
if reject(uint64(reqCnt), MaxTxStatus) {
|
|
|
|
return errResp(ErrRequestRejected, "")
|
|
|
|
}
|
|
|
|
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
|
|
|
|
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
|
2017-10-25 12:18:44 +03:00
|
|
|
|
|
|
|
return p.SendTxStatus(req.ReqID, bv, pm.txStatus(req.Hashes))
|
2017-10-24 16:19:09 +03:00
|
|
|
|
|
|
|
case TxStatusMsg:
|
|
|
|
if pm.odr == nil {
|
|
|
|
return errResp(ErrUnexpectedResponse, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Log().Trace("Received tx status response")
|
|
|
|
var resp struct {
|
|
|
|
ReqID, BV uint64
|
2018-01-26 20:30:45 +03:00
|
|
|
Status []txStatus
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
if err := msg.Decode(&resp); err != nil {
|
|
|
|
return errResp(ErrDecode, "msg %v: %v", msg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.fcServer.GotReply(resp.ReqID, resp.BV)
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
default:
|
2017-03-03 12:41:52 +03:00
|
|
|
p.Log().Trace("Received unknown message", "code", msg.Code)
|
2016-10-14 06:51:29 +03:00
|
|
|
return errResp(ErrInvalidMsgCode, "%v", msg.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if deliverMsg != nil {
|
2017-06-21 13:27:38 +03:00
|
|
|
err := pm.retriever.deliver(p, deliverMsg)
|
2017-03-22 22:44:22 +03:00
|
|
|
if err != nil {
|
|
|
|
p.responseErrors++
|
|
|
|
if p.responseErrors > maxResponseErrors {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// getAccount retrieves an account from the state based at root.
|
|
|
|
func (pm *ProtocolManager) getAccount(statedb *state.StateDB, root, hash common.Hash) (state.Account, error) {
|
|
|
|
trie, err := trie.New(root, statedb.Database().TrieDB())
|
|
|
|
if err != nil {
|
|
|
|
return state.Account{}, err
|
|
|
|
}
|
|
|
|
blob, err := trie.TryGet(hash[:])
|
|
|
|
if err != nil {
|
|
|
|
return state.Account{}, err
|
|
|
|
}
|
|
|
|
var account state.Account
|
|
|
|
if err = rlp.DecodeBytes(blob, &account); err != nil {
|
|
|
|
return state.Account{}, err
|
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:19:09 +03:00
|
|
|
// getHelperTrie returns the post-processed trie root for the given trie ID and section index
|
|
|
|
func (pm *ProtocolManager) getHelperTrie(id uint, idx uint64) (common.Hash, string) {
|
|
|
|
switch id {
|
|
|
|
case htCanonical:
|
2018-05-07 14:35:06 +03:00
|
|
|
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*light.CHTFrequencyClient-1)
|
2017-10-24 16:19:09 +03:00
|
|
|
return light.GetChtV2Root(pm.chainDb, idx, sectionHead), light.ChtTablePrefix
|
|
|
|
case htBloomBits:
|
2018-05-07 14:35:06 +03:00
|
|
|
sectionHead := rawdb.ReadCanonicalHash(pm.chainDb, (idx+1)*light.BloomTrieFrequency-1)
|
2017-10-24 16:19:09 +03:00
|
|
|
return light.GetBloomTrieRoot(pm.chainDb, idx, sectionHead), light.BloomTrieTablePrefix
|
|
|
|
}
|
|
|
|
return common.Hash{}, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// getHelperTrieAuxData returns requested auxiliary data for the given HelperTrie request
|
|
|
|
func (pm *ProtocolManager) getHelperTrieAuxData(req HelperTrieReq) []byte {
|
2018-02-11 15:57:46 +03:00
|
|
|
switch {
|
|
|
|
case req.Type == htCanonical && req.AuxReq == auxHeader && len(req.Key) == 8:
|
2017-10-24 16:19:09 +03:00
|
|
|
blockNum := binary.BigEndian.Uint64(req.Key)
|
2018-05-07 14:35:06 +03:00
|
|
|
hash := rawdb.ReadCanonicalHash(pm.chainDb, blockNum)
|
|
|
|
return rawdb.ReadHeaderRLP(pm.chainDb, hash, blockNum)
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-25 12:18:44 +03:00
|
|
|
func (pm *ProtocolManager) txStatus(hashes []common.Hash) []txStatus {
|
|
|
|
stats := make([]txStatus, len(hashes))
|
|
|
|
for i, stat := range pm.txpool.Status(hashes) {
|
|
|
|
// Save the status we've got from the transaction pool
|
|
|
|
stats[i].Status = stat
|
|
|
|
|
|
|
|
// If the transaction is unknown to the pool, try looking it up locally
|
|
|
|
if stat == core.TxStatusUnknown {
|
2018-05-07 14:35:06 +03:00
|
|
|
if block, number, index := rawdb.ReadTxLookupEntry(pm.chainDb, hashes[i]); block != (common.Hash{}) {
|
2017-10-25 12:18:44 +03:00
|
|
|
stats[i].Status = core.TxStatusIncluded
|
2018-05-07 14:35:06 +03:00
|
|
|
stats[i].Lookup = &rawdb.TxLookupEntry{BlockHash: block, BlockIndex: number, Index: index}
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-25 12:18:44 +03:00
|
|
|
return stats
|
2017-10-24 16:19:09 +03:00
|
|
|
}
|
|
|
|
|
2017-12-28 16:18:34 +03:00
|
|
|
// NodeInfo represents a short summary of the Ethereum sub-protocol metadata
|
|
|
|
// known about the host peer.
|
|
|
|
type NodeInfo struct {
|
|
|
|
Network uint64 `json:"network"` // Ethereum network ID (1=Frontier, 2=Morden, Ropsten=3, Rinkeby=4)
|
|
|
|
Difficulty *big.Int `json:"difficulty"` // Total difficulty of the host's blockchain
|
|
|
|
Genesis common.Hash `json:"genesis"` // SHA3 hash of the host's genesis block
|
|
|
|
Config *params.ChainConfig `json:"config"` // Chain configuration for the fork rules
|
|
|
|
Head common.Hash `json:"head"` // SHA3 hash of the host's best owned block
|
|
|
|
}
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
// NodeInfo retrieves some protocol metadata about the running host node.
|
2017-12-28 16:18:34 +03:00
|
|
|
func (self *ProtocolManager) NodeInfo() *NodeInfo {
|
2018-01-30 19:39:32 +03:00
|
|
|
head := self.blockchain.CurrentHeader()
|
|
|
|
hash := head.Hash()
|
|
|
|
|
2017-12-28 16:18:34 +03:00
|
|
|
return &NodeInfo{
|
2016-10-14 06:51:29 +03:00
|
|
|
Network: self.networkId,
|
2018-01-30 19:39:32 +03:00
|
|
|
Difficulty: self.blockchain.GetTd(hash, head.Number.Uint64()),
|
2016-10-14 06:51:29 +03:00
|
|
|
Genesis: self.blockchain.Genesis().Hash(),
|
2017-12-28 16:18:34 +03:00
|
|
|
Config: self.blockchain.Config(),
|
2018-01-30 19:39:32 +03:00
|
|
|
Head: hash,
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
}
|
2017-06-21 13:27:38 +03:00
|
|
|
|
|
|
|
// downloaderPeerNotify implements peerSetNotify
|
|
|
|
type downloaderPeerNotify ProtocolManager
|
|
|
|
|
2017-06-28 15:25:08 +03:00
|
|
|
type peerConnection struct {
|
|
|
|
manager *ProtocolManager
|
|
|
|
peer *peer
|
|
|
|
}
|
2017-06-21 13:27:38 +03:00
|
|
|
|
2017-06-28 15:25:08 +03:00
|
|
|
func (pc *peerConnection) Head() (common.Hash, *big.Int) {
|
|
|
|
return pc.peer.HeadAndTd()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *peerConnection) RequestHeadersByHash(origin common.Hash, amount int, skip int, reverse bool) error {
|
|
|
|
reqID := genReqID()
|
|
|
|
rq := &distReq{
|
|
|
|
getCost: func(dp distPeer) uint64 {
|
|
|
|
peer := dp.(*peer)
|
|
|
|
return peer.GetRequestCost(GetBlockHeadersMsg, amount)
|
|
|
|
},
|
|
|
|
canSend: func(dp distPeer) bool {
|
|
|
|
return dp.(*peer) == pc.peer
|
|
|
|
},
|
|
|
|
request: func(dp distPeer) func() {
|
|
|
|
peer := dp.(*peer)
|
|
|
|
cost := peer.GetRequestCost(GetBlockHeadersMsg, amount)
|
|
|
|
peer.fcServer.QueueRequest(reqID, cost)
|
|
|
|
return func() { peer.RequestHeadersByHash(reqID, cost, origin, amount, skip, reverse) }
|
|
|
|
},
|
2017-06-21 13:27:38 +03:00
|
|
|
}
|
2017-06-28 15:25:08 +03:00
|
|
|
_, ok := <-pc.manager.reqDist.queue(rq)
|
|
|
|
if !ok {
|
|
|
|
return ErrNoPeers
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *peerConnection) RequestHeadersByNumber(origin uint64, amount int, skip int, reverse bool) error {
|
|
|
|
reqID := genReqID()
|
|
|
|
rq := &distReq{
|
|
|
|
getCost: func(dp distPeer) uint64 {
|
|
|
|
peer := dp.(*peer)
|
|
|
|
return peer.GetRequestCost(GetBlockHeadersMsg, amount)
|
|
|
|
},
|
|
|
|
canSend: func(dp distPeer) bool {
|
|
|
|
return dp.(*peer) == pc.peer
|
|
|
|
},
|
|
|
|
request: func(dp distPeer) func() {
|
|
|
|
peer := dp.(*peer)
|
|
|
|
cost := peer.GetRequestCost(GetBlockHeadersMsg, amount)
|
|
|
|
peer.fcServer.QueueRequest(reqID, cost)
|
|
|
|
return func() { peer.RequestHeadersByNumber(reqID, cost, origin, amount, skip, reverse) }
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, ok := <-pc.manager.reqDist.queue(rq)
|
|
|
|
if !ok {
|
|
|
|
return ErrNoPeers
|
2017-06-21 13:27:38 +03:00
|
|
|
}
|
2017-06-28 15:25:08 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *downloaderPeerNotify) registerPeer(p *peer) {
|
|
|
|
pm := (*ProtocolManager)(d)
|
|
|
|
pc := &peerConnection{
|
|
|
|
manager: pm,
|
|
|
|
peer: p,
|
|
|
|
}
|
2017-07-03 17:17:12 +03:00
|
|
|
pm.downloader.RegisterLightPeer(p.id, ethVersion, pc)
|
2017-06-21 13:27:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *downloaderPeerNotify) unregisterPeer(p *peer) {
|
|
|
|
pm := (*ProtocolManager)(d)
|
|
|
|
pm.downloader.UnregisterPeer(p.id)
|
|
|
|
}
|