mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
les/*: golint updates for this or self warning
This commit is contained in:
parent
36606883d6
commit
4f6aea31d0
3 changed files with 100 additions and 100 deletions
|
|
@ -89,16 +89,16 @@ func NewClientManager(rcTarget, maxSimReq, maxRcSum uint64) *ClientManager {
|
|||
return cm
|
||||
}
|
||||
|
||||
func (self *ClientManager) Stop() {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (m *ClientManager) Stop() {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
// signal any waiting accept routines to return false
|
||||
self.nodes = make(map[*cmNode]struct{})
|
||||
close(self.resumeQueue)
|
||||
m.nodes = make(map[*cmNode]struct{})
|
||||
close(m.resumeQueue)
|
||||
}
|
||||
|
||||
func (self *ClientManager) addNode(cnode *ClientNode) *cmNode {
|
||||
func (m *ClientManager) addNode(cnode *ClientNode) *cmNode {
|
||||
time := mclock.Now()
|
||||
node := &cmNode{
|
||||
node: cnode,
|
||||
|
|
@ -106,28 +106,28 @@ func (self *ClientManager) addNode(cnode *ClientNode) *cmNode {
|
|||
finishRecharge: time,
|
||||
rcWeight: 1,
|
||||
}
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
self.nodes[node] = struct{}{}
|
||||
self.update(mclock.Now())
|
||||
m.nodes[node] = struct{}{}
|
||||
m.update(mclock.Now())
|
||||
return node
|
||||
}
|
||||
|
||||
func (self *ClientManager) removeNode(node *cmNode) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (m *ClientManager) removeNode(node *cmNode) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
time := mclock.Now()
|
||||
self.stop(node, time)
|
||||
delete(self.nodes, node)
|
||||
self.update(time)
|
||||
m.stop(node, time)
|
||||
delete(m.nodes, node)
|
||||
m.update(time)
|
||||
}
|
||||
|
||||
// recalc sumWeight
|
||||
func (self *ClientManager) updateNodes(time mclock.AbsTime) (rce bool) {
|
||||
func (m *ClientManager) updateNodes(time mclock.AbsTime) (rce bool) {
|
||||
var sumWeight, rcSum uint64
|
||||
for node := range self.nodes {
|
||||
for node := range m.nodes {
|
||||
rc := node.recharging
|
||||
node.update(time)
|
||||
if rc && !node.recharging {
|
||||
|
|
@ -138,44 +138,44 @@ func (self *ClientManager) updateNodes(time mclock.AbsTime) (rce bool) {
|
|||
}
|
||||
rcSum += uint64(node.rcValue)
|
||||
}
|
||||
self.sumWeight = sumWeight
|
||||
self.rcSumValue = rcSum
|
||||
m.sumWeight = sumWeight
|
||||
m.rcSumValue = rcSum
|
||||
return
|
||||
}
|
||||
|
||||
func (self *ClientManager) update(time mclock.AbsTime) {
|
||||
func (m *ClientManager) update(time mclock.AbsTime) {
|
||||
for {
|
||||
firstTime := time
|
||||
for node := range self.nodes {
|
||||
for node := range m.nodes {
|
||||
if node.recharging && node.finishRecharge < firstTime {
|
||||
firstTime = node.finishRecharge
|
||||
}
|
||||
}
|
||||
if self.updateNodes(firstTime) {
|
||||
for node := range self.nodes {
|
||||
if m.updateNodes(firstTime) {
|
||||
for node := range m.nodes {
|
||||
if node.recharging {
|
||||
node.set(node.serving, self.simReqCnt, self.sumWeight)
|
||||
node.set(node.serving, m.simReqCnt, m.sumWeight)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.time = time
|
||||
m.time = time
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *ClientManager) canStartReq() bool {
|
||||
return self.simReqCnt < self.maxSimReq && self.rcSumValue < self.maxRcSum
|
||||
func (m *ClientManager) canStartReq() bool {
|
||||
return m.simReqCnt < m.maxSimReq && m.rcSumValue < m.maxRcSum
|
||||
}
|
||||
|
||||
func (self *ClientManager) queueProc() {
|
||||
for rc := range self.resumeQueue {
|
||||
func (m *ClientManager) queueProc() {
|
||||
for rc := range m.resumeQueue {
|
||||
for {
|
||||
time.Sleep(time.Millisecond * 10)
|
||||
self.lock.Lock()
|
||||
self.update(mclock.Now())
|
||||
cs := self.canStartReq()
|
||||
self.lock.Unlock()
|
||||
m.lock.Lock()
|
||||
m.update(mclock.Now())
|
||||
cs := m.canStartReq()
|
||||
m.lock.Unlock()
|
||||
if cs {
|
||||
break
|
||||
}
|
||||
|
|
@ -184,41 +184,41 @@ func (self *ClientManager) queueProc() {
|
|||
}
|
||||
}
|
||||
|
||||
func (self *ClientManager) accept(node *cmNode, time mclock.AbsTime) bool {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (m *ClientManager) accept(node *cmNode, time mclock.AbsTime) bool {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
self.update(time)
|
||||
if !self.canStartReq() {
|
||||
m.update(time)
|
||||
if !m.canStartReq() {
|
||||
resume := make(chan bool)
|
||||
self.lock.Unlock()
|
||||
self.resumeQueue <- resume
|
||||
m.lock.Unlock()
|
||||
m.resumeQueue <- resume
|
||||
<-resume
|
||||
self.lock.Lock()
|
||||
if _, ok := self.nodes[node]; !ok {
|
||||
m.lock.Lock()
|
||||
if _, ok := m.nodes[node]; !ok {
|
||||
return false // reject if node has been removed or manager has been stopped
|
||||
}
|
||||
}
|
||||
self.simReqCnt++
|
||||
node.set(true, self.simReqCnt, self.sumWeight)
|
||||
m.simReqCnt++
|
||||
node.set(true, m.simReqCnt, m.sumWeight)
|
||||
node.startValue = node.rcValue
|
||||
self.update(self.time)
|
||||
m.update(m.time)
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *ClientManager) stop(node *cmNode, time mclock.AbsTime) {
|
||||
func (m *ClientManager) stop(node *cmNode, time mclock.AbsTime) {
|
||||
if node.serving {
|
||||
self.update(time)
|
||||
self.simReqCnt--
|
||||
node.set(false, self.simReqCnt, self.sumWeight)
|
||||
self.update(time)
|
||||
m.update(time)
|
||||
m.simReqCnt--
|
||||
node.set(false, m.simReqCnt, m.sumWeight)
|
||||
m.update(time)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *ClientManager) processed(node *cmNode, time mclock.AbsTime) (rcValue, rcCost uint64) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (m *ClientManager) processed(node *cmNode, time mclock.AbsTime) (rcValue, rcCost uint64) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
self.stop(node, time)
|
||||
m.stop(node, time)
|
||||
return uint64(node.rcValue), uint64(node.rcValue - node.startValue)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1162,15 +1162,15 @@ type NodeInfo struct {
|
|||
}
|
||||
|
||||
// NodeInfo retrieves some protocol metadata about the running host node.
|
||||
func (self *ProtocolManager) NodeInfo() *NodeInfo {
|
||||
func (pm *ProtocolManager) NodeInfo() *NodeInfo {
|
||||
head := self.blockchain.CurrentHeader()
|
||||
hash := head.Hash()
|
||||
|
||||
return &NodeInfo{
|
||||
Network: self.networkId,
|
||||
Difficulty: self.blockchain.GetTd(hash, head.Number.Uint64()),
|
||||
Genesis: self.blockchain.Genesis().Hash(),
|
||||
Config: self.blockchain.Config(),
|
||||
Network: pm.networkId,
|
||||
Difficulty: pm.blockchain.GetTd(hash, head.Number.Uint64()),
|
||||
Genesis: pm.blockchain.Genesis().Hash(),
|
||||
Config: pm.blockchain.Config(),
|
||||
Head: hash,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,47 +50,47 @@ func NewLesTxRelay(ps *peerSet, reqDist *requestDistributor) *LesTxRelay {
|
|||
return r
|
||||
}
|
||||
|
||||
func (self *LesTxRelay) registerPeer(p *peer) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (r *LesTxRelay) registerPeer(p *peer) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
self.peerList = self.ps.AllPeers()
|
||||
r.peerList = r.ps.AllPeers()
|
||||
}
|
||||
|
||||
func (self *LesTxRelay) unregisterPeer(p *peer) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (r *LesTxRelay) unregisterPeer(p *peer) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
self.peerList = self.ps.AllPeers()
|
||||
r.peerList = r.ps.AllPeers()
|
||||
}
|
||||
|
||||
// send sends a list of transactions to at most a given number of peers at
|
||||
// once, never resending any particular transaction to the same peer twice
|
||||
func (self *LesTxRelay) send(txs types.Transactions, count int) {
|
||||
func (r *LesTxRelay) send(txs types.Transactions, count int) {
|
||||
sendTo := make(map[*peer]types.Transactions)
|
||||
|
||||
self.peerStartPos++ // rotate the starting position of the peer list
|
||||
if self.peerStartPos >= len(self.peerList) {
|
||||
self.peerStartPos = 0
|
||||
r.peerStartPos++ // rotate the starting position of the peer list
|
||||
if r.peerStartPos >= len(r.peerList) {
|
||||
r.peerStartPos = 0
|
||||
}
|
||||
|
||||
for _, tx := range txs {
|
||||
hash := tx.Hash()
|
||||
ltr, ok := self.txSent[hash]
|
||||
ltr, ok := r.txSent[hash]
|
||||
if !ok {
|
||||
ltr = <rInfo{
|
||||
tx: tx,
|
||||
sentTo: make(map[*peer]struct{}),
|
||||
}
|
||||
self.txSent[hash] = ltr
|
||||
self.txPending[hash] = struct{}{}
|
||||
r.txSent[hash] = ltr
|
||||
r.txPending[hash] = struct{}{}
|
||||
}
|
||||
|
||||
if len(self.peerList) > 0 {
|
||||
if len(r.peerList) > 0 {
|
||||
cnt := count
|
||||
pos := self.peerStartPos
|
||||
pos := r.peerStartPos
|
||||
for {
|
||||
peer := self.peerList[pos]
|
||||
peer := r.peerList[pos]
|
||||
if _, ok := ltr.sentTo[peer]; !ok {
|
||||
sendTo[peer] = append(sendTo[peer], tx)
|
||||
ltr.sentTo[peer] = struct{}{}
|
||||
|
|
@ -100,10 +100,10 @@ func (self *LesTxRelay) send(txs types.Transactions, count int) {
|
|||
break // sent it to the desired number of peers
|
||||
}
|
||||
pos++
|
||||
if pos == len(self.peerList) {
|
||||
if pos == len(r.peerList) {
|
||||
pos = 0
|
||||
}
|
||||
if pos == self.peerStartPos {
|
||||
if pos == r.peerStartPos {
|
||||
break // tried all available peers
|
||||
}
|
||||
}
|
||||
|
|
@ -130,46 +130,46 @@ func (self *LesTxRelay) send(txs types.Transactions, count int) {
|
|||
return func() { peer.SendTxs(reqID, cost, ll) }
|
||||
},
|
||||
}
|
||||
self.reqDist.queue(rq)
|
||||
r.reqDist.queue(rq)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *LesTxRelay) Send(txs types.Transactions) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (r *LesTxRelay) Send(txs types.Transactions) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
self.send(txs, 3)
|
||||
r.send(txs, 3)
|
||||
}
|
||||
|
||||
func (self *LesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (r *LesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
for _, hash := range mined {
|
||||
delete(self.txPending, hash)
|
||||
delete(r.txPending, hash)
|
||||
}
|
||||
|
||||
for _, hash := range rollback {
|
||||
self.txPending[hash] = struct{}{}
|
||||
r.txPending[hash] = struct{}{}
|
||||
}
|
||||
|
||||
if len(self.txPending) > 0 {
|
||||
txs := make(types.Transactions, len(self.txPending))
|
||||
if len(r.txPending) > 0 {
|
||||
txs := make(types.Transactions, len(r.txPending))
|
||||
i := 0
|
||||
for hash := range self.txPending {
|
||||
txs[i] = self.txSent[hash].tx
|
||||
for hash := range r.txPending {
|
||||
txs[i] = r.txSent[hash].tx
|
||||
i++
|
||||
}
|
||||
self.send(txs, 1)
|
||||
r.send(txs, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *LesTxRelay) Discard(hashes []common.Hash) {
|
||||
self.lock.Lock()
|
||||
defer self.lock.Unlock()
|
||||
func (r *LesTxRelay) Discard(hashes []common.Hash) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
for _, hash := range hashes {
|
||||
delete(self.txSent, hash)
|
||||
delete(self.txPending, hash)
|
||||
delete(r.txSent, hash)
|
||||
delete(r.txPending, hash)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue