evidence: refactor evidence mocks throughout packages (#4787)

Predominantly following the discussions regarding the conventions of using mocks, I have decided to revert back to the previous state where mocks were specialized and stored in the separate packages that used them rather then have a generalized mock in the evidence package.

This also was a problem as the state package were running tests too slow and occasionally timing out unnecessarily.

For the replay file I renamed mockEvidencePool to emptyEvidencePool to illustrate that it was intentionally like this and not give the impression that testing software was being used in production

Closes: #4786
This commit is contained in:
Callum Waters
2020-05-05 11:11:44 +02:00
committed by GitHub
parent b7c2d7a977
commit 47cfadb0aa
13 changed files with 141 additions and 167 deletions

View File

@@ -4,20 +4,23 @@ import (
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/tmhash"
evmock "github.com/tendermint/tendermint/evidence/mock"
"github.com/tendermint/tendermint/libs/log"
"github.com/tendermint/tendermint/mempool/mock"
memmock "github.com/tendermint/tendermint/mempool/mock"
sm "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/state/mocks"
"github.com/tendermint/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
const validationTestsStopHeight int64 = 10
var defaultTestTime = time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
func TestValidateBlockHeader(t *testing.T) {
proxyApp := newTestApp()
require.NoError(t, proxyApp.Start())
@@ -28,8 +31,8 @@ func TestValidateBlockHeader(t *testing.T) {
stateDB,
log.TestingLogger(),
proxyApp.Consensus(),
mock.Mempool{},
evmock.NewDefaultEvidencePool(),
memmock.Mempool{},
sm.MockEvidencePool{},
)
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil)
@@ -98,8 +101,8 @@ func TestValidateBlockCommit(t *testing.T) {
stateDB,
log.TestingLogger(),
proxyApp.Consensus(),
mock.Mempool{},
evmock.NewDefaultEvidencePool(),
memmock.Mempool{},
sm.MockEvidencePool{},
)
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil)
wrongSigsCommit := types.NewCommit(1, 0, types.BlockID{}, nil)
@@ -205,8 +208,8 @@ func TestValidateBlockEvidence(t *testing.T) {
stateDB,
log.TestingLogger(),
proxyApp.Consensus(),
mock.Mempool{},
evmock.NewDefaultEvidencePool(),
memmock.Mempool{},
sm.MockEvidencePool{},
)
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil)
@@ -259,9 +262,17 @@ func TestValidateBlockEvidence(t *testing.T) {
func TestValidateFailBlockOnCommittedEvidence(t *testing.T) {
var height int64 = 1
state, stateDB, _ := makeState(1, int(height))
state, stateDB, _ := makeState(2, int(height))
addr, _ := state.Validators.GetByIndex(0)
addr2, _ := state.Validators.GetByIndex(1)
ev := types.NewMockEvidence(height, defaultTestTime, addr)
ev2 := types.NewMockEvidence(height, defaultTestTime, addr2)
evpool := &mocks.EvidencePool{}
evpool.On("IsPending", mock.AnythingOfType("types.MockEvidence")).Return(false)
evpool.On("IsCommitted", ev).Return(false)
evpool.On("IsCommitted", ev2).Return(true)
evpool := evmock.NewDefaultEvidencePool()
blockExec := sm.NewBlockExecutor(
stateDB, log.TestingLogger(),
nil,
@@ -269,9 +280,7 @@ func TestValidateFailBlockOnCommittedEvidence(t *testing.T) {
evpool)
// A block with a couple pieces of evidence passes.
block := makeBlock(state, height)
addr, _ := state.Validators.GetByIndex(0)
evpool.CommitEvidence(evpool.AddMockEvidence(height, addr))
block.Evidence.Evidence = evpool.CommittedEvidenceList
block.Evidence.Evidence = []types.Evidence{ev, ev2}
block.EvidenceHash = block.Evidence.Hash()
err := blockExec.ValidateBlock(state, block)
@@ -282,8 +291,16 @@ func TestValidateFailBlockOnCommittedEvidence(t *testing.T) {
func TestValidateAlreadyPendingEvidence(t *testing.T) {
var height int64 = 1
state, stateDB, _ := makeState(2, int(height))
addr, _ := state.Validators.GetByIndex(0)
addr2, _ := state.Validators.GetByIndex(1)
ev := types.NewMockEvidence(height, defaultTestTime, addr)
ev2 := types.NewMockEvidence(height, defaultTestTime, addr2)
evpool := &mocks.EvidencePool{}
evpool.On("IsPending", ev).Return(false)
evpool.On("IsPending", ev2).Return(true)
evpool.On("IsCommitted", mock.AnythingOfType("types.MockEvidence")).Return(false)
evpool := evmock.NewDefaultEvidencePool()
blockExec := sm.NewBlockExecutor(
stateDB, log.TestingLogger(),
nil,
@@ -291,13 +308,8 @@ func TestValidateAlreadyPendingEvidence(t *testing.T) {
evpool)
// A block with a couple pieces of evidence passes.
block := makeBlock(state, height)
addr, _ := state.Validators.GetByIndex(0)
addr2, _ := state.Validators.GetByIndex(0)
// add pending evidence
pendingEv := evpool.AddMockEvidence(height, addr)
// add evidence that hasn't seen before
ev := types.NewMockEvidence(height, time.Now(), addr2)
block.Evidence.Evidence = []types.Evidence{pendingEv, ev}
// add one evidence seen before and one evidence that hasn't
block.Evidence.Evidence = []types.Evidence{ev, ev2}
block.EvidenceHash = block.Evidence.Hash()
err := blockExec.ValidateBlock(state, block)