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