les: fix staticcheck warnings (#20371)

This commit is contained in:
Guillaume Ballet 2019-11-27 09:49:41 +01:00 committed by GitHub
parent 23c8c74131
commit c4844e9ee2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 43 deletions

View File

@ -119,7 +119,7 @@ func (rm *retrieveManager) retrieve(ctx context.Context, reqID uint64, req *dist
case <-ctx.Done(): case <-ctx.Done():
sentReq.stop(ctx.Err()) sentReq.stop(ctx.Err())
case <-shutdown: case <-shutdown:
sentReq.stop(fmt.Errorf("Client is shutting down")) sentReq.stop(fmt.Errorf("client is shutting down"))
} }
return sentReq.getError() return sentReq.getError()
} }

View File

@ -54,51 +54,51 @@ func newLesTxRelay(ps *peerSet, retriever *retrieveManager) *lesTxRelay {
return r return r
} }
func (self *lesTxRelay) Stop() { func (ltrx *lesTxRelay) Stop() {
close(self.stop) close(ltrx.stop)
} }
func (self *lesTxRelay) registerPeer(p *peer) { func (ltrx *lesTxRelay) registerPeer(p *peer) {
self.lock.Lock() ltrx.lock.Lock()
defer self.lock.Unlock() defer ltrx.lock.Unlock()
self.peerList = self.ps.AllPeers() ltrx.peerList = ltrx.ps.AllPeers()
} }
func (self *lesTxRelay) unregisterPeer(p *peer) { func (ltrx *lesTxRelay) unregisterPeer(p *peer) {
self.lock.Lock() ltrx.lock.Lock()
defer self.lock.Unlock() defer ltrx.lock.Unlock()
self.peerList = self.ps.AllPeers() ltrx.peerList = ltrx.ps.AllPeers()
} }
// send sends a list of transactions to at most a given number of peers at // 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 // once, never resending any particular transaction to the same peer twice
func (self *lesTxRelay) send(txs types.Transactions, count int) { func (ltrx *lesTxRelay) send(txs types.Transactions, count int) {
sendTo := make(map[*peer]types.Transactions) sendTo := make(map[*peer]types.Transactions)
self.peerStartPos++ // rotate the starting position of the peer list ltrx.peerStartPos++ // rotate the starting position of the peer list
if self.peerStartPos >= len(self.peerList) { if ltrx.peerStartPos >= len(ltrx.peerList) {
self.peerStartPos = 0 ltrx.peerStartPos = 0
} }
for _, tx := range txs { for _, tx := range txs {
hash := tx.Hash() hash := tx.Hash()
ltr, ok := self.txSent[hash] ltr, ok := ltrx.txSent[hash]
if !ok { if !ok {
ltr = &ltrInfo{ ltr = &ltrInfo{
tx: tx, tx: tx,
sentTo: make(map[*peer]struct{}), sentTo: make(map[*peer]struct{}),
} }
self.txSent[hash] = ltr ltrx.txSent[hash] = ltr
self.txPending[hash] = struct{}{} ltrx.txPending[hash] = struct{}{}
} }
if len(self.peerList) > 0 { if len(ltrx.peerList) > 0 {
cnt := count cnt := count
pos := self.peerStartPos pos := ltrx.peerStartPos
for { for {
peer := self.peerList[pos] peer := ltrx.peerList[pos]
if _, ok := ltr.sentTo[peer]; !ok { if _, ok := ltr.sentTo[peer]; !ok {
sendTo[peer] = append(sendTo[peer], tx) sendTo[peer] = append(sendTo[peer], tx)
ltr.sentTo[peer] = struct{}{} ltr.sentTo[peer] = struct{}{}
@ -108,10 +108,10 @@ func (self *lesTxRelay) send(txs types.Transactions, count int) {
break // sent it to the desired number of peers break // sent it to the desired number of peers
} }
pos++ pos++
if pos == len(self.peerList) { if pos == len(ltrx.peerList) {
pos = 0 pos = 0
} }
if pos == self.peerStartPos { if pos == ltrx.peerStartPos {
break // tried all available peers break // tried all available peers
} }
} }
@ -139,46 +139,46 @@ func (self *lesTxRelay) send(txs types.Transactions, count int) {
return func() { peer.SendTxs(reqID, cost, enc) } return func() { peer.SendTxs(reqID, cost, enc) }
}, },
} }
go self.retriever.retrieve(context.Background(), reqID, rq, func(p distPeer, msg *Msg) error { return nil }, self.stop) go ltrx.retriever.retrieve(context.Background(), reqID, rq, func(p distPeer, msg *Msg) error { return nil }, ltrx.stop)
} }
} }
func (self *lesTxRelay) Send(txs types.Transactions) { func (ltrx *lesTxRelay) Send(txs types.Transactions) {
self.lock.Lock() ltrx.lock.Lock()
defer self.lock.Unlock() defer ltrx.lock.Unlock()
self.send(txs, 3) ltrx.send(txs, 3)
} }
func (self *lesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) { func (ltrx *lesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
self.lock.Lock() ltrx.lock.Lock()
defer self.lock.Unlock() defer ltrx.lock.Unlock()
for _, hash := range mined { for _, hash := range mined {
delete(self.txPending, hash) delete(ltrx.txPending, hash)
} }
for _, hash := range rollback { for _, hash := range rollback {
self.txPending[hash] = struct{}{} ltrx.txPending[hash] = struct{}{}
} }
if len(self.txPending) > 0 { if len(ltrx.txPending) > 0 {
txs := make(types.Transactions, len(self.txPending)) txs := make(types.Transactions, len(ltrx.txPending))
i := 0 i := 0
for hash := range self.txPending { for hash := range ltrx.txPending {
txs[i] = self.txSent[hash].tx txs[i] = ltrx.txSent[hash].tx
i++ i++
} }
self.send(txs, 1) ltrx.send(txs, 1)
} }
} }
func (self *lesTxRelay) Discard(hashes []common.Hash) { func (ltrx *lesTxRelay) Discard(hashes []common.Hash) {
self.lock.Lock() ltrx.lock.Lock()
defer self.lock.Unlock() defer ltrx.lock.Unlock()
for _, hash := range hashes { for _, hash := range hashes {
delete(self.txSent, hash) delete(ltrx.txSent, hash)
delete(self.txPending, hash) delete(ltrx.txPending, hash)
} }
} }