Skip to content

Commit

Permalink
test(evidence): update evidence tests for new proposal selection algo
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Sep 20, 2024
1 parent 4fe0a18 commit 53b6967
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 14 deletions.
136 changes: 136 additions & 0 deletions internal/consensus/versioned/selectproposer/mocks/proposer_selector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/dashpay/tenderdash/types"
)

//go:generate ../../../../scripts/mockery_generate.sh ProposerSelector

type ProposerSelector interface {
// GetProposer returns the proposer for the given height and round. It calls Update if necessary.
GetProposer(height int64, round int32) (*types.Validator, error)
Expand Down
64 changes: 52 additions & 12 deletions internal/evidence/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
dbm "github.com/tendermint/tm-db"

"github.com/dashpay/tenderdash/crypto"
"github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer"
psmocks "github.com/dashpay/tenderdash/internal/consensus/versioned/selectproposer/mocks"
"github.com/dashpay/tenderdash/internal/eventbus"
"github.com/dashpay/tenderdash/internal/evidence"
"github.com/dashpay/tenderdash/internal/evidence/mocks"
Expand Down Expand Up @@ -108,27 +110,41 @@ func TestEvidencePoolBasic(t *testing.T) {
require.Equal(t, 1, len(evs))
}

func makeBlockMeta(height int64, time time.Time, vals *types.ValidatorSet) *types.BlockMeta {
return &types.BlockMeta{
Header: types.Header{
Height: height,
Time: time,
ProposerProTxHash: vals.Proposer().ProTxHash,
ValidatorsHash: vals.Hash(),
},
}
}

// Tests inbound evidence for the right time and height
func TestAddExpiredEvidence(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var (
quorumHash = crypto.RandQuorumHash()
val = types.NewMockPVForQuorum(quorumHash)
privval = types.NewMockPVForQuorum(quorumHash)
val = privval.ExtractIntoValidator(ctx, quorumHash)
valSet = types.NewValidatorSet([]*types.Validator{val}, val.PubKey, btcjson.LLMQType_5_60, quorumHash, true)
height = int64(30)
stateStore = initializeValidatorState(ctx, t, val, height, btcjson.LLMQType_5_60, quorumHash)
stateStore = initializeValidatorState(ctx, t, privval, height, btcjson.LLMQType_5_60, quorumHash)
evidenceDB = dbm.NewMemDB()
blockStore = &mocks.BlockStore{}
expiredEvidenceTime = time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC)
expiredHeight = int64(2)
)

blockStore.On("Base").Return(int64(3))
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(func(h int64) *types.BlockMeta {
if h == height || h == expiredHeight {
return &types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}}
return makeBlockMeta(h, defaultEvidenceTime, valSet)
}
return &types.BlockMeta{Header: types.Header{Time: expiredEvidenceTime}}
return makeBlockMeta(h, expiredEvidenceTime, valSet)
})

logger := log.NewNopLogger()
Expand Down Expand Up @@ -160,7 +176,7 @@ func TestAddExpiredEvidence(t *testing.T) {
defer cancel()

vals := pool.State().Validators
ev, err := types.NewMockDuplicateVoteEvidenceWithValidator(ctx, tc.evHeight, tc.evTime, val, evidenceChainID, vals.QuorumType, vals.QuorumHash)
ev, err := types.NewMockDuplicateVoteEvidenceWithValidator(ctx, tc.evHeight, tc.evTime, privval, evidenceChainID, vals.QuorumType, vals.QuorumHash)
require.NoError(t, err)
err = pool.AddEvidence(ctx, ev)
if tc.expErr {
Expand Down Expand Up @@ -408,7 +424,9 @@ func TestRecoverPendingEvidence(t *testing.T) {
state, err := stateStore.Load()
require.NoError(t, err)

blockStore, err := initializeBlockStore(dbm.NewMemDB(), state)
propSel := mockProposerSelector(t, val.ExtractIntoValidator(ctx, quorumHash))

blockStore, err := initializeBlockStore(dbm.NewMemDB(), state, propSel)
require.NoError(t, err)

logger := log.NewNopLogger()
Expand Down Expand Up @@ -535,12 +553,12 @@ func initializeValidatorState(

// initializeBlockStore creates a block storage and populates it w/ a dummy
// block at +height+.
func initializeBlockStore(db dbm.DB, state sm.State) (*store.BlockStore, error) {
func initializeBlockStore(db dbm.DB, state sm.State, propsel selectproposer.ProposerSelector) (*store.BlockStore, error) {
blockStore := store.NewBlockStore(db)

for i := int64(1); i <= state.LastBlockHeight; i++ {
lastCommit := makeCommit(i-1, state.Validators.QuorumHash)
prop := state.GetProposerFromState(i, 0)
prop := propsel.MustGetProposer(i, 0)
block := state.MakeBlock(i, []types.Tx{}, lastCommit, nil, prop.ProTxHash, 0)

block.Header.Time = defaultEvidenceTime.Add(time.Duration(i) * time.Minute)
Expand Down Expand Up @@ -573,16 +591,38 @@ func makeCommit(height int64, quorumHash []byte) *types.Commit {
)
}

func mockProposerSelector(t *testing.T, validator *types.Validator) selectproposer.ProposerSelector {
t.Helper()
propSel := psmocks.NewProposerSelector(t)
propSel.On("GetProposer", mock.Anything, mock.Anything).
Return(validator, nil).
Maybe()
propSel.On("MustGetProposer", mock.Anything, mock.Anything).
Return(validator).
Maybe()
propSel.On("UpdateHeightRound", mock.Anything, mock.Anything).
Return(nil).
Maybe()

return propSel
}

func defaultTestPool(ctx context.Context, t *testing.T, height int64) (*evidence.Pool, *types.MockPV, *eventbus.EventBus) {
t.Helper()
quorumHash := crypto.RandQuorumHash()
val := types.NewMockPVForQuorum(quorumHash)
privval := types.NewMockPVForQuorum(quorumHash)
val := privval.ExtractIntoValidator(ctx, quorumHash)

evidenceDB := dbm.NewMemDB()
stateStore := initializeValidatorState(ctx, t, val, height, btcjson.LLMQType_5_60, quorumHash)
stateStore := initializeValidatorState(ctx, t, privval, height, btcjson.LLMQType_5_60, quorumHash)
state, err := stateStore.Load()
require.NoError(t, err)
blockStore, err := initializeBlockStore(dbm.NewMemDB(), state)

propSel := mockProposerSelector(t, val)

require.NoError(t, err)

blockStore, err := initializeBlockStore(dbm.NewMemDB(), state, propSel)
require.NoError(t, err)

logger := log.NewNopLogger()
Expand All @@ -592,7 +632,7 @@ func defaultTestPool(ctx context.Context, t *testing.T, height int64) (*evidence

pool := evidence.NewPool(logger, evidenceDB, stateStore, blockStore, evidence.NopMetrics(), eventBus)
startPool(t, pool, stateStore)
return pool, val, eventBus
return pool, privval, eventBus
}

func createState(height int64, valSet *types.ValidatorSet) sm.State {
Expand Down
3 changes: 2 additions & 1 deletion internal/evidence/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ func setup(ctx context.Context, t *testing.T, stateStores []sm.Store) *reactorTe
evidenceDB := dbm.NewMemDB()
blockStore := &mocks.BlockStore{}
state, _ := stateStores[idx].Load()
blockStore.On("Base").Return(int64(1))
blockStore.On("LoadBlockMeta", mock.AnythingOfType("int64")).Return(func(h int64) *types.BlockMeta {
if h <= state.LastBlockHeight {
return &types.BlockMeta{Header: types.Header{Time: evidenceTime}}
return makeBlockMeta(h, evidenceTime, state.Validators)
}
return nil
})
Expand Down
3 changes: 2 additions & 1 deletion internal/evidence/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dashpay/dashd-go/btcjson"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"

Expand Down Expand Up @@ -110,7 +111,7 @@ func TestVerifyDuplicateVoteEvidence(t *testing.T) {
ConsensusParams: *types.DefaultConsensusParams(),
}
stateStore := &smmocks.Store{}
stateStore.On("LoadValidators", int64(10)).Return(valSet, nil)
stateStore.On("LoadValidators", int64(10), mock.Anything).Return(valSet, nil)
stateStore.On("Load").Return(state, nil)
blockStore := &mocks.BlockStore{}
blockStore.On("LoadBlockMeta", int64(10)).Return(&types.BlockMeta{Header: types.Header{Time: defaultEvidenceTime}})
Expand Down

0 comments on commit 53b6967

Please sign in to comment.