Browse Source

create tests for validating evidence

pull/4743/head
Callum 5 years ago
parent
commit
08aa4765b0
3 changed files with 69 additions and 7 deletions
  1. +1
    -1
      state/validation.go
  2. +61
    -5
      state/validation_test.go
  3. +7
    -1
      types/evidence.go

+ 1
- 1
state/validation.go View File

@ -137,7 +137,7 @@ func validateBlock(evidencePool EvidencePool, stateDB dbm.DB, state State, block
return types.NewErrEvidenceInvalid(ev, errors.New("evidence was already committed"))
}
if evidencePool.IsPending(ev) {
return nil
continue
}
}
if err := VerifyEvidence(stateDB, state, ev, &block.Header); err != nil {


+ 61
- 5
state/validation_test.go View File

@ -8,6 +8,7 @@ import (
"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/mock"
sm "github.com/tendermint/tendermint/state"
@ -98,7 +99,7 @@ func TestValidateBlockCommit(t *testing.T) {
log.TestingLogger(),
proxyApp.Consensus(),
mock.Mempool{},
sm.MockEvidencePool{},
evmock.NewDefaultEvidencePool(),
)
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil)
wrongSigsCommit := types.NewCommit(1, 0, types.BlockID{}, nil)
@ -205,7 +206,7 @@ func TestValidateBlockEvidence(t *testing.T) {
log.TestingLogger(),
proxyApp.Consensus(),
mock.Mempool{},
sm.MockEvidencePool{},
evmock.NewDefaultEvidencePool(),
)
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil)
@ -260,15 +261,70 @@ func TestValidateFailBlockOnCommittedEvidence(t *testing.T) {
var height int64 = 1
state, stateDB, _ := makeState(1, int(height))
blockExec := sm.NewBlockExecutor(stateDB, log.TestingLogger(), nil, nil, mockEvPoolAlwaysCommitted{})
evpool := evmock.NewDefaultEvidencePool()
blockExec := sm.NewBlockExecutor(
stateDB, log.TestingLogger(),
nil,
nil,
evpool)
// A block with a couple pieces of evidence passes.
block := makeBlock(state, height)
addr, _ := state.Validators.GetByIndex(0)
alreadyCommittedEvidence := types.NewMockEvidence(height, time.Now(), addr)
block.Evidence.Evidence = []types.Evidence{alreadyCommittedEvidence}
evpool.CommitEvidence(evpool.AddMockEvidence(height, addr))
block.Evidence.Evidence = evpool.CommittedEvidenceList
block.EvidenceHash = block.Evidence.Hash()
err := blockExec.ValidateBlock(state, block)
require.Error(t, err)
require.IsType(t, err, &types.ErrEvidenceInvalid{})
}
func TestValidateAlreadyPendingEvidence(t *testing.T) {
var height int64 = 1
state, stateDB, _ := makeState(2, int(height))
evpool := evmock.NewDefaultEvidencePool()
blockExec := sm.NewBlockExecutor(
stateDB, log.TestingLogger(),
nil,
nil,
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}
block.EvidenceHash = block.Evidence.Hash()
err := blockExec.ValidateBlock(state, block)
require.NoError(t, err)
}
// TODO: prevent committing duplicate votes
//func TestValidateDuplicateEvidenceShouldFail(t *testing.T) {
// var height int64 = 1
// var evidence []types.Evidence
// state, stateDB, _ := makeState(1, int(height))
//
// evpool := evmock.NewDefaultEvidencePool()
// blockExec := sm.NewBlockExecutor(
// stateDB, log.TestingLogger(),
// nil,
// nil,
// evpool)
// // A block with a couple pieces of evidence passes.
// block := makeBlock(state, height)
// addr, _ := state.Validators.GetByIndex(0)
// for i := 0; i < 2; i++ {
// evidence = append(evidence, types.NewMockEvidence(height, time.Now(), addr))
// }
// block.Evidence.Evidence = evidence
// block.EvidenceHash = block.Evidence.Hash()
// err := blockExec.ValidateBlock(state, block)
//
// require.Error(t, err)
//}

+ 7
- 1
types/evidence.go View File

@ -975,7 +975,13 @@ func (e MockEvidence) Bytes() []byte {
return []byte(fmt.Sprintf("%d-%x-%s",
e.EvidenceHeight, e.EvidenceAddress, e.EvidenceTime))
}
func (e MockEvidence) Verify(chainID string, pubKey crypto.PubKey) error { return nil }
func (e MockEvidence) Verify(chainID string, pubKey crypto.PubKey) error {
if !bytes.Equal(pubKey.Address(), e.Address()) {
return fmt.Errorf("address (%X) doesn't match pubkey (%v - %X)",
e.Address(), pubKey, pubKey.Address())
}
return nil
}
func (e MockEvidence) Equal(ev Evidence) bool {
e2 := ev.(MockEvidence)
return e.EvidenceHeight == e2.EvidenceHeight &&


Loading…
Cancel
Save