github.com/number571/tendermint@v0.34.11-gost/internal/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  	dbm "github.com/tendermint/tm-db"
    13  
    14  	"github.com/number571/tendermint/libs/log"
    15  )
    16  
    17  func TestTrustMetricStoreSaveLoad(t *testing.T) {
    18  	dir := t.TempDir()
    19  
    20  	historyDB, err := dbm.NewDB("trusthistory", "goleveldb", dir)
    21  	require.NoError(t, err)
    22  
    23  	// 0 peers saved
    24  	store := NewTrustMetricStore(historyDB, DefaultConfig())
    25  	store.SetLogger(log.TestingLogger())
    26  	store.saveToDB()
    27  	// Load the data from the file
    28  	store = NewTrustMetricStore(historyDB, DefaultConfig())
    29  	store.SetLogger(log.TestingLogger())
    30  	err = store.Start()
    31  	require.NoError(t, err)
    32  	// Make sure we still have 0 entries
    33  	assert.Zero(t, store.Size())
    34  
    35  	// 100 TestTickers
    36  	var tt []*TestTicker
    37  	for i := 0; i < 100; i++ {
    38  		// The TestTicker will provide manual control over
    39  		// the passing of time within the metric
    40  		tt = append(tt, NewTestTicker())
    41  	}
    42  	// 100 peers
    43  	for i := 0; i < 100; i++ {
    44  		key := fmt.Sprintf("peer_%d", i)
    45  		tm := NewMetric()
    46  
    47  		tm.SetTicker(tt[i])
    48  		err = tm.Start()
    49  		require.NoError(t, err)
    50  		store.AddPeerTrustMetric(key, tm)
    51  
    52  		tm.BadEvents(10)
    53  		tm.GoodEvents(1)
    54  	}
    55  	// Check that we have 100 entries and save
    56  	assert.Equal(t, 100, store.Size())
    57  	// Give the 100 metrics time to process the history data
    58  	for i := 0; i < 100; i++ {
    59  		tt[i].NextTick()
    60  		tt[i].NextTick()
    61  	}
    62  	// Stop all the trust metrics and save
    63  	err = store.Stop()
    64  	require.NoError(t, err)
    65  
    66  	// Load the data from the DB
    67  	store = NewTrustMetricStore(historyDB, DefaultConfig())
    68  	store.SetLogger(log.TestingLogger())
    69  	err = store.Start()
    70  	require.NoError(t, err)
    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  	err = store.Stop()
    79  	require.NoError(t, err)
    80  }
    81  
    82  func TestTrustMetricStoreConfig(t *testing.T) {
    83  	historyDB, err := dbm.NewDB("", "memdb", "")
    84  	require.NoError(t, err)
    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, err := dbm.NewDB("", "memdb", "")
   109  	require.NoError(t, err)
   110  
   111  	store := NewTrustMetricStore(historyDB, DefaultConfig())
   112  	store.SetLogger(log.TestingLogger())
   113  	err = store.Start()
   114  	require.NoError(t, err)
   115  
   116  	// Create 100 peers in the trust metric store
   117  	for i := 0; i < 100; i++ {
   118  		key := fmt.Sprintf("peer_%d", i)
   119  		store.GetPeerTrustMetric(key)
   120  
   121  		// Check that the trust metric was successfully entered
   122  		ktm := store.peerMetrics[key]
   123  		assert.NotNil(t, ktm, "Expected to find TrustMetric %s but wasn't there.", key)
   124  	}
   125  
   126  	err = store.Stop()
   127  	require.NoError(t, err)
   128  }
   129  
   130  func TestTrustMetricStorePeerScore(t *testing.T) {
   131  	historyDB, err := dbm.NewDB("", "memdb", "")
   132  	require.NoError(t, err)
   133  
   134  	store := NewTrustMetricStore(historyDB, DefaultConfig())
   135  	store.SetLogger(log.TestingLogger())
   136  	err = store.Start()
   137  	require.NoError(t, err)
   138  
   139  	key := "TestKey"
   140  	tm := store.GetPeerTrustMetric(key)
   141  
   142  	// This peer is innocent so far
   143  	first := tm.TrustScore()
   144  	assert.Equal(t, 100, first)
   145  
   146  	// Add some undesirable events and disconnect
   147  	tm.BadEvents(1)
   148  	first = tm.TrustScore()
   149  	assert.NotEqual(t, 100, first)
   150  	tm.BadEvents(10)
   151  	second := tm.TrustScore()
   152  
   153  	if second > first {
   154  		t.Errorf("a greater number of bad events should lower the trust score")
   155  	}
   156  	store.PeerDisconnected(key)
   157  
   158  	// We will remember our experiences with this peer
   159  	tm = store.GetPeerTrustMetric(key)
   160  	assert.NotEqual(t, 100, tm.TrustScore())
   161  	err = store.Stop()
   162  	require.NoError(t, err)
   163  }