2017-11-19 20:22:25 -08:00
|
|
|
package evidence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"testing"
|
2017-12-28 18:08:39 -08:00
|
|
|
"time"
|
2017-11-19 20:22:25 -08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2017-12-27 19:39:58 -08:00
|
|
|
sm "github.com/tendermint/tendermint/state"
|
2017-11-19 20:22:25 -08:00
|
|
|
"github.com/tendermint/tendermint/types"
|
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
|
|
|
)
|
|
|
|
|
2017-12-27 19:39:58 -08:00
|
|
|
var mockState = sm.State{}
|
2017-11-19 20:22:25 -08:00
|
|
|
|
2017-12-28 18:08:39 -08:00
|
|
|
func initializeValidatorState(valAddr []byte, height int64) dbm.DB {
|
|
|
|
stateDB := dbm.NewMemDB()
|
|
|
|
|
|
|
|
// create validator set and state
|
|
|
|
valSet := &types.ValidatorSet{
|
|
|
|
Validators: []*types.Validator{
|
|
|
|
{Address: valAddr},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
state := sm.State{
|
|
|
|
LastBlockHeight: 0,
|
|
|
|
LastBlockTime: time.Now(),
|
|
|
|
Validators: valSet,
|
|
|
|
LastHeightValidatorsChanged: 1,
|
|
|
|
ConsensusParams: types.ConsensusParams{
|
|
|
|
EvidenceParams: types.EvidenceParams{
|
|
|
|
MaxAge: 1000000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// save all states up to height
|
|
|
|
for i := int64(0); i < height; i++ {
|
|
|
|
state.LastBlockHeight = i
|
|
|
|
sm.SaveState(stateDB, state)
|
|
|
|
}
|
|
|
|
|
|
|
|
return stateDB
|
|
|
|
}
|
|
|
|
|
2017-11-19 20:22:25 -08:00
|
|
|
func TestEvidencePool(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2017-12-28 18:08:39 -08:00
|
|
|
valAddr := []byte("val1")
|
|
|
|
height := int64(5)
|
|
|
|
stateDB := initializeValidatorState(valAddr, height)
|
2017-11-19 20:22:25 -08:00
|
|
|
store := NewEvidenceStore(dbm.NewMemDB())
|
2017-12-28 18:08:39 -08:00
|
|
|
pool := NewEvidencePool(stateDB, store)
|
2017-11-19 20:22:25 -08:00
|
|
|
|
2017-12-28 18:08:39 -08:00
|
|
|
goodEvidence := newMockGoodEvidence(height, 0, valAddr)
|
2017-11-19 20:22:25 -08:00
|
|
|
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:
|
|
|
|
}
|
|
|
|
}
|