txrelay.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "sync"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/types"
  21. )
  22. type ltrInfo struct {
  23. tx *types.Transaction
  24. sentTo map[*peer]struct{}
  25. }
  26. type LesTxRelay struct {
  27. txSent map[common.Hash]*ltrInfo
  28. txPending map[common.Hash]struct{}
  29. ps *peerSet
  30. peerList []*peer
  31. peerStartPos int
  32. lock sync.RWMutex
  33. reqDist *requestDistributor
  34. }
  35. func NewLesTxRelay(ps *peerSet, reqDist *requestDistributor) *LesTxRelay {
  36. r := &LesTxRelay{
  37. txSent: make(map[common.Hash]*ltrInfo),
  38. txPending: make(map[common.Hash]struct{}),
  39. ps: ps,
  40. reqDist: reqDist,
  41. }
  42. ps.notify(r)
  43. return r
  44. }
  45. func (self *LesTxRelay) registerPeer(p *peer) {
  46. self.lock.Lock()
  47. defer self.lock.Unlock()
  48. self.peerList = self.ps.AllPeers()
  49. }
  50. func (self *LesTxRelay) unregisterPeer(p *peer) {
  51. self.lock.Lock()
  52. defer self.lock.Unlock()
  53. self.peerList = self.ps.AllPeers()
  54. }
  55. // send sends a list of transactions to at most a given number of peers at
  56. // once, never resending any particular transaction to the same peer twice
  57. func (self *LesTxRelay) send(txs types.Transactions, count int) {
  58. sendTo := make(map[*peer]types.Transactions)
  59. self.peerStartPos++ // rotate the starting position of the peer list
  60. if self.peerStartPos >= len(self.peerList) {
  61. self.peerStartPos = 0
  62. }
  63. for _, tx := range txs {
  64. hash := tx.Hash()
  65. ltr, ok := self.txSent[hash]
  66. if !ok {
  67. ltr = &ltrInfo{
  68. tx: tx,
  69. sentTo: make(map[*peer]struct{}),
  70. }
  71. self.txSent[hash] = ltr
  72. self.txPending[hash] = struct{}{}
  73. }
  74. if len(self.peerList) > 0 {
  75. cnt := count
  76. pos := self.peerStartPos
  77. for {
  78. peer := self.peerList[pos]
  79. if _, ok := ltr.sentTo[peer]; !ok {
  80. sendTo[peer] = append(sendTo[peer], tx)
  81. ltr.sentTo[peer] = struct{}{}
  82. cnt--
  83. }
  84. if cnt == 0 {
  85. break // sent it to the desired number of peers
  86. }
  87. pos++
  88. if pos == len(self.peerList) {
  89. pos = 0
  90. }
  91. if pos == self.peerStartPos {
  92. break // tried all available peers
  93. }
  94. }
  95. }
  96. }
  97. for p, list := range sendTo {
  98. pp := p
  99. ll := list
  100. reqID := genReqID()
  101. rq := &distReq{
  102. getCost: func(dp distPeer) uint64 {
  103. peer := dp.(*peer)
  104. return peer.GetRequestCost(SendTxMsg, len(ll))
  105. },
  106. canSend: func(dp distPeer) bool {
  107. return dp.(*peer) == pp
  108. },
  109. request: func(dp distPeer) func() {
  110. peer := dp.(*peer)
  111. cost := peer.GetRequestCost(SendTxMsg, len(ll))
  112. peer.fcServer.QueueRequest(reqID, cost)
  113. return func() { peer.SendTxs(reqID, cost, ll) }
  114. },
  115. }
  116. self.reqDist.queue(rq)
  117. }
  118. }
  119. func (self *LesTxRelay) Send(txs types.Transactions) {
  120. self.lock.Lock()
  121. defer self.lock.Unlock()
  122. self.send(txs, 3)
  123. }
  124. func (self *LesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
  125. self.lock.Lock()
  126. defer self.lock.Unlock()
  127. for _, hash := range mined {
  128. delete(self.txPending, hash)
  129. }
  130. for _, hash := range rollback {
  131. self.txPending[hash] = struct{}{}
  132. }
  133. if len(self.txPending) > 0 {
  134. txs := make(types.Transactions, len(self.txPending))
  135. i := 0
  136. for hash := range self.txPending {
  137. txs[i] = self.txSent[hash].tx
  138. i++
  139. }
  140. self.send(txs, 1)
  141. }
  142. }
  143. func (self *LesTxRelay) Discard(hashes []common.Hash) {
  144. self.lock.Lock()
  145. defer self.lock.Unlock()
  146. for _, hash := range hashes {
  147. delete(self.txSent, hash)
  148. delete(self.txPending, hash)
  149. }
  150. }