txrelay.go 4.4 KB

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