txrelay.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "context"
  19. "sync"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/types"
  22. "github.com/ethereum/go-ethereum/rlp"
  23. )
  24. type ltrInfo struct {
  25. tx *types.Transaction
  26. sentTo map[*serverPeer]struct{}
  27. }
  28. type lesTxRelay struct {
  29. txSent map[common.Hash]*ltrInfo
  30. txPending map[common.Hash]struct{}
  31. peerList []*serverPeer
  32. peerStartPos int
  33. lock sync.Mutex
  34. stop chan struct{}
  35. retriever *retrieveManager
  36. }
  37. func newLesTxRelay(ps *serverPeerSet, retriever *retrieveManager) *lesTxRelay {
  38. r := &lesTxRelay{
  39. txSent: make(map[common.Hash]*ltrInfo),
  40. txPending: make(map[common.Hash]struct{}),
  41. retriever: retriever,
  42. stop: make(chan struct{}),
  43. }
  44. ps.subscribe(r)
  45. return r
  46. }
  47. func (ltrx *lesTxRelay) Stop() {
  48. close(ltrx.stop)
  49. }
  50. func (ltrx *lesTxRelay) registerPeer(p *serverPeer) {
  51. ltrx.lock.Lock()
  52. defer ltrx.lock.Unlock()
  53. // Short circuit if the peer is announce only.
  54. if p.onlyAnnounce {
  55. return
  56. }
  57. ltrx.peerList = append(ltrx.peerList, p)
  58. }
  59. func (ltrx *lesTxRelay) unregisterPeer(p *serverPeer) {
  60. ltrx.lock.Lock()
  61. defer ltrx.lock.Unlock()
  62. for i, peer := range ltrx.peerList {
  63. if peer == p {
  64. // Remove from the peer list
  65. ltrx.peerList = append(ltrx.peerList[:i], ltrx.peerList[i+1:]...)
  66. return
  67. }
  68. }
  69. }
  70. // send sends a list of transactions to at most a given number of peers at
  71. // once, never resending any particular transaction to the same peer twice
  72. func (ltrx *lesTxRelay) send(txs types.Transactions, count int) {
  73. sendTo := make(map[*serverPeer]types.Transactions)
  74. ltrx.peerStartPos++ // rotate the starting position of the peer list
  75. if ltrx.peerStartPos >= len(ltrx.peerList) {
  76. ltrx.peerStartPos = 0
  77. }
  78. for _, tx := range txs {
  79. hash := tx.Hash()
  80. ltr, ok := ltrx.txSent[hash]
  81. if !ok {
  82. ltr = &ltrInfo{
  83. tx: tx,
  84. sentTo: make(map[*serverPeer]struct{}),
  85. }
  86. ltrx.txSent[hash] = ltr
  87. ltrx.txPending[hash] = struct{}{}
  88. }
  89. if len(ltrx.peerList) > 0 {
  90. cnt := count
  91. pos := ltrx.peerStartPos
  92. for {
  93. peer := ltrx.peerList[pos]
  94. if _, ok := ltr.sentTo[peer]; !ok {
  95. sendTo[peer] = append(sendTo[peer], tx)
  96. ltr.sentTo[peer] = struct{}{}
  97. cnt--
  98. }
  99. if cnt == 0 {
  100. break // sent it to the desired number of peers
  101. }
  102. pos++
  103. if pos == len(ltrx.peerList) {
  104. pos = 0
  105. }
  106. if pos == ltrx.peerStartPos {
  107. break // tried all available peers
  108. }
  109. }
  110. }
  111. }
  112. for p, list := range sendTo {
  113. pp := p
  114. ll := list
  115. enc, _ := rlp.EncodeToBytes(ll)
  116. reqID := genReqID()
  117. rq := &distReq{
  118. getCost: func(dp distPeer) uint64 {
  119. peer := dp.(*serverPeer)
  120. return peer.getTxRelayCost(len(ll), len(enc))
  121. },
  122. canSend: func(dp distPeer) bool {
  123. return !dp.(*serverPeer).onlyAnnounce && dp.(*serverPeer) == pp
  124. },
  125. request: func(dp distPeer) func() {
  126. peer := dp.(*serverPeer)
  127. cost := peer.getTxRelayCost(len(ll), len(enc))
  128. peer.fcServer.QueuedRequest(reqID, cost)
  129. return func() { peer.sendTxs(reqID, len(ll), enc) }
  130. },
  131. }
  132. go ltrx.retriever.retrieve(context.Background(), reqID, rq, func(p distPeer, msg *Msg) error { return nil }, ltrx.stop)
  133. }
  134. }
  135. func (ltrx *lesTxRelay) Send(txs types.Transactions) {
  136. ltrx.lock.Lock()
  137. defer ltrx.lock.Unlock()
  138. ltrx.send(txs, 3)
  139. }
  140. func (ltrx *lesTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
  141. ltrx.lock.Lock()
  142. defer ltrx.lock.Unlock()
  143. for _, hash := range mined {
  144. delete(ltrx.txPending, hash)
  145. }
  146. for _, hash := range rollback {
  147. ltrx.txPending[hash] = struct{}{}
  148. }
  149. if len(ltrx.txPending) > 0 {
  150. txs := make(types.Transactions, len(ltrx.txPending))
  151. i := 0
  152. for hash := range ltrx.txPending {
  153. txs[i] = ltrx.txSent[hash].tx
  154. i++
  155. }
  156. ltrx.send(txs, 1)
  157. }
  158. }
  159. func (ltrx *lesTxRelay) Discard(hashes []common.Hash) {
  160. ltrx.lock.Lock()
  161. defer ltrx.lock.Unlock()
  162. for _, hash := range hashes {
  163. delete(ltrx.txSent, hash)
  164. delete(ltrx.txPending, hash)
  165. }
  166. }