txrelay.go 4.4 KB

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