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.

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