txrelay.go 4.4 KB

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