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.

299 lines
8.3 KiB

lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
  1. package lite
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. dbm "github.com/tendermint/tm-db"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. log "github.com/tendermint/tendermint/libs/log"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. const testChainID = "inquiry-test"
  14. func TestInquirerValidPath(t *testing.T) {
  15. assert, require := assert.New(t), require.New(t)
  16. trust := NewDBProvider("trust", dbm.NewMemDB())
  17. source := NewDBProvider("source", dbm.NewMemDB())
  18. // Set up the validators to generate test blocks.
  19. var vote int64 = 10
  20. keys := genPrivKeys(5)
  21. nkeys := keys.Extend(1)
  22. // Construct a bunch of commits, each with one more height than the last.
  23. chainID := testChainID
  24. consHash := []byte("params")
  25. resHash := []byte("results")
  26. count := 50
  27. fcz := make([]FullCommit, count)
  28. for i := 0; i < count; i++ {
  29. vals := keys.ToValidators(vote, 0)
  30. nextVals := nkeys.ToValidators(vote, 0)
  31. h := int64(1 + i)
  32. appHash := []byte(fmt.Sprintf("h=%d", h))
  33. fcz[i] = keys.GenFullCommit(
  34. chainID, h, nil,
  35. vals, nextVals,
  36. appHash, consHash, resHash, 0, len(keys))
  37. // Extend the keys by 1 each time.
  38. keys = nkeys
  39. nkeys = nkeys.Extend(1)
  40. }
  41. // Initialize a Verifier with the initial state.
  42. err := trust.SaveFullCommit(fcz[0])
  43. require.Nil(err)
  44. cert := NewDynamicVerifier(chainID, trust, source)
  45. cert.SetLogger(log.TestingLogger())
  46. // This should fail validation:
  47. sh := fcz[count-1].SignedHeader
  48. err = cert.Verify(sh)
  49. require.NotNil(err)
  50. // Adding a few commits in the middle should be insufficient.
  51. for i := 10; i < 13; i++ {
  52. err := source.SaveFullCommit(fcz[i])
  53. require.Nil(err)
  54. }
  55. err = cert.Verify(sh)
  56. assert.NotNil(err)
  57. // With more info, we succeed.
  58. for i := 0; i < count; i++ {
  59. err := source.SaveFullCommit(fcz[i])
  60. require.Nil(err)
  61. }
  62. // TODO: Requires proposer address to be set in header.
  63. // err = cert.Verify(sh)
  64. // assert.Nil(err, "%+v", err)
  65. }
  66. func TestDynamicVerify(t *testing.T) {
  67. trust := NewDBProvider("trust", dbm.NewMemDB())
  68. source := NewDBProvider("source", dbm.NewMemDB())
  69. // 10 commits with one valset, 1 to change,
  70. // 10 commits with the next one
  71. n1, n2 := 10, 10
  72. nCommits := n1 + n2 + 1
  73. maxHeight := int64(nCommits)
  74. fcz := make([]FullCommit, nCommits)
  75. // gen the 2 val sets
  76. chainID := "dynamic-verifier"
  77. power := int64(10)
  78. keys1 := genPrivKeys(5)
  79. vals1 := keys1.ToValidators(power, 0)
  80. keys2 := genPrivKeys(5)
  81. vals2 := keys2.ToValidators(power, 0)
  82. // make some commits with the first
  83. for i := 0; i < n1; i++ {
  84. fcz[i] = makeFullCommit(int64(i), keys1, vals1, vals1, chainID)
  85. }
  86. // update the val set
  87. fcz[n1] = makeFullCommit(int64(n1), keys1, vals1, vals2, chainID)
  88. // make some commits with the new one
  89. for i := n1 + 1; i < nCommits; i++ {
  90. fcz[i] = makeFullCommit(int64(i), keys2, vals2, vals2, chainID)
  91. }
  92. // Save everything in the source
  93. for _, fc := range fcz {
  94. source.SaveFullCommit(fc)
  95. }
  96. // Initialize a Verifier with the initial state.
  97. err := trust.SaveFullCommit(fcz[0])
  98. require.Nil(t, err)
  99. ver := NewDynamicVerifier(chainID, trust, source)
  100. ver.SetLogger(log.TestingLogger())
  101. // fetch the latest from the source
  102. _, err = source.LatestFullCommit(chainID, 1, maxHeight)
  103. require.NoError(t, err)
  104. // TODO: Requires proposer address to be set in header.
  105. // try to update to the latest
  106. // err = ver.Verify(latestFC.SignedHeader)
  107. // require.NoError(t, err)
  108. }
  109. func makeFullCommit(height int64, keys privKeys, vals, nextVals *types.ValidatorSet, chainID string) FullCommit {
  110. height++
  111. consHash := tmhash.Sum([]byte("special-params"))
  112. appHash := tmhash.Sum([]byte(fmt.Sprintf("h=%d", height)))
  113. resHash := tmhash.Sum([]byte(fmt.Sprintf("res=%d", height)))
  114. return keys.GenFullCommit(
  115. chainID, height, nil,
  116. vals, nextVals,
  117. appHash, consHash, resHash, 0, len(keys),
  118. )
  119. }
  120. func TestInquirerVerifyHistorical(t *testing.T) {
  121. assert, require := assert.New(t), require.New(t)
  122. trust := NewDBProvider("trust", dbm.NewMemDB())
  123. source := NewDBProvider("source", dbm.NewMemDB())
  124. // Set up the validators to generate test blocks.
  125. var vote int64 = 10
  126. keys := genPrivKeys(5)
  127. nkeys := keys.Extend(1)
  128. // Construct a bunch of commits, each with one more height than the last.
  129. chainID := testChainID
  130. count := 10
  131. consHash := []byte("special-params")
  132. fcz := make([]FullCommit, count)
  133. for i := 0; i < count; i++ {
  134. vals := keys.ToValidators(vote, 0)
  135. nextVals := nkeys.ToValidators(vote, 0)
  136. h := int64(1 + i)
  137. appHash := []byte(fmt.Sprintf("h=%d", h))
  138. resHash := []byte(fmt.Sprintf("res=%d", h))
  139. fcz[i] = keys.GenFullCommit(
  140. chainID, h, nil,
  141. vals, nextVals,
  142. appHash, consHash, resHash, 0, len(keys))
  143. // Extend the keys by 1 each time.
  144. keys = nkeys
  145. nkeys = nkeys.Extend(1)
  146. }
  147. // Initialize a Verifier with the initial state.
  148. err := trust.SaveFullCommit(fcz[0])
  149. require.Nil(err)
  150. cert := NewDynamicVerifier(chainID, trust, source)
  151. cert.SetLogger(log.TestingLogger())
  152. // Store a few full commits as trust.
  153. for _, i := range []int{2, 5} {
  154. trust.SaveFullCommit(fcz[i])
  155. }
  156. // See if we can jump forward using trusted full commits.
  157. // Souce doesn't have fcz[9] so cert.LastTrustedHeight wont' change.
  158. err = source.SaveFullCommit(fcz[7])
  159. require.Nil(err, "%+v", err)
  160. // TODO: Requires proposer address to be set in header.
  161. // sh := fcz[8].SignedHeader
  162. // err = cert.Verify(sh)
  163. // require.Nil(err, "%+v", err)
  164. // assert.Equal(fcz[7].Height(), cert.LastTrustedHeight())
  165. commit, err := trust.LatestFullCommit(chainID, fcz[8].Height(), fcz[8].Height())
  166. require.NotNil(err, "%+v", err)
  167. assert.Equal(commit, (FullCommit{}))
  168. // With fcz[9] Verify will update last trusted height.
  169. err = source.SaveFullCommit(fcz[9])
  170. require.Nil(err, "%+v", err)
  171. // TODO: Requires proposer address to be set in header.
  172. // sh = fcz[8].SignedHeader
  173. // err = cert.Verify(sh)
  174. // require.Nil(err, "%+v", err)
  175. // assert.Equal(fcz[8].Height(), cert.LastTrustedHeight())
  176. // TODO: Requires proposer address to be set in header.
  177. // commit, err = trust.LatestFullCommit(chainID, fcz[8].Height(), fcz[8].Height())
  178. // require.Nil(err, "%+v", err)
  179. // assert.Equal(commit.Height(), fcz[8].Height())
  180. // Add access to all full commits via untrusted source.
  181. for i := 0; i < count; i++ {
  182. err := source.SaveFullCommit(fcz[i])
  183. require.Nil(err)
  184. }
  185. // TODO: Requires proposer address to be set in header.
  186. // Try to check an unknown seed in the past.
  187. // sh = fcz[3].SignedHeader
  188. // err = cert.Verify(sh)
  189. // require.Nil(err, "%+v", err)
  190. // assert.Equal(fcz[8].Height(), cert.LastTrustedHeight())
  191. // TODO: Requires proposer address to be set in header.
  192. // Jump all the way forward again.
  193. // sh = fcz[count-1].SignedHeader
  194. // err = cert.Verify(sh)
  195. // require.Nil(err, "%+v", err)
  196. // assert.Equal(fcz[9].Height(), cert.LastTrustedHeight())
  197. }
  198. func TestConcurrencyInquirerVerify(t *testing.T) {
  199. _, require := assert.New(t), require.New(t)
  200. trust := NewDBProvider("trust", dbm.NewMemDB()).SetLimit(10)
  201. source := NewDBProvider("source", dbm.NewMemDB())
  202. // Set up the validators to generate test blocks.
  203. var vote int64 = 10
  204. keys := genPrivKeys(5)
  205. nkeys := keys.Extend(1)
  206. // Construct a bunch of commits, each with one more height than the last.
  207. chainID := testChainID
  208. count := 10
  209. consHash := []byte("special-params")
  210. fcz := make([]FullCommit, count)
  211. for i := 0; i < count; i++ {
  212. vals := keys.ToValidators(vote, 0)
  213. nextVals := nkeys.ToValidators(vote, 0)
  214. h := int64(1 + i)
  215. appHash := []byte(fmt.Sprintf("h=%d", h))
  216. resHash := []byte(fmt.Sprintf("res=%d", h))
  217. fcz[i] = keys.GenFullCommit(
  218. chainID, h, nil,
  219. vals, nextVals,
  220. appHash, consHash, resHash, 0, len(keys))
  221. // Extend the keys by 1 each time.
  222. keys = nkeys
  223. nkeys = nkeys.Extend(1)
  224. }
  225. // Initialize a Verifier with the initial state.
  226. err := trust.SaveFullCommit(fcz[0])
  227. require.Nil(err)
  228. cert := NewDynamicVerifier(chainID, trust, source)
  229. cert.SetLogger(log.TestingLogger())
  230. err = source.SaveFullCommit(fcz[7])
  231. require.Nil(err, "%+v", err)
  232. err = source.SaveFullCommit(fcz[8])
  233. require.Nil(err, "%+v", err)
  234. sh := fcz[8].SignedHeader
  235. var wg sync.WaitGroup
  236. count = 100
  237. errList := make([]error, count)
  238. for i := 0; i < count; i++ {
  239. wg.Add(1)
  240. go func(index int) {
  241. errList[index] = cert.Verify(sh)
  242. defer wg.Done()
  243. }(i)
  244. }
  245. wg.Wait()
  246. // TODO: Requires proposer address to be set in header.
  247. // for _, err := range errList {
  248. // require.Nil(err)
  249. // }
  250. }