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.

324 lines
9.2 KiB

  1. package statesync
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/mock"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tendermint/tendermint/p2p"
  7. "github.com/tendermint/tendermint/statesync/mocks"
  8. )
  9. func TestSnapshot_Key(t *testing.T) {
  10. testcases := map[string]struct {
  11. modify func(*snapshot)
  12. }{
  13. "new height": {func(s *snapshot) { s.Height = 9 }},
  14. "new format": {func(s *snapshot) { s.Format = 9 }},
  15. "new chunk count": {func(s *snapshot) { s.Chunks = 9 }},
  16. "new hash": {func(s *snapshot) { s.Hash = []byte{9} }},
  17. "no metadata": {func(s *snapshot) { s.Metadata = nil }},
  18. }
  19. for name, tc := range testcases {
  20. tc := tc
  21. t.Run(name, func(t *testing.T) {
  22. s := snapshot{
  23. Height: 3,
  24. Format: 1,
  25. Chunks: 7,
  26. Hash: []byte{1, 2, 3},
  27. Metadata: []byte{255},
  28. }
  29. before := s.Key()
  30. tc.modify(&s)
  31. after := s.Key()
  32. require.NotEqual(t, before, after)
  33. })
  34. }
  35. }
  36. func TestSnapshotPool_Add(t *testing.T) {
  37. stateProvider := &mocks.StateProvider{}
  38. stateProvider.On("AppHash", mock.Anything, uint64(1)).Return([]byte("app_hash"), nil)
  39. peerID := p2p.NodeID("aa")
  40. // Adding to the pool should work
  41. pool := newSnapshotPool(stateProvider)
  42. added, err := pool.Add(peerID, &snapshot{
  43. Height: 1,
  44. Format: 1,
  45. Chunks: 1,
  46. Hash: []byte{1},
  47. })
  48. require.NoError(t, err)
  49. require.True(t, added)
  50. // Adding again from a different peer should return false
  51. otherNodeID := p2p.NodeID("bb")
  52. added, err = pool.Add(otherNodeID, &snapshot{
  53. Height: 1,
  54. Format: 1,
  55. Chunks: 1,
  56. Hash: []byte{1},
  57. })
  58. require.NoError(t, err)
  59. require.False(t, added)
  60. // The pool should have populated the snapshot with the trusted app hash
  61. snapshot := pool.Best()
  62. require.NotNil(t, snapshot)
  63. require.Equal(t, []byte("app_hash"), snapshot.trustedAppHash)
  64. stateProvider.AssertExpectations(t)
  65. }
  66. func TestSnapshotPool_GetPeer(t *testing.T) {
  67. stateProvider := &mocks.StateProvider{}
  68. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  69. pool := newSnapshotPool(stateProvider)
  70. s := &snapshot{Height: 1, Format: 1, Chunks: 1, Hash: []byte{1}}
  71. peerAID := p2p.NodeID("aa")
  72. peerBID := p2p.NodeID("bb")
  73. _, err := pool.Add(peerAID, s)
  74. require.NoError(t, err)
  75. _, err = pool.Add(peerBID, s)
  76. require.NoError(t, err)
  77. _, err = pool.Add(peerAID, &snapshot{Height: 2, Format: 1, Chunks: 1, Hash: []byte{1}})
  78. require.NoError(t, err)
  79. // GetPeer currently picks a random peer, so lets run it until we've seen both.
  80. seenA := false
  81. seenB := false
  82. for !seenA || !seenB {
  83. peer := pool.GetPeer(s)
  84. if peer == peerAID {
  85. seenA = true
  86. }
  87. if peer == peerBID {
  88. seenB = true
  89. }
  90. }
  91. // GetPeer should return empty for an unknown snapshot
  92. peer := pool.GetPeer(&snapshot{Height: 9, Format: 9})
  93. require.EqualValues(t, "", peer)
  94. }
  95. func TestSnapshotPool_GetPeers(t *testing.T) {
  96. stateProvider := &mocks.StateProvider{}
  97. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  98. pool := newSnapshotPool(stateProvider)
  99. s := &snapshot{Height: 1, Format: 1, Chunks: 1, Hash: []byte{1}}
  100. peerAID := p2p.NodeID("aa")
  101. peerBID := p2p.NodeID("bb")
  102. _, err := pool.Add(peerAID, s)
  103. require.NoError(t, err)
  104. _, err = pool.Add(peerBID, s)
  105. require.NoError(t, err)
  106. _, err = pool.Add(peerAID, &snapshot{Height: 2, Format: 1, Chunks: 1, Hash: []byte{2}})
  107. require.NoError(t, err)
  108. peers := pool.GetPeers(s)
  109. require.Len(t, peers, 2)
  110. require.Equal(t, peerAID, peers[0])
  111. require.EqualValues(t, peerBID, peers[1])
  112. }
  113. func TestSnapshotPool_Ranked_Best(t *testing.T) {
  114. stateProvider := &mocks.StateProvider{}
  115. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  116. pool := newSnapshotPool(stateProvider)
  117. // snapshots in expected order (best to worst). Highest height wins, then highest format.
  118. // Snapshots with different chunk hashes are considered different, and the most peers is
  119. // tie-breaker.
  120. expectSnapshots := []struct {
  121. snapshot *snapshot
  122. peers []p2p.NodeID
  123. }{
  124. {&snapshot{Height: 2, Format: 2, Chunks: 4, Hash: []byte{1, 3}}, []p2p.NodeID{"AA", "BB", "CC", "DD"}},
  125. {&snapshot{Height: 1, Format: 1, Chunks: 4, Hash: []byte{1, 2}}, []p2p.NodeID{"AA", "BB", "CC", "DD"}},
  126. {&snapshot{Height: 2, Format: 2, Chunks: 5, Hash: []byte{1, 2}}, []p2p.NodeID{"AA", "BB", "CC"}},
  127. {&snapshot{Height: 2, Format: 1, Chunks: 3, Hash: []byte{1, 2}}, []p2p.NodeID{"AA", "BB", "CC"}},
  128. {&snapshot{Height: 1, Format: 2, Chunks: 5, Hash: []byte{1, 2}}, []p2p.NodeID{"AA", "BB", "CC"}},
  129. }
  130. // Add snapshots in reverse order, to make sure the pool enforces some order.
  131. for i := len(expectSnapshots) - 1; i >= 0; i-- {
  132. for _, peerID := range expectSnapshots[i].peers {
  133. _, err := pool.Add(peerID, expectSnapshots[i].snapshot)
  134. require.NoError(t, err)
  135. }
  136. }
  137. // Ranked should return the snapshots in the same order
  138. ranked := pool.Ranked()
  139. require.Len(t, ranked, len(expectSnapshots))
  140. for i := range ranked {
  141. require.Equal(t, expectSnapshots[i].snapshot, ranked[i])
  142. }
  143. // Check that best snapshots are returned in expected order
  144. for i := range expectSnapshots {
  145. snapshot := expectSnapshots[i].snapshot
  146. require.Equal(t, snapshot, pool.Best())
  147. pool.Reject(snapshot)
  148. }
  149. require.Nil(t, pool.Best())
  150. }
  151. func TestSnapshotPool_Reject(t *testing.T) {
  152. stateProvider := &mocks.StateProvider{}
  153. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  154. pool := newSnapshotPool(stateProvider)
  155. peerID := p2p.NodeID("aa")
  156. snapshots := []*snapshot{
  157. {Height: 2, Format: 2, Chunks: 1, Hash: []byte{1, 2}},
  158. {Height: 2, Format: 1, Chunks: 1, Hash: []byte{1, 2}},
  159. {Height: 1, Format: 2, Chunks: 1, Hash: []byte{1, 2}},
  160. {Height: 1, Format: 1, Chunks: 1, Hash: []byte{1, 2}},
  161. }
  162. for _, s := range snapshots {
  163. _, err := pool.Add(peerID, s)
  164. require.NoError(t, err)
  165. }
  166. pool.Reject(snapshots[0])
  167. require.Equal(t, snapshots[1:], pool.Ranked())
  168. added, err := pool.Add(peerID, snapshots[0])
  169. require.NoError(t, err)
  170. require.False(t, added)
  171. added, err = pool.Add(peerID, &snapshot{Height: 3, Format: 3, Chunks: 1, Hash: []byte{1}})
  172. require.NoError(t, err)
  173. require.True(t, added)
  174. }
  175. func TestSnapshotPool_RejectFormat(t *testing.T) {
  176. stateProvider := &mocks.StateProvider{}
  177. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  178. pool := newSnapshotPool(stateProvider)
  179. peerID := p2p.NodeID("aa")
  180. snapshots := []*snapshot{
  181. {Height: 2, Format: 2, Chunks: 1, Hash: []byte{1, 2}},
  182. {Height: 2, Format: 1, Chunks: 1, Hash: []byte{1, 2}},
  183. {Height: 1, Format: 2, Chunks: 1, Hash: []byte{1, 2}},
  184. {Height: 1, Format: 1, Chunks: 1, Hash: []byte{1, 2}},
  185. }
  186. for _, s := range snapshots {
  187. _, err := pool.Add(peerID, s)
  188. require.NoError(t, err)
  189. }
  190. pool.RejectFormat(1)
  191. require.Equal(t, []*snapshot{snapshots[0], snapshots[2]}, pool.Ranked())
  192. added, err := pool.Add(peerID, &snapshot{Height: 3, Format: 1, Chunks: 1, Hash: []byte{1}})
  193. require.NoError(t, err)
  194. require.False(t, added)
  195. require.Equal(t, []*snapshot{snapshots[0], snapshots[2]}, pool.Ranked())
  196. added, err = pool.Add(peerID, &snapshot{Height: 3, Format: 3, Chunks: 1, Hash: []byte{1}})
  197. require.NoError(t, err)
  198. require.True(t, added)
  199. }
  200. func TestSnapshotPool_RejectPeer(t *testing.T) {
  201. stateProvider := &mocks.StateProvider{}
  202. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  203. pool := newSnapshotPool(stateProvider)
  204. peerAID := p2p.NodeID("aa")
  205. peerBID := p2p.NodeID("bb")
  206. s1 := &snapshot{Height: 1, Format: 1, Chunks: 1, Hash: []byte{1}}
  207. s2 := &snapshot{Height: 2, Format: 1, Chunks: 1, Hash: []byte{2}}
  208. s3 := &snapshot{Height: 3, Format: 1, Chunks: 1, Hash: []byte{2}}
  209. _, err := pool.Add(peerAID, s1)
  210. require.NoError(t, err)
  211. _, err = pool.Add(peerAID, s2)
  212. require.NoError(t, err)
  213. _, err = pool.Add(peerBID, s2)
  214. require.NoError(t, err)
  215. _, err = pool.Add(peerBID, s3)
  216. require.NoError(t, err)
  217. pool.RejectPeer(peerAID)
  218. require.Empty(t, pool.GetPeers(s1))
  219. peers2 := pool.GetPeers(s2)
  220. require.Len(t, peers2, 1)
  221. require.Equal(t, peerBID, peers2[0])
  222. peers3 := pool.GetPeers(s2)
  223. require.Len(t, peers3, 1)
  224. require.Equal(t, peerBID, peers3[0])
  225. // it should no longer be possible to add the peer back
  226. _, err = pool.Add(peerAID, s1)
  227. require.NoError(t, err)
  228. require.Empty(t, pool.GetPeers(s1))
  229. }
  230. func TestSnapshotPool_RemovePeer(t *testing.T) {
  231. stateProvider := &mocks.StateProvider{}
  232. stateProvider.On("AppHash", mock.Anything, mock.Anything).Return([]byte("app_hash"), nil)
  233. pool := newSnapshotPool(stateProvider)
  234. peerAID := p2p.NodeID("aa")
  235. peerBID := p2p.NodeID("bb")
  236. s1 := &snapshot{Height: 1, Format: 1, Chunks: 1, Hash: []byte{1}}
  237. s2 := &snapshot{Height: 2, Format: 1, Chunks: 1, Hash: []byte{2}}
  238. _, err := pool.Add(peerAID, s1)
  239. require.NoError(t, err)
  240. _, err = pool.Add(peerAID, s2)
  241. require.NoError(t, err)
  242. _, err = pool.Add(peerBID, s1)
  243. require.NoError(t, err)
  244. pool.RemovePeer(peerAID)
  245. peers1 := pool.GetPeers(s1)
  246. require.Len(t, peers1, 1)
  247. require.Equal(t, peerBID, peers1[0])
  248. peers2 := pool.GetPeers(s2)
  249. require.Empty(t, peers2)
  250. // it should still be possible to add the peer back
  251. _, err = pool.Add(peerAID, s1)
  252. require.NoError(t, err)
  253. peers1 = pool.GetPeers(s1)
  254. require.Len(t, peers1, 2)
  255. require.Equal(t, peerAID, peers1[0])
  256. require.Equal(t, peerBID, peers1[1])
  257. }