aboutsummaryrefslogtreecommitdiffstats
path: root/les/odr_peerset.go
blob: e9b7eec7ff0af95a14efdc22404cae80ad5daf4f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 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

import (
    "sync"
)

const dropTimeoutRatio = 20

type odrPeerInfo struct {
    reqTimeSum, reqTimeCnt, reqCnt, timeoutCnt uint64
}

// odrPeerSet represents the collection of active peer participating in the block
// download procedure.
type odrPeerSet struct {
    peers map[*peer]*odrPeerInfo
    lock  sync.RWMutex
}

// newPeerSet creates a new peer set top track the active download sources.
func newOdrPeerSet() *odrPeerSet {
    return &odrPeerSet{
        peers: make(map[*peer]*odrPeerInfo),
    }
}

// Register injects a new peer into the working set, or returns an error if the
// peer is already known.
func (ps *odrPeerSet) register(p *peer) error {
    ps.lock.Lock()
    defer ps.lock.Unlock()

    if _, ok := ps.peers[p]; ok {
        return errAlreadyRegistered
    }
    ps.peers[p] = &odrPeerInfo{}
    return nil
}

// Unregister removes a remote peer from the active set, disabling any further
// actions to/from that particular entity.
func (ps *odrPeerSet) unregister(p *peer) error {
    ps.lock.Lock()
    defer ps.lock.Unlock()

    if _, ok := ps.peers[p]; !ok {
        return errNotRegistered
    }
    delete(ps.peers, p)
    return nil
}

func (ps *odrPeerSet) peerPriority(p *peer, info *odrPeerInfo, req LesOdrRequest) uint64 {
    tm := p.fcServer.CanSend(req.GetCost(p))
    if info.reqTimeCnt > 0 {
        tm += info.reqTimeSum / info.reqTimeCnt
    }
    return tm
}

func (ps *odrPeerSet) bestPeer(req LesOdrRequest, exclude map[*peer]struct{}) *peer {
    var best *peer
    var bpv uint64
    ps.lock.Lock()
    defer ps.lock.Unlock()

    for p, info := range ps.peers {
        if _, ok := exclude[p]; !ok {
            pv := ps.peerPriority(p, info, req)
            if best == nil || pv < bpv {
                best = p
                bpv = pv
            }
        }
    }
    return best
}

func (ps *odrPeerSet) updateTimeout(p *peer, timeout bool) (drop bool) {
    ps.lock.Lock()
    defer ps.lock.Unlock()

    if info, ok := ps.peers[p]; ok {
        info.reqCnt++
        if timeout {
            // check ratio before increase to allow an extra timeout
            if info.timeoutCnt*dropTimeoutRatio >= info.reqCnt {
                return true
            }
            info.timeoutCnt++
        }
    }
    return false
}

func (ps *odrPeerSet) updateServTime(p *peer, servTime uint64) {
    ps.lock.Lock()
    defer ps.lock.Unlock()

    if info, ok := ps.peers[p]; ok {
        info.reqTimeSum += servTime
        info.reqTimeCnt++
    }
}