tendermint/evidence/pool.go

154 lines
4.3 KiB
Go
Raw Permalink Normal View History

2017-11-19 20:22:25 -08:00
package evidence
2017-11-02 11:06:48 -07:00
import (
2017-12-28 18:08:39 -08:00
"fmt"
"sync"
2018-07-01 19:36:49 -07:00
clist "github.com/tendermint/tendermint/libs/clist"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
2017-11-02 11:06:48 -07:00
2017-12-27 19:39:58 -08:00
sm "github.com/tendermint/tendermint/state"
2017-11-02 11:06:48 -07:00
"github.com/tendermint/tendermint/types"
)
2017-11-18 16:57:55 -08:00
// EvidencePool maintains a pool of valid evidence
// in an EvidenceStore.
2017-11-02 11:06:48 -07:00
type EvidencePool struct {
2017-11-02 17:26:07 -07:00
logger log.Logger
2017-11-02 11:06:48 -07:00
2017-11-18 16:57:55 -08:00
evidenceStore *EvidenceStore
2018-06-04 17:38:44 -07:00
evidenceList *clist.CList // concurrent linked-list of evidence
2017-11-02 11:06:48 -07:00
2017-12-28 18:08:39 -08:00
// needed to load validators to verify evidence
stateDB dbm.DB
// latest state
mtx sync.Mutex
state sm.State
2017-11-02 11:06:48 -07:00
}
2017-12-28 18:08:39 -08:00
func NewEvidencePool(stateDB dbm.DB, evidenceStore *EvidenceStore) *EvidencePool {
2017-11-02 11:06:48 -07:00
evpool := &EvidencePool{
2017-12-28 18:08:39 -08:00
stateDB: stateDB,
state: sm.LoadState(stateDB),
2017-11-18 16:57:55 -08:00
logger: log.NewNopLogger(),
evidenceStore: evidenceStore,
2018-06-04 17:38:44 -07:00
evidenceList: clist.New(),
2017-11-02 11:06:48 -07:00
}
return evpool
}
2018-06-04 17:38:44 -07:00
func (evpool *EvidencePool) EvidenceFront() *clist.CElement {
return evpool.evidenceList.Front()
}
func (evpool *EvidencePool) EvidenceWaitChan() <-chan struct{} {
return evpool.evidenceList.WaitChan()
}
2017-11-02 11:06:48 -07:00
// SetLogger sets the Logger.
func (evpool *EvidencePool) SetLogger(l log.Logger) {
evpool.logger = l
}
2017-11-02 17:26:07 -07:00
// PriorityEvidence returns the priority evidence.
func (evpool *EvidencePool) PriorityEvidence() []types.Evidence {
return evpool.evidenceStore.PriorityEvidence()
2017-11-02 11:06:48 -07:00
}
// PendingEvidence returns uncommitted evidence up to maxBytes.
// If maxBytes is -1, all evidence is returned.
func (evpool *EvidencePool) PendingEvidence(maxBytes int64) []types.Evidence {
return evpool.evidenceStore.PendingEvidence(maxBytes)
2017-11-02 11:06:48 -07:00
}
2017-12-28 18:08:39 -08:00
// State returns the current state of the evpool.
func (evpool *EvidencePool) State() sm.State {
evpool.mtx.Lock()
defer evpool.mtx.Unlock()
return evpool.state
}
// Update loads the latest
func (evpool *EvidencePool) Update(block *types.Block, state sm.State) {
2017-12-28 18:08:39 -08:00
// sanity check
2017-12-28 18:08:39 -08:00
if state.LastBlockHeight != block.Height {
panic(fmt.Sprintf("Failed EvidencePool.Update sanity check: got state.Height=%d with block.Height=%d", state.LastBlockHeight, block.Height))
2017-12-28 18:08:39 -08:00
}
2018-06-04 13:59:28 -07:00
// update the state
evpool.mtx.Lock()
2017-12-28 18:08:39 -08:00
evpool.state = state
2018-06-04 13:59:28 -07:00
evpool.mtx.Unlock()
2017-12-28 18:08:39 -08:00
2018-06-04 13:59:28 -07:00
// remove evidence from pending and mark committed
2018-06-04 17:38:44 -07:00
evpool.MarkEvidenceAsCommitted(block.Height, block.Evidence.Evidence)
2017-12-28 18:08:39 -08:00
}
2017-11-02 11:06:48 -07:00
// AddEvidence checks the evidence is valid and adds it to the pool.
func (evpool *EvidencePool) AddEvidence(evidence types.Evidence) (err error) {
2017-12-28 18:08:39 -08:00
2017-12-26 22:27:03 -08:00
// TODO: check if we already have evidence for this
// validator at this height so we dont get spammed
2017-12-28 18:08:39 -08:00
if err := sm.VerifyEvidence(evpool.stateDB, evpool.State(), evidence); err != nil {
2017-12-27 19:39:58 -08:00
return err
}
2017-12-28 18:08:39 -08:00
// fetch the validator and return its voting power as its priority
// TODO: something better ?
valset, _ := sm.LoadValidators(evpool.stateDB, evidence.Height())
_, val := valset.GetByAddress(evidence.Address())
priority := val.VotingPower
2017-11-19 20:22:25 -08:00
added := evpool.evidenceStore.AddNewEvidence(evidence, priority)
if !added {
2017-11-02 17:26:07 -07:00
// evidence already known, just ignore
return
2017-11-02 11:06:48 -07:00
}
2017-11-02 17:26:07 -07:00
evpool.logger.Info("Verified new evidence of byzantine behaviour", "evidence", evidence)
2018-06-04 17:38:44 -07:00
// add evidence to clist
evpool.evidenceList.PushBack(evidence)
2017-11-02 11:06:48 -07:00
return nil
}
2018-06-04 21:50:29 -07:00
// MarkEvidenceAsCommitted marks all the evidence as committed and removes it from the queue.
2018-06-04 17:38:44 -07:00
func (evpool *EvidencePool) MarkEvidenceAsCommitted(height int64, evidence []types.Evidence) {
2018-06-04 21:50:29 -07:00
// make a map of committed evidence to remove from the clist
2018-06-04 17:38:44 -07:00
blockEvidenceMap := make(map[string]struct{})
2017-11-02 11:06:48 -07:00
for _, ev := range evidence {
2017-11-02 17:26:07 -07:00
evpool.evidenceStore.MarkEvidenceAsCommitted(ev)
2018-06-04 21:50:29 -07:00
blockEvidenceMap[evMapKey(ev)] = struct{}{}
2018-06-04 17:38:44 -07:00
}
// remove committed evidence from the clist
2018-06-04 21:50:29 -07:00
maxAge := evpool.State().ConsensusParams.EvidenceParams.MaxAge
evpool.removeEvidence(height, maxAge, blockEvidenceMap)
2018-06-04 17:38:44 -07:00
}
2018-06-04 21:50:29 -07:00
func (evpool *EvidencePool) removeEvidence(height, maxAge int64, blockEvidenceMap map[string]struct{}) {
2018-06-04 17:38:44 -07:00
for e := evpool.evidenceList.Front(); e != nil; e = e.Next() {
ev := e.Value.(types.Evidence)
// Remove the evidence if it's already in a block
// or if it's now too old.
2018-06-04 21:50:29 -07:00
if _, ok := blockEvidenceMap[evMapKey(ev)]; ok ||
2018-06-04 17:38:44 -07:00
ev.Height() < height-maxAge {
// remove from clist
evpool.evidenceList.Remove(e)
e.DetachPrev()
}
2017-11-02 11:06:48 -07:00
}
}
2018-06-04 21:50:29 -07:00
func evMapKey(ev types.Evidence) string {
return string(ev.Hash())
}