header: check block protocol (#5340)

## Description

Check block protocol version in header validate basic. 

I tried searching for where we check the P2P protocol version but was unable to find it. When we check compatibility with a node we check we both have the same block protocol and are on the same network, but we do not check if we are on the same P2P protocol. It makes sense if there is a handshake change because we would not be able to establish a secure connection, but a p2p protocol version bump may be because of a p2p message change, which would go unnoticed until that message is sent over the wire.  Is this purposeful?

Closes: #4790
This commit is contained in:
Marko
2020-09-09 11:13:18 +02:00
committed by GitHub
parent c237e06078
commit 6ab2a19088
9 changed files with 39 additions and 11 deletions

View File

@@ -19,6 +19,7 @@ import (
tmsync "github.com/tendermint/tendermint/libs/sync"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/version"
)
const (
@@ -375,6 +376,9 @@ func (h *Header) Populate(
//
// NOTE: Timestamp validation is subtle and handled elsewhere.
func (h Header) ValidateBasic() error {
if h.Version.Block != version.BlockProtocol {
return fmt.Errorf("block protocol is incorrect: got: %d, want: %d ", h.Version.Block, version.BlockProtocol)
}
if len(h.ChainID) > MaxChainIDLen {
return fmt.Errorf("chainID is too long; got: %d, max: %d", len(h.ChainID), MaxChainIDLen)
}

View File

@@ -22,8 +22,9 @@ import (
"github.com/tendermint/tendermint/libs/bytes"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/tendermint/tendermint/proto/tendermint/version"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/tendermint/tendermint/version"
)
func TestMain(m *testing.M) {
@@ -86,6 +87,9 @@ func TestBlockValidateBasic(t *testing.T) {
{"Tampered EvidenceHash", func(blk *Block) {
blk.EvidenceHash = []byte("something else")
}, true},
{"Incorrect block protocol version", func(blk *Block) {
blk.Version.Block = 1
}, true},
}
for i, tc := range testCases {
tc := tc
@@ -260,7 +264,7 @@ func TestHeaderHash(t *testing.T) {
expectHash bytes.HexBytes
}{
{"Generates expected hash", &Header{
Version: version.Consensus{Block: 1, App: 2},
Version: tmversion.Consensus{Block: 1, App: 2},
ChainID: "chainId",
Height: 3,
Time: time.Date(2019, 10, 13, 16, 14, 44, 0, time.UTC),
@@ -277,7 +281,7 @@ func TestHeaderHash(t *testing.T) {
}, hexBytesFromString("F740121F553B5418C3EFBD343C2DBFE9E007BB67B0D020A0741374BAB65242A4")},
{"nil header yields nil", nil, nil},
{"nil ValidatorsHash yields nil", &Header{
Version: version.Consensus{Block: 1, App: 2},
Version: tmversion.Consensus{Block: 1, App: 2},
ChainID: "chainId",
Height: 3,
Time: time.Date(2019, 10, 13, 16, 14, 44, 0, time.UTC),
@@ -317,7 +321,7 @@ func TestHeaderHash(t *testing.T) {
bz, err := gogotypes.StdTimeMarshal(f)
require.NoError(t, err)
byteSlices = append(byteSlices, bz)
case version.Consensus:
case tmversion.Consensus:
bz, err := f.Marshal()
require.NoError(t, err)
byteSlices = append(byteSlices, bz)
@@ -352,7 +356,7 @@ func TestMaxHeaderBytes(t *testing.T) {
timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
h := Header{
Version: version.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
Version: tmversion.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
ChainID: maxChainID,
Height: math.MaxInt64,
Time: timestamp,
@@ -696,7 +700,7 @@ func makeRandHeader() Header {
randBytes := tmrand.Bytes(tmhash.Size)
randAddress := tmrand.Bytes(crypto.AddressSize)
h := Header{
Version: version.Consensus{Block: 1, App: 1},
Version: tmversion.Consensus{Block: version.BlockProtocol, App: 1},
ChainID: chainID,
Height: height,
Time: t,

View File

@@ -8,7 +8,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/proto/tendermint/version"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/version"
)
func TestLightBlockValidateBasic(t *testing.T) {
@@ -18,6 +19,7 @@ func TestLightBlockValidateBasic(t *testing.T) {
header.Height = commit.Height
header.LastBlockID = commit.BlockID
header.ValidatorsHash = vals.Hash()
header.Version.Block = version.BlockProtocol
vals2, _ := RandValidatorSet(3, 1)
vals3 := vals.Copy()
vals3.Proposer = &Validator{}
@@ -61,6 +63,7 @@ func TestLightBlockProtobuf(t *testing.T) {
vals, _ := RandValidatorSet(5, 1)
header.Height = commit.Height
header.LastBlockID = commit.BlockID
header.Version.Block = version.BlockProtocol
header.ValidatorsHash = vals.Hash()
vals3 := vals.Copy()
vals3.Proposer = &Validator{}
@@ -112,7 +115,7 @@ func TestSignedHeaderValidateBasic(t *testing.T) {
chainID := "𠜎"
timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
h := Header{
Version: version.Consensus{Block: math.MaxInt64, App: math.MaxInt64},
Version: tmversion.Consensus{Block: version.BlockProtocol, App: math.MaxInt64},
ChainID: chainID,
Height: commit.Height,
Time: timestamp,

View File

@@ -5,6 +5,8 @@ import (
"time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
"github.com/tendermint/tendermint/version"
)
func MakeCommit(blockID BlockID, height int64, round int32,
@@ -85,7 +87,8 @@ func MakeVote(
func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
block := &Block{
Header: Header{
Height: height,
Version: tmversion.Consensus{Block: version.BlockProtocol, App: 0},
Height: height,
},
Data: Data{
Txs: txs,