2016-07-03 19:44:31 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
|
|
|
// 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 (
|
2016-07-26 02:54:35 +00:00
|
|
|
//"fmt"
|
|
|
|
"math/big"
|
2016-07-03 19:44:31 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-07-03 19:44:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
2016-07-26 02:54:35 +00:00
|
|
|
|
|
|
|
type lightFetcher struct {
|
|
|
|
pm *ProtocolManager
|
|
|
|
odr *LesOdr
|
|
|
|
chain BlockChain
|
|
|
|
|
|
|
|
headAnnouncedMu sync.Mutex
|
|
|
|
headAnnouncedBy map[common.Hash][]*peer
|
|
|
|
currentTd *big.Int
|
|
|
|
deliverChn chan fetchResponse
|
|
|
|
reqMu sync.RWMutex
|
|
|
|
requested map[uint64]fetchRequest
|
|
|
|
timeoutChn chan uint64
|
|
|
|
notifyChn chan bool // true if initiated from outside
|
|
|
|
syncing bool
|
|
|
|
syncDone chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fetchRequest struct {
|
|
|
|
hash common.Hash
|
|
|
|
amount uint64
|
|
|
|
peer *peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type fetchResponse struct {
|
|
|
|
reqID uint64
|
|
|
|
headers []*types.Header
|
|
|
|
}
|
|
|
|
|
2016-07-03 19:44:31 +00:00
|
|
|
func newLightFetcher(pm *ProtocolManager) *lightFetcher {
|
|
|
|
f := &lightFetcher{
|
2016-07-26 02:54:35 +00:00
|
|
|
pm: pm,
|
|
|
|
chain: pm.blockchain,
|
|
|
|
odr: pm.odr,
|
|
|
|
headAnnouncedBy: make(map[common.Hash][]*peer),
|
|
|
|
deliverChn: make(chan fetchResponse, 100),
|
|
|
|
requested: make(map[uint64]fetchRequest),
|
|
|
|
timeoutChn: make(chan uint64),
|
|
|
|
notifyChn: make(chan bool, 100),
|
|
|
|
syncDone: make(chan struct{}),
|
|
|
|
currentTd: big.NewInt(0),
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
|
|
|
go f.syncLoop()
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func (f *lightFetcher) notify(p *peer, head *newBlockHashData) {
|
|
|
|
var headHash common.Hash
|
|
|
|
if head == nil {
|
|
|
|
// initial notify
|
|
|
|
headHash = p.Head()
|
|
|
|
} else {
|
|
|
|
if !p.addNotify(head) {
|
|
|
|
//fmt.Println("addNotify fail")
|
|
|
|
f.pm.removePeer(p.id)
|
|
|
|
}
|
|
|
|
headHash = head.Hash
|
|
|
|
}
|
|
|
|
f.headAnnouncedMu.Lock()
|
|
|
|
f.headAnnouncedBy[headHash] = append(f.headAnnouncedBy[headHash], p)
|
|
|
|
f.headAnnouncedMu.Unlock()
|
|
|
|
f.notifyChn <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *lightFetcher) gotHeader(header *types.Header) {
|
|
|
|
f.headAnnouncedMu.Lock()
|
|
|
|
defer f.headAnnouncedMu.Unlock()
|
|
|
|
|
|
|
|
hash := header.Hash()
|
|
|
|
peerList := f.headAnnouncedBy[hash]
|
|
|
|
if peerList == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
number := header.GetNumberU64()
|
|
|
|
td := core.GetTd(f.pm.chainDb, hash, number)
|
|
|
|
for _, peer := range peerList {
|
|
|
|
peer.lock.Lock()
|
|
|
|
ok := peer.gotHeader(hash, number, td)
|
|
|
|
peer.lock.Unlock()
|
|
|
|
if !ok {
|
|
|
|
//fmt.Println("gotHeader fail")
|
|
|
|
f.pm.removePeer(peer.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(f.headAnnouncedBy, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *lightFetcher) nextRequest() (*peer, *newBlockHashData) {
|
|
|
|
var bestPeer *peer
|
|
|
|
bestTd := f.currentTd
|
|
|
|
for _, peer := range f.pm.peers.AllPeers() {
|
|
|
|
peer.lock.RLock()
|
|
|
|
if !peer.headInfo.requested && (peer.headInfo.Td.Cmp(bestTd) > 0 ||
|
|
|
|
(bestPeer != nil && peer.headInfo.Td.Cmp(bestTd) == 0 && peer.headInfo.haveHeaders > bestPeer.headInfo.haveHeaders)) {
|
|
|
|
bestPeer = peer
|
|
|
|
bestTd = peer.headInfo.Td
|
|
|
|
}
|
|
|
|
peer.lock.RUnlock()
|
|
|
|
}
|
|
|
|
if bestPeer == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
bestPeer.lock.Lock()
|
|
|
|
res := bestPeer.headInfo
|
|
|
|
res.requested = true
|
|
|
|
bestPeer.lock.Unlock()
|
|
|
|
return bestPeer, res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *lightFetcher) deliverHeaders(reqID uint64, headers []*types.Header) {
|
|
|
|
f.deliverChn <- fetchResponse{reqID: reqID, headers: headers}
|
|
|
|
}
|
|
|
|
|
2016-07-03 19:44:31 +00:00
|
|
|
func (f *lightFetcher) requestedID(reqID uint64) bool {
|
|
|
|
f.reqMu.RLock()
|
|
|
|
_, ok := f.requested[reqID]
|
|
|
|
f.reqMu.RUnlock()
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func (f *lightFetcher) request(p *peer, block *newBlockHashData) {
|
|
|
|
//fmt.Println("request", block.Number, block.haveHeaders)
|
|
|
|
amount := block.Number-block.haveHeaders
|
|
|
|
if amount == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if amount > 100 {
|
|
|
|
f.syncing = true
|
|
|
|
go func() {
|
|
|
|
//fmt.Println("f.pm.synchronise(p)")
|
|
|
|
f.pm.synchronise(p)
|
|
|
|
//fmt.Println("sync done")
|
|
|
|
f.syncDone <- struct{}{}
|
|
|
|
}()
|
|
|
|
return
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
|
|
|
|
reqID := f.odr.getNextReqID()
|
|
|
|
f.reqMu.Lock()
|
|
|
|
f.requested[reqID] = fetchRequest{hash: block.Hash, amount: amount, peer: p}
|
2016-07-03 19:44:31 +00:00
|
|
|
f.reqMu.Unlock()
|
2016-07-26 02:54:35 +00:00
|
|
|
cost := p.GetRequestCost(GetBlockHeadersMsg, int(amount))
|
|
|
|
p.fcServer.SendRequest(reqID, cost)
|
|
|
|
go p.RequestHeadersByHash(reqID, cost, block.Hash, int(amount), 0, true)
|
|
|
|
go func() {
|
|
|
|
time.Sleep(hardRequestTimeout)
|
|
|
|
f.timeoutChn <- reqID
|
|
|
|
}()
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func (f *lightFetcher) processResponse(req fetchRequest, resp fetchResponse) bool {
|
|
|
|
if uint64(len(resp.headers)) != req.amount || resp.headers[0].Hash() != req.hash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
headers := make([]*types.Header, req.amount)
|
|
|
|
for i, header := range resp.headers {
|
|
|
|
headers[int(req.amount)-1-i] = header
|
|
|
|
}
|
|
|
|
if _, err := f.chain.InsertHeaderChain(headers, 1); err != nil {
|
|
|
|
return false
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
for _, header := range headers {
|
|
|
|
td := core.GetTd(f.pm.chainDb, header.Hash(), header.GetNumberU64())
|
|
|
|
if td == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if td.Cmp(f.currentTd) > 0 {
|
|
|
|
f.currentTd = td
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
f.gotHeader(header)
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
return true
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
|
|
|
|
2016-07-26 02:54:35 +00:00
|
|
|
func (f *lightFetcher) checkSyncedHeaders() {
|
|
|
|
//fmt.Println("checkSyncedHeaders()")
|
|
|
|
for _, peer := range f.pm.peers.AllPeers() {
|
|
|
|
peer.lock.Lock()
|
|
|
|
h := peer.firstHeadInfo
|
|
|
|
remove := false
|
|
|
|
loop:
|
|
|
|
for h != nil {
|
|
|
|
if td := core.GetTd(f.pm.chainDb, h.Hash, h.Number); td != nil {
|
|
|
|
//fmt.Println(" found", h.Number)
|
|
|
|
ok := peer.gotHeader(h.Hash, h.Number, td)
|
|
|
|
if !ok {
|
|
|
|
remove = true
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
if td.Cmp(f.currentTd) > 0 {
|
|
|
|
f.currentTd = td
|
|
|
|
}
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
h = h.next
|
|
|
|
}
|
|
|
|
peer.lock.Unlock()
|
|
|
|
if remove {
|
|
|
|
//fmt.Println("checkSync fail")
|
|
|
|
f.pm.removePeer(peer.id)
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *lightFetcher) syncLoop() {
|
|
|
|
f.pm.wg.Add(1)
|
|
|
|
defer f.pm.wg.Done()
|
2016-07-26 02:54:35 +00:00
|
|
|
|
|
|
|
srtoNotify := false
|
2016-07-03 19:44:31 +00:00
|
|
|
for {
|
2016-07-26 02:54:35 +00:00
|
|
|
select {
|
2016-07-03 19:44:31 +00:00
|
|
|
case <-f.pm.quitSync:
|
|
|
|
return
|
2016-07-26 02:54:35 +00:00
|
|
|
case ext := <-f.notifyChn:
|
|
|
|
//fmt.Println("<-f.notifyChn", f.syncing, ext, srtoNotify)
|
|
|
|
s := srtoNotify
|
|
|
|
srtoNotify = false
|
|
|
|
if !f.syncing && !(ext && s) {
|
|
|
|
if p, r := f.nextRequest(); r != nil {
|
|
|
|
srtoNotify = true
|
2016-07-03 19:44:31 +00:00
|
|
|
go func() {
|
2016-07-26 02:54:35 +00:00
|
|
|
time.Sleep(softRequestTimeout)
|
|
|
|
f.notifyChn <- false
|
2016-07-03 19:44:31 +00:00
|
|
|
}()
|
2016-07-26 02:54:35 +00:00
|
|
|
f.request(p, r)
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
case reqID := <-f.timeoutChn:
|
|
|
|
f.reqMu.Lock()
|
|
|
|
req, ok := f.requested[reqID]
|
|
|
|
if ok {
|
|
|
|
delete(f.requested, reqID)
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
f.reqMu.Unlock()
|
|
|
|
if ok {
|
|
|
|
//fmt.Println("hard timeout")
|
|
|
|
f.pm.removePeer(req.peer.id)
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
case resp := <-f.deliverChn:
|
|
|
|
//fmt.Println("<-f.deliverChn", f.syncing)
|
2016-07-03 19:44:31 +00:00
|
|
|
f.reqMu.Lock()
|
2016-07-26 02:54:35 +00:00
|
|
|
req, ok := f.requested[resp.reqID]
|
|
|
|
delete(f.requested, resp.reqID)
|
2016-07-03 19:44:31 +00:00
|
|
|
f.reqMu.Unlock()
|
2016-07-26 02:54:35 +00:00
|
|
|
if !ok || !(f.syncing || f.processResponse(req, resp)) {
|
|
|
|
//fmt.Println("processResponse fail")
|
|
|
|
f.pm.removePeer(req.peer.id)
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
2016-07-26 02:54:35 +00:00
|
|
|
case <-f.syncDone:
|
|
|
|
//fmt.Println("<-f.syncDone", f.syncing)
|
|
|
|
f.checkSyncedHeaders()
|
|
|
|
f.syncing = false
|
2016-07-03 19:44:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|