github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/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/lazyledger/lazyledger-core/crypto" 12 "github.com/lazyledger/lazyledger-core/crypto/ed25519" 13 cryptoenc "github.com/lazyledger/lazyledger-core/crypto/encoding" 14 "github.com/lazyledger/lazyledger-core/crypto/tmhash" 15 cryptoproto "github.com/lazyledger/lazyledger-core/proto/tendermint/crypto" 16 privproto "github.com/lazyledger/lazyledger-core/proto/tendermint/privval" 17 tmproto "github.com/lazyledger/lazyledger-core/proto/tendermint/types" 18 "github.com/lazyledger/lazyledger-core/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 DAHeader: &types.DataAvailabilityHeader{}, 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, err := proposal.ToProto() 74 require.NoError(t, err) 75 76 // Create a Reuseable remote error 77 remoteError := &privproto.RemoteSignerError{Code: 1, Description: "it's a error"} 78 79 testCases := []struct { 80 testName string 81 msg proto.Message 82 expBytes string 83 }{ 84 {"ping request", &privproto.PingRequest{}, "3a00"}, 85 {"ping response", &privproto.PingResponse{}, "4200"}, 86 {"pubKey request", &privproto.PubKeyRequest{}, "0a00"}, 87 {"pubKey response", &privproto.PubKeyResponse{PubKey: ppk, Error: nil}, "12240a220a20556a436f1218d30942efe798420f51dc9b6a311b929c578257457d05c5fcf230"}, 88 {"pubKey response with error", &privproto.PubKeyResponse{PubKey: cryptoproto.PublicKey{}, Error: remoteError}, "12140a0012100801120c697427732061206572726f72"}, 89 {"Vote Request", &privproto.SignVoteRequest{Vote: votepb}, "1a760a74080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03"}, 90 {"Vote Response", &privproto.SignedVoteResponse{Vote: *votepb, Error: nil}, "22760a74080110031802224a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a2a0608f49a8ded0532146af1f4111082efb388211bc72c55bcd61e9ac3d538d5bb03"}, 91 {"Vote Response with error", &privproto.SignedVoteResponse{Vote: tmproto.Vote{}, Error: remoteError}, "22250a11220212002a0b088092b8c398feffffff0112100801120c697427732061206572726f72"}, 92 {"Proposal Request", &privproto.SignProposalRequest{Proposal: proposalpb}, "2a720a7008011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e61747572654200"}, 93 {"Proposal Response", &privproto.SignedProposalResponse{Proposal: *proposalpb, Error: nil}, "32720a7008011003180220022a4a0a208b01023386c371778ecb6368573e539afc3cc860ec3a2f614e54fe5652f4fc80122608c0843d122072db3d959635dff1bb567bedaa70573392c5159666a3f8caf11e413aac52207a320608f49a8ded053a10697427732061207369676e61747572654200"}, 94 {"Proposal Response with error", &privproto.SignedProposalResponse{Proposal: tmproto.Proposal{}, Error: remoteError}, "32250a112a021200320b088092b8c398feffffff0112100801120c697427732061206572726f72"}, 95 } 96 97 for _, tc := range testCases { 98 tc := tc 99 100 pm := mustWrapMsg(tc.msg) 101 bz, err := pm.Marshal() 102 require.NoError(t, err, tc.testName) 103 104 require.Equal(t, tc.expBytes, hex.EncodeToString(bz), tc.testName) 105 } 106 }