Merge branch 'master' into marko/remove-apphash

This commit is contained in:
Marko
2021-04-27 09:48:34 +00:00
committed by GitHub
112 changed files with 4146 additions and 1368 deletions
+3 -2
View File
@@ -7,6 +7,7 @@ import (
"crypto/rand"
"encoding/hex"
"math"
mrand "math/rand"
"os"
"reflect"
"testing"
@@ -643,7 +644,7 @@ func TestBlockIDValidateBasic(t *testing.T) {
}
func TestBlockProtoBuf(t *testing.T) {
h := tmrand.Int63()
h := mrand.Int63()
c1 := randCommit(time.Now())
b1 := MakeBlock(h, []Tx{Tx([]byte{1})}, &Commit{Signatures: []CommitSig{}}, []Evidence{})
b1.ProposerAddress = tmrand.Bytes(crypto.AddressSize)
@@ -751,7 +752,7 @@ func TestEvidenceDataProtoBuf(t *testing.T) {
func makeRandHeader() Header {
chainID := "test"
t := time.Now()
height := tmrand.Int63()
height := mrand.Int63()
randBytes := tmrand.Bytes(tmhash.Size)
randAddress := tmrand.Bytes(crypto.AddressSize)
h := Header{
+4 -5
View File
@@ -3,7 +3,7 @@ package types
import (
"context"
"fmt"
"math/rand"
mrand "math/rand"
"testing"
"time"
@@ -13,7 +13,6 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
tmpubsub "github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
tmrand "github.com/tendermint/tendermint/libs/rand"
)
func TestEventBusPublishEventTx(t *testing.T) {
@@ -410,7 +409,7 @@ func BenchmarkEventBus(b *testing.B) {
func benchmarkEventBus(numClients int, randQueries bool, randEvents bool, b *testing.B) {
// for random* functions
rand.Seed(time.Now().Unix())
mrand.Seed(time.Now().Unix())
eventBus := NewEventBusWithBufferCapacity(0) // set buffer capacity to 0 so we are not testing cache
err := eventBus.Start()
@@ -476,7 +475,7 @@ var events = []string{
EventVote}
func randEvent() string {
return events[tmrand.Intn(len(events))]
return events[mrand.Intn(len(events))]
}
var queries = []tmpubsub.Query{
@@ -494,5 +493,5 @@ var queries = []tmpubsub.Query{
EventQueryVote}
func randQuery() tmpubsub.Query {
return queries[tmrand.Intn(len(queries))]
return queries[mrand.Intn(len(queries))]
}
+20 -5
View File
@@ -296,7 +296,10 @@ func (l *LightClientAttackEvidence) ConflictingHeaderIsInvalid(trustedHeader *He
// with evidence that have the same conflicting header and common height but different permutations
// of validator commit signatures. The reason for this is that we don't want to allow several
// permutations of the same evidence to be committed on chain. Ideally we commit the header with the
// most commit signatures (captures the most byzantine validators) but anything greater than 1/3 is sufficient.
// most commit signatures (captures the most byzantine validators) but anything greater than 1/3 is
// sufficient.
// TODO: We should change the hash to include the commit, header, total voting power, byzantine
// validators and timestamp
func (l *LightClientAttackEvidence) Hash() []byte {
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutVarint(buf, l.CommonHeight)
@@ -315,8 +318,14 @@ func (l *LightClientAttackEvidence) Height() int64 {
// String returns a string representation of LightClientAttackEvidence
func (l *LightClientAttackEvidence) String() string {
return fmt.Sprintf("LightClientAttackEvidence{ConflictingBlock: %v, CommonHeight: %d}",
l.ConflictingBlock.String(), l.CommonHeight)
return fmt.Sprintf(`LightClientAttackEvidence{
ConflictingBlock: %v,
CommonHeight: %d,
ByzatineValidators: %v,
TotalVotingPower: %d,
Timestamp: %v}#%X`,
l.ConflictingBlock.String(), l.CommonHeight, l.ByzantineValidators,
l.TotalVotingPower, l.Timestamp, l.Hash())
}
// Time returns the time of the common block where the infraction leveraged off.
@@ -335,8 +344,8 @@ func (l *LightClientAttackEvidence) ValidateBasic() error {
return errors.New("conflicting block missing header")
}
if err := l.ConflictingBlock.ValidateBasic(l.ConflictingBlock.ChainID); err != nil {
return fmt.Errorf("invalid conflicting light block: %w", err)
if l.TotalVotingPower <= 0 {
return errors.New("negative or zero total voting power")
}
if l.CommonHeight <= 0 {
@@ -351,6 +360,10 @@ func (l *LightClientAttackEvidence) ValidateBasic() error {
l.CommonHeight, l.ConflictingBlock.Height)
}
if err := l.ConflictingBlock.ValidateBasic(l.ConflictingBlock.ChainID); err != nil {
return fmt.Errorf("invalid conflicting light block: %w", err)
}
return nil
}
@@ -422,6 +435,8 @@ func (evl EvidenceList) Hash() []byte {
// the Evidence size is capped.
evidenceBzs := make([][]byte, len(evl))
for i := 0; i < len(evl); i++ {
// TODO: We should change this to the hash. Using bytes contains some unexported data that
// may cause different hashes
evidenceBzs[i] = evl[i].Bytes()
}
return merkle.HashFromByteSlices(evidenceBzs)
+59 -48
View File
@@ -3,6 +3,7 @@ package types
import (
"context"
"math"
mrand "math/rand"
"testing"
"time"
@@ -89,9 +90,11 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) {
}
}
func TestLightClientAttackEvidence(t *testing.T) {
func TestLightClientAttackEvidenceBasic(t *testing.T) {
height := int64(5)
voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, 10, 1)
commonHeight := height - 1
nValidators := 10
voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, nValidators, 1)
header := makeHeaderRandom()
header.Height = height
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
@@ -105,56 +108,52 @@ func TestLightClientAttackEvidence(t *testing.T) {
},
ValidatorSet: valSet,
},
CommonHeight: height - 1,
CommonHeight: commonHeight,
TotalVotingPower: valSet.TotalVotingPower(),
Timestamp: header.Time,
ByzantineValidators: valSet.Validators[:nValidators/2],
}
assert.NotNil(t, lcae.String())
assert.NotNil(t, lcae.Hash())
// only 7 validators sign
differentCommit, err := MakeCommit(blockID, height, 1, voteSet, privVals[:7], defaultVoteTime)
require.NoError(t, err)
differentEv := &LightClientAttackEvidence{
ConflictingBlock: &LightBlock{
SignedHeader: &SignedHeader{
Header: header,
Commit: differentCommit,
},
ValidatorSet: valSet,
},
CommonHeight: height - 1,
}
assert.Equal(t, lcae.Hash(), differentEv.Hash())
// different header hash
differentHeader := makeHeaderRandom()
differentEv = &LightClientAttackEvidence{
ConflictingBlock: &LightBlock{
SignedHeader: &SignedHeader{
Header: differentHeader,
Commit: differentCommit,
},
ValidatorSet: valSet,
},
CommonHeight: height - 1,
}
assert.NotEqual(t, lcae.Hash(), differentEv.Hash())
// different common height should produce a different header
differentEv = &LightClientAttackEvidence{
ConflictingBlock: &LightBlock{
SignedHeader: &SignedHeader{
Header: header,
Commit: differentCommit,
},
ValidatorSet: valSet,
},
CommonHeight: height - 2,
}
assert.NotEqual(t, lcae.Hash(), differentEv.Hash())
assert.Equal(t, lcae.Height(), int64(4)) // Height should be the common Height
assert.Equal(t, lcae.Height(), commonHeight) // Height should be the common Height
assert.NotNil(t, lcae.Bytes())
// maleate evidence to test hash uniqueness
testCases := []struct {
testName string
malleateEvidence func(*LightClientAttackEvidence)
}{
{"Different header", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.Header = makeHeaderRandom() }},
{"Different common height", func(ev *LightClientAttackEvidence) {
ev.CommonHeight = height + 1
}},
}
for _, tc := range testCases {
lcae := &LightClientAttackEvidence{
ConflictingBlock: &LightBlock{
SignedHeader: &SignedHeader{
Header: header,
Commit: commit,
},
ValidatorSet: valSet,
},
CommonHeight: commonHeight,
TotalVotingPower: valSet.TotalVotingPower(),
Timestamp: header.Time,
ByzantineValidators: valSet.Validators[:nValidators/2],
}
hash := lcae.Hash()
tc.malleateEvidence(lcae)
assert.NotEqual(t, hash, lcae.Hash(), tc.testName)
}
}
func TestLightClientAttackEvidenceValidation(t *testing.T) {
height := int64(5)
voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, 10, 1)
commonHeight := height - 1
nValidators := 10
voteSet, valSet, privVals := randVoteSet(height, 1, tmproto.PrecommitType, nValidators, 1)
header := makeHeaderRandom()
header.Height = height
header.ValidatorsHash = valSet.Hash()
@@ -169,7 +168,10 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) {
},
ValidatorSet: valSet,
},
CommonHeight: height - 1,
CommonHeight: commonHeight,
TotalVotingPower: valSet.TotalVotingPower(),
Timestamp: header.Time,
ByzantineValidators: valSet.Validators[:nValidators/2],
}
assert.NoError(t, lcae.ValidateBasic())
@@ -178,16 +180,22 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) {
malleateEvidence func(*LightClientAttackEvidence)
expectErr bool
}{
{"Good DuplicateVoteEvidence", func(ev *LightClientAttackEvidence) {}, false},
{"Good LightClientAttackEvidence", func(ev *LightClientAttackEvidence) {}, false},
{"Negative height", func(ev *LightClientAttackEvidence) { ev.CommonHeight = -10 }, true},
{"Height is greater than divergent block", func(ev *LightClientAttackEvidence) {
ev.CommonHeight = height + 1
}, true},
{"Height is equal to the divergent block", func(ev *LightClientAttackEvidence) {
ev.CommonHeight = height
}, false},
{"Nil conflicting header", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock.Header = nil }, true},
{"Nil conflicting blocl", func(ev *LightClientAttackEvidence) { ev.ConflictingBlock = nil }, true},
{"Nil validator set", func(ev *LightClientAttackEvidence) {
ev.ConflictingBlock.ValidatorSet = &ValidatorSet{}
}, true},
{"Negative total voting power", func(ev *LightClientAttackEvidence) {
ev.TotalVotingPower = -1
}, true},
}
for _, tc := range testCases {
tc := tc
@@ -200,7 +208,10 @@ func TestLightClientAttackEvidenceValidation(t *testing.T) {
},
ValidatorSet: valSet,
},
CommonHeight: height - 1,
CommonHeight: commonHeight,
TotalVotingPower: valSet.TotalVotingPower(),
Timestamp: header.Time,
ByzantineValidators: valSet.Validators[:nValidators/2],
}
tc.malleateEvidence(lcae)
if tc.expectErr {
@@ -246,7 +257,7 @@ func makeHeaderRandom() *Header {
return &Header{
Version: version.Consensus{Block: version.BlockProtocol, App: 1},
ChainID: tmrand.Str(12),
Height: int64(tmrand.Uint16()) + 1,
Height: int64(mrand.Uint32() + 1),
Time: time.Now(),
LastBlockID: makeBlockIDRandom(),
LastCommitHash: crypto.CRandBytes(tmhash.Size),
+1 -1
View File
@@ -149,7 +149,7 @@ func (sh SignedHeader) ValidateBasic(chainID string) error {
if sh.Commit.Height != sh.Height {
return fmt.Errorf("header and commit height mismatch: %d vs %d", sh.Height, sh.Commit.Height)
}
if hhash, chash := sh.Hash(), sh.Commit.BlockID.Hash; !bytes.Equal(hhash, chash) {
if hhash, chash := sh.Header.Hash(), sh.Commit.BlockID.Hash; !bytes.Equal(hhash, chash) {
return fmt.Errorf("commit signs block %X, header is block %X", chash, hhash)
}
+4 -2
View File
@@ -152,11 +152,13 @@ func TestSignedHeaderValidateBasic(t *testing.T) {
Header: tc.shHeader,
Commit: tc.shCommit,
}
assert.Equal(
err := sh.ValidateBasic(validSignedHeader.Header.ChainID)
assert.Equalf(
t,
tc.expectErr,
sh.ValidateBasic(validSignedHeader.Header.ChainID) != nil,
err != nil,
"Validate Basic had an unexpected result",
err,
)
})
}
+3
View File
@@ -7,6 +7,7 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/crypto/sr25519"
"github.com/tendermint/tendermint/crypto/tmhash"
tmstrings "github.com/tendermint/tendermint/libs/strings"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
@@ -24,11 +25,13 @@ const (
ABCIPubKeyTypeEd25519 = ed25519.KeyType
ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
ABCIPubKeyTypeSr25519 = sr25519.KeyType
)
var ABCIPubKeyTypesToNames = map[string]string{
ABCIPubKeyTypeEd25519: ed25519.PubKeyName,
ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName,
ABCIPubKeyTypeSr25519: sr25519.PubKeyName,
}
// ConsensusParams contains consensus critical parameters that determine the
+19
View File
@@ -14,6 +14,7 @@ import (
var (
valEd25519 = []string{ABCIPubKeyTypeEd25519}
valSecp256k1 = []string{ABCIPubKeyTypeSecp256k1}
valSr25519 = []string{ABCIPubKeyTypeSr25519}
)
func TestConsensusParamsValidation(t *testing.T) {
@@ -129,7 +130,25 @@ func TestConsensusParamsUpdate(t *testing.T) {
},
makeParams(100, 200, 300, 50, valSecp256k1),
},
{
makeParams(1, 2, 3, 0, valEd25519),
&tmproto.ConsensusParams{
Block: &tmproto.BlockParams{
MaxBytes: 100,
MaxGas: 200,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 300,
MaxAgeDuration: time.Duration(300),
MaxBytes: 50,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: valSr25519,
},
}, makeParams(100, 200, 300, 50, valSr25519),
},
}
for _, tc := range testCases {
assert.Equal(t, tc.updatedParams, tc.params.UpdateConsensusParams(tc.updates))
}
+2 -1
View File
@@ -2,6 +2,7 @@ package types
import (
"bytes"
mrand "math/rand"
"testing"
"github.com/stretchr/testify/assert"
@@ -21,7 +22,7 @@ func makeTxs(cnt, size int) Txs {
}
func randInt(low, high int) int {
off := tmrand.Int() % (high - low)
off := mrand.Int() % (high - low)
return low + off
}
+3 -2
View File
@@ -5,11 +5,11 @@ import (
"context"
"errors"
"fmt"
mrand "math/rand"
"strings"
"github.com/tendermint/tendermint/crypto"
ce "github.com/tendermint/tendermint/crypto/encoding"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
@@ -183,7 +183,8 @@ func RandValidator(randPower bool, minPower int64) (*Validator, PrivValidator) {
privVal := NewMockPV()
votePower := minPower
if randPower {
votePower += int64(tmrand.Uint32())
// nolint:gosec // G404: Use of weak random number generator
votePower += int64(mrand.Uint32())
}
pubKey, err := privVal.GetPubKey(context.Background())
if err != nil {
+9 -2
View File
@@ -1028,7 +1028,9 @@ func (vals *ValidatorSet) ToProto() (*tmproto.ValidatorSet, error) {
}
vp.Proposer = valProposer
vp.TotalVotingPower = vals.totalVotingPower
// NOTE: Sometimes we use the bytes of the proto form as a hash. This means that we need to
// be consistent with cached data
vp.TotalVotingPower = 0
return vp, nil
}
@@ -1059,7 +1061,12 @@ func ValidatorSetFromProto(vp *tmproto.ValidatorSet) (*ValidatorSet, error) {
vals.Proposer = p
vals.totalVotingPower = vp.GetTotalVotingPower()
// NOTE: We can't trust the total voting power given to us by other peers. If someone were to
// inject a non-zeo value that wasn't the correct voting power we could assume a wrong total
// power hence we need to recompute it.
// FIXME: We should look to remove TotalVotingPower from proto or add it in the validators hash
// so we don't have to do this
vals.TotalVotingPower()
return vals, vals.ValidateBasic()
}
+14 -13
View File
@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"math"
"math/rand"
"sort"
"strings"
"testing"
@@ -352,10 +353,10 @@ func TestProposerSelection3(t *testing.T) {
// times is usually 1
times := int32(1)
mod := (tmrand.Int() % 5) + 1
if tmrand.Int()%mod > 0 {
mod := (rand.Int() % 5) + 1
if rand.Int()%mod > 0 {
// sometimes its up to 5
times = (tmrand.Int31() % 4) + 1
times = (rand.Int31() % 4) + 1
}
vset.IncrementProposerPriority(times)
@@ -376,8 +377,8 @@ func randPubKey() crypto.PubKey {
func randValidator(totalVotingPower int64) *Validator {
// this modulo limits the ProposerPriority/VotingPower to stay in the
// bounds of MaxTotalVotingPower minus the already existing voting power:
val := NewValidator(randPubKey(), int64(tmrand.Uint64()%uint64(MaxTotalVotingPower-totalVotingPower)))
val.ProposerPriority = tmrand.Int64() % (MaxTotalVotingPower - totalVotingPower)
val := NewValidator(randPubKey(), int64(rand.Uint64()%uint64(MaxTotalVotingPower-totalVotingPower)))
val.ProposerPriority = rand.Int63() % (MaxTotalVotingPower - totalVotingPower)
return val
}
@@ -882,7 +883,7 @@ func permutation(valList []testVal) []testVal {
return nil
}
permList := make([]testVal, len(valList))
perm := tmrand.Perm(len(valList))
perm := rand.Perm(len(valList))
for i, v := range perm {
permList[v] = valList[i]
}
@@ -1284,14 +1285,14 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
const maxPower = 1000
var nOld, nDel, nChanged, nAdd int
nOld = int(tmrand.Uint()%uint(nBase)) + 1
nOld = int(uint(rand.Int())%uint(nBase)) + 1
if nBase-nOld > 0 {
nDel = int(tmrand.Uint() % uint(nBase-nOld))
nDel = int(uint(rand.Int()) % uint(nBase-nOld))
}
nChanged = nBase - nOld - nDel
if nAddMax > 0 {
nAdd = tmrand.Int()%nAddMax + 1
nAdd = rand.Int()%nAddMax + 1
}
cfg := testVSetCfg{}
@@ -1303,12 +1304,12 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
cfg.expectedVals = make([]testVal, nBase-nDel+nAdd)
for i := 0; i < nBase; i++ {
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), int64(tmrand.Uint()%maxPower + 1)}
cfg.startVals[i] = testVal{fmt.Sprintf("v%d", i), int64(uint(rand.Int())%maxPower + 1)}
if i < nOld {
cfg.expectedVals[i] = cfg.startVals[i]
}
if i >= nOld && i < nOld+nChanged {
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), int64(tmrand.Uint()%maxPower + 1)}
cfg.updatedVals[i-nOld] = testVal{fmt.Sprintf("v%d", i), int64(uint(rand.Int())%maxPower + 1)}
cfg.expectedVals[i] = cfg.updatedVals[i-nOld]
}
if i >= nOld+nChanged {
@@ -1317,7 +1318,7 @@ func randTestVSetCfg(t *testing.T, nBase, nAddMax int) testVSetCfg {
}
for i := nBase; i < nBase+nAdd; i++ {
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), int64(tmrand.Uint()%maxPower + 1)}
cfg.addedVals[i-nBase] = testVal{fmt.Sprintf("v%d", i), int64(uint(rand.Int())%maxPower + 1)}
cfg.expectedVals[i-nDel] = cfg.addedVals[i-nBase]
}
@@ -1398,7 +1399,7 @@ func TestValSetUpdatePriorityOrderTests(t *testing.T) {
func verifyValSetUpdatePriorityOrder(t *testing.T, valSet *ValidatorSet, cfg testVSetCfg, nMaxElections int32) {
// Run election up to nMaxElections times, sort validators by priorities
valSet.IncrementProposerPriority(tmrand.Int31()%nMaxElections + 1)
valSet.IncrementProposerPriority(rand.Int31()%nMaxElections + 1)
// apply the changes, get the updated validators, sort by priorities
applyChangesToValSet(t, nil, valSet, cfg.addedVals, cfg.updatedVals, cfg.deletedVals)