tendermint/evidence/pool_test.go

108 lines
2.5 KiB
Go
Raw Normal View History

2017-11-20 04:22:25 +00:00
package evidence
import (
"os"
2017-11-20 04:22:25 +00:00
"sync"
"testing"
"github.com/stretchr/testify/assert"
2017-12-27 22:39:58 -05:00
sm "github.com/tendermint/tendermint/state"
2017-11-20 04:22:25 +00:00
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
dbm "github.com/tendermint/tm-cmn/db"
2017-11-20 04:22:25 +00:00
)
func TestMain(m *testing.M) {
types.RegisterMockEvidences(cdc)
code := m.Run()
os.Exit(code)
}
2017-12-28 21:08:39 -05: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: tmtime.Now(),
2017-12-28 21:08:39 -05:00
Validators: valSet,
NextValidators: valSet.CopyIncrementProposerPriority(1),
2017-12-28 21:08:39 -05:00
LastHeightValidatorsChanged: 1,
ConsensusParams: types.ConsensusParams{
Evidence: types.EvidenceParams{
2017-12-28 21:08:39 -05:00
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-20 04:22:25 +00:00
func TestEvidencePool(t *testing.T) {
2017-12-28 21:08:39 -05:00
valAddr := []byte("val1")
height := int64(5)
stateDB := initializeValidatorState(valAddr, height)
evidenceDB := dbm.NewMemDB()
pool := NewEvidencePool(stateDB, evidenceDB)
2017-11-20 04:22:25 +00:00
goodEvidence := types.NewMockGoodEvidence(height, 0, valAddr)
badEvidence := types.MockBadEvidence{MockGoodEvidence: goodEvidence}
2017-11-20 04:22:25 +00:00
2018-06-04 17:38:44 -07:00
// bad evidence
2017-11-20 04:22:25 +00:00
err := pool.AddEvidence(badEvidence)
2018-06-04 17:38:44 -07:00
assert.NotNil(t, err)
2017-11-20 04:22:25 +00:00
var wg sync.WaitGroup
wg.Add(1)
go func() {
2018-06-04 17:38:44 -07:00
<-pool.EvidenceWaitChan()
2017-11-20 04:22:25 +00:00
wg.Done()
}()
err = pool.AddEvidence(goodEvidence)
2018-06-04 17:38:44 -07:00
assert.Nil(t, err)
2017-11-20 04:22:25 +00: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-20 04:22:25 +00: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-20 04:22:25 +00:00
}
func TestEvidencePoolIsCommitted(t *testing.T) {
// Initialization:
valAddr := []byte("validator_address")
height := int64(42)
stateDB := initializeValidatorState(valAddr, height)
evidenceDB := dbm.NewMemDB()
pool := NewEvidencePool(stateDB, evidenceDB)
// evidence not seen yet:
evidence := types.NewMockGoodEvidence(height, 0, valAddr)
assert.False(t, pool.IsCommitted(evidence))
// evidence seen but not yet committed:
assert.NoError(t, pool.AddEvidence(evidence))
assert.False(t, pool.IsCommitted(evidence))
// evidence seen and committed:
pool.MarkEvidenceAsCommitted(height, []types.Evidence{evidence})
assert.True(t, pool.IsCommitted(evidence))
}