Files
tendermint/evidence/store_test.go
Marko 6d91c1faf4 evidence: introduce time.Duration to evidence params (#4254)
* evidence: introduce time.Duration to evidence params

- add time.duration to evidence
- this pr is taking pr #2606 and updating it to use both time and height

- closes #2565

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* fix testing and genesis cfg in signer harness

* remove debugging fmt

* change maxageheight to maxagenumblocks, rename other things to block instead of height

* further check of duration

* check duration to not send peers outdated evidence

* change some lines, onward and upward

* refactor evidence package

* add a changelog pending entry

* make mockbadevidence have time and use it

* add what could possibly be called a test case

* remove mockbadevidence and mockgoodevidence in favor of mockevidence

* add a comment for err that is returned

* add a changelog for removal of good & bad evidence

* add a test for adding evidence

* fix test

* add ev to types in testcase

* Update evidence/pool_test.go

Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>

* Update evidence/pool_test.go

Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>

* fix tests

* fix linting

Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
2020-01-08 10:46:37 +01:00

127 lines
3.2 KiB
Go

package evidence
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/types"
dbm "github.com/tendermint/tm-db"
)
//-------------------------------------------
func TestStoreAddDuplicate(t *testing.T) {
assert := assert.New(t)
db := dbm.NewMemDB()
store := NewStore(db)
priority := int64(10)
ev := types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1"))
added := store.AddNewEvidence(ev, priority)
assert.True(added)
// cant add twice
added = store.AddNewEvidence(ev, priority)
assert.False(added)
}
func TestStoreCommitDuplicate(t *testing.T) {
assert := assert.New(t)
db := dbm.NewMemDB()
store := NewStore(db)
priority := int64(10)
ev := types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1"))
store.MarkEvidenceAsCommitted(ev)
added := store.AddNewEvidence(ev, priority)
assert.False(added)
}
func TestStoreMark(t *testing.T) {
assert := assert.New(t)
db := dbm.NewMemDB()
store := NewStore(db)
// before we do anything, priority/pending are empty
priorityEv := store.PriorityEvidence()
pendingEv := store.PendingEvidence(-1)
assert.Equal(0, len(priorityEv))
assert.Equal(0, len(pendingEv))
priority := int64(10)
ev := types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1"))
added := store.AddNewEvidence(ev, priority)
assert.True(added)
// get the evidence. verify. should be uncommitted
ei := store.GetInfo(ev.Height(), ev.Hash())
assert.Equal(ev, ei.Evidence)
assert.Equal(priority, ei.Priority)
assert.False(ei.Committed)
// new evidence should be returns in priority/pending
priorityEv = store.PriorityEvidence()
pendingEv = store.PendingEvidence(-1)
assert.Equal(1, len(priorityEv))
assert.Equal(1, len(pendingEv))
// priority is now empty
store.MarkEvidenceAsBroadcasted(ev)
priorityEv = store.PriorityEvidence()
pendingEv = store.PendingEvidence(-1)
assert.Equal(0, len(priorityEv))
assert.Equal(1, len(pendingEv))
// priority and pending are now empty
store.MarkEvidenceAsCommitted(ev)
priorityEv = store.PriorityEvidence()
pendingEv = store.PendingEvidence(-1)
assert.Equal(0, len(priorityEv))
assert.Equal(0, len(pendingEv))
// evidence should show committed
newPriority := int64(0)
ei = store.GetInfo(ev.Height(), ev.Hash())
assert.Equal(ev, ei.Evidence)
assert.Equal(newPriority, ei.Priority)
assert.True(ei.Committed)
}
func TestStorePriority(t *testing.T) {
assert := assert.New(t)
db := dbm.NewMemDB()
store := NewStore(db)
// sorted by priority and then height
cases := []struct {
ev types.MockEvidence
priority int64
}{
{types.NewMockEvidence(2, time.Now().UTC(), 1, []byte("val1")), 17},
{types.NewMockEvidence(5, time.Now().UTC(), 2, []byte("val2")), 15},
{types.NewMockEvidence(10, time.Now().UTC(), 2, []byte("val2")), 13},
{types.NewMockEvidence(100, time.Now().UTC(), 2, []byte("val2")), 11},
{types.NewMockEvidence(90, time.Now().UTC(), 2, []byte("val2")), 11},
{types.NewMockEvidence(80, time.Now().UTC(), 2, []byte("val2")), 11},
}
for _, c := range cases {
added := store.AddNewEvidence(c.ev, c.priority)
assert.True(added)
}
evList := store.PriorityEvidence()
for i, ev := range evList {
assert.Equal(ev, cases[i].ev)
}
}