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.

152 lines
3.7 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/tendermint/tendermint/libs/log"
  11. dbm "github.com/tendermint/tm-db"
  12. )
  13. func TestTrustMetricStoreSaveLoad(t *testing.T) {
  14. dir, err := ioutil.TempDir("", "trust_test")
  15. if err != nil {
  16. panic(err)
  17. }
  18. defer os.Remove(dir)
  19. historyDB := dbm.NewDB("trusthistory", "goleveldb", dir)
  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 := dbm.NewDB("", "memdb", "")
  69. config := MetricConfig{
  70. ProportionalWeight: 0.5,
  71. IntegralWeight: 0.5,
  72. }
  73. // Create a store with custom config
  74. store := NewTrustMetricStore(historyDB, config)
  75. store.SetLogger(log.TestingLogger())
  76. store.Start()
  77. // Have the store make us a metric with the config
  78. tm := store.GetPeerTrustMetric("TestKey")
  79. // Check that the options made it to the metric
  80. assert.Equal(t, 0.5, tm.proportionalWeight)
  81. assert.Equal(t, 0.5, tm.integralWeight)
  82. store.Stop()
  83. }
  84. func TestTrustMetricStoreLookup(t *testing.T) {
  85. historyDB := dbm.NewDB("", "memdb", "")
  86. store := NewTrustMetricStore(historyDB, DefaultConfig())
  87. store.SetLogger(log.TestingLogger())
  88. store.Start()
  89. // Create 100 peers in the trust metric store
  90. for i := 0; i < 100; i++ {
  91. key := fmt.Sprintf("peer_%d", i)
  92. store.GetPeerTrustMetric(key)
  93. // Check that the trust metric was successfully entered
  94. ktm := store.peerMetrics[key]
  95. assert.NotNil(t, ktm, "Expected to find TrustMetric %s but wasn't there.", key)
  96. }
  97. store.Stop()
  98. }
  99. func TestTrustMetricStorePeerScore(t *testing.T) {
  100. historyDB := dbm.NewDB("", "memdb", "")
  101. store := NewTrustMetricStore(historyDB, DefaultConfig())
  102. store.SetLogger(log.TestingLogger())
  103. store.Start()
  104. key := "TestKey"
  105. tm := store.GetPeerTrustMetric(key)
  106. // This peer is innocent so far
  107. first := tm.TrustScore()
  108. assert.Equal(t, 100, first)
  109. // Add some undesirable events and disconnect
  110. tm.BadEvents(1)
  111. first = tm.TrustScore()
  112. assert.NotEqual(t, 100, first)
  113. tm.BadEvents(10)
  114. second := tm.TrustScore()
  115. if second > first {
  116. t.Errorf("a greater number of bad events should lower the trust score")
  117. }
  118. store.PeerDisconnected(key)
  119. // We will remember our experiences with this peer
  120. tm = store.GetPeerTrustMetric(key)
  121. assert.NotEqual(t, 100, tm.TrustScore())
  122. store.Stop()
  123. }