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.

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