ints: stricter numbers (#4939)

This commit is contained in:
Marko
2020-06-04 16:34:56 +02:00
committed by GitHub
parent 7c576f02ab
commit a88537bb88
56 changed files with 738 additions and 674 deletions

View File

@@ -21,7 +21,7 @@ import (
const (
// MaxHeaderBytes is a maximum header size (including amino overhead).
MaxHeaderBytes int64 = 632
MaxHeaderBytes int64 = 628
// MaxAminoOverheadForBlock - maximum amino overhead to encode a block (up to
// MaxBlockSizeBytes in size) not including it's parts except Data.
@@ -134,7 +134,7 @@ func (b *Block) Hash() tmbytes.HexBytes {
// MakePartSet returns a PartSet containing parts of a serialized block.
// This is the form in which the block is gossipped to peers.
// CONTRACT: partSize is greater than zero.
func (b *Block) MakePartSet(partSize int) *PartSet {
func (b *Block) MakePartSet(partSize uint32) *PartSet {
if b == nil {
return nil
}
@@ -657,7 +657,7 @@ type Commit struct {
// Any peer with a block can gossip signatures by index with a peer without
// recalculating the active ValidatorSet.
Height int64 `json:"height"`
Round int `json:"round"`
Round int32 `json:"round"`
BlockID BlockID `json:"block_id"`
Signatures []CommitSig `json:"signatures"`
@@ -669,7 +669,7 @@ type Commit struct {
}
// NewCommit returns a new Commit.
func NewCommit(height int64, round int, blockID BlockID, commitSigs []CommitSig) *Commit {
func NewCommit(height int64, round int32, blockID BlockID, commitSigs []CommitSig) *Commit {
return &Commit{
Height: height,
Round: round,
@@ -682,12 +682,12 @@ func NewCommit(height int64, round int, blockID BlockID, commitSigs []CommitSig)
// Panics if signatures from the commit can't be added to the voteset.
// Inverse of VoteSet.MakeCommit().
func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSet {
voteSet := NewVoteSet(chainID, commit.Height, commit.Round, PrecommitType, vals)
voteSet := NewVoteSet(chainID, commit.Height, commit.Round, tmproto.PrecommitType, vals)
for idx, commitSig := range commit.Signatures {
if commitSig.Absent() {
continue // OK, some precommits can be missing.
}
added, err := voteSet.AddVote(commit.GetVote(idx))
added, err := voteSet.AddVote(commit.GetVote(int32(idx)))
if !added || err != nil {
panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
}
@@ -698,10 +698,10 @@ func CommitToVoteSet(chainID string, commit *Commit, vals *ValidatorSet) *VoteSe
// GetVote converts the CommitSig for the given valIdx to a Vote.
// Returns nil if the precommit at valIdx is nil.
// Panics if valIdx >= commit.Size().
func (commit *Commit) GetVote(valIdx int) *Vote {
func (commit *Commit) GetVote(valIdx int32) *Vote {
commitSig := commit.Signatures[valIdx]
return &Vote{
Type: PrecommitType,
Type: tmproto.PrecommitType,
Height: commit.Height,
Round: commit.Round,
BlockID: commitSig.BlockID(commit.BlockID),
@@ -716,14 +716,14 @@ func (commit *Commit) GetVote(valIdx int) *Vote {
// The only unique part of the SignBytes is the Timestamp - all other fields
// signed over are otherwise the same for all validators.
// Panics if valIdx >= commit.Size().
func (commit *Commit) VoteSignBytes(chainID string, valIdx int) []byte {
func (commit *Commit) VoteSignBytes(chainID string, valIdx int32) []byte {
return commit.GetVote(valIdx).SignBytes(chainID)
}
// Type returns the vote type of the commit, which is always VoteTypePrecommit
// Implements VoteSetReader.
func (commit *Commit) Type() byte {
return byte(PrecommitType)
return byte(tmproto.PrecommitType)
}
// GetHeight returns height of the commit.
@@ -734,7 +734,7 @@ func (commit *Commit) GetHeight() int64 {
// GetRound returns height of the commit.
// Implements VoteSetReader.
func (commit *Commit) GetRound() int {
func (commit *Commit) GetRound() int32 {
return commit.Round
}
@@ -764,7 +764,7 @@ func (commit *Commit) BitArray() *bits.BitArray {
// GetByIndex returns the vote corresponding to a given validator index.
// Panics if `index >= commit.Size()`.
// Implements VoteSetReader.
func (commit *Commit) GetByIndex(valIdx int) *Vote {
func (commit *Commit) GetByIndex(valIdx int32) *Vote {
return commit.GetVote(valIdx)
}
@@ -854,7 +854,7 @@ func (commit *Commit) ToProto() *tmproto.Commit {
c.Signatures = sigs
c.Height = commit.Height
c.Round = int32(commit.Round)
c.Round = commit.Round
c.BlockID = commit.BlockID.ToProto()
if commit.hash != nil {
c.Hash = commit.hash
@@ -891,7 +891,7 @@ func CommitFromProto(cp *tmproto.Commit) (*Commit, error) {
commit.Signatures = sigs
commit.Height = cp.Height
commit.Round = int(cp.Round)
commit.Round = cp.Round
commit.BlockID = *bi
commit.hash = cp.Hash
commit.bitArray = bitArray

View File

@@ -20,6 +20,7 @@ import (
"github.com/tendermint/tendermint/libs/bits"
"github.com/tendermint/tendermint/libs/bytes"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/types"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/tendermint/tendermint/version"
)
@@ -36,7 +37,7 @@ func TestBlockAddEvidence(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, valSet, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
require.NoError(t, err)
@@ -56,7 +57,7 @@ func TestBlockValidateBasic(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, valSet, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
require.NoError(t, err)
@@ -110,7 +111,7 @@ func TestBlockMakePartSet(t *testing.T) {
partSet := MakeBlock(int64(3), []Tx{Tx("Hello World")}, nil, nil).MakePartSet(1024)
assert.NotNil(t, partSet)
assert.Equal(t, 1, partSet.Total())
assert.EqualValues(t, 1, partSet.Total())
}
func TestBlockMakePartSetWithEvidence(t *testing.T) {
@@ -119,7 +120,7 @@ func TestBlockMakePartSetWithEvidence(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, valSet, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
require.NoError(t, err)
@@ -128,7 +129,7 @@ func TestBlockMakePartSetWithEvidence(t *testing.T) {
partSet := MakeBlock(h, []Tx{Tx("Hello World")}, commit, evList).MakePartSet(512)
assert.NotNil(t, partSet)
assert.Equal(t, 3, partSet.Total())
assert.EqualValues(t, 3, partSet.Total())
}
func TestBlockHashesTo(t *testing.T) {
@@ -136,7 +137,7 @@ func TestBlockHashesTo(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, valSet, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
require.NoError(t, err)
@@ -178,7 +179,7 @@ func makeBlockIDRandom() BlockID {
return BlockID{blockHash, PartSetHeader{123, partSetHash}}
}
func makeBlockID(hash []byte, partSetSize int, partSetHash []byte) BlockID {
func makeBlockID(hash []byte, partSetSize uint32, partSetHash []byte) BlockID {
var (
h = make([]byte, tmhash.Size)
psH = make([]byte, tmhash.Size)
@@ -209,13 +210,13 @@ func TestNilDataHashDoesntCrash(t *testing.T) {
func TestCommit(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, _, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
require.NoError(t, err)
assert.Equal(t, h-1, commit.Height)
assert.Equal(t, 1, commit.Round)
assert.Equal(t, PrecommitType, SignedMsgType(commit.Type()))
assert.EqualValues(t, 1, commit.Round)
assert.Equal(t, tmproto.PrecommitType, tmproto.SignedMsgType(commit.Type()))
if commit.Size() <= 0 {
t.Fatalf("commit %v has a zero or negative size: %d", commit, commit.Size())
}
@@ -330,7 +331,7 @@ func TestMaxHeaderBytes(t *testing.T) {
ChainID: maxChainID,
Height: math.MaxInt64,
Time: timestamp,
LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt64, make([]byte, tmhash.Size)),
LastBlockID: makeBlockID(make([]byte, tmhash.Size), math.MaxInt32, make([]byte, tmhash.Size)),
LastCommitHash: tmhash.Sum([]byte("last_commit_hash")),
DataHash: tmhash.Sum([]byte("data_hash")),
ValidatorsHash: tmhash.Sum([]byte("validators_hash")),
@@ -351,7 +352,7 @@ func TestMaxHeaderBytes(t *testing.T) {
func randCommit(now time.Time) *Commit {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, _, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, _, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, now)
if err != nil {
panic(err)
@@ -377,9 +378,9 @@ func TestBlockMaxDataBytes(t *testing.T) {
}{
0: {-10, 1, 0, true, 0},
1: {10, 1, 0, true, 0},
2: {865, 1, 0, true, 0},
3: {866, 1, 0, false, 0},
4: {867, 1, 0, false, 1},
2: {849, 1, 0, true, 0},
3: {850, 1, 0, false, 0},
4: {851, 1, 0, false, 1},
}
for i, tc := range testCases {
@@ -407,10 +408,10 @@ func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
}{
0: {-10, 0, 1, true, 0},
1: {10, 0, 1, true, 0},
2: {865, 0, 1, true, 0},
3: {866, 0, 1, false, 0},
4: {1310, 1, 1, false, 0},
5: {1311, 1, 1, false, 1},
2: {849, 0, 1, true, 0},
3: {850, 0, 1, false, 0},
4: {1294, 1, 1, false, 0},
5: {1295, 1, 1, false, 1},
}
for i, tc := range testCases {
@@ -432,14 +433,14 @@ func TestCommitToVoteSet(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)
voteSet, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, valSet, vals := randVoteSet(h-1, 1, tmproto.PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
assert.NoError(t, err)
chainID := voteSet.ChainID()
voteSet2 := CommitToVoteSet(chainID, commit, valSet)
for i := 0; i < len(vals); i++ {
for i := int32(0); int(i) < len(vals); i++ {
vote1 := voteSet.GetByIndex(i)
vote2 := voteSet2.GetByIndex(i)
vote3 := commit.GetVote(i)
@@ -472,9 +473,9 @@ func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) {
}
for _, tc := range testCases {
voteSet, valSet, vals := randVoteSet(height-1, round, PrecommitType, tc.numValidators, 1)
voteSet, valSet, vals := randVoteSet(height-1, round, tmproto.PrecommitType, tc.numValidators, 1)
vi := 0
vi := int32(0)
for n := range tc.blockIDs {
for i := 0; i < tc.numVotes[n]; i++ {
pubKey, err := vals[vi].GetPubKey()
@@ -484,7 +485,7 @@ func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) {
ValidatorIndex: vi,
Height: height - 1,
Round: round,
Type: PrecommitType,
Type: tmproto.PrecommitType,
BlockID: tc.blockIDs[n],
Timestamp: tmtime.Now(),
}
@@ -573,8 +574,8 @@ func TestBlockIDValidateBasic(t *testing.T) {
invalidBlockID := BlockID{
Hash: []byte{0},
PartsHeader: PartSetHeader{
Total: -1,
Hash: bytes.HexBytes{},
Total: 1,
Hash: []byte{0},
},
}

View File

@@ -4,6 +4,7 @@ import (
"time"
"github.com/tendermint/tendermint/libs/bytes"
tmproto "github.com/tendermint/tendermint/proto/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -19,23 +20,23 @@ type CanonicalBlockID struct {
type CanonicalPartSetHeader struct {
Hash bytes.HexBytes
Total int
Total uint32
}
type CanonicalProposal struct {
Type SignedMsgType // type alias for byte
Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"`
POLRound int64 `binary:"fixed64"`
Type tmproto.SignedMsgType // type alias for byte
Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"`
POLRound int64 `binary:"fixed64"`
BlockID CanonicalBlockID
Timestamp time.Time
ChainID string
}
type CanonicalVote struct {
Type SignedMsgType // type alias for byte
Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"`
Type tmproto.SignedMsgType // type alias for byte
Height int64 `binary:"fixed64"`
Round int64 `binary:"fixed64"`
BlockID CanonicalBlockID
Timestamp time.Time
ChainID string
@@ -60,7 +61,7 @@ func CanonicalizePartSetHeader(psh PartSetHeader) CanonicalPartSetHeader {
func CanonicalizeProposal(chainID string, proposal *Proposal) CanonicalProposal {
return CanonicalProposal{
Type: ProposalType,
Type: tmproto.ProposalType,
Height: proposal.Height,
Round: int64(proposal.Round), // cast int->int64 to make amino encode it fixed64 (does not work for int)
POLRound: int64(proposal.POLRound),

View File

@@ -86,18 +86,18 @@ type EventDataTx struct {
// NOTE: This goes into the replay WAL
type EventDataRoundState struct {
Height int64 `json:"height"`
Round int `json:"round"`
Round int32 `json:"round"`
Step string `json:"step"`
}
type ValidatorInfo struct {
Address Address `json:"address"`
Index int `json:"index"`
Index int32 `json:"index"`
}
type EventDataNewRound struct {
Height int64 `json:"height"`
Round int `json:"round"`
Round int32 `json:"round"`
Step string `json:"step"`
Proposer ValidatorInfo `json:"proposer"`
@@ -105,7 +105,7 @@ type EventDataNewRound struct {
type EventDataCompleteProposal struct {
Height int64 `json:"height"`
Round int `json:"round"`
Round int32 `json:"round"`
Step string `json:"step"`
BlockID BlockID `json:"block_id"`

View File

@@ -567,7 +567,7 @@ func (ev ConflictingHeadersEvidence) Split(committedHeader *Header, valSet *Vali
if !valSet.HasAddress(sig.ValidatorAddress) {
evList = append(evList, &PhantomValidatorEvidence{
Vote: alternativeHeader.Commit.GetVote(i),
Vote: alternativeHeader.Commit.GetVote(int32(i)),
LastHeightValidatorWasInSet: lastHeightValidatorWasInSet,
})
}
@@ -597,7 +597,7 @@ func (ev ConflictingHeadersEvidence) Split(committedHeader *Header, valSet *Vali
}
evList = append(evList, &LunaticValidatorEvidence{
Header: alternativeHeader.Header,
Vote: alternativeHeader.Commit.GetVote(i),
Vote: alternativeHeader.Commit.GetVote(int32(i)),
InvalidHeaderField: invalidField,
})
}
@@ -638,15 +638,15 @@ OUTER_LOOP:
// immediately slashable (#F1).
if ev.H1.Commit.Round == ev.H2.Commit.Round {
evList = append(evList, &DuplicateVoteEvidence{
VoteA: ev.H1.Commit.GetVote(i),
VoteB: ev.H2.Commit.GetVote(j),
VoteA: ev.H1.Commit.GetVote(int32(i)),
VoteB: ev.H2.Commit.GetVote(int32(j)),
})
} else {
// if H1.Round != H2.Round we need to run full detection procedure => not
// immediately slashable.
evList = append(evList, &PotentialAmnesiaEvidence{
VoteA: ev.H1.Commit.GetVote(i),
VoteB: ev.H2.Commit.GetVote(j),
VoteA: ev.H1.Commit.GetVote(int32(i)),
VoteB: ev.H2.Commit.GetVote(int32(j)),
})
}
@@ -1128,7 +1128,7 @@ func MakePOLCFromVoteSet(voteSet *VoteSet, pubKey crypto.PubKey, blockID BlockID
func makePOLCFromVoteSet(voteSet *VoteSet, pubKey crypto.PubKey, blockID BlockID) ProofOfLockChange {
var votes []Vote
valSetSize := voteSet.Size()
for valIdx := 0; valIdx < valSetSize; valIdx++ {
for valIdx := int32(0); int(valIdx) < valSetSize; valIdx++ {
vote := voteSet.GetByIndex(valIdx)
if vote != nil && vote.BlockID.Equals(blockID) {
votes = append(votes, *vote)
@@ -1155,7 +1155,7 @@ func (e ProofOfLockChange) Time() time.Time {
return latest
}
func (e ProofOfLockChange) Round() int {
func (e ProofOfLockChange) Round() int32 {
return e.Votes[0].Round
}
@@ -1315,7 +1315,7 @@ func (e MockEvidence) String() string {
func NewMockPOLC(height int64, time time.Time, pubKey crypto.PubKey) ProofOfLockChange {
voteVal := NewMockPV()
pKey, _ := voteVal.GetPubKey()
vote := Vote{Type: PrecommitType, Height: height, Round: 1, BlockID: BlockID{},
vote := Vote{Type: tmproto.PrecommitType, Height: height, Round: 1, BlockID: BlockID{},
Timestamp: time, ValidatorAddress: pKey.Address(), ValidatorIndex: 1, Signature: []byte{}}
_ = voteVal.SignVote("mock-chain-id", &vote)
return ProofOfLockChange{

View File

@@ -11,6 +11,7 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/tmhash"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/types"
)
type voteData struct {
@@ -22,7 +23,7 @@ type voteData struct {
var defaultVoteTime = time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
func makeVote(
t *testing.T, val PrivValidator, chainID string, valIndex int, height int64, round, step int, blockID BlockID,
t *testing.T, val PrivValidator, chainID string, valIndex int32, height int64, round int32, step int, blockID BlockID,
time time.Time) *Vote {
pubKey, err := val.GetPubKey()
require.NoError(t, err)
@@ -31,7 +32,7 @@ func makeVote(
ValidatorIndex: valIndex,
Height: height,
Round: round,
Type: SignedMsgType(step),
Type: tmproto.SignedMsgType(step),
Timestamp: time,
BlockID: blockID,
}
@@ -108,13 +109,13 @@ func TestEvidenceList(t *testing.T) {
func TestMaxEvidenceBytes(t *testing.T) {
val := NewMockPV()
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
maxTime := time.Date(9999, 0, 0, 0, 0, 0, 0, time.UTC)
const chainID = "mychain"
ev := &DuplicateVoteEvidence{
VoteA: makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID, maxTime),
VoteB: makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, math.MaxInt64, blockID2, maxTime),
VoteA: makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, math.MaxInt64, blockID, maxTime),
VoteB: makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, math.MaxInt64, blockID2, maxTime),
}
//TODO: Add other types of evidence to test and set MaxEvidenceBytes accordingly
@@ -171,8 +172,8 @@ func randomDuplicatedVoteEvidence(t *testing.T) *DuplicateVoteEvidence {
func TestDuplicateVoteEvidenceValidation(t *testing.T) {
val := NewMockPV()
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
const chainID = "mychain"
testCases := []struct {
@@ -188,7 +189,7 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) {
ev.VoteB = nil
}, true},
{"Invalid vote type", func(ev *DuplicateVoteEvidence) {
ev.VoteA = makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0, blockID2, defaultVoteTime)
ev.VoteA = makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0, blockID2, defaultVoteTime)
}, true},
{"Invalid vote order", func(ev *DuplicateVoteEvidence) {
swap := ev.VoteA.Copy()
@@ -199,8 +200,8 @@ func TestDuplicateVoteEvidenceValidation(t *testing.T) {
for _, tc := range testCases {
tc := tc
t.Run(tc.testName, func(t *testing.T) {
vote1 := makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID, defaultVoteTime)
vote2 := makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, math.MaxInt64, 0x02, blockID2, defaultVoteTime)
vote1 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0x02, blockID, defaultVoteTime)
vote2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, math.MaxInt32, 0x02, blockID2, defaultVoteTime)
ev := NewDuplicateVoteEvidence(vote1, vote2)
tc.malleateEvidence(ev)
assert.Equal(t, tc.expectErr, ev.ValidateBasic() != nil, "Validate Basic had an unexpected result")
@@ -302,8 +303,8 @@ func TestConflictingHeadersEvidence(t *testing.T) {
header2.LastBlockID = blockID
header2.ChainID = chainID
voteSet1, valSet, vals := randVoteSet(height, 1, PrecommitType, 10, 1)
voteSet2 := NewVoteSet(chainID, height, 1, PrecommitType, valSet)
voteSet1, valSet, vals := randVoteSet(height, 1, tmproto.PrecommitType, 10, 1)
voteSet2 := NewVoteSet(chainID, height, 1, tmproto.PrecommitType, valSet)
commit1, err := MakeCommit(BlockID{
Hash: header1.Hash(),
@@ -360,8 +361,8 @@ func TestPotentialAmnesiaEvidence(t *testing.T) {
var (
val = NewMockPV()
blockID = makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID2 = makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID = makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
blockID2 = makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
vote1 = makeVote(t, val, chainID, 0, height, 0, 2, blockID, defaultVoteTime)
vote2 = makeVote(t, val, chainID, 0, height, 1, 2, blockID2, defaultVoteTime)
)
@@ -394,7 +395,7 @@ func TestProofOfLockChange(t *testing.T) {
height int64 = 37
)
// 1: valid POLC - nothing should fail
voteSet, valSet, privValidators, blockID := buildVoteSet(height, 1, 3, 7, 0, PrecommitType)
voteSet, valSet, privValidators, blockID := buildVoteSet(height, 1, 3, 7, 0, tmproto.PrecommitType)
pubKey, err := privValidators[7].GetPubKey()
require.NoError(t, err)
polc := makePOLCFromVoteSet(voteSet, pubKey, blockID)
@@ -412,7 +413,7 @@ func TestProofOfLockChange(t *testing.T) {
polc2 := makePOLCFromVoteSet(voteSet, pubKey, blockID)
badPOLCs = append(badPOLCs, polc2)
// 3: one vote was from a different round
voteSet, _, privValidators, blockID = buildVoteSet(height, 1, 3, 7, 0, PrecommitType)
voteSet, _, privValidators, blockID = buildVoteSet(height, 1, 3, 7, 0, tmproto.PrecommitType)
pubKey, err = privValidators[7].GetPubKey()
require.NoError(t, err)
polc = makePOLCFromVoteSet(voteSet, pubKey, blockID)
@@ -466,11 +467,11 @@ func makeHeaderRandom() *Header {
func TestEvidenceProto(t *testing.T) {
// -------- Votes --------
val := NewMockPV()
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
blockID2 := makeBlockID(tmhash.Sum([]byte("blockhash2")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
const chainID = "mychain"
v := makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime)
v2 := makeVote(t, val, chainID, math.MaxInt64, math.MaxInt64, 2, 0x01, blockID2, defaultVoteTime)
v := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 1, 0x01, blockID, defaultVoteTime)
v2 := makeVote(t, val, chainID, math.MaxInt32, math.MaxInt64, 2, 0x01, blockID2, defaultVoteTime)
// -------- SignedHeaders --------
const height int64 = 37
@@ -488,8 +489,8 @@ func TestEvidenceProto(t *testing.T) {
header2.LastBlockID = blockID
header2.ChainID = chainID
voteSet1, valSet, vals := randVoteSet(height, 1, PrecommitType, 10, 1)
voteSet2 := NewVoteSet(chainID, height, 1, PrecommitType, valSet)
voteSet1, valSet, vals := randVoteSet(height, 1, tmproto.PrecommitType, 10, 1)
voteSet2 := NewVoteSet(chainID, height, 1, tmproto.PrecommitType, valSet)
commit1, err := MakeCommit(BlockID{
Hash: header1.Hash(),

View File

@@ -15,7 +15,7 @@ const (
MaxBlockSizeBytes = 104857600 // 100MB
// BlockPartSizeBytes is the size of one block part.
BlockPartSizeBytes = 65536 // 64kB
BlockPartSizeBytes uint32 = 65536 // 64kB
// MaxBlockPartsCount is the maximum number of block parts.
MaxBlockPartsCount = (MaxBlockSizeBytes / BlockPartSizeBytes) + 1

View File

@@ -20,17 +20,14 @@ var (
)
type Part struct {
Index int `json:"index"`
Index uint32 `json:"index"`
Bytes tmbytes.HexBytes `json:"bytes"`
Proof merkle.SimpleProof `json:"proof"`
}
// ValidateBasic performs basic validation.
func (part *Part) ValidateBasic() error {
if part.Index < 0 {
return errors.New("negative Index")
}
if len(part.Bytes) > BlockPartSizeBytes {
if len(part.Bytes) > int(BlockPartSizeBytes) {
return fmt.Errorf("too big: %d bytes, max: %d", len(part.Bytes), BlockPartSizeBytes)
}
if err := part.Proof.ValidateBasic(); err != nil {
@@ -57,7 +54,7 @@ func (part *Part) StringIndented(indent string) string {
//-------------------------------------
type PartSetHeader struct {
Total int `json:"total"`
Total uint32 `json:"total"`
Hash tmbytes.HexBytes `json:"hash"`
}
@@ -75,9 +72,6 @@ func (psh PartSetHeader) Equals(other PartSetHeader) bool {
// ValidateBasic performs basic validation.
func (psh PartSetHeader) ValidateBasic() error {
if psh.Total < 0 {
return errors.New("negative Total")
}
// Hash can be empty in case of POLBlockID.PartsHeader in Proposal.
if err := ValidateHash(psh.Hash); err != nil {
return fmt.Errorf("wrong Hash: %w", err)
@@ -92,7 +86,7 @@ func (psh *PartSetHeader) ToProto() tmproto.PartSetHeader {
}
return tmproto.PartSetHeader{
Total: int64(psh.Total),
Total: psh.Total,
Hash: psh.Hash,
}
}
@@ -103,7 +97,7 @@ func PartSetHeaderFromProto(ppsh *tmproto.PartSetHeader) (*PartSetHeader, error)
return nil, errors.New("nil PartSetHeader")
}
psh := new(PartSetHeader)
psh.Total = int(ppsh.Total)
psh.Total = ppsh.Total
psh.Hash = ppsh.Hash
return psh, psh.ValidateBasic()
@@ -112,35 +106,36 @@ func PartSetHeaderFromProto(ppsh *tmproto.PartSetHeader) (*PartSetHeader, error)
//-------------------------------------
type PartSet struct {
total int
total uint32
hash []byte
mtx sync.Mutex
parts []*Part
partsBitArray *bits.BitArray
count int
count uint32
}
// Returns an immutable, full PartSet from the data bytes.
// The data bytes are split into "partSize" chunks, and merkle tree computed.
func NewPartSetFromData(data []byte, partSize int) *PartSet {
// CONTRACT: partSize is greater than zero.
func NewPartSetFromData(data []byte, partSize uint32) *PartSet {
// divide data into 4kb parts.
total := (len(data) + partSize - 1) / partSize
total := (uint32(len(data)) + partSize - 1) / partSize
parts := make([]*Part, total)
partsBytes := make([][]byte, total)
partsBitArray := bits.NewBitArray(total)
for i := 0; i < total; i++ {
partsBitArray := bits.NewBitArray(int(total))
for i := uint32(0); i < total; i++ {
part := &Part{
Index: i,
Bytes: data[i*partSize : tmmath.MinInt(len(data), (i+1)*partSize)],
Bytes: data[i*partSize : tmmath.MinInt(len(data), int((i+1)*partSize))],
}
parts[i] = part
partsBytes[i] = part.Bytes
partsBitArray.SetIndex(i, true)
partsBitArray.SetIndex(int(i), true)
}
// Compute merkle proofs
root, proofs := merkle.SimpleProofsFromByteSlices(partsBytes)
for i := 0; i < total; i++ {
for i := uint32(0); i < total; i++ {
parts[i].Proof = *proofs[i]
}
return &PartSet{
@@ -158,7 +153,7 @@ func NewPartSetFromHeader(header PartSetHeader) *PartSet {
total: header.Total,
hash: header.Hash,
parts: make([]*Part, header.Total),
partsBitArray: bits.NewBitArray(header.Total),
partsBitArray: bits.NewBitArray(int(header.Total)),
count: 0,
}
}
@@ -200,14 +195,14 @@ func (ps *PartSet) HashesTo(hash []byte) bool {
return bytes.Equal(ps.hash, hash)
}
func (ps *PartSet) Count() int {
func (ps *PartSet) Count() uint32 {
if ps == nil {
return 0
}
return ps.count
}
func (ps *PartSet) Total() int {
func (ps *PartSet) Total() uint32 {
if ps == nil {
return 0
}
@@ -238,7 +233,7 @@ func (ps *PartSet) AddPart(part *Part) (bool, error) {
// Add part
ps.parts[part.Index] = part
ps.partsBitArray.SetIndex(part.Index, true)
ps.partsBitArray.SetIndex(int(part.Index), true)
ps.count++
return true, nil
}

View File

@@ -21,17 +21,17 @@ func TestBasicPartSet(t *testing.T) {
partSet := NewPartSetFromData(data, testPartSize)
assert.NotEmpty(t, partSet.Hash())
assert.Equal(t, 100, partSet.Total())
assert.EqualValues(t, 100, partSet.Total())
assert.Equal(t, 100, partSet.BitArray().Size())
assert.True(t, partSet.HashesTo(partSet.Hash()))
assert.True(t, partSet.IsComplete())
assert.Equal(t, 100, partSet.Count())
assert.EqualValues(t, 100, partSet.Count())
// Test adding parts to a new partSet.
partSet2 := NewPartSetFromHeader(partSet.Header())
assert.True(t, partSet2.HasHeader(partSet.Header()))
for i := 0; i < partSet.Total(); i++ {
for i := 0; i < int(partSet.Total()); i++ {
part := partSet.GetPart(i)
//t.Logf("\n%v", part)
added, err := partSet2.AddPart(part)
@@ -49,7 +49,7 @@ func TestBasicPartSet(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, partSet.Hash(), partSet2.Hash())
assert.Equal(t, 100, partSet2.Total())
assert.EqualValues(t, 100, partSet2.Total())
assert.True(t, partSet2.IsComplete())
// Reconstruct data, assert that they are equal.
@@ -92,7 +92,6 @@ func TestPartSetHeaderValidateBasic(t *testing.T) {
expectErr bool
}{
{"Good PartSet", func(psHeader *PartSetHeader) {}, false},
{"Negative Total", func(psHeader *PartSetHeader) { psHeader.Total = -2 }, true},
{"Invalid Hash", func(psHeader *PartSetHeader) { psHeader.Hash = make([]byte, 1) }, true},
}
for _, tc := range testCases {
@@ -114,7 +113,6 @@ func TestPartValidateBasic(t *testing.T) {
expectErr bool
}{
{"Good Part", func(pt *Part) {}, false},
{"Negative index", func(pt *Part) { pt.Index = -1 }, true},
{"Too big part", func(pt *Part) { pt.Bytes = make([]byte, BlockPartSizeBytes+1) }, true},
{"Too big proof", func(pt *Part) {
pt.Proof = merkle.SimpleProof{

View File

@@ -22,10 +22,10 @@ var (
// a so-called Proof-of-Lock (POL) round, as noted in the POLRound.
// If POLRound >= 0, then BlockID corresponds to the block that is locked in POLRound.
type Proposal struct {
Type SignedMsgType
Type tmproto.SignedMsgType
Height int64 `json:"height"`
Round int `json:"round"`
POLRound int `json:"pol_round"` // -1 if null.
Round int32 `json:"round"` // there can not be greater than 2_147_483_647 rounds
POLRound int32 `json:"pol_round"` // -1 if null.
BlockID BlockID `json:"block_id"`
Timestamp time.Time `json:"timestamp"`
Signature []byte `json:"signature"`
@@ -33,9 +33,9 @@ type Proposal struct {
// NewProposal returns a new Proposal.
// If there is no POLRound, polRound should be -1.
func NewProposal(height int64, round int, polRound int, blockID BlockID) *Proposal {
func NewProposal(height int64, round int32, polRound int32, blockID BlockID) *Proposal {
return &Proposal{
Type: ProposalType,
Type: tmproto.ProposalType,
Height: height,
Round: round,
BlockID: blockID,
@@ -46,7 +46,7 @@ func NewProposal(height int64, round int, polRound int, blockID BlockID) *Propos
// ValidateBasic performs basic validation.
func (p *Proposal) ValidateBasic() error {
if p.Type != ProposalType {
if p.Type != tmproto.ProposalType {
return errors.New("invalid Type")
}
if p.Height < 0 {
@@ -105,10 +105,10 @@ func (p *Proposal) ToProto() *tmproto.Proposal {
pb := new(tmproto.Proposal)
pb.BlockID = p.BlockID.ToProto()
pb.Type = tmproto.SignedMsgType(p.Type)
pb.Type = p.Type
pb.Height = p.Height
pb.Round = int32(p.Round)
pb.PolRound = int32(p.POLRound)
pb.Round = p.Round
pb.PolRound = p.POLRound
pb.Timestamp = p.Timestamp
pb.Signature = p.Signature
@@ -130,10 +130,10 @@ func ProposalFromProto(pp *tmproto.Proposal) (*Proposal, error) {
}
p.BlockID = *blockID
p.Type = SignedMsgType(pp.Type)
p.Type = pp.Type
p.Height = pp.Height
p.Round = int(pp.Round)
p.POLRound = int(pp.PolRound)
p.Round = pp.Round
p.POLRound = pp.PolRound
p.Timestamp = pp.Timestamp
p.Signature = pp.Signature

View File

@@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/tmhash"
tmproto "github.com/tendermint/tendermint/proto/types"
)
var testProposal *Proposal
@@ -113,7 +114,7 @@ func TestProposalValidateBasic(t *testing.T) {
expectErr bool
}{
{"Good Proposal", func(p *Proposal) {}, false},
{"Invalid Type", func(p *Proposal) { p.Type = PrecommitType }, true},
{"Invalid Type", func(p *Proposal) { p.Type = tmproto.PrecommitType }, true},
{"Invalid Height", func(p *Proposal) { p.Height = -1 }, true},
{"Invalid Round", func(p *Proposal) { p.Round = -1 }, true},
{"Invalid POLRound", func(p *Proposal) { p.POLRound = -2 }, true},
@@ -127,7 +128,7 @@ func TestProposalValidateBasic(t *testing.T) {
p.Signature = make([]byte, MaxSignatureSize+1)
}, true},
}
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt64, tmhash.Sum([]byte("partshash")))
blockID := makeBlockID(tmhash.Sum([]byte("blockhash")), math.MaxInt32, tmhash.Sum([]byte("partshash")))
for _, tc := range testCases {
tc := tc

View File

@@ -1,21 +1,11 @@
package types
// SignedMsgType is a type of signed message in the consensus.
type SignedMsgType byte
const (
// Votes
PrevoteType SignedMsgType = 0x01
PrecommitType SignedMsgType = 0x02
// Proposals
ProposalType SignedMsgType = 0x20
)
import tmproto "github.com/tendermint/tendermint/proto/types"
// IsVoteTypeValid returns true if t is a valid vote type.
func IsVoteTypeValid(t SignedMsgType) bool {
func IsVoteTypeValid(t tmproto.SignedMsgType) bool {
switch t {
case PrevoteType, PrecommitType:
case tmproto.PrevoteType, tmproto.PrecommitType:
return true
default:
return false

View File

@@ -3,9 +3,11 @@ package types
import (
"fmt"
"time"
tmproto "github.com/tendermint/tendermint/proto/types"
)
func MakeCommit(blockID BlockID, height int64, round int,
func MakeCommit(blockID BlockID, height int64, round int32,
voteSet *VoteSet, validators []PrivValidator, now time.Time) (*Commit, error) {
// all sign
@@ -16,10 +18,10 @@ func MakeCommit(blockID BlockID, height int64, round int,
}
vote := &Vote{
ValidatorAddress: pubKey.Address(),
ValidatorIndex: i,
ValidatorIndex: int32(i),
Height: height,
Round: round,
Type: PrecommitType,
Type: tmproto.PrecommitType,
BlockID: blockID,
Timestamp: now,
}
@@ -61,7 +63,7 @@ func MakeVote(
Height: height,
Round: 0,
Timestamp: now,
Type: PrecommitType,
Type: tmproto.PrecommitType,
BlockID: blockID,
}
if err := privVal.SignVote(chainID, vote); err != nil {

View File

@@ -104,7 +104,7 @@ func (vals *ValidatorSet) IsNilOrEmpty() bool {
// CopyIncrementProposerPriority increments ProposerPriority and updates the
// proposer on a copy, and returns it.
func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet {
func (vals *ValidatorSet) CopyIncrementProposerPriority(times int32) *ValidatorSet {
copy := vals.Copy()
copy.IncrementProposerPriority(times)
return copy
@@ -113,7 +113,7 @@ func (vals *ValidatorSet) CopyIncrementProposerPriority(times int) *ValidatorSet
// IncrementProposerPriority increments ProposerPriority of each validator and
// updates the proposer. Panics if validator set is empty.
// `times` must be positive.
func (vals *ValidatorSet) IncrementProposerPriority(times int) {
func (vals *ValidatorSet) IncrementProposerPriority(times int32) {
if vals.IsNilOrEmpty() {
panic("empty validator set")
}
@@ -130,7 +130,7 @@ func (vals *ValidatorSet) IncrementProposerPriority(times int) {
var proposer *Validator
// Call IncrementProposerPriority(1) times times.
for i := 0; i < times; i++ {
for i := int32(0); i < times; i++ {
proposer = vals.incrementProposerPriority()
}
@@ -267,10 +267,10 @@ func (vals *ValidatorSet) HasAddress(address []byte) bool {
// GetByAddress returns an index of the validator with address and validator
// itself (copy) if found. Otherwise, -1 and nil are returned.
func (vals *ValidatorSet) GetByAddress(address []byte) (index int, val *Validator) {
func (vals *ValidatorSet) GetByAddress(address []byte) (index int32, val *Validator) {
for idx, val := range vals.Validators {
if bytes.Equal(val.Address, address) {
return idx, val.Copy()
return int32(idx), val.Copy()
}
}
return -1, nil
@@ -280,8 +280,8 @@ func (vals *ValidatorSet) GetByAddress(address []byte) (index int, val *Validato
// index.
// It returns nil values if index is less than 0 or greater or equal to
// len(ValidatorSet.Validators).
func (vals *ValidatorSet) GetByIndex(index int) (address []byte, val *Validator) {
if index < 0 || index >= len(vals.Validators) {
func (vals *ValidatorSet) GetByIndex(index int32) (address []byte, val *Validator) {
if index < 0 || int(index) >= len(vals.Validators) {
return nil, nil
}
val = vals.Validators[index]
@@ -691,7 +691,7 @@ func (vals *ValidatorSet) VerifyCommit(chainID string, blockID BlockID,
val := vals.Validators[idx]
// Validate signature.
voteSignBytes := commit.VoteSignBytes(chainID, idx)
voteSignBytes := commit.VoteSignBytes(chainID, int32(idx))
if !val.PubKey.VerifyBytes(voteSignBytes, commitSig.Signature) {
return fmt.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
}
@@ -755,7 +755,7 @@ func (vals *ValidatorSet) VerifyFutureCommit(newSet *ValidatorSet, chainID strin
// Check old voting power.
oldVotingPower := int64(0)
seen := map[int]bool{}
seen := map[int32]bool{}
for idx, commitSig := range commit.Signatures {
if commitSig.Absent() {
@@ -770,7 +770,7 @@ func (vals *ValidatorSet) VerifyFutureCommit(newSet *ValidatorSet, chainID strin
seen[oldIdx] = true
// Validate signature.
voteSignBytes := commit.VoteSignBytes(chainID, idx)
voteSignBytes := commit.VoteSignBytes(chainID, int32(idx))
if !val.PubKey.VerifyBytes(voteSignBytes, commitSig.Signature) {
return fmt.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
}
@@ -803,7 +803,7 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, commit *Commit, t
var (
talliedVotingPower int64
seenVals = make(map[int]int, len(commit.Signatures)) // validator index -> commit index
seenVals = make(map[int32]int, len(commit.Signatures)) // validator index -> commit index
)
// Safely calculate voting power needed.
@@ -831,7 +831,7 @@ func (vals *ValidatorSet) VerifyCommitTrusting(chainID string, commit *Commit, t
seenVals[valIdx] = idx
// Validate signature.
voteSignBytes := commit.VoteSignBytes(chainID, idx)
voteSignBytes := commit.VoteSignBytes(chainID, int32(idx))
if !val.PubKey.VerifyBytes(voteSignBytes, commitSig.Signature) {
return fmt.Errorf("wrong signature (#%d): %X", idx, commitSig.Signature)
}

View File

@@ -17,6 +17,7 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
tmmath "github.com/tendermint/tendermint/libs/math"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
@@ -32,7 +33,7 @@ func TestValidatorSetBasic(t *testing.T) {
assert.EqualValues(t, vset, vset.Copy())
assert.False(t, vset.HasAddress([]byte("some val")))
idx, val := vset.GetByAddress([]byte("some val"))
assert.Equal(t, -1, idx)
assert.EqualValues(t, -1, idx)
assert.Nil(t, val)
addr, val := vset.GetByIndex(-100)
assert.Nil(t, addr)
@@ -54,7 +55,7 @@ func TestValidatorSetBasic(t *testing.T) {
assert.True(t, vset.HasAddress(val.Address))
idx, _ = vset.GetByAddress(val.Address)
assert.Equal(t, 0, idx)
assert.EqualValues(t, 0, idx)
addr, _ = vset.GetByIndex(0)
assert.Equal(t, []byte(val.Address), addr)
assert.Equal(t, 1, vset.Size())
@@ -315,7 +316,10 @@ func TestProposerSelection3(t *testing.T) {
// i for the loop
// j for the times
// we should go in order for ever, despite some IncrementProposerPriority with times > 1
var i, j int
var (
i int
j int32
)
for ; i < 10000; i++ {
got := vset.GetProposer().Address
expected := proposerOrder[j%4].Address
@@ -343,11 +347,11 @@ func TestProposerSelection3(t *testing.T) {
}
// times is usually 1
times := 1
times := int32(1)
mod := (tmrand.Int() % 5) + 1
if tmrand.Int()%mod > 0 {
// sometimes its up to 5
times = (tmrand.Int() % 4) + 1
times = (tmrand.Int31() % 4) + 1
}
vset.IncrementProposerPriority(times)
@@ -455,7 +459,7 @@ func TestAveragingInIncrementProposerPriority(t *testing.T) {
// the expected ProposerPriority.
tcs := []struct {
vs ValidatorSet
times int
times int32
avg int64
}{
0: {ValidatorSet{
@@ -505,7 +509,7 @@ func TestAveragingInIncrementProposerPriorityWithVotingPower(t *testing.T) {
tcs := []struct {
vals *ValidatorSet
wantProposerPrioritys []int64
times int
times int32
wantProposer *Validator
}{
@@ -660,7 +664,7 @@ func TestValidatorSetVerifyCommit(t *testing.T) {
Height: height,
Round: 0,
Timestamp: tmtime.Now(),
Type: PrecommitType,
Type: tmproto.PrecommitType,
BlockID: blockID,
}
sig, err := privKey.Sign(vote.SignBytes(chainID))
@@ -1233,7 +1237,7 @@ func applyChangesToValSet(t *testing.T, expErr error, valSet *ValidatorSet, vals
}
func TestValSetUpdatePriorityOrderTests(t *testing.T) {
const nMaxElections = 5000
const nMaxElections int32 = 5000
testCases := []testVSetCfg{
0: { // remove high power validator, keep old equal lower power validators
@@ -1283,9 +1287,9 @@ func TestValSetUpdatePriorityOrderTests(t *testing.T) {
}
}
func verifyValSetUpdatePriorityOrder(t *testing.T, valSet *ValidatorSet, cfg testVSetCfg, nMaxElections int) {
func verifyValSetUpdatePriorityOrder(t *testing.T, valSet *ValidatorSet, cfg testVSetCfg, nMaxElections int32) {
// Run election up to nMaxElections times, sort validators by priorities
valSet.IncrementProposerPriority(tmrand.Int()%nMaxElections + 1)
valSet.IncrementProposerPriority(tmrand.Int31()%nMaxElections + 1)
// apply the changes, get the updated validators, sort by priorities
applyChangesToValSet(t, nil, valSet, cfg.addedVals, cfg.updatedVals, cfg.deletedVals)
@@ -1387,7 +1391,7 @@ func TestValSetUpdateOverflowRelated(t *testing.T) {
func TestVerifyCommitTrusting(t *testing.T) {
var (
blockID = makeBlockIDRandom()
voteSet, originalValset, vals = randVoteSet(1, 1, PrecommitType, 6, 1)
voteSet, originalValset, vals = randVoteSet(1, 1, tmproto.PrecommitType, 6, 1)
commit, err = MakeCommit(blockID, 1, 1, voteSet, vals, time.Now())
newValSet, _ = RandValidatorSet(2, 1)
)
@@ -1428,7 +1432,7 @@ func TestVerifyCommitTrusting(t *testing.T) {
func TestVerifyCommitTrustingErrorsOnOverflow(t *testing.T) {
var (
blockID = makeBlockIDRandom()
voteSet, valSet, vals = randVoteSet(1, 1, PrecommitType, 1, MaxTotalVotingPower)
voteSet, valSet, vals = randVoteSet(1, 1, tmproto.PrecommitType, 1, MaxTotalVotingPower)
commit, err = MakeCommit(blockID, 1, 1, voteSet, vals, time.Now())
)
require.NoError(t, err)

View File

@@ -13,7 +13,7 @@ import (
const (
// MaxVoteBytes is a maximum vote size (including amino overhead).
MaxVoteBytes int64 = 223
MaxVoteBytes int64 = 211
nilVoteStr string = "nil-Vote"
)
@@ -47,14 +47,14 @@ type Address = crypto.Address
// Vote represents a prevote, precommit, or commit vote from validators for
// consensus.
type Vote struct {
Type SignedMsgType `json:"type"`
Height int64 `json:"height"`
Round int `json:"round"`
BlockID BlockID `json:"block_id"` // zero if vote is nil.
Timestamp time.Time `json:"timestamp"`
ValidatorAddress Address `json:"validator_address"`
ValidatorIndex int `json:"validator_index"`
Signature []byte `json:"signature"`
Type tmproto.SignedMsgType `json:"type"`
Height int64 `json:"height"`
Round int32 `json:"round"` // assume there will not be greater than 2_147_483_647 rounds
BlockID BlockID `json:"block_id"` // zero if vote is nil.
Timestamp time.Time `json:"timestamp"`
ValidatorAddress Address `json:"validator_address"`
ValidatorIndex int32 `json:"validator_index"`
Signature []byte `json:"signature"`
}
// CommitSig converts the Vote to a CommitSig.
@@ -101,9 +101,9 @@ func (vote *Vote) String() string {
var typeString string
switch vote.Type {
case PrevoteType:
case tmproto.PrevoteType:
typeString = "Prevote"
case PrecommitType:
case tmproto.PrecommitType:
typeString = "Precommit"
default:
panic("Unknown vote type")
@@ -138,9 +138,11 @@ func (vote *Vote) ValidateBasic() error {
if !IsVoteTypeValid(vote.Type) {
return errors.New("invalid Type")
}
if vote.Height < 0 {
return errors.New("negative Height")
}
if vote.Round < 0 {
return errors.New("negative Round")
}
@@ -181,13 +183,13 @@ func (vote *Vote) ToProto() *tmproto.Vote {
}
return &tmproto.Vote{
Type: tmproto.SignedMsgType(vote.Type),
Type: vote.Type,
Height: vote.Height,
Round: int64(vote.Round),
Round: vote.Round,
BlockID: vote.BlockID.ToProto(),
Timestamp: vote.Timestamp,
ValidatorAddress: vote.ValidatorAddress,
ValidatorIndex: int64(vote.ValidatorIndex),
ValidatorIndex: vote.ValidatorIndex,
Signature: vote.Signature,
}
}
@@ -205,13 +207,13 @@ func VoteFromProto(pv *tmproto.Vote) (*Vote, error) {
}
vote := new(Vote)
vote.Type = SignedMsgType(pv.Type)
vote.Type = pv.Type
vote.Height = pv.Height
vote.Round = int(pv.Round)
vote.Round = pv.Round
vote.BlockID = *blockID
vote.Timestamp = pv.Timestamp
vote.ValidatorAddress = pv.ValidatorAddress
vote.ValidatorIndex = int(pv.ValidatorIndex)
vote.ValidatorIndex = pv.ValidatorIndex
vote.Signature = pv.Signature
return vote, vote.ValidateBasic()

View File

@@ -7,6 +7,7 @@ import (
"sync"
"github.com/tendermint/tendermint/libs/bits"
tmproto "github.com/tendermint/tendermint/proto/types"
)
const (
@@ -59,8 +60,8 @@ type P2PID string
type VoteSet struct {
chainID string
height int64
round int
signedMsgType SignedMsgType
round int32
signedMsgType tmproto.SignedMsgType
valSet *ValidatorSet
mtx sync.Mutex
@@ -73,7 +74,8 @@ type VoteSet struct {
}
// Constructs a new VoteSet struct used to accumulate votes for given height/round.
func NewVoteSet(chainID string, height int64, round int, signedMsgType SignedMsgType, valSet *ValidatorSet) *VoteSet {
func NewVoteSet(chainID string, height int64, round int32,
signedMsgType tmproto.SignedMsgType, valSet *ValidatorSet) *VoteSet {
if height == 0 {
panic("Cannot make VoteSet for height == 0, doesn't make sense.")
}
@@ -105,7 +107,7 @@ func (voteSet *VoteSet) GetHeight() int64 {
}
// Implements VoteSetReader.
func (voteSet *VoteSet) GetRound() int {
func (voteSet *VoteSet) GetRound() int32 {
if voteSet == nil {
return -1
}
@@ -213,7 +215,7 @@ func (voteSet *VoteSet) addVote(vote *Vote) (added bool, err error) {
}
// Returns (vote, true) if vote exists for valIndex and blockKey.
func (voteSet *VoteSet) getVote(valIndex int, blockKey string) (vote *Vote, ok bool) {
func (voteSet *VoteSet) getVote(valIndex int32, blockKey string) (vote *Vote, ok bool) {
if existing := voteSet.votes[valIndex]; existing != nil && existing.BlockID.Key() == blockKey {
return existing, true
}
@@ -242,13 +244,13 @@ func (voteSet *VoteSet) addVerifiedVote(
// Replace vote if blockKey matches voteSet.maj23.
if voteSet.maj23 != nil && voteSet.maj23.Key() == blockKey {
voteSet.votes[valIndex] = vote
voteSet.votesBitArray.SetIndex(valIndex, true)
voteSet.votesBitArray.SetIndex(int(valIndex), true)
}
// Otherwise don't add it to voteSet.votes
} else {
// Add to voteSet.votes and incr .sum
voteSet.votes[valIndex] = vote
voteSet.votesBitArray.SetIndex(valIndex, true)
voteSet.votesBitArray.SetIndex(int(valIndex), true)
voteSet.sum += votingPower
}
@@ -363,7 +365,7 @@ func (voteSet *VoteSet) BitArrayByBlockID(blockID BlockID) *bits.BitArray {
// NOTE: if validator has conflicting votes, returns "canonical" vote
// Implements VoteSetReader.
func (voteSet *VoteSet) GetByIndex(valIndex int) *Vote {
func (voteSet *VoteSet) GetByIndex(valIndex int32) *Vote {
if voteSet == nil {
return nil
}
@@ -399,7 +401,7 @@ func (voteSet *VoteSet) IsCommit() bool {
if voteSet == nil {
return false
}
if voteSet.signedMsgType != PrecommitType {
if voteSet.signedMsgType != tmproto.PrecommitType {
return false
}
voteSet.mtx.Lock()
@@ -550,7 +552,7 @@ func (voteSet *VoteSet) sumTotalFrac() (int64, int64, float64) {
// Panics if the vote type is not PrecommitType or if
// there's no +2/3 votes for a single block.
func (voteSet *VoteSet) MakeCommit() *Commit {
if voteSet.signedMsgType != PrecommitType {
if voteSet.signedMsgType != tmproto.PrecommitType {
panic("Cannot MakeCommit() unless VoteSet.Type is PrecommitType")
}
voteSet.mtx.Lock()
@@ -597,13 +599,13 @@ func newBlockVotes(peerMaj23 bool, numValidators int) *blockVotes {
func (vs *blockVotes) addVerifiedVote(vote *Vote, votingPower int64) {
valIndex := vote.ValidatorIndex
if existing := vs.votes[valIndex]; existing == nil {
vs.bitArray.SetIndex(valIndex, true)
vs.bitArray.SetIndex(int(valIndex), true)
vs.votes[valIndex] = vote
vs.sum += votingPower
}
}
func (vs *blockVotes) getByIndex(index int) *Vote {
func (vs *blockVotes) getByIndex(index int32) *Vote {
if vs == nil {
return nil
}
@@ -615,10 +617,10 @@ func (vs *blockVotes) getByIndex(index int) *Vote {
// Common interface between *consensus.VoteSet and types.Commit
type VoteSetReader interface {
GetHeight() int64
GetRound() int
GetRound() int32
Type() byte
Size() int
BitArray() *bits.BitArray
GetByIndex(int) *Vote
GetByIndex(int32) *Vote
IsCommit() bool
}

View File

@@ -9,14 +9,15 @@ import (
"github.com/tendermint/tendermint/crypto"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
// NOTE: privValidators are in order
func randVoteSet(
height int64,
round int,
signedMsgType SignedMsgType,
round int32,
signedMsgType tmproto.SignedMsgType,
numValidators int,
votingPower int64,
) (*VoteSet, *ValidatorSet, []PrivValidator) {
@@ -25,7 +26,7 @@ func randVoteSet(
}
// Convenience: Return new vote with different validator address/index
func withValidator(vote *Vote, addr []byte, idx int) *Vote {
func withValidator(vote *Vote, addr []byte, idx int32) *Vote {
vote = vote.Copy()
vote.ValidatorAddress = addr
vote.ValidatorIndex = idx
@@ -40,7 +41,7 @@ func withHeight(vote *Vote, height int64) *Vote {
}
// Convenience: Return new vote with different round
func withRound(vote *Vote, round int) *Vote {
func withRound(vote *Vote, round int32) *Vote {
vote = vote.Copy()
vote.Round = round
return vote
@@ -49,7 +50,7 @@ func withRound(vote *Vote, round int) *Vote {
// Convenience: Return new vote with different type
func withType(vote *Vote, signedMsgType byte) *Vote {
vote = vote.Copy()
vote.Type = SignedMsgType(signedMsgType)
vote.Type = tmproto.SignedMsgType(signedMsgType)
return vote
}
@@ -68,8 +69,8 @@ func withBlockPartsHeader(vote *Vote, blockPartsHeader PartSetHeader) *Vote {
}
func TestAddVote(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 10, 1)
height, round := int64(1), int32(0)
voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrevoteType, 10, 1)
val0 := privValidators[0]
// t.Logf(">> %v", voteSet)
@@ -94,7 +95,7 @@ func TestAddVote(t *testing.T) {
ValidatorIndex: 0, // since privValidators are in order
Height: height,
Round: round,
Type: PrevoteType,
Type: tmproto.PrevoteType,
Timestamp: tmtime.Now(),
BlockID: BlockID{nil, PartSetHeader{}},
}
@@ -116,20 +117,20 @@ func TestAddVote(t *testing.T) {
}
func Test2_3Majority(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 10, 1)
height, round := int64(1), int32(0)
voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrevoteType, 10, 1)
voteProto := &Vote{
ValidatorAddress: nil, // NOTE: must fill in
ValidatorIndex: -1, // NOTE: must fill in
Height: height,
Round: round,
Type: PrevoteType,
Type: tmproto.PrevoteType,
Timestamp: tmtime.Now(),
BlockID: BlockID{nil, PartSetHeader{}},
}
// 6 out of 10 voted for nil.
for i := 0; i < 6; i++ {
for i := int32(0); i < 6; i++ {
pubKey, err := privValidators[i].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
@@ -178,11 +179,11 @@ func Test2_3Majority(t *testing.T) {
}
func Test2_3MajorityRedux(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 100, 1)
height, round := int64(1), int32(0)
voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrevoteType, 100, 1)
blockHash := crypto.CRandBytes(32)
blockPartsTotal := 123
blockPartsTotal := uint32(123)
blockPartsHeader := PartSetHeader{blockPartsTotal, crypto.CRandBytes(32)}
voteProto := &Vote{
@@ -191,12 +192,12 @@ func Test2_3MajorityRedux(t *testing.T) {
Height: height,
Round: round,
Timestamp: tmtime.Now(),
Type: PrevoteType,
Type: tmproto.PrevoteType,
BlockID: BlockID{blockHash, blockPartsHeader},
}
// 66 out of 100 voted for nil.
for i := 0; i < 66; i++ {
for i := int32(0); i < 66; i++ {
pubKey, err := privValidators[i].GetPubKey()
require.NoError(t, err)
addr := pubKey.Address()
@@ -295,8 +296,8 @@ func Test2_3MajorityRedux(t *testing.T) {
}
func TestBadVotes(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 10, 1)
height, round := int64(1), int32(0)
voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrevoteType, 10, 1)
voteProto := &Vote{
ValidatorAddress: nil,
@@ -304,7 +305,7 @@ func TestBadVotes(t *testing.T) {
Height: height,
Round: round,
Timestamp: tmtime.Now(),
Type: PrevoteType,
Type: tmproto.PrevoteType,
BlockID: BlockID{nil, PartSetHeader{}},
}
@@ -362,7 +363,7 @@ func TestBadVotes(t *testing.T) {
require.NoError(t, err)
addr := pubKey.Address()
vote := withValidator(voteProto, addr, 3)
added, err := signAddVote(privValidators[3], withType(vote, byte(PrecommitType)), voteSet)
added, err := signAddVote(privValidators[3], withType(vote, byte(tmproto.PrecommitType)), voteSet)
if added || err == nil {
t.Errorf("expected VoteSet.Add to fail, wrong type")
}
@@ -370,8 +371,8 @@ func TestBadVotes(t *testing.T) {
}
func TestConflicts(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrevoteType, 4, 1)
height, round := int64(1), int32(0)
voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrevoteType, 4, 1)
blockHash1 := tmrand.Bytes(32)
blockHash2 := tmrand.Bytes(32)
@@ -381,7 +382,7 @@ func TestConflicts(t *testing.T) {
Height: height,
Round: round,
Timestamp: tmtime.Now(),
Type: PrevoteType,
Type: tmproto.PrevoteType,
BlockID: BlockID{nil, PartSetHeader{}},
}
@@ -513,8 +514,8 @@ func TestConflicts(t *testing.T) {
}
func TestMakeCommit(t *testing.T) {
height, round := int64(1), 0
voteSet, _, privValidators := randVoteSet(height, round, PrecommitType, 10, 1)
height, round := int64(1), int32(0)
voteSet, _, privValidators := randVoteSet(height, round, tmproto.PrecommitType, 10, 1)
blockHash, blockPartsHeader := crypto.CRandBytes(32), PartSetHeader{123, crypto.CRandBytes(32)}
voteProto := &Vote{
@@ -523,12 +524,12 @@ func TestMakeCommit(t *testing.T) {
Height: height,
Round: round,
Timestamp: tmtime.Now(),
Type: PrecommitType,
Type: tmproto.PrecommitType,
BlockID: BlockID{blockHash, blockPartsHeader},
}
// 6 out of 10 voted for some block.
for i := 0; i < 6; i++ {
for i := int32(0); i < 6; i++ {
pv, err := privValidators[i].GetPubKey()
assert.NoError(t, err)
addr := pv.Address()
@@ -598,8 +599,8 @@ func TestMakeCommit(t *testing.T) {
func buildVoteSet(
height int64,
round, nonVotes, nonNilVotes, nilVotes int,
voteType SignedMsgType) (*VoteSet, *ValidatorSet, []PrivValidator, BlockID) {
round int32, nonVotes, nonNilVotes, nilVotes int,
voteType tmproto.SignedMsgType) (*VoteSet, *ValidatorSet, []PrivValidator, BlockID) {
valSize := nonVotes + nilVotes + nonNilVotes
voteSet, valSet, privValidators := randVoteSet(height, round, voteType, valSize, 1)
blockID := makeBlockIDRandom()
@@ -615,13 +616,13 @@ func buildVoteSet(
for i := 0; i < nonNilVotes; i++ {
pubKey, _ := privValidators[i].GetPubKey()
addr := pubKey.Address()
vote := withValidator(voteProto, addr, i)
vote := withValidator(voteProto, addr, int32(i))
_, _ = signAddVote(privValidators[i], vote, voteSet)
}
for i := nonNilVotes; i < nonNilVotes+nilVotes; i++ {
pubKey, _ := privValidators[i].GetPubKey()
addr := pubKey.Address()
vote := withValidator(voteProto, addr, i)
vote := withValidator(voteProto, addr, int32(i))
_, _ = signAddVote(privValidators[i], withBlockHash(vote, nil), voteSet)
}
return voteSet, valSet, privValidators, blockID

View File

@@ -11,14 +11,15 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/tmhash"
tmproto "github.com/tendermint/tendermint/proto/types"
)
func examplePrevote() *Vote {
return exampleVote(byte(PrevoteType))
return exampleVote(byte(tmproto.PrevoteType))
}
func examplePrecommit() *Vote {
return exampleVote(byte(PrecommitType))
return exampleVote(byte(tmproto.PrecommitType))
}
func exampleVote(t byte) *Vote {
@@ -28,7 +29,7 @@ func exampleVote(t byte) *Vote {
}
return &Vote{
Type: SignedMsgType(t),
Type: tmproto.SignedMsgType(t),
Height: 12345,
Round: 2,
Timestamp: stamp,
@@ -68,7 +69,7 @@ func TestVoteSignBytesTestVectors(t *testing.T) {
},
// with proper (fixed size) height and round (PreCommit):
1: {
"", &Vote{Height: 1, Round: 1, Type: PrecommitType},
"", &Vote{Height: 1, Round: 1, Type: tmproto.PrecommitType},
[]byte{
0x21, // length
0x8, // (field_number << 3) | wire_type
@@ -83,7 +84,7 @@ func TestVoteSignBytesTestVectors(t *testing.T) {
},
// with proper (fixed size) height and round (PreVote):
2: {
"", &Vote{Height: 1, Round: 1, Type: PrevoteType},
"", &Vote{Height: 1, Round: 1, Type: tmproto.PrevoteType},
[]byte{
0x21, // length
0x8, // (field_number << 3) | wire_type
@@ -174,12 +175,12 @@ func TestVoteVerifySignature(t *testing.T) {
func TestIsVoteTypeValid(t *testing.T) {
tc := []struct {
name string
in SignedMsgType
in tmproto.SignedMsgType
out bool
}{
{"Prevote", PrevoteType, true},
{"Precommit", PrecommitType, true},
{"InvalidType", SignedMsgType(0x3), false},
{"Prevote", tmproto.PrevoteType, true},
{"Precommit", tmproto.PrecommitType, true},
{"InvalidType", tmproto.SignedMsgType(0x3), false},
}
for _, tt := range tc {
@@ -218,15 +219,15 @@ func TestMaxVoteBytes(t *testing.T) {
vote := &Vote{
ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
ValidatorIndex: math.MaxInt64,
ValidatorIndex: math.MaxInt32,
Height: math.MaxInt64,
Round: math.MaxInt64,
Round: math.MaxInt32,
Timestamp: timestamp,
Type: PrevoteType,
Type: tmproto.PrevoteType,
BlockID: BlockID{
Hash: tmhash.Sum([]byte("blockID_hash")),
PartsHeader: PartSetHeader{
Total: math.MaxInt64,
Total: math.MaxInt32,
Hash: tmhash.Sum([]byte("blockID_part_set_header_hash")),
},
},