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.

163 lines
4.1 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
  1. // Copyright 2017 Tendermint. All rights reserved.
  2. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
  3. package trust
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. dbm "github.com/tendermint/tm-db"
  10. "github.com/tendermint/tendermint/libs/log"
  11. )
  12. func TestTrustMetricStoreSaveLoad(t *testing.T) {
  13. dir := t.TempDir()
  14. historyDB, err := dbm.NewDB("trusthistory", "goleveldb", dir)
  15. require.NoError(t, err)
  16. // 0 peers saved
  17. store := NewTrustMetricStore(historyDB, DefaultConfig())
  18. store.SetLogger(log.TestingLogger())
  19. store.saveToDB()
  20. // Load the data from the file
  21. store = NewTrustMetricStore(historyDB, DefaultConfig())
  22. store.SetLogger(log.TestingLogger())
  23. err = store.Start()
  24. require.NoError(t, err)
  25. // Make sure we still have 0 entries
  26. assert.Zero(t, store.Size())
  27. // 100 TestTickers
  28. var tt []*TestTicker
  29. for i := 0; i < 100; i++ {
  30. // The TestTicker will provide manual control over
  31. // the passing of time within the metric
  32. tt = append(tt, NewTestTicker())
  33. }
  34. // 100 peers
  35. for i := 0; i < 100; i++ {
  36. key := fmt.Sprintf("peer_%d", i)
  37. tm := NewMetric()
  38. tm.SetTicker(tt[i])
  39. err = tm.Start()
  40. require.NoError(t, err)
  41. store.AddPeerTrustMetric(key, tm)
  42. tm.BadEvents(10)
  43. tm.GoodEvents(1)
  44. }
  45. // Check that we have 100 entries and save
  46. assert.Equal(t, 100, store.Size())
  47. // Give the 100 metrics time to process the history data
  48. for i := 0; i < 100; i++ {
  49. tt[i].NextTick()
  50. tt[i].NextTick()
  51. }
  52. // Stop all the trust metrics and save
  53. err = store.Stop()
  54. require.NoError(t, err)
  55. // Load the data from the DB
  56. store = NewTrustMetricStore(historyDB, DefaultConfig())
  57. store.SetLogger(log.TestingLogger())
  58. err = store.Start()
  59. require.NoError(t, err)
  60. // Check that we still have 100 peers with imperfect trust values
  61. assert.Equal(t, 100, store.Size())
  62. for _, tm := range store.peerMetrics {
  63. assert.NotEqual(t, 1.0, tm.TrustValue())
  64. }
  65. err = store.Stop()
  66. require.NoError(t, err)
  67. }
  68. func TestTrustMetricStoreConfig(t *testing.T) {
  69. historyDB, err := dbm.NewDB("", "memdb", "")
  70. require.NoError(t, err)
  71. config := MetricConfig{
  72. ProportionalWeight: 0.5,
  73. IntegralWeight: 0.5,
  74. }
  75. // Create a store with custom config
  76. store := NewTrustMetricStore(historyDB, config)
  77. store.SetLogger(log.TestingLogger())
  78. err = store.Start()
  79. require.NoError(t, err)
  80. // Have the store make us a metric with the config
  81. tm := store.GetPeerTrustMetric("TestKey")
  82. // Check that the options made it to the metric
  83. assert.Equal(t, 0.5, tm.proportionalWeight)
  84. assert.Equal(t, 0.5, tm.integralWeight)
  85. err = store.Stop()
  86. require.NoError(t, err)
  87. }
  88. func TestTrustMetricStoreLookup(t *testing.T) {
  89. historyDB, err := dbm.NewDB("", "memdb", "")
  90. require.NoError(t, err)
  91. store := NewTrustMetricStore(historyDB, DefaultConfig())
  92. store.SetLogger(log.TestingLogger())
  93. err = store.Start()
  94. require.NoError(t, err)
  95. // Create 100 peers in the trust metric store
  96. for i := 0; i < 100; i++ {
  97. key := fmt.Sprintf("peer_%d", i)
  98. store.GetPeerTrustMetric(key)
  99. // Check that the trust metric was successfully entered
  100. ktm := store.peerMetrics[key]
  101. assert.NotNil(t, ktm, "Expected to find TrustMetric %s but wasn't there.", key)
  102. }
  103. err = store.Stop()
  104. require.NoError(t, err)
  105. }
  106. func TestTrustMetricStorePeerScore(t *testing.T) {
  107. historyDB, err := dbm.NewDB("", "memdb", "")
  108. require.NoError(t, err)
  109. store := NewTrustMetricStore(historyDB, DefaultConfig())
  110. store.SetLogger(log.TestingLogger())
  111. err = store.Start()
  112. require.NoError(t, err)
  113. key := "TestKey"
  114. tm := store.GetPeerTrustMetric(key)
  115. // This peer is innocent so far
  116. first := tm.TrustScore()
  117. assert.Equal(t, 100, first)
  118. // Add some undesirable events and disconnect
  119. tm.BadEvents(1)
  120. first = tm.TrustScore()
  121. assert.NotEqual(t, 100, first)
  122. tm.BadEvents(10)
  123. second := tm.TrustScore()
  124. if second > first {
  125. t.Errorf("a greater number of bad events should lower the trust score")
  126. }
  127. store.PeerDisconnected(key)
  128. // We will remember our experiences with this peer
  129. tm = store.GetPeerTrustMetric(key)
  130. assert.NotEqual(t, 100, tm.TrustScore())
  131. err = store.Stop()
  132. require.NoError(t, err)
  133. }