2017-11-19 20:22:25 -08:00
|
|
|
package evidence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/types"
|
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockState struct{}
|
|
|
|
|
2017-12-26 17:34:57 -08:00
|
|
|
func (m mockState) VerifyEvidence(ev types.Evidence) (int64, error) {
|
2017-11-19 20:22:25 -08:00
|
|
|
err := ev.Verify("")
|
|
|
|
return 10, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvidencePool(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-12-26 17:34:57 -08:00
|
|
|
params := types.EvidenceParams{}
|
2017-11-19 20:22:25 -08:00
|
|
|
store := NewEvidenceStore(dbm.NewMemDB())
|
|
|
|
state := mockState{}
|
2017-12-26 17:34:57 -08:00
|
|
|
pool := NewEvidencePool(params, store, state)
|
2017-11-19 20:22:25 -08:00
|
|
|
|
|
|
|
goodEvidence := newMockGoodEvidence(5, 1, []byte("val1"))
|
|
|
|
badEvidence := MockBadEvidence{goodEvidence}
|
|
|
|
|
|
|
|
err := pool.AddEvidence(badEvidence)
|
|
|
|
assert.NotNil(err)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
<-pool.EvidenceChan()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = pool.AddEvidence(goodEvidence)
|
|
|
|
assert.Nil(err)
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// if we send it again it wont fire on the chan
|
|
|
|
err = pool.AddEvidence(goodEvidence)
|
|
|
|
assert.Nil(err)
|
|
|
|
select {
|
|
|
|
case <-pool.EvidenceChan():
|
|
|
|
t.Fatal("unexpected read on EvidenceChan")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|