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