You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
7.4 KiB

  1. package statesync
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/sha256"
  6. "fmt"
  7. "math/rand"
  8. "sort"
  9. "time"
  10. tmsync "github.com/tendermint/tendermint/libs/sync"
  11. "github.com/tendermint/tendermint/p2p"
  12. )
  13. // snapshotKey is a snapshot key used for lookups.
  14. type snapshotKey [sha256.Size]byte
  15. // snapshot contains data about a snapshot.
  16. type snapshot struct {
  17. Height uint64
  18. Format uint32
  19. Chunks uint32
  20. Hash []byte
  21. Metadata []byte
  22. trustedAppHash []byte // populated by light client
  23. }
  24. // Key generates a snapshot key, used for lookups. It takes into account not only the height and
  25. // format, but also the chunks, hash, and metadata in case peers have generated snapshots in a
  26. // non-deterministic manner. All fields must be equal for the snapshot to be considered the same.
  27. func (s *snapshot) Key() snapshotKey {
  28. // Hash.Write() never returns an error.
  29. hasher := sha256.New()
  30. hasher.Write([]byte(fmt.Sprintf("%v:%v:%v", s.Height, s.Format, s.Chunks))) //nolint:errcheck // ignore error
  31. hasher.Write(s.Hash) //nolint:errcheck // ignore error
  32. hasher.Write(s.Metadata) //nolint:errcheck // ignore error
  33. var key snapshotKey
  34. copy(key[:], hasher.Sum(nil))
  35. return key
  36. }
  37. // snapshotPool discovers and aggregates snapshots across peers.
  38. type snapshotPool struct {
  39. stateProvider StateProvider
  40. tmsync.Mutex
  41. snapshots map[snapshotKey]*snapshot
  42. snapshotPeers map[snapshotKey]map[string]p2p.PeerID
  43. // indexes for fast searches
  44. formatIndex map[uint32]map[snapshotKey]bool
  45. heightIndex map[uint64]map[snapshotKey]bool
  46. peerIndex map[string]map[snapshotKey]bool
  47. // blacklists for rejected items
  48. formatBlacklist map[uint32]bool
  49. peerBlacklist map[string]bool
  50. snapshotBlacklist map[snapshotKey]bool
  51. }
  52. // newSnapshotPool creates a new snapshot pool. The state source is used for
  53. func newSnapshotPool(stateProvider StateProvider) *snapshotPool {
  54. return &snapshotPool{
  55. stateProvider: stateProvider,
  56. snapshots: make(map[snapshotKey]*snapshot),
  57. snapshotPeers: make(map[snapshotKey]map[string]p2p.PeerID),
  58. formatIndex: make(map[uint32]map[snapshotKey]bool),
  59. heightIndex: make(map[uint64]map[snapshotKey]bool),
  60. peerIndex: make(map[string]map[snapshotKey]bool),
  61. formatBlacklist: make(map[uint32]bool),
  62. peerBlacklist: make(map[string]bool),
  63. snapshotBlacklist: make(map[snapshotKey]bool),
  64. }
  65. }
  66. // Add adds a snapshot to the pool, unless the peer has already sent recentSnapshots
  67. // snapshots. It returns true if this was a new, non-blacklisted snapshot. The
  68. // snapshot height is verified using the light client, and the expected app hash
  69. // is set for the snapshot.
  70. func (p *snapshotPool) Add(peer p2p.PeerID, snapshot *snapshot) (bool, error) {
  71. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  72. defer cancel()
  73. appHash, err := p.stateProvider.AppHash(ctx, snapshot.Height)
  74. if err != nil {
  75. return false, err
  76. }
  77. snapshot.trustedAppHash = appHash
  78. key := snapshot.Key()
  79. p.Lock()
  80. defer p.Unlock()
  81. switch {
  82. case p.formatBlacklist[snapshot.Format]:
  83. return false, nil
  84. case p.peerBlacklist[peer.String()]:
  85. return false, nil
  86. case p.snapshotBlacklist[key]:
  87. return false, nil
  88. case len(p.peerIndex[peer.String()]) >= recentSnapshots:
  89. return false, nil
  90. }
  91. if p.snapshotPeers[key] == nil {
  92. p.snapshotPeers[key] = make(map[string]p2p.PeerID)
  93. }
  94. p.snapshotPeers[key][peer.String()] = peer
  95. if p.peerIndex[peer.String()] == nil {
  96. p.peerIndex[peer.String()] = make(map[snapshotKey]bool)
  97. }
  98. p.peerIndex[peer.String()][key] = true
  99. if p.snapshots[key] != nil {
  100. return false, nil
  101. }
  102. p.snapshots[key] = snapshot
  103. if p.formatIndex[snapshot.Format] == nil {
  104. p.formatIndex[snapshot.Format] = make(map[snapshotKey]bool)
  105. }
  106. p.formatIndex[snapshot.Format][key] = true
  107. if p.heightIndex[snapshot.Height] == nil {
  108. p.heightIndex[snapshot.Height] = make(map[snapshotKey]bool)
  109. }
  110. p.heightIndex[snapshot.Height][key] = true
  111. return true, nil
  112. }
  113. // Best returns the "best" currently known snapshot, if any.
  114. func (p *snapshotPool) Best() *snapshot {
  115. ranked := p.Ranked()
  116. if len(ranked) == 0 {
  117. return nil
  118. }
  119. return ranked[0]
  120. }
  121. // GetPeer returns a random peer for a snapshot, if any.
  122. func (p *snapshotPool) GetPeer(snapshot *snapshot) p2p.PeerID {
  123. peers := p.GetPeers(snapshot)
  124. if len(peers) == 0 {
  125. return nil
  126. }
  127. return peers[rand.Intn(len(peers))] // nolint:gosec // G404: Use of weak random number generator
  128. }
  129. // GetPeers returns the peers for a snapshot.
  130. func (p *snapshotPool) GetPeers(snapshot *snapshot) []p2p.PeerID {
  131. key := snapshot.Key()
  132. p.Lock()
  133. defer p.Unlock()
  134. peers := make([]p2p.PeerID, 0, len(p.snapshotPeers[key]))
  135. for _, peer := range p.snapshotPeers[key] {
  136. peers = append(peers, peer)
  137. }
  138. // sort results, for testability (otherwise order is random, so tests randomly fail)
  139. sort.Slice(peers, func(a int, b int) bool {
  140. return bytes.Compare(peers[a], peers[b]) < 0
  141. })
  142. return peers
  143. }
  144. // Ranked returns a list of snapshots ranked by preference. The current heuristic is very naïve,
  145. // preferring the snapshot with the greatest height, then greatest format, then greatest number of
  146. // peers. This can be improved quite a lot.
  147. func (p *snapshotPool) Ranked() []*snapshot {
  148. p.Lock()
  149. defer p.Unlock()
  150. candidates := make([]*snapshot, 0, len(p.snapshots))
  151. for _, snapshot := range p.snapshots {
  152. candidates = append(candidates, snapshot)
  153. }
  154. sort.Slice(candidates, func(i, j int) bool {
  155. a := candidates[i]
  156. b := candidates[j]
  157. switch {
  158. case a.Height > b.Height:
  159. return true
  160. case a.Height < b.Height:
  161. return false
  162. case a.Format > b.Format:
  163. return true
  164. case a.Format < b.Format:
  165. return false
  166. case len(p.snapshotPeers[a.Key()]) > len(p.snapshotPeers[b.Key()]):
  167. return true
  168. default:
  169. return false
  170. }
  171. })
  172. return candidates
  173. }
  174. // Reject rejects a snapshot. Rejected snapshots will never be used again.
  175. func (p *snapshotPool) Reject(snapshot *snapshot) {
  176. key := snapshot.Key()
  177. p.Lock()
  178. defer p.Unlock()
  179. p.snapshotBlacklist[key] = true
  180. p.removeSnapshot(key)
  181. }
  182. // RejectFormat rejects a snapshot format. It will never be used again.
  183. func (p *snapshotPool) RejectFormat(format uint32) {
  184. p.Lock()
  185. defer p.Unlock()
  186. p.formatBlacklist[format] = true
  187. for key := range p.formatIndex[format] {
  188. p.removeSnapshot(key)
  189. }
  190. }
  191. // RejectPeer rejects a peer. It will never be used again.
  192. func (p *snapshotPool) RejectPeer(peerID p2p.PeerID) {
  193. if len(peerID) == 0 {
  194. return
  195. }
  196. p.Lock()
  197. defer p.Unlock()
  198. p.removePeer(peerID)
  199. p.peerBlacklist[peerID.String()] = true
  200. }
  201. // RemovePeer removes a peer from the pool, and any snapshots that no longer have peers.
  202. func (p *snapshotPool) RemovePeer(peerID p2p.PeerID) {
  203. p.Lock()
  204. defer p.Unlock()
  205. p.removePeer(peerID)
  206. }
  207. // removePeer removes a peer. The caller must hold the mutex lock.
  208. func (p *snapshotPool) removePeer(peerID p2p.PeerID) {
  209. for key := range p.peerIndex[peerID.String()] {
  210. delete(p.snapshotPeers[key], peerID.String())
  211. if len(p.snapshotPeers[key]) == 0 {
  212. p.removeSnapshot(key)
  213. }
  214. }
  215. delete(p.peerIndex, peerID.String())
  216. }
  217. // removeSnapshot removes a snapshot. The caller must hold the mutex lock.
  218. func (p *snapshotPool) removeSnapshot(key snapshotKey) {
  219. snapshot := p.snapshots[key]
  220. if snapshot == nil {
  221. return
  222. }
  223. delete(p.snapshots, key)
  224. delete(p.formatIndex[snapshot.Format], key)
  225. delete(p.heightIndex[snapshot.Height], key)
  226. for peerID := range p.snapshotPeers[key] {
  227. delete(p.peerIndex[peerID], key)
  228. }
  229. delete(p.snapshotPeers, key)
  230. }