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