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