tendermint/evidence/pool_test.go

80 lines
1.7 KiB
Go
Raw Normal View History

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"
2018-07-01 19:36:49 -07:00
dbm "github.com/tendermint/tendermint/libs/db"
2017-11-19 20:22:25 -08:00
)
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) {
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
goodEvidence := types.NewMockGoodEvidence(height, 0, valAddr)
badEvidence := types.MockBadEvidence{goodEvidence}
2017-11-19 20:22:25 -08:00
2018-06-04 17:38:44 -07:00
// bad evidence
2017-11-19 20:22:25 -08:00
err := pool.AddEvidence(badEvidence)
2018-06-04 17:38:44 -07:00
assert.NotNil(t, err)
2017-11-19 20:22:25 -08:00
var wg sync.WaitGroup
wg.Add(1)
go func() {
2018-06-04 17:38:44 -07:00
<-pool.EvidenceWaitChan()
2017-11-19 20:22:25 -08:00
wg.Done()
}()
err = pool.AddEvidence(goodEvidence)
2018-06-04 17:38:44 -07:00
assert.Nil(t, err)
2017-11-19 20:22:25 -08:00
wg.Wait()
2018-06-04 17:38:44 -07:00
assert.Equal(t, 1, pool.evidenceList.Len())
// if we send it again, it shouldnt change the size
2017-11-19 20:22:25 -08:00
err = pool.AddEvidence(goodEvidence)
2018-06-04 17:38:44 -07:00
assert.Nil(t, err)
assert.Equal(t, 1, pool.evidenceList.Len())
2017-11-19 20:22:25 -08:00
}