refactor: improve string representation for a vote against the proposal (#8745) (#8769)

This commit is contained in:
mergify[bot]
2022-06-15 15:19:07 +02:00
committed by GitHub
parent efda2ff816
commit cbba7f3d74
3 changed files with 67 additions and 16 deletions

View File

@@ -13,7 +13,8 @@ import (
)
const (
nilVoteStr string = "nil-Vote"
absentVoteStr string = "Vote{absent}"
nilVoteStr string = "nil"
// The maximum supported number of bytes in a vote extension.
MaxVoteExtensionSize int = 1024 * 1024
@@ -189,7 +190,7 @@ func (vote *Vote) Copy() *Vote {
// 10. timestamp
func (vote *Vote) String() string {
if vote == nil {
return nilVoteStr
return absentVoteStr
}
var typeString string
@@ -202,16 +203,22 @@ func (vote *Vote) String() string {
panic("Unknown vote type")
}
return fmt.Sprintf("Vote{%v:%X %v/%02d/%v(%v) %X %X %X @ %s}",
var blockHashString string
if len(vote.BlockID.Hash) > 0 {
blockHashString = fmt.Sprintf("%X", tmbytes.Fingerprint(vote.BlockID.Hash))
} else {
blockHashString = nilVoteStr
}
return fmt.Sprintf("Vote{%v:%X %v/%d %s %s %X %d @ %s}",
vote.ValidatorIndex,
tmbytes.Fingerprint(vote.ValidatorAddress),
vote.Height,
vote.Round,
vote.Type,
typeString,
tmbytes.Fingerprint(vote.BlockID.Hash),
blockHashString,
tmbytes.Fingerprint(vote.Signature),
tmbytes.Fingerprint(vote.Extension),
len(vote.Extension),
CanonicalTime(vote.Timestamp),
)
}

View File

@@ -505,7 +505,7 @@ func (voteSet *VoteSet) StringIndented(indent string) string {
voteStrings := make([]string, len(voteSet.votes))
for i, vote := range voteSet.votes {
if vote == nil {
voteStrings[i] = nilVoteStr
voteStrings[i] = absentVoteStr
} else {
voteStrings[i] = vote.String()
}
@@ -570,7 +570,7 @@ func (voteSet *VoteSet) voteStrings() []string {
voteStrings := make([]string, len(voteSet.votes))
for i, vote := range voteSet.votes {
if vote == nil {
voteStrings[i] = nilVoteStr
voteStrings[i] = absentVoteStr
} else {
voteStrings[i] = vote.String()
}

View File

@@ -2,6 +2,7 @@ package types
import (
"context"
"fmt"
"testing"
"time"
@@ -16,6 +17,22 @@ import (
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
const (
//nolint: lll
preCommitTestStr = `Vote{56789:6AF1F4111082 12345/2 Precommit 8B01023386C3 000000000000 0 @ 2017-12-25T03:00:01.234Z}`
//nolint: lll
preVoteTestStr = `Vote{56789:6AF1F4111082 12345/2 Prevote 8B01023386C3 000000000000 0 @ 2017-12-25T03:00:01.234Z}`
)
var (
// nolint: lll
nilVoteTestStr = fmt.Sprintf(`Vote{56789:6AF1F4111082 12345/2 Precommit %s 000000000000 0 @ 2017-12-25T03:00:01.234Z}`, nilVoteStr)
formatNonEmptyVoteExtensionFn = func(voteExtensionLength int) string {
// nolint: lll
return fmt.Sprintf(`Vote{56789:6AF1F4111082 12345/2 Precommit 8B01023386C3 000000000000 %d @ 2017-12-25T03:00:01.234Z}`, voteExtensionLength)
}
)
func examplePrevote(t *testing.T) *Vote {
t.Helper()
return exampleVote(t, byte(tmproto.PrevoteType))
@@ -321,16 +338,43 @@ func TestVoteVerify(t *testing.T) {
}
func TestVoteString(t *testing.T) {
str := examplePrecommit(t).String()
expected := `Vote{56789:6AF1F4111082 12345/02/SIGNED_MSG_TYPE_PRECOMMIT(Precommit) 8B01023386C3 000000000000 000000000000 @ 2017-12-25T03:00:01.234Z}` //nolint:lll //ignore line length for tests
if str != expected {
t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str)
testcases := map[string]struct {
vote *Vote
expectedResult string
}{
"pre-commit": {
vote: examplePrecommit(t),
expectedResult: preCommitTestStr,
},
"pre-vote": {
vote: examplePrevote(t),
expectedResult: preVoteTestStr,
},
"absent vote": {
expectedResult: absentVoteStr,
},
"nil vote": {
vote: func() *Vote {
v := examplePrecommit(t)
v.BlockID.Hash = nil
return v
}(),
expectedResult: nilVoteTestStr,
},
"non-empty vote extension": {
vote: func() *Vote {
v := examplePrecommit(t)
v.Extension = []byte{1, 2}
return v
}(),
expectedResult: formatNonEmptyVoteExtensionFn(2),
},
}
str2 := examplePrevote(t).String()
expected = `Vote{56789:6AF1F4111082 12345/02/SIGNED_MSG_TYPE_PREVOTE(Prevote) 8B01023386C3 000000000000 000000000000 @ 2017-12-25T03:00:01.234Z}` //nolint:lll //ignore line length for tests
if str2 != expected {
t.Errorf("got unexpected string for Vote. Expected:\n%v\nGot:\n%v", expected, str2)
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
require.Equal(t, tc.expectedResult, tc.vote.String())
})
}
}