2018-06-20 17:35:30 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
max-bytes PR follow-up (#2318)
* ReapMaxTxs: return all txs if max is negative
this mirrors ReapMaxBytes behavior
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950
* increase MaxAminoOverheadForBlock
tested with:
```
func TestMaxAminoOverheadForBlock(t *testing.T) {
maxChainID := ""
for i := 0; i < MaxChainIDLen; i++ {
maxChainID += "𠜎"
}
h := Header{
ChainID: maxChainID,
Height: 10,
Time: time.Now().UTC(),
NumTxs: 100,
TotalTxs: 200,
LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: tmhash.Sum([]byte("proposer_address")),
}
b := Block{
Header: h,
Data: Data{Txs: makeTxs(10000, 100)},
Evidence: EvidenceData{},
LastCommit: &Commit{},
}
bz, err := cdc.MarshalBinary(b)
require.NoError(t, err)
assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1)
}
```
* fix MaxYYY constants calculation
by using math.MaxInt64
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244
* pass mempool filter as an option
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869
* fixes after Dev's comments
2018-09-04 11:46:34 +04:00
|
|
|
"math"
|
2018-06-20 17:35:30 -07:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-07-18 08:38:44 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-20 17:35:30 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-14 16:02:48 +02:00
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
amino "github.com/tendermint/go-amino"
|
2018-10-31 12:42:05 -04:00
|
|
|
"github.com/tendermint/tendermint/crypto"
|
2018-07-18 08:38:44 -07:00
|
|
|
"github.com/tendermint/tendermint/crypto/ed25519"
|
2018-08-08 16:03:58 +04:00
|
|
|
"github.com/tendermint/tendermint/crypto/tmhash"
|
2018-06-20 17:35:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func examplePrevote() *Vote {
|
2018-10-13 01:21:46 +02:00
|
|
|
return exampleVote(byte(PrevoteType))
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func examplePrecommit() *Vote {
|
2018-10-13 01:21:46 +02:00
|
|
|
return exampleVote(byte(PrecommitType))
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func exampleVote(t byte) *Vote {
|
|
|
|
var stamp, err = time.Parse(TimeFormat, "2017-12-25T03:00:01.234Z")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Vote{
|
2018-10-30 17:16:55 +01:00
|
|
|
Type: SignedMsgType(t),
|
|
|
|
Height: 12345,
|
|
|
|
Round: 2,
|
|
|
|
Timestamp: stamp,
|
2018-06-20 17:35:30 -07:00
|
|
|
BlockID: BlockID{
|
2018-08-08 16:03:58 +04:00
|
|
|
Hash: tmhash.Sum([]byte("blockID_hash")),
|
2018-06-20 17:35:30 -07:00
|
|
|
PartsHeader: PartSetHeader{
|
|
|
|
Total: 1000000,
|
2018-08-08 16:03:58 +04:00
|
|
|
Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
|
2018-06-20 17:35:30 -07:00
|
|
|
},
|
|
|
|
},
|
2018-10-31 12:42:05 -04:00
|
|
|
ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
|
2018-10-30 17:16:55 +01:00
|
|
|
ValidatorIndex: 56789,
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 13:01:59 -05:00
|
|
|
// Ensure that Vote and CommitSig have the same encoding.
|
|
|
|
// This ensures using CommitSig isn't a breaking change.
|
|
|
|
// This test will fail and can be removed once CommitSig contains only sigs and
|
|
|
|
// timestamps.
|
|
|
|
func TestVoteEncoding(t *testing.T) {
|
|
|
|
vote := examplePrecommit()
|
|
|
|
commitSig := vote.CommitSig()
|
|
|
|
cdc := amino.NewCodec()
|
|
|
|
bz1 := cdc.MustMarshalBinaryBare(vote)
|
|
|
|
bz2 := cdc.MustMarshalBinaryBare(commitSig)
|
|
|
|
assert.Equal(t, bz1, bz2)
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:35:30 -07:00
|
|
|
func TestVoteSignable(t *testing.T) {
|
|
|
|
vote := examplePrecommit()
|
|
|
|
signBytes := vote.SignBytes("test_chain_id")
|
|
|
|
|
2018-10-25 03:34:01 +02:00
|
|
|
expected, err := cdc.MarshalBinaryLengthPrefixed(CanonicalizeVote("test_chain_id", vote))
|
2018-09-29 01:57:29 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, expected, signBytes, "Got unexpected sign bytes for Vote.")
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
|
2019-02-16 06:12:00 +01:00
|
|
|
func TestVoteSignBytesTestVectors(t *testing.T) {
|
2018-10-13 01:21:46 +02:00
|
|
|
|
|
|
|
tests := []struct {
|
2019-02-16 06:12:00 +01:00
|
|
|
chainID string
|
|
|
|
vote *Vote
|
|
|
|
want []byte
|
2018-10-13 01:21:46 +02:00
|
|
|
}{
|
2019-02-16 06:12:00 +01:00
|
|
|
0: {
|
|
|
|
"", &Vote{},
|
2018-10-13 01:21:46 +02:00
|
|
|
// NOTE: Height and Round are skipped here. This case needs to be considered while parsing.
|
2019-02-16 06:12:00 +01:00
|
|
|
[]byte{0xd, 0x2a, 0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
|
2018-10-13 01:21:46 +02:00
|
|
|
},
|
|
|
|
// with proper (fixed size) height and round (PreCommit):
|
2019-02-16 06:12:00 +01:00
|
|
|
1: {
|
|
|
|
"", &Vote{Height: 1, Round: 1, Type: PrecommitType},
|
2018-10-13 01:21:46 +02:00
|
|
|
[]byte{
|
2019-02-16 06:12:00 +01:00
|
|
|
0x21, // length
|
2018-10-19 14:23:14 -04:00
|
|
|
0x8, // (field_number << 3) | wire_type
|
|
|
|
0x2, // PrecommitType
|
2018-10-18 18:02:20 -04:00
|
|
|
0x11, // (field_number << 3) | wire_type
|
2018-10-19 14:23:14 -04:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
|
|
|
|
0x19, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
|
2019-01-13 23:56:36 +01:00
|
|
|
0x2a, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
// remaining fields (timestamp):
|
2018-10-23 13:21:47 -04:00
|
|
|
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
|
2018-10-13 01:21:46 +02:00
|
|
|
},
|
|
|
|
// with proper (fixed size) height and round (PreVote):
|
2019-02-16 06:12:00 +01:00
|
|
|
2: {
|
|
|
|
"", &Vote{Height: 1, Round: 1, Type: PrevoteType},
|
2018-10-13 01:21:46 +02:00
|
|
|
[]byte{
|
2019-02-16 06:12:00 +01:00
|
|
|
0x21, // length
|
2018-10-19 14:23:14 -04:00
|
|
|
0x8, // (field_number << 3) | wire_type
|
|
|
|
0x1, // PrevoteType
|
2018-10-18 18:02:20 -04:00
|
|
|
0x11, // (field_number << 3) | wire_type
|
2018-10-19 14:23:14 -04:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
|
|
|
|
0x19, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
|
2019-01-13 23:56:36 +01:00
|
|
|
0x2a, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
// remaining fields (timestamp):
|
2018-10-23 13:21:47 -04:00
|
|
|
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
|
2018-10-13 01:21:46 +02:00
|
|
|
},
|
2019-02-16 06:12:00 +01:00
|
|
|
3: {
|
|
|
|
"", &Vote{Height: 1, Round: 1},
|
2018-10-13 01:21:46 +02:00
|
|
|
[]byte{
|
2019-02-16 06:12:00 +01:00
|
|
|
0x1f, // length
|
2018-10-18 18:02:20 -04:00
|
|
|
0x11, // (field_number << 3) | wire_type
|
2018-10-19 14:23:14 -04:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
|
|
|
|
0x19, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
|
|
|
|
// remaining fields (timestamp):
|
2019-01-13 23:56:36 +01:00
|
|
|
0x2a,
|
2018-10-23 13:21:47 -04:00
|
|
|
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1},
|
2018-10-13 01:21:46 +02:00
|
|
|
},
|
|
|
|
// containing non-empty chain_id:
|
2019-02-16 06:12:00 +01:00
|
|
|
4: {
|
|
|
|
"test_chain_id", &Vote{Height: 1, Round: 1},
|
2018-10-13 01:21:46 +02:00
|
|
|
[]byte{
|
2019-02-16 06:12:00 +01:00
|
|
|
0x2e, // length
|
2018-10-18 18:02:20 -04:00
|
|
|
0x11, // (field_number << 3) | wire_type
|
2018-10-19 14:23:14 -04:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // height
|
|
|
|
0x19, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // round
|
|
|
|
// remaining fields:
|
2019-01-13 23:56:36 +01:00
|
|
|
0x2a, // (field_number << 3) | wire_type
|
2018-10-23 13:21:47 -04:00
|
|
|
0xb, 0x8, 0x80, 0x92, 0xb8, 0xc3, 0x98, 0xfe, 0xff, 0xff, 0xff, 0x1, // timestamp
|
2018-10-18 18:02:20 -04:00
|
|
|
0x32, // (field_number << 3) | wire_type
|
2018-10-13 01:21:46 +02:00
|
|
|
0xd, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64}, // chainID
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tc := range tests {
|
2019-02-16 06:12:00 +01:00
|
|
|
got := tc.vote.SignBytes(tc.chainID)
|
2018-10-13 01:21:46 +02:00
|
|
|
require.Equal(t, tc.want, got, "test case #%v: got unexpected sign bytes for Vote.", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVoteProposalNotEq(t *testing.T) {
|
|
|
|
cv := CanonicalizeVote("", &Vote{Height: 1, Round: 1})
|
|
|
|
p := CanonicalizeProposal("", &Proposal{Height: 1, Round: 1})
|
2018-10-25 03:34:01 +02:00
|
|
|
vb, err := cdc.MarshalBinaryLengthPrefixed(cv)
|
2018-10-13 01:21:46 +02:00
|
|
|
require.NoError(t, err)
|
2018-10-25 03:34:01 +02:00
|
|
|
pb, err := cdc.MarshalBinaryLengthPrefixed(p)
|
2018-10-13 01:21:46 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, vb, pb)
|
|
|
|
}
|
|
|
|
|
2018-06-20 17:35:30 -07:00
|
|
|
func TestVoteVerifySignature(t *testing.T) {
|
|
|
|
privVal := NewMockPV()
|
2018-07-18 08:38:44 -07:00
|
|
|
pubkey := privVal.GetPubKey()
|
2018-06-20 17:35:30 -07:00
|
|
|
|
|
|
|
vote := examplePrecommit()
|
|
|
|
signBytes := vote.SignBytes("test_chain_id")
|
|
|
|
|
|
|
|
// sign it
|
|
|
|
err := privVal.SignVote("test_chain_id", vote)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// verify the same vote
|
2018-07-18 08:38:44 -07:00
|
|
|
valid := pubkey.VerifyBytes(vote.SignBytes("test_chain_id"), vote.Signature)
|
2018-06-20 17:35:30 -07:00
|
|
|
require.True(t, valid)
|
|
|
|
|
|
|
|
// serialize, deserialize and verify again....
|
|
|
|
precommit := new(Vote)
|
2018-10-25 03:34:01 +02:00
|
|
|
bs, err := cdc.MarshalBinaryLengthPrefixed(vote)
|
2018-06-20 17:35:30 -07:00
|
|
|
require.NoError(t, err)
|
2018-10-25 03:34:01 +02:00
|
|
|
err = cdc.UnmarshalBinaryLengthPrefixed(bs, &precommit)
|
2018-06-20 17:35:30 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// verify the transmitted vote
|
|
|
|
newSignBytes := precommit.SignBytes("test_chain_id")
|
|
|
|
require.Equal(t, string(signBytes), string(newSignBytes))
|
2018-07-18 08:38:44 -07:00
|
|
|
valid = pubkey.VerifyBytes(newSignBytes, precommit.Signature)
|
2018-06-20 17:35:30 -07:00
|
|
|
require.True(t, valid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsVoteTypeValid(t *testing.T) {
|
|
|
|
tc := []struct {
|
|
|
|
name string
|
2018-10-13 01:21:46 +02:00
|
|
|
in SignedMsgType
|
2018-06-20 17:35:30 -07:00
|
|
|
out bool
|
|
|
|
}{
|
2018-10-13 01:21:46 +02:00
|
|
|
{"Prevote", PrevoteType, true},
|
|
|
|
{"Precommit", PrecommitType, true},
|
|
|
|
{"InvalidType", SignedMsgType(0x3), false},
|
2018-06-20 17:35:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tc {
|
|
|
|
tt := tt
|
|
|
|
t.Run(tt.name, func(st *testing.T) {
|
|
|
|
if rs := IsVoteTypeValid(tt.in); rs != tt.out {
|
|
|
|
t.Errorf("Got unexpected Vote type. Expected:\n%v\nGot:\n%v", rs, tt.out)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-07-18 08:38:44 -07:00
|
|
|
|
|
|
|
func TestVoteVerify(t *testing.T) {
|
|
|
|
privVal := NewMockPV()
|
|
|
|
pubkey := privVal.GetPubKey()
|
|
|
|
|
|
|
|
vote := examplePrevote()
|
|
|
|
vote.ValidatorAddress = pubkey.Address()
|
|
|
|
|
2018-07-20 10:44:21 -07:00
|
|
|
err := vote.Verify("test_chain_id", ed25519.GenPrivKey().PubKey())
|
2018-07-18 08:38:44 -07:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, ErrVoteInvalidValidatorAddress, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = vote.Verify("test_chain_id", pubkey)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Equal(t, ErrVoteInvalidSignature, err)
|
|
|
|
}
|
|
|
|
}
|
2018-08-08 16:03:58 +04:00
|
|
|
|
|
|
|
func TestMaxVoteBytes(t *testing.T) {
|
2018-10-23 13:21:47 -04:00
|
|
|
// time is varint encoded so need to pick the max.
|
|
|
|
// year int, month Month, day, hour, min, sec, nsec int, loc *Location
|
|
|
|
timestamp := time.Date(math.MaxInt64, 0, 0, 0, 0, 0, math.MaxInt64, time.UTC)
|
|
|
|
|
max-bytes PR follow-up (#2318)
* ReapMaxTxs: return all txs if max is negative
this mirrors ReapMaxBytes behavior
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950
* increase MaxAminoOverheadForBlock
tested with:
```
func TestMaxAminoOverheadForBlock(t *testing.T) {
maxChainID := ""
for i := 0; i < MaxChainIDLen; i++ {
maxChainID += "𠜎"
}
h := Header{
ChainID: maxChainID,
Height: 10,
Time: time.Now().UTC(),
NumTxs: 100,
TotalTxs: 200,
LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: tmhash.Sum([]byte("proposer_address")),
}
b := Block{
Header: h,
Data: Data{Txs: makeTxs(10000, 100)},
Evidence: EvidenceData{},
LastCommit: &Commit{},
}
bz, err := cdc.MarshalBinary(b)
require.NoError(t, err)
assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1)
}
```
* fix MaxYYY constants calculation
by using math.MaxInt64
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244
* pass mempool filter as an option
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869
* fixes after Dev's comments
2018-09-04 11:46:34 +04:00
|
|
|
vote := &Vote{
|
2018-10-31 12:42:05 -04:00
|
|
|
ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
|
max-bytes PR follow-up (#2318)
* ReapMaxTxs: return all txs if max is negative
this mirrors ReapMaxBytes behavior
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950
* increase MaxAminoOverheadForBlock
tested with:
```
func TestMaxAminoOverheadForBlock(t *testing.T) {
maxChainID := ""
for i := 0; i < MaxChainIDLen; i++ {
maxChainID += "𠜎"
}
h := Header{
ChainID: maxChainID,
Height: 10,
Time: time.Now().UTC(),
NumTxs: 100,
TotalTxs: 200,
LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: tmhash.Sum([]byte("proposer_address")),
}
b := Block{
Header: h,
Data: Data{Txs: makeTxs(10000, 100)},
Evidence: EvidenceData{},
LastCommit: &Commit{},
}
bz, err := cdc.MarshalBinary(b)
require.NoError(t, err)
assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1)
}
```
* fix MaxYYY constants calculation
by using math.MaxInt64
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244
* pass mempool filter as an option
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869
* fixes after Dev's comments
2018-09-04 11:46:34 +04:00
|
|
|
ValidatorIndex: math.MaxInt64,
|
|
|
|
Height: math.MaxInt64,
|
|
|
|
Round: math.MaxInt64,
|
2018-10-23 13:21:47 -04:00
|
|
|
Timestamp: timestamp,
|
2018-10-13 01:21:46 +02:00
|
|
|
Type: PrevoteType,
|
max-bytes PR follow-up (#2318)
* ReapMaxTxs: return all txs if max is negative
this mirrors ReapMaxBytes behavior
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950
* increase MaxAminoOverheadForBlock
tested with:
```
func TestMaxAminoOverheadForBlock(t *testing.T) {
maxChainID := ""
for i := 0; i < MaxChainIDLen; i++ {
maxChainID += "𠜎"
}
h := Header{
ChainID: maxChainID,
Height: 10,
Time: time.Now().UTC(),
NumTxs: 100,
TotalTxs: 200,
LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")),
ConsensusHash: tmhash.Sum([]byte("consensus_hash")),
AppHash: tmhash.Sum([]byte("app_hash")),
LastResultsHash: tmhash.Sum([]byte("last_results_hash")),
EvidenceHash: tmhash.Sum([]byte("evidence_hash")),
ProposerAddress: tmhash.Sum([]byte("proposer_address")),
}
b := Block{
Header: h,
Data: Data{Txs: makeTxs(10000, 100)},
Evidence: EvidenceData{},
LastCommit: &Commit{},
}
bz, err := cdc.MarshalBinary(b)
require.NoError(t, err)
assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1)
}
```
* fix MaxYYY constants calculation
by using math.MaxInt64
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244
* pass mempool filter as an option
See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869
* fixes after Dev's comments
2018-09-04 11:46:34 +04:00
|
|
|
BlockID: BlockID{
|
|
|
|
Hash: tmhash.Sum([]byte("blockID_hash")),
|
|
|
|
PartsHeader: PartSetHeader{
|
|
|
|
Total: math.MaxInt64,
|
|
|
|
Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-08-08 16:03:58 +04:00
|
|
|
|
|
|
|
privVal := NewMockPV()
|
|
|
|
err := privVal.SignVote("test_chain_id", vote)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-10-25 03:34:01 +02:00
|
|
|
bz, err := cdc.MarshalBinaryLengthPrefixed(vote)
|
2018-08-08 16:03:58 +04:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-09-21 13:00:36 +04:00
|
|
|
assert.EqualValues(t, MaxVoteBytes, len(bz))
|
2018-08-08 16:03:58 +04:00
|
|
|
}
|
2018-10-30 17:16:55 +01:00
|
|
|
|
|
|
|
func TestVoteString(t *testing.T) {
|
|
|
|
str := examplePrecommit().String()
|
|
|
|
expected := `Vote{56789:6AF1F4111082 12345/02/2(Precommit) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
|
|
|
|
if str != expected {
|
|
|
|
t.Errorf("Got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
str2 := examplePrevote().String()
|
|
|
|
expected = `Vote{56789:6AF1F4111082 12345/02/1(Prevote) 8B01023386C3 000000000000 @ 2017-12-25T03:00:01.234Z}`
|
|
|
|
if str2 != expected {
|
|
|
|
t.Errorf("Got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2)
|
|
|
|
}
|
|
|
|
}
|
2018-11-09 16:59:04 +02:00
|
|
|
|
|
|
|
func TestVoteValidateBasic(t *testing.T) {
|
|
|
|
privVal := NewMockPV()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
testName string
|
|
|
|
malleateVote func(*Vote)
|
|
|
|
expectErr bool
|
|
|
|
}{
|
|
|
|
{"Good Vote", func(v *Vote) {}, false},
|
|
|
|
{"Negative Height", func(v *Vote) { v.Height = -1 }, true},
|
|
|
|
{"Negative Round", func(v *Vote) { v.Round = -1 }, true},
|
|
|
|
{"Invalid BlockID", func(v *Vote) { v.BlockID = BlockID{[]byte{1, 2, 3}, PartSetHeader{111, []byte("blockparts")}} }, true},
|
|
|
|
{"Invalid Address", func(v *Vote) { v.ValidatorAddress = make([]byte, 1) }, true},
|
|
|
|
{"Invalid ValidatorIndex", func(v *Vote) { v.ValidatorIndex = -1 }, true},
|
|
|
|
{"Invalid Signature", func(v *Vote) { v.Signature = nil }, true},
|
|
|
|
{"Too big Signature", func(v *Vote) { v.Signature = make([]byte, MaxSignatureSize+1) }, true},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2019-09-11 01:15:18 -04:00
|
|
|
tc := tc
|
2018-11-09 16:59:04 +02:00
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
|
|
|
vote := examplePrecommit()
|
|
|
|
err := privVal.SignVote("test_chain_id", vote)
|
|
|
|
require.NoError(t, err)
|
|
|
|
tc.malleateVote(vote)
|
|
|
|
assert.Equal(t, tc.expectErr, vote.ValidateBasic() != nil, "Validate Basic had an unexpected result")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|