diff --git a/config/config.go b/config/config.go index 46fb55ec6..25d6c44a5 100644 --- a/config/config.go +++ b/config/config.go @@ -214,9 +214,6 @@ type P2PConfig struct { // Set true for strict address routability rules AddrBookStrict bool `mapstructure:"addr_book_strict"` - // Path to the trust history file - TrustHistory string `mapstructure:"trust_history_file"` - // Set true to enable the peer-exchange reactor PexReactor bool `mapstructure:"pex"` @@ -242,7 +239,6 @@ func DefaultP2PConfig() *P2PConfig { ListenAddress: "tcp://0.0.0.0:46656", AddrBook: "addrbook.json", AddrBookStrict: true, - TrustHistory: "trusthistory.json", MaxNumPeers: 50, FlushThrottleTimeout: 100, MaxMsgPacketPayloadSize: 1024, // 1 kB @@ -264,11 +260,6 @@ func (p *P2PConfig) AddrBookFile() string { return rootify(p.AddrBook, p.RootDir) } -// TrustHistoryFile returns the full path to the trust metric store history -func (p *P2PConfig) TrustHistoryFile() string { - return rootify(p.TrustHistory, p.RootDir) -} - //----------------------------------------------------------------------------- // MempoolConfig diff --git a/docs/architecture/adr-006-trust-metric.md b/docs/architecture/adr-006-trust-metric.md index 6fc5f9ea6..961830cf9 100644 --- a/docs/architecture/adr-006-trust-metric.md +++ b/docs/architecture/adr-006-trust-metric.md @@ -1,10 +1,10 @@ -# Trust Metric Design +# ADR 006: Trust Metric Design -## Overview +## Context The proposed trust metric will allow Tendermint to maintain local trust rankings for peers it has directly interacted with, which can then be used to implement soft security controls. The calculations were obtained from the [TrustGuard](https://dl.acm.org/citation.cfm?id=1060808) project. -## Background +### Background The Tendermint Core project developers would like to improve Tendermint security and reliability by keeping track of the level of trustworthiness peers have demonstrated within the peer-to-peer network. This way, undesirable outcomes from peers will not immediately result in them being dropped from the network (potentially causing drastic changes to take place). Instead, peers behavior can be monitored with appropriate metrics and be removed from the network once Tendermint Core is certain the peer is a threat. For example, when the PEXReactor makes a request for peers network addresses from a already known peer, and the returned network addresses are unreachable, this untrustworthy behavior should be tracked. Returning a few bad network addresses probably shouldn’t cause a peer to be dropped, while excessive amounts of this behavior does qualify the peer being dropped. @@ -12,13 +12,15 @@ Trust metrics can be circumvented by malicious nodes through the use of strategi Instead, having shorter intervals, but keeping a history of interval values, will give our metric the flexibility needed in order to keep the network stable, while also making it resilient against a strategic malicious node in the Tendermint peer-to-peer network. Also, the metric can access trust data over a rather long period of time while not greatly increasing its history size by aggregating older history values over a larger number of intervals, and at the same time, maintain great precision for the recent intervals. This approach is referred to as fading memories, and closely resembles the way human beings remember their experiences. The trade-off to using history data is that the interval values should be preserved in-between executions of the node. -## Scope +### References -The proposed trust metric will be implemented as a Go programming language object that will allow a developer to inform the object of all good and bad events relevant to the trust object instantiation, and at any time, the metric can be queried for the current trust ranking. Methods will be provided for storing trust metric history data that is required across instantiations. +S. Mudhakar, L. Xiong, and L. Liu, “TrustGuard: Countering Vulnerabilities in Reputation Management for Decentralized Overlay Networks,” in *Proceedings of the 14th international conference on World Wide Web, pp. 422-431*, May 2005. -## Detailed Design +## Decision -This section will cover the process being considered for calculating the trust ranking and the interface for the trust metric. +The proposed trust metric will allow a developer to inform the trust metric store of all good and bad events relevant to a peer's behavior, and at any time, the metric can be queried for a peer's current trust ranking. + +The three subsections below will cover the process being considered for calculating the trust ranking, the concept of the trust metric store, and the interface for the trust metric. ### Proposed Process @@ -33,7 +35,7 @@ The equation being proposed resembles a Proportional-Integral-Derivative (PID) c where *R*[*i*] denotes the raw trust value at time interval *i* (where *i* == 0 being current time) and *a* is the weight applied to the contribution of the current reports. The next component of our equation uses a weighted sum over the last *maxH* intervals to calculate the history value for time *i*: -`H[i] = ` ![formula1](https://github.com/tendermint/tendermint/blob/develop/docs/architecture/img/formula1.png "Weighted Sum Formula") +`H[i] = ` ![formula1](img/formula1.png "Weighted Sum Formula") The weights can be chosen either optimistically or pessimistically. With the history value available, we can now finish calculating the integral value: @@ -68,75 +70,71 @@ Where *j* is one of *(0, 1, 2, … , m – 1)* indices used to access history in R[0] = raw data for current time interval ``` -`R[j] = ` ![formula2](https://github.com/tendermint/tendermint/blob/develop/docs/architecture/img/formula2.png "Fading Memories Formula") - +`R[j] = ` ![formula2](img/formula2.png "Fading Memories Formula") -### Interface Detailed Design +### Trust Metric Store -This section will cover the Go programming language API designed for the previously proposed process. Below is the interface for a TrustMetric: - -```go +Similar to the P2P subsystem AddrBook, the trust metric store will maintain information relevant to Tendermint peers. Additionally, the trust metric store will ensure that trust metrics will only be active for peers that a node is currently and directly engaged with. -package trust +Reactors will provide a peer key to the trust metric store in order to retrieve the associated trust metric. The trust metric can then record new positive and negative events experienced by the reactor, as well as provided the current trust score calculated by the metric. +When the node is shutting down, the trust metric store will save history data for trust metrics associated with all known peers. This saved information allows experiences with a peer to be preserved across node executions, which can span a tracking windows of days or weeks. The trust history data is loaded automatically during OnStart. -// TrustMetricStore - Manages all trust metrics for peers -type TrustMetricStore struct { - cmn.BaseService - - // Private elements -} - -// OnStart implements Service -func (tms *TrustMetricStore) OnStart() error - -/ OnStop implements Service -func (tms *TrustMetricStore) OnStop() +### Interface Detailed Design -// NewTrustMetricStore returns a store that optionally saves data to -// the file path and uses the optional config when creating new trust metrics -func NewTrustMetricStore(filePath string, tmc *TrustMetricConfig) *TrustMetricStore +Each trust metric allows for the recording of positive/negative events, querying the current trust value/score, and the stopping/pausing of tracking over time intervals. This can be seen below: -// GetPeerTrustMetric returns a trust metric by peer key -func (tms *TrustMetricStore) GetPeerTrustMetric(key string) *TrustMetric - -// PeerDisconnected pauses the trust metric associated with the peer identified by the key -func (tms *TrustMetricStore) PeerDisconnected(key string) +```go -//---------------------------------------------------------------------------------------- // TrustMetric - keeps track of peer reliability type TrustMetric struct { // Private elements. } -// Pause tells the metric to pause recording data over time intervals -func (tm *TrustMetric) Pause() +// Pause tells the metric to pause recording data over time intervals. +// All method calls that indicate events will unpause the metric +func (tm *TrustMetric) Pause() {} // Stop tells the metric to stop recording data over time intervals -func (tm *TrustMetric) Stop() +func (tm *TrustMetric) Stop() {} // BadEvent indicates that an undesirable event took place -func (tm *TrustMetric) BadEvent() +func (tm *TrustMetric) BadEvent() {} // AddBadEvents acknowledges multiple undesirable events -func (tm *TrustMetric) AddBadEvents(num int) +func (tm *TrustMetric) AddBadEvents(num int) {} // GoodEvent indicates that a desirable event took place -func (tm *TrustMetric) GoodEvent() +func (tm *TrustMetric) GoodEvent() {} // AddGoodEvents acknowledges multiple desirable events -func (tm *TrustMetric) AddGoodEvents(num int) +func (tm *TrustMetric) AddGoodEvents(num int) {} // TrustValue gets the dependable trust value; always between 0 and 1 -func (tm *TrustMetric) TrustValue() float64 +func (tm *TrustMetric) TrustValue() float64 {} // TrustScore gets a score based on the trust value always between 0 and 100 -func (tm *TrustMetric) TrustScore() int +func (tm *TrustMetric) TrustScore() int {} // NewMetric returns a trust metric with the default configuration -func NewMetric() *TrustMetric +func NewMetric() *TrustMetric {} + +//------------------------------------------------------------------------------------------------ +// For example + +tm := NewMetric() + +tm.BadEvent() +score := tm.TrustScore() + +tm.Stop() + +``` +Some of the trust metric parameters can be configured. The weight values should probably be left alone in more cases, yet the time durations for the tracking window and individual time interval should be considered. + +```go // TrustMetricConfig - Configures the weight functions and time intervals for the metric type TrustMetricConfig struct { @@ -153,17 +151,94 @@ type TrustMetricConfig struct { // Each interval should be short for adapability. // Less than 30 seconds is too sensitive, // and greater than 5 minutes will make the metric numb - IntervalLen time.Duration + IntervalLength time.Duration } // DefaultConfig returns a config with values that have been tested and produce desirable results -func DefaultConfig() *TrustMetricConfig +func DefaultConfig() TrustMetricConfig {} // NewMetricWithConfig returns a trust metric with a custom configuration -func NewMetricWithConfig(tmc *TrustMetricConfig) *TrustMetric +func NewMetricWithConfig(tmc TrustMetricConfig) *TrustMetric {} + +//------------------------------------------------------------------------------------------------ +// For example + +config := TrustMetricConfig{ + TrackingWindow: time.Minute * 60 * 24, // one day + IntervalLength: time.Minute * 2, +} + +tm := NewMetricWithConfig(config) + +tm.AddBadEvents(10) +tm.Pause() +tm.GoodEvent() // becomes active again + +``` + +A trust metric store should be created with a DB that has persistent storage so it can save history data across node executions. All trust metrics instantiated by the store will be created with the provided TrustMetricConfig configuration. + +When you attempt to fetch the trust metric for a peer, and an entry does not exist in the trust metric store, a new metric is automatically created and the entry made within the store. + +In additional to the fetching method, GetPeerTrustMetric, the trust metric store provides a method to call when a peer has disconnected from the node. This is so the metric can be paused (history data will not be saved) for periods of time when the node is not having direct experiences with the peer. + +```go + +// TrustMetricStore - Manages all trust metrics for peers +type TrustMetricStore struct { + cmn.BaseService + + // Private elements +} + +// OnStart implements Service +func (tms *TrustMetricStore) OnStart() error {} + +// OnStop implements Service +func (tms *TrustMetricStore) OnStop() {} + +// NewTrustMetricStore returns a store that saves data to the DB +// and uses the config when creating new trust metrics +func NewTrustMetricStore(db dbm.DB, tmc TrustMetricConfig) *TrustMetricStore {} + +// Size returns the number of entries in the trust metric store +func (tms *TrustMetricStore) Size() int {} + +// GetPeerTrustMetric returns a trust metric by peer key +func (tms *TrustMetricStore) GetPeerTrustMetric(key string) *TrustMetric {} + +// PeerDisconnected pauses the trust metric associated with the peer identified by the key +func (tms *TrustMetricStore) PeerDisconnected(key string) {} + +//------------------------------------------------------------------------------------------------ +// For example + +db := dbm.NewDB("trusthistory", "goleveldb", dirPathStr) +tms := NewTrustMetricStore(db, DefaultConfig()) + +tm := tms.GetPeerTrustMetric(key) +tm.BadEvent() + +tms.PeerDisconnected(key) ``` -## References +## Status + +Proposed. + +## Consequences + +### Positive + +- The trust metric will allow Tendermint to make non-binary security and reliability decisions +- Will help Tendermint implement deterrents that provide soft security controls, yet avoids disruption on the network +- Will provide useful profiling information when analyzing performance over time related to peer interaction + +### Negative + +- Requires saving the trust metric history data across node executions + +### Neutral -S. Mudhakar, L. Xiong, and L. Liu, “TrustGuard: Countering Vulnerabilities in Reputation Management for Decentralized Overlay Networks,” in *Proceedings of the 14th international conference on World Wide Web, pp. 422-431*, May 2005. \ No newline at end of file +- Keep in mind that, good events need to be recorded just as bad events do using this implementation diff --git a/node/node.go b/node/node.go index 29be71caf..97e0693e0 100644 --- a/node/node.go +++ b/node/node.go @@ -96,10 +96,10 @@ type Node struct { privValidator types.PrivValidator // local node's validator key // network - privKey crypto.PrivKeyEd25519 // local node's p2p key - sw *p2p.Switch // p2p connections - addrBook *p2p.AddrBook // known peers - tmStore *trust.TrustMetricStore // trust metrics for all peers + privKey crypto.PrivKeyEd25519 // local node's p2p key + sw *p2p.Switch // p2p connections + addrBook *p2p.AddrBook // known peers + trustMetricStore *trust.TrustMetricStore // trust metrics for all peers // services eventBus *types.EventBus // pub/sub for services @@ -241,12 +241,19 @@ func NewNode(config *cfg.Config, // Optionally, start the pex reactor var addrBook *p2p.AddrBook - var tmStore *trust.TrustMetricStore + var trustMetricStore *trust.TrustMetricStore if config.P2P.PexReactor { addrBook = p2p.NewAddrBook(config.P2P.AddrBookFile(), config.P2P.AddrBookStrict) addrBook.SetLogger(p2pLogger.With("book", config.P2P.AddrBookFile())) - tmStore = trust.NewTrustMetricStore(config.P2P.TrustHistoryFile(), nil) - tmStore.SetLogger(p2pLogger.With("trust", config.P2P.TrustHistoryFile())) + + // Get the trust metric history data + trustHistoryDB, err := dbProvider(&DBContext{"trusthistory", config}) + if err != nil { + return nil, err + } + trustMetricStore = trust.NewTrustMetricStore(trustHistoryDB, trust.DefaultConfig()) + trustMetricStore.SetLogger(p2pLogger) + pexReactor := p2p.NewPEXReactor(addrBook) pexReactor.SetLogger(p2pLogger) sw.AddReactor("PEX", pexReactor) @@ -299,10 +306,10 @@ func NewNode(config *cfg.Config, genesisDoc: genDoc, privValidator: privValidator, - privKey: privKey, - sw: sw, - addrBook: addrBook, - tmStore: tmStore, + privKey: privKey, + sw: sw, + addrBook: addrBook, + trustMetricStore: trustMetricStore, blockStore: blockStore, bcReactor: bcReactor, diff --git a/p2p/trust/trustmetric.go b/p2p/trust/trustmetric.go index e4f202bb2..6733996b0 100644 --- a/p2p/trust/trustmetric.go +++ b/p2p/trust/trustmetric.go @@ -5,12 +5,12 @@ package trust import ( "encoding/json" - "io/ioutil" "math" "sync" "time" cmn "github.com/tendermint/tmlibs/common" + dbm "github.com/tendermint/tmlibs/db" ) // TrustMetricStore - Manages all trust metrics for peers @@ -23,19 +23,19 @@ type TrustMetricStore struct { // Mutex that protects the map and history data file mtx sync.Mutex - // The file path where peer trust metric history data will be stored - filePath string + // The db where peer trust metric history data will be stored + db dbm.DB // This configuration will be used when creating new TrustMetrics - config *TrustMetricConfig + config TrustMetricConfig } -// NewTrustMetricStore returns a store that optionally saves data to -// the file path and uses the optional config when creating new trust metrics -func NewTrustMetricStore(filePath string, tmc *TrustMetricConfig) *TrustMetricStore { +// NewTrustMetricStore returns a store that saves data to the DB +// and uses the config when creating new trust metrics +func NewTrustMetricStore(db dbm.DB, tmc TrustMetricConfig) *TrustMetricStore { tms := &TrustMetricStore{ peerMetrics: make(map[string]*TrustMetric), - filePath: filePath, + db: db, config: tmc, } @@ -49,7 +49,8 @@ func (tms *TrustMetricStore) OnStart() error { tms.mtx.Lock() defer tms.mtx.Unlock() - tms.loadFromFile() + + tms.loadFromDB() return nil } @@ -63,10 +64,18 @@ func (tms *TrustMetricStore) OnStop() { tm.Stop() } - tms.saveToFile() + tms.saveToDB() tms.BaseService.OnStop() } +// Size returns the number of entries in the trust metric store +func (tms *TrustMetricStore) Size() int { + tms.mtx.Lock() + defer tms.mtx.Unlock() + + return tms.size() +} + // GetPeerTrustMetric returns a trust metric by peer key func (tms *TrustMetricStore) GetPeerTrustMetric(key string) *TrustMetric { tms.mtx.Lock() @@ -95,41 +104,43 @@ func (tms *TrustMetricStore) PeerDisconnected(key string) { } } +/* Private methods */ + +// size returns the number of entries in the store without acquiring the mutex +func (tms *TrustMetricStore) size() int { + return len(tms.peerMetrics) +} + /* Loading & Saving */ +/* Both of these methods assume the mutex has been acquired, since they write to the map */ + +var trustMetricKey = []byte("trustMetricStore") type peerHistoryJSON struct { NumIntervals int `json:"intervals"` History []float64 `json:"history"` } -// Loads the history data for the Peer identified by key from the store file. +// Loads the history data for the Peer identified by key from the store DB. // cmn.Panics if file is corrupt -func (tms *TrustMetricStore) loadFromFile() bool { - // Check that a file has been configured for use - if tms.filePath == "" { - // The trust metric store can operate without the file - return false - } - +func (tms *TrustMetricStore) loadFromDB() bool { // Obtain the history data we have so far - content, err := ioutil.ReadFile(tms.filePath) - if err != nil { - cmn.PanicCrisis(cmn.Fmt("Error reading file %s: %v", tms.filePath, err)) + bytes := tms.db.Get(trustMetricKey) + if bytes == nil { + return false } peers := make(map[string]peerHistoryJSON, 0) - err = json.Unmarshal(content, &peers) + err := json.Unmarshal(bytes, &peers) if err != nil { - cmn.PanicCrisis(cmn.Fmt("Error decoding file %s: %v", tms.filePath, err)) + cmn.PanicCrisis(cmn.Fmt("Could not unmarchal Trust Metric Store DB data: %v", err)) } // If history data exists in the file, // load it into trust metrics and recalc for key, p := range peers { tm := NewMetricWithConfig(tms.config) - if tm == nil { - continue - } + // Restore the number of time intervals we have previously tracked if p.NumIntervals > tm.maxIntervals { p.NumIntervals = tm.maxIntervals @@ -149,15 +160,9 @@ func (tms *TrustMetricStore) loadFromFile() bool { return true } -// Saves the history data for all peers to the store file -func (tms *TrustMetricStore) saveToFile() { - // Check that a file has been configured for use - if tms.filePath == "" { - // The trust metric store can operate without the file - return - } - - tms.Logger.Info("Saving TrustHistory to file", "size", len(tms.peerMetrics)) +// Saves the history data for all peers to the store DB +func (tms *TrustMetricStore) saveToDB() { + tms.Logger.Info("Saving TrustHistory to DB", "size", tms.size()) peers := make(map[string]peerHistoryJSON, 0) @@ -169,20 +174,23 @@ func (tms *TrustMetricStore) saveToFile() { } } - // Write all the data back to the file - b, err := json.Marshal(peers) + // Write all the data back to the DB + bytes, err := json.Marshal(peers) if err != nil { tms.Logger.Error("Failed to encode the TrustHistory", "err", err) return } - - err = ioutil.WriteFile(tms.filePath, b, 0644) - if err != nil { - tms.Logger.Error("Failed to save TrustHistory to file", "err", err) - } + tms.db.SetSync(trustMetricKey, bytes) } //--------------------------------------------------------------------------------------- + +// The number of event updates that can be sent on a single metric before blocking +const defaultUpdateChanCapacity = 10 + +// The number of trust value requests that can be made simultaneously before blocking +const defaultRequestChanCapacity = 10 + // TrustMetric - keeps track of peer reliability // See tendermint/docs/architecture/adr-006-trust-metric.md for details type TrustMetric struct { @@ -216,6 +224,9 @@ type TrustMetric struct { // The number of recorded good and bad events for the current time interval bad, good float64 + // While true, history data is not modified + paused bool + // Sending true on this channel stops tracking, while false pauses tracking stop chan bool @@ -238,7 +249,8 @@ type reqTrustValue struct { Resp chan float64 } -// Pause tells the metric to pause recording data over time intervals +// Pause tells the metric to pause recording data over time intervals. +// All method calls that indicate events will unpause the metric func (tm *TrustMetric) Pause() { tm.stop <- false } @@ -299,40 +311,33 @@ type TrustMetricConfig struct { // Each interval should be short for adapability. // Less than 30 seconds is too sensitive, // and greater than 5 minutes will make the metric numb - IntervalLen time.Duration + IntervalLength time.Duration } // DefaultConfig returns a config with values that have been tested and produce desirable results -func DefaultConfig() *TrustMetricConfig { - return &TrustMetricConfig{ +func DefaultConfig() TrustMetricConfig { + return TrustMetricConfig{ ProportionalWeight: 0.4, IntegralWeight: 0.6, TrackingWindow: (time.Minute * 60 * 24) * 14, // 14 days. - IntervalLen: 1 * time.Minute, + IntervalLength: 1 * time.Minute, } } // NewMetric returns a trust metric with the default configuration func NewMetric() *TrustMetric { - return NewMetricWithConfig(nil) + return NewMetricWithConfig(DefaultConfig()) } // NewMetricWithConfig returns a trust metric with a custom configuration -func NewMetricWithConfig(tmc *TrustMetricConfig) *TrustMetric { - var config *TrustMetricConfig - - if tmc == nil { - config = DefaultConfig() - } else { - config = customConfig(tmc) - } - +func NewMetricWithConfig(tmc TrustMetricConfig) *TrustMetric { tm := new(TrustMetric) + config := customConfig(tmc) // Setup using the configuration values tm.proportionalWeight = config.ProportionalWeight tm.integralWeight = config.IntegralWeight - tm.intervalLen = config.IntervalLen + tm.intervalLen = config.IntervalLength // The maximum number of time intervals is the tracking window / interval length tm.maxIntervals = int(config.TrackingWindow / tm.intervalLen) // The history size will be determined by the maximum number of time intervals @@ -340,8 +345,8 @@ func NewMetricWithConfig(tmc *TrustMetricConfig) *TrustMetric { // This metric has a perfect history so far tm.historyValue = 1.0 // Setup the channels - tm.update = make(chan *updateBadGood, 10) - tm.trustValue = make(chan *reqTrustValue, 10) + tm.update = make(chan *updateBadGood, defaultUpdateChanCapacity) + tm.trustValue = make(chan *reqTrustValue, defaultRequestChanCapacity) tm.stop = make(chan bool, 2) go tm.processRequests() @@ -351,25 +356,27 @@ func NewMetricWithConfig(tmc *TrustMetricConfig) *TrustMetric { /* Private methods */ // Ensures that all configuration elements have valid values -func customConfig(tmc *TrustMetricConfig) *TrustMetricConfig { +func customConfig(tmc TrustMetricConfig) TrustMetricConfig { config := DefaultConfig() // Check the config for set values, and setup appropriately - if tmc.ProportionalWeight != 0 { + if tmc.ProportionalWeight > 0 { config.ProportionalWeight = tmc.ProportionalWeight } - if tmc.IntegralWeight != 0 { + if tmc.IntegralWeight > 0 { config.IntegralWeight = tmc.IntegralWeight } - if tmc.TrackingWindow != time.Duration(0) { - config.TrackingWindow = tmc.TrackingWindow + if tmc.IntervalLength > time.Duration(0) { + config.IntervalLength = tmc.IntervalLength } - if tmc.IntervalLen != time.Duration(0) { - config.IntervalLen = tmc.IntervalLen + if tmc.TrackingWindow > time.Duration(0) && + tmc.TrackingWindow >= config.IntervalLength { + config.TrackingWindow = tmc.TrackingWindow } + return config } @@ -480,18 +487,19 @@ func (tm *TrustMetric) calcTrustValue() float64 { // This method is for a goroutine that handles all requests on the metric func (tm *TrustMetric) processRequests() { - var t *time.Ticker - + t := time.NewTicker(tm.intervalLen) + defer t.Stop() loop: for { select { case bg := <-tm.update: // Check if this is the first experience with - // what we are tracking since being started or paused - if t == nil { - t = time.NewTicker(tm.intervalLen) + // what we are tracking since being paused + if tm.paused { tm.good = 0 tm.bad = 0 + // New events cause us to unpause the metric + tm.paused = false } if bg.IsBad { @@ -502,41 +510,36 @@ loop: case rtv := <-tm.trustValue: rtv.Resp <- tm.calcTrustValue() case <-t.C: - // Add the current trust value to the history data - newHist := tm.calcTrustValue() - tm.history = append([]float64{newHist}, tm.history...) - - // Update history and interval counters - if tm.historySize < tm.historyMaxSize { - tm.historySize++ - } else { - tm.history = tm.history[:tm.historyMaxSize] - } - - if tm.numIntervals < tm.maxIntervals { - tm.numIntervals++ + if !tm.paused { + // Add the current trust value to the history data + newHist := tm.calcTrustValue() + tm.history = append([]float64{newHist}, tm.history...) + + // Update history and interval counters + if tm.historySize < tm.historyMaxSize { + tm.historySize++ + } else { + tm.history = tm.history[:tm.historyMaxSize] + } + + if tm.numIntervals < tm.maxIntervals { + tm.numIntervals++ + } + + // Update the history data using Faded Memories + tm.updateFadedMemory() + // Calculate the history value for the upcoming time interval + tm.historyValue = tm.calcHistoryValue() + tm.good = 0 + tm.bad = 0 } - - // Update the history data using Faded Memories - tm.updateFadedMemory() - // Calculate the history value for the upcoming time interval - tm.historyValue = tm.calcHistoryValue() - tm.good = 0 - tm.bad = 0 case stop := <-tm.stop: if stop { // Stop all further tracking for this metric break loop } - // Pause the metric for now by stopping the ticker - if t != nil { - t.Stop() - t = nil - } + // Pause the metric for now + tm.paused = true } } - - if t != nil { - t.Stop() - } } diff --git a/p2p/trust/trustmetric_test.go b/p2p/trust/trustmetric_test.go new file mode 100644 index 000000000..9c61bec96 --- /dev/null +++ b/p2p/trust/trustmetric_test.go @@ -0,0 +1,239 @@ +// Copyright 2017 Tendermint. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package trust + +import ( + "fmt" + "io/ioutil" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" + dbm "github.com/tendermint/tmlibs/db" + "github.com/tendermint/tmlibs/log" +) + +func getTempDir(prefix string) string { + dir, err := ioutil.TempDir("", prefix) + if err != nil { + panic(err) + } + return dir +} + +func TestTrustMetricStoreSaveLoad(t *testing.T) { + dir := getTempDir("trustMetricStoreTest") + defer os.Remove(dir) + + historyDB := dbm.NewDB("trusthistory", "goleveldb", dir) + + config := TrustMetricConfig{ + TrackingWindow: 5 * time.Minute, + IntervalLength: 50 * time.Millisecond, + } + + // 0 peers saved + store := NewTrustMetricStore(historyDB, config) + store.SetLogger(log.TestingLogger()) + store.saveToDB() + // Load the data from the file + store = NewTrustMetricStore(historyDB, config) + store.SetLogger(log.TestingLogger()) + store.loadFromDB() + // Make sure we still have 0 entries + assert.Zero(t, store.Size()) + + // 100 peers + for i := 0; i < 100; i++ { + key := fmt.Sprintf("peer_%d", i) + tm := store.GetPeerTrustMetric(key) + + tm.AddBadEvents(10) + tm.GoodEvent() + } + + // Check that we have 100 entries and save + assert.Equal(t, 100, store.Size()) + // Give the metrics time to process the history data + time.Sleep(1 * time.Second) + + // Stop all the trust metrics and save + for _, tm := range store.peerMetrics { + tm.Stop() + } + store.saveToDB() + + // Load the data from the DB + store = NewTrustMetricStore(historyDB, config) + store.SetLogger(log.TestingLogger()) + store.loadFromDB() + + // Check that we still have 100 peers with imperfect trust values + assert.Equal(t, 100, store.Size()) + for _, tm := range store.peerMetrics { + assert.NotEqual(t, 1.0, tm.TrustValue()) + } + + // Stop all the trust metrics + for _, tm := range store.peerMetrics { + tm.Stop() + } +} + +func TestTrustMetricStoreConfig(t *testing.T) { + historyDB := dbm.NewDB("", "memdb", "") + + config := TrustMetricConfig{ + ProportionalWeight: 0.5, + IntegralWeight: 0.5, + } + + // Create a store with custom config + store := NewTrustMetricStore(historyDB, config) + store.SetLogger(log.TestingLogger()) + + // Have the store make us a metric with the config + tm := store.GetPeerTrustMetric("TestKey") + + // Check that the options made it to the metric + assert.Equal(t, 0.5, tm.proportionalWeight) + assert.Equal(t, 0.5, tm.integralWeight) + tm.Stop() +} + +func TestTrustMetricStoreLookup(t *testing.T) { + historyDB := dbm.NewDB("", "memdb", "") + + store := NewTrustMetricStore(historyDB, DefaultConfig()) + store.SetLogger(log.TestingLogger()) + + // Create 100 peers in the trust metric store + for i := 0; i < 100; i++ { + key := fmt.Sprintf("peer_%d", i) + store.GetPeerTrustMetric(key) + + // Check that the trust metric was successfully entered + ktm := store.peerMetrics[key] + assert.NotNil(t, ktm, "Expected to find TrustMetric %s but wasn't there.", key) + } + + // Stop all the trust metrics + for _, tm := range store.peerMetrics { + tm.Stop() + } +} + +func TestTrustMetricStorePeerScore(t *testing.T) { + historyDB := dbm.NewDB("", "memdb", "") + + store := NewTrustMetricStore(historyDB, DefaultConfig()) + store.SetLogger(log.TestingLogger()) + + key := "TestKey" + tm := store.GetPeerTrustMetric(key) + + // This peer is innocent so far + first := tm.TrustScore() + assert.Equal(t, 100, first) + + // Add some undesirable events and disconnect + tm.BadEvent() + first = tm.TrustScore() + assert.NotEqual(t, 100, first) + tm.AddBadEvents(10) + second := tm.TrustScore() + + if second > first { + t.Errorf("A greater number of bad events should lower the trust score") + } + store.PeerDisconnected(key) + + // We will remember our experiences with this peer + tm = store.GetPeerTrustMetric(key) + assert.NotEqual(t, 100, tm.TrustScore()) + tm.Stop() +} + +func TestTrustMetricScores(t *testing.T) { + tm := NewMetric() + + // Perfect score + tm.GoodEvent() + score := tm.TrustScore() + assert.Equal(t, 100, score) + + // Less than perfect score + tm.AddBadEvents(10) + score = tm.TrustScore() + assert.NotEqual(t, 100, score) + tm.Stop() +} + +func TestTrustMetricConfig(t *testing.T) { + // 7 days + window := time.Minute * 60 * 24 * 7 + config := TrustMetricConfig{ + TrackingWindow: window, + IntervalLength: 2 * time.Minute, + } + + tm := NewMetricWithConfig(config) + + // The max time intervals should be the TrackingWindow / IntervalLen + assert.Equal(t, int(config.TrackingWindow/config.IntervalLength), tm.maxIntervals) + + dc := DefaultConfig() + // These weights should still be the default values + assert.Equal(t, dc.ProportionalWeight, tm.proportionalWeight) + assert.Equal(t, dc.IntegralWeight, tm.integralWeight) + tm.Stop() + + config.ProportionalWeight = 0.3 + config.IntegralWeight = 0.7 + tm = NewMetricWithConfig(config) + + // These weights should be equal to our custom values + assert.Equal(t, config.ProportionalWeight, tm.proportionalWeight) + assert.Equal(t, config.IntegralWeight, tm.integralWeight) + tm.Stop() +} + +func TestTrustMetricStopPause(t *testing.T) { + // Cause time intervals to pass quickly + config := TrustMetricConfig{ + TrackingWindow: 5 * time.Minute, + IntervalLength: 10 * time.Millisecond, + } + + tm := NewMetricWithConfig(config) + + // Allow some time intervals to pass and pause + time.Sleep(50 * time.Millisecond) + tm.Pause() + // Give the pause some time to take place + time.Sleep(10 * time.Millisecond) + + first := tm.numIntervals + // Allow more time to pass and check the intervals are unchanged + time.Sleep(50 * time.Millisecond) + assert.Equal(t, first, tm.numIntervals) + + // Get the trust metric activated again + tm.AddGoodEvents(5) + // Allow some time intervals to pass and stop + time.Sleep(50 * time.Millisecond) + tm.Stop() + // Give the stop some time to take place + time.Sleep(10 * time.Millisecond) + + second := tm.numIntervals + // Allow more time to pass and check the intervals are unchanged + time.Sleep(50 * time.Millisecond) + assert.Equal(t, second, tm.numIntervals) + + if first >= second { + t.Fatalf("numIntervals should always increase or stay the same over time") + } +}