evidence: introduce time.Duration to evidence params (#4254)

* evidence: introduce time.Duration to evidence params

- add time.duration to evidence
- this pr is taking pr #2606 and updating it to use both time and height

- closes #2565

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* fix testing and genesis cfg in signer harness

* remove debugging fmt

* change maxageheight to maxagenumblocks, rename other things to block instead of height

* further check of duration

* check duration to not send peers outdated evidence

* change some lines, onward and upward

* refactor evidence package

* add a changelog pending entry

* make mockbadevidence have time and use it

* add what could possibly be called a test case

* remove mockbadevidence and mockgoodevidence in favor of mockevidence

* add a comment for err that is returned

* add a changelog for removal of good & bad evidence

* add a test for adding evidence

* fix test

* add ev to types in testcase

* Update evidence/pool_test.go

Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>

* Update evidence/pool_test.go

Co-Authored-By: Anton Kaliaev <anton.kalyaev@gmail.com>

* fix tests

* fix linting

Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
This commit is contained in:
Marko
2020-01-08 10:46:37 +01:00
committed by GitHub
co-authored by Anton Kaliaev
parent d7f4ce30ca
commit 6d91c1faf4
26 changed files with 540 additions and 382 deletions
+2 -2
View File
@@ -126,8 +126,8 @@ func TestBeginBlockByzantineValidators(t *testing.T) {
height1, idx1, val1 := int64(8), 0, state.Validators.Validators[0].Address
height2, idx2, val2 := int64(3), 1, state.Validators.Validators[1].Address
ev1 := types.NewMockGoodEvidence(height1, idx1, val1)
ev2 := types.NewMockGoodEvidence(height2, idx2, val2)
ev1 := types.NewMockEvidence(height1, time.Now(), idx1, val1)
ev2 := types.NewMockEvidence(height2, time.Now(), idx2, val2)
now := tmtime.Now()
valSet := state.Validators
+3 -1
View File
@@ -3,6 +3,7 @@ package state_test
import (
"bytes"
"fmt"
"time"
dbm "github.com/tendermint/tm-db"
@@ -161,7 +162,8 @@ func makeConsensusParams(
TimeIotaMs: blockTimeIotaMs,
},
Evidence: types.EvidenceParams{
MaxAge: evidenceAge,
MaxAgeNumBlocks: evidenceAge,
MaxAgeDuration: time.Duration(evidenceAge),
},
}
}
+5 -3
View File
@@ -7,6 +7,7 @@ import (
"math/big"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -985,7 +986,7 @@ func TestConsensusParamsChangesSaveLoad(t *testing.T) {
func TestApplyUpdates(t *testing.T) {
initParams := makeConsensusParams(1, 2, 3, 4)
const maxAge int64 = 66
cases := [...]struct {
init types.ConsensusParams
updates abci.ConsensusParams
@@ -1004,10 +1005,11 @@ func TestApplyUpdates(t *testing.T) {
3: {initParams,
abci.ConsensusParams{
Evidence: &abci.EvidenceParams{
MaxAge: 66,
MaxAgeNumBlocks: maxAge,
MaxAgeDuration: time.Duration(maxAge),
},
},
makeConsensusParams(1, 2, 3, 66)},
makeConsensusParams(1, 2, 3, maxAge)},
}
for i, tc := range cases {
+13 -5
View File
@@ -158,13 +158,21 @@ func validateBlock(evidencePool EvidencePool, stateDB dbm.DB, state State, block
// - it is internally consistent
// - it was properly signed by the alleged equivocator
func VerifyEvidence(stateDB dbm.DB, state State, evidence types.Evidence) error {
height := state.LastBlockHeight
var (
height = state.LastBlockHeight
evidenceParams = state.ConsensusParams.Evidence
)
evidenceAge := height - evidence.Height()
maxAge := state.ConsensusParams.Evidence.MaxAge
if evidenceAge > maxAge {
ageNumBlocks := height - evidence.Height()
if ageNumBlocks > evidenceParams.MaxAgeNumBlocks {
return fmt.Errorf("evidence from height %d is too old. Min height is %d",
evidence.Height(), height-maxAge)
evidence.Height(), height-evidenceParams.MaxAgeNumBlocks)
}
ageDuration := state.LastBlockTime.Sub(evidence.Time())
if ageDuration > evidenceParams.MaxAgeDuration {
return fmt.Errorf("evidence created at %v has expired. Evidence can not be older than: %v",
evidence.Time(), state.LastBlockTime.Add(evidenceParams.MaxAgeDuration))
}
valset, err := LoadValidators(stateDB, evidence.Height())
+2 -2
View File
@@ -201,7 +201,7 @@ func TestValidateBlockEvidence(t *testing.T) {
for height := int64(1); height < validationTestsStopHeight; height++ {
proposerAddr := state.Validators.GetProposer().Address
proposerIdx, _ := state.Validators.GetByAddress(proposerAddr)
goodEvidence := types.NewMockGoodEvidence(height, proposerIdx, proposerAddr)
goodEvidence := types.NewMockEvidence(height, time.Now(), proposerIdx, proposerAddr)
if height > 1 {
/*
A block with too much evidence fails
@@ -254,7 +254,7 @@ func TestValidateFailBlockOnCommittedEvidence(t *testing.T) {
// A block with a couple pieces of evidence passes.
block := makeBlock(state, height)
addr, _ := state.Validators.GetByIndex(0)
alreadyCommittedEvidence := types.NewMockGoodEvidence(height, 0, addr)
alreadyCommittedEvidence := types.NewMockEvidence(height, time.Now(), 0, addr)
block.Evidence.Evidence = []types.Evidence{alreadyCommittedEvidence}
block.EvidenceHash = block.Evidence.Hash()
err := blockExec.ValidateBlock(state, block)