gitlab.com/gpdionisio/tendermint@v0.34.19-dev2/privval/msgs_test.go (about)

     1  package privval
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/gogo/protobuf/proto"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/tendermint/tendermint/crypto"
    12  	"github.com/tendermint/tendermint/crypto/ed25519"
    13  	cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
    14  	"github.com/tendermint/tendermint/crypto/tmhash"
    15  	cryptoproto "github.com/tendermint/tendermint/proto/tendermint/crypto"
    16  	privproto "github.com/tendermint/tendermint/proto/tendermint/privval"
    17  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    18  	"github.com/tendermint/tendermint/types"
    19  )
    20  
    21  var stamp = time.Date(2019, 10, 13, 16, 14, 44, 0, time.UTC)
    22  
    23  func exampleVote() *types.Vote {
    24  	return &types.Vote{
    25  		Type:      tmproto.SignedMsgType(1),
    26  		Height:    3,
    27  		Round:     2,
    28  		Timestamp: stamp,
    29  		BlockID: types.BlockID{
    30  			Hash: tmhash.Sum([]byte("blockID_hash")),
    31  			PartSetHeader: types.PartSetHeader{
    32  				Total: 1000000,
    33  				Hash:  tmhash.Sum([]byte("blockID_part_set_header_hash")),
    34  			},
    35  		},
    36  		ValidatorAddress: crypto.AddressHash([]byte("validator_address")),
    37  		ValidatorIndex:   56789,
    38  	}
    39  }
    40  
    41  func exampleProposal() *types.Proposal {
    42  
    43  	return &types.Proposal{
    44  		Type:      tmproto.SignedMsgType(1),
    45  		Height:    3,
    46  		Round:     2,
    47  		Timestamp: stamp,
    48  		POLRound:  2,
    49  		Signature: []byte("it's a signature"),
    50  		BlockID: types.BlockID{
    51  			Hash: tmhash.Sum([]byte("blockID_hash")),
    52  			PartSetHeader: types.PartSetHeader{
    53  				Total: 1000000,
    54  				Hash:  tmhash.Sum([]byte("blockID_part_set_header_hash")),
    55  			},
    56  		},
    57  	}
    58  }
    59  
    60  // nolint:lll // ignore line length for tests
    61  func TestPrivvalVectors(t *testing.T) {
    62  	pk := ed25519.GenPrivKeyFromSecret([]byte("it's a secret")).PubKey()
    63  	ppk, err := cryptoenc.PubKeyToProto(pk)
    64  	require.NoError(t, err)
    65  
    66  	// Generate a simple vote
    67  	vote := exampleVote()
    68  	votepb := vote.ToProto()
    69  
    70  	// Generate a simple proposal
    71  	proposal := exampleProposal()
    72  	proposalpb := proposal.ToProto()
    73  
    74  	// Create a Reuseable remote error
    75  	remoteError := &privproto.RemoteSignerError{Code: 1, Description: "it's a error"}
    76  
    77  	testCases := []struct {
    78  		testName string
    79  		msg      proto.Message
    80  		expBytes string
    81  	}{
    82  		{"ping request", &privproto.PingRequest{}, "3a00"},
    83  		{"ping response", &privproto.PingResponse{}, "4200"},
    84  		{"pubKey request", &privproto.PubKeyRequest{}, "0a00"},
    85  		{"pubKey response", &privproto.PubKeyResponse{PubKey: ppk, Error: nil}, "12240a220a20556a436f1218d30942efe798420f51dc9b6a311b929c578257457d05c5fcf230"},
    86  		{"pubKey response with error", &privproto.PubKeyResponse{PubKey: cryptoproto.PublicKey{}, Error: remoteError}, "12140a0012100801120c697427732061206572726f72"},
    87  		{"Vote Request", &privproto.SignVoteRequest{Vote: votepb}, "1a760a74080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03"},
    88  		{"Vote Response", &privproto.SignedVoteResponse{Vote: *votepb, Error: nil}, "22760a74080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03"},
    89  		{"Vote Response with error", &privproto.SignedVoteResponse{Vote: tmproto.Vote{}, Error: remoteError}, "22250a11220212002a0b088092b8c398feffffff0112100801120c697427732061206572726f72"},
    90  		{"Proposal Request", &privproto.SignProposalRequest{Proposal: proposalpb}, "2a700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
    91  		{"Proposal Response", &privproto.SignedProposalResponse{Proposal: *proposalpb, Error: nil}, "32700a6e08011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e6174757265"},
    92  		{"Proposal Response with error", &privproto.SignedProposalResponse{Proposal: tmproto.Proposal{}, Error: remoteError}, "32250a112a021200320b088092b8c398feffffff0112100801120c697427732061206572726f72"},
    93  	}
    94  
    95  	for _, tc := range testCases {
    96  		tc := tc
    97  
    98  		pm := mustWrapMsg(tc.msg)
    99  		bz, err := pm.Marshal()
   100  		require.NoError(t, err, tc.testName)
   101  
   102  		require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName)
   103  	}
   104  }