tendermint/evidence/store.go

189 lines
5.6 KiB
Go
Raw Normal View History

2017-11-02 17:26:07 -07:00
package evpool
import (
"fmt"
wire "github.com/tendermint/go-wire"
"github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tmlibs/db"
)
/*
2017-11-19 14:06:01 -08:00
Requirements:
- Valid new evidence must be persisted immediately and never forgotten
- Uncommitted evidence must be continuously broadcast
- Uncommitted evidence has a partial order, the evidence's priority
Impl:
- First commit atomically in outqueue, pending, lookup.
- Once broadcast, remove from outqueue. No need to sync
- Once committed, atomically remove from pending and update lookup.
- TODO: If we crash after committed but before removing/updating,
we'll be stuck broadcasting evidence we never know we committed.
so either share the state db and atomically MarkCommitted
with ApplyBlock, or check all outqueue/pending on Start to see if its committed
Schema for indexing evidence (note you need both height and hash to find a piece of evidence):
2017-11-19 19:55:59 -08:00
"evidence-lookup"/<evidence-height>/<evidence-hash> -> EvidenceInfo
"evidence-outqueue"/<priority>/<evidence-height>/<evidence-hash> -> EvidenceInfo
"evidence-pending"/<evidence-height>/<evidence-hash> -> EvidenceInfo
2017-11-02 17:26:07 -07:00
*/
2017-11-19 19:55:59 -08:00
type EvidenceInfo struct {
2017-11-02 17:26:07 -07:00
Committed bool
Priority int
Evidence types.Evidence
}
const (
2017-11-18 16:57:55 -08:00
baseKeyLookup = "evidence-lookup" // all evidence
baseKeyOutqueue = "evidence-outqueue" // not-yet broadcast
baseKeyPending = "evidence-pending" // broadcast but not committed
)
2017-11-02 17:26:07 -07:00
func keyLookup(evidence types.Evidence) []byte {
2017-11-19 15:43:36 -08:00
return keyLookupFromHeightAndHash(evidence.Height(), evidence.Hash())
}
2017-11-19 19:55:59 -08:00
// big endian padded hex
func be(h int) string {
return fmt.Sprintf("%0.16X", h)
}
2017-11-19 15:43:36 -08:00
func keyLookupFromHeightAndHash(height int, hash []byte) []byte {
2017-11-19 19:55:59 -08:00
return _key("%s/%s/%X", baseKeyLookup, be(height), hash)
2017-11-02 17:26:07 -07:00
}
2017-11-19 14:06:01 -08:00
func keyOutqueue(evidence types.Evidence, priority int) []byte {
2017-11-19 19:55:59 -08:00
return _key("%s/%s/%s/%X", baseKeyOutqueue, be(priority), be(evidence.Height()), evidence.Hash())
2017-11-02 17:26:07 -07:00
}
func keyPending(evidence types.Evidence) []byte {
2017-11-19 19:55:59 -08:00
return _key("%s/%s/%X", baseKeyPending, be(evidence.Height()), evidence.Hash())
2017-11-18 16:57:55 -08:00
}
2017-11-19 14:06:01 -08:00
func _key(fmt_ string, o ...interface{}) []byte {
return []byte(fmt.Sprintf(fmt_, o...))
2017-11-02 17:26:07 -07:00
}
2017-11-19 15:43:36 -08:00
// EvidenceStore is a store of all the evidence we've seen, including
// evidence that has been committed, evidence that has been verified but not broadcast,
2017-11-02 17:26:07 -07:00
// and evidence that has been broadcast but not yet committed.
type EvidenceStore struct {
db dbm.DB
2017-11-02 17:26:07 -07:00
}
func NewEvidenceStore(db dbm.DB) *EvidenceStore {
2017-11-02 17:26:07 -07:00
return &EvidenceStore{
db: db,
}
}
// PriorityEvidence returns the evidence from the outqueue, sorted by highest priority.
func (store *EvidenceStore) PriorityEvidence() (evidence []types.Evidence) {
2017-11-19 19:55:59 -08:00
// reverse the order so highest priority is first
l := store.ListEvidence(baseKeyOutqueue)
l2 := make([]types.Evidence, len(l))
for i, _ := range l {
l2[i] = l[len(l)-1-i]
}
return l2
}
2017-11-19 14:06:01 -08:00
// PendingEvidence returns all known uncommitted evidence.
func (store *EvidenceStore) PendingEvidence() (evidence []types.Evidence) {
2017-11-19 15:43:36 -08:00
return store.ListEvidence(baseKeyPending)
}
// ListEvidence lists the evidence for the given prefix key.
// It is wrapped by PriorityEvidence and PendingEvidence for convenience.
func (store *EvidenceStore) ListEvidence(prefixKey string) (evidence []types.Evidence) {
iter := store.db.IteratorPrefix([]byte(prefixKey))
for iter.Next() {
val := iter.Value()
2017-11-19 19:55:59 -08:00
var ei EvidenceInfo
wire.ReadBinaryBytes(val, &ei)
evidence = append(evidence, ei.Evidence)
2017-11-02 17:26:07 -07:00
}
return evidence
2017-11-02 17:26:07 -07:00
}
2017-11-19 15:43:36 -08:00
// GetEvidence fetches the evidence with the given height and hash.
2017-11-19 19:55:59 -08:00
func (store *EvidenceStore) GetEvidence(height int, hash []byte) *EvidenceInfo {
2017-11-19 15:43:36 -08:00
key := keyLookupFromHeightAndHash(height, hash)
val := store.db.Get(key)
2017-11-19 19:55:59 -08:00
2017-11-19 15:43:36 -08:00
if len(val) == 0 {
return nil
}
2017-11-19 19:55:59 -08:00
ei := new(EvidenceInfo)
wire.ReadBinaryBytes(val, ei)
return ei
2017-11-19 15:43:36 -08:00
}
2017-11-02 17:26:07 -07:00
// AddNewEvidence adds the given evidence to the database.
func (store *EvidenceStore) AddNewEvidence(evidence types.Evidence, priority int) (bool, error) {
2017-11-02 17:26:07 -07:00
// check if we already have seen it
2017-11-19 19:55:59 -08:00
ei_ := store.GetEvidence(evidence.Height(), evidence.Hash())
if ei_ != nil && ei_.Evidence != nil {
2017-11-02 17:26:07 -07:00
return false, nil
}
2017-11-19 19:55:59 -08:00
ei := EvidenceInfo{
2017-11-02 17:26:07 -07:00
Committed: false,
Priority: priority,
2017-11-02 17:26:07 -07:00
Evidence: evidence,
}
eiBytes := wire.BinaryBytes(ei)
// add it to the store
2017-11-19 15:43:36 -08:00
key := keyOutqueue(evidence, priority)
store.db.Set(key, eiBytes)
2017-11-02 17:26:07 -07:00
key = keyPending(evidence)
store.db.Set(key, eiBytes)
2017-11-02 17:26:07 -07:00
2017-11-19 14:06:01 -08:00
key = keyLookup(evidence)
store.db.SetSync(key, eiBytes)
2017-11-02 17:26:07 -07:00
return true, nil
}
2017-11-19 14:06:01 -08:00
// MarkEvidenceAsBroadcasted removes evidence from Outqueue.
func (store *EvidenceStore) MarkEvidenceAsBroadcasted(evidence types.Evidence) {
2017-11-19 14:06:01 -08:00
ei := store.getEvidenceInfo(evidence)
key := keyOutqueue(evidence, ei.Priority)
2017-11-02 17:26:07 -07:00
store.db.Delete(key)
}
2017-11-18 16:57:55 -08:00
// MarkEvidenceAsPending removes evidence from pending and outqueue and sets the state to committed.
2017-11-02 17:26:07 -07:00
func (store *EvidenceStore) MarkEvidenceAsCommitted(evidence types.Evidence) {
2017-11-18 16:57:55 -08:00
// if its committed, its been broadcast
store.MarkEvidenceAsBroadcasted(evidence)
2017-11-19 19:55:59 -08:00
pendingKey := keyPending(evidence)
store.db.Delete(pendingKey)
2017-11-02 17:26:07 -07:00
2017-11-19 14:06:01 -08:00
ei := store.getEvidenceInfo(evidence)
ei.Committed = true
// TODO: we should use the state db and db.Sync in state.Save instead.
// Else, if we call this before state.Save, we may never mark committed evidence as committed.
// Else, if we call this after state.Save, we may get stuck broadcasting evidence we never know we committed.
2017-11-19 19:55:59 -08:00
lookupKey := keyLookup(evidence)
store.db.SetSync(lookupKey, wire.BinaryBytes(ei))
2017-11-19 14:06:01 -08:00
}
2017-11-19 15:43:36 -08:00
//---------------------------------------------------
// utils
2017-11-19 19:55:59 -08:00
func (store *EvidenceStore) getEvidenceInfo(evidence types.Evidence) EvidenceInfo {
2017-11-19 14:06:01 -08:00
key := keyLookup(evidence)
2017-11-19 19:55:59 -08:00
var ei EvidenceInfo
2017-11-02 17:26:07 -07:00
b := store.db.Get(key)
wire.ReadBinaryBytes(b, &ei)
2017-11-19 14:06:01 -08:00
return ei
2017-11-02 17:26:07 -07:00
}