You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
7.0 KiB

  1. package consensus
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/tendermint/tendermint/crypto/merkle"
  8. "github.com/tendermint/tendermint/libs/bits"
  9. tmrand "github.com/tendermint/tendermint/libs/rand"
  10. "github.com/tendermint/tendermint/p2p"
  11. tmcons "github.com/tendermint/tendermint/proto/consensus"
  12. tmproto "github.com/tendermint/tendermint/proto/types"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. func TestMsgToProto(t *testing.T) {
  16. psh := types.PartSetHeader{
  17. Total: 1,
  18. Hash: tmrand.Bytes(32),
  19. }
  20. pbPsh := psh.ToProto()
  21. bi := types.BlockID{
  22. Hash: tmrand.Bytes(32),
  23. PartSetHeader: psh,
  24. }
  25. pbBi := bi.ToProto()
  26. bits := bits.NewBitArray(1)
  27. pbBits := bits.ToProto()
  28. parts := types.Part{
  29. Index: 1,
  30. Bytes: []byte("test"),
  31. Proof: merkle.Proof{
  32. Total: 1,
  33. Index: 1,
  34. LeafHash: tmrand.Bytes(32),
  35. Aunts: [][]byte{},
  36. },
  37. }
  38. pbParts, err := parts.ToProto()
  39. require.NoError(t, err)
  40. proposal := types.Proposal{
  41. Type: tmproto.ProposalType,
  42. Height: 1,
  43. Round: 1,
  44. POLRound: 1,
  45. BlockID: bi,
  46. Timestamp: time.Now(),
  47. Signature: tmrand.Bytes(20),
  48. }
  49. pbProposal := proposal.ToProto()
  50. pv := types.NewMockPV()
  51. pk, err := pv.GetPubKey()
  52. require.NoError(t, err)
  53. val := types.NewValidator(pk, 100)
  54. vote, err := types.MakeVote(
  55. 1, types.BlockID{}, &types.ValidatorSet{Proposer: val, Validators: []*types.Validator{val}},
  56. pv, "chainID", time.Now())
  57. require.NoError(t, err)
  58. pbVote := vote.ToProto()
  59. testsCases := []struct {
  60. testName string
  61. msg Message
  62. want *tmcons.Message
  63. wantErr bool
  64. }{
  65. {"successful NewRoundStepMessage", &NewRoundStepMessage{
  66. Height: 2,
  67. Round: 1,
  68. Step: 1,
  69. SecondsSinceStartTime: 1,
  70. LastCommitRound: 2,
  71. }, &tmcons.Message{
  72. Sum: &tmcons.Message_NewRoundStep{
  73. NewRoundStep: &tmcons.NewRoundStep{
  74. Height: 2,
  75. Round: 1,
  76. Step: 1,
  77. SecondsSinceStartTime: 1,
  78. LastCommitRound: 2,
  79. },
  80. },
  81. }, false},
  82. {"successful NewValidBlockMessage", &NewValidBlockMessage{
  83. Height: 1,
  84. Round: 1,
  85. BlockPartSetHeader: psh,
  86. BlockParts: bits,
  87. IsCommit: false,
  88. }, &tmcons.Message{
  89. Sum: &tmcons.Message_NewValidBlock{
  90. NewValidBlock: &tmcons.NewValidBlock{
  91. Height: 1,
  92. Round: 1,
  93. BlockPartSetHeader: pbPsh,
  94. BlockParts: pbBits,
  95. IsCommit: false,
  96. },
  97. },
  98. }, false},
  99. {"successful BlockPartMessage", &BlockPartMessage{
  100. Height: 100,
  101. Round: 1,
  102. Part: &parts,
  103. }, &tmcons.Message{
  104. Sum: &tmcons.Message_BlockPart{
  105. BlockPart: &tmcons.BlockPart{
  106. Height: 100,
  107. Round: 1,
  108. Part: *pbParts,
  109. },
  110. },
  111. }, false},
  112. {"successful ProposalPOLMessage", &ProposalPOLMessage{
  113. Height: 1,
  114. ProposalPOLRound: 1,
  115. ProposalPOL: bits,
  116. }, &tmcons.Message{
  117. Sum: &tmcons.Message_ProposalPol{
  118. ProposalPol: &tmcons.ProposalPOL{
  119. Height: 1,
  120. ProposalPolRound: 1,
  121. ProposalPol: *pbBits,
  122. },
  123. }}, false},
  124. {"successful ProposalMessage", &ProposalMessage{
  125. Proposal: &proposal,
  126. }, &tmcons.Message{
  127. Sum: &tmcons.Message_Proposal{
  128. Proposal: &tmcons.Proposal{
  129. Proposal: *pbProposal,
  130. },
  131. },
  132. }, false},
  133. {"successful VoteMessage", &VoteMessage{
  134. Vote: vote,
  135. }, &tmcons.Message{
  136. Sum: &tmcons.Message_Vote{
  137. Vote: &tmcons.Vote{
  138. Vote: pbVote,
  139. },
  140. },
  141. }, false},
  142. {"successful VoteSetMaj23", &VoteSetMaj23Message{
  143. Height: 1,
  144. Round: 1,
  145. Type: 1,
  146. BlockID: bi,
  147. }, &tmcons.Message{
  148. Sum: &tmcons.Message_VoteSetMaj23{
  149. VoteSetMaj23: &tmcons.VoteSetMaj23{
  150. Height: 1,
  151. Round: 1,
  152. Type: 1,
  153. BlockID: pbBi,
  154. },
  155. },
  156. }, false},
  157. {"successful VoteSetBits", &VoteSetBitsMessage{
  158. Height: 1,
  159. Round: 1,
  160. Type: 1,
  161. BlockID: bi,
  162. Votes: bits,
  163. }, &tmcons.Message{
  164. Sum: &tmcons.Message_VoteSetBits{
  165. VoteSetBits: &tmcons.VoteSetBits{
  166. Height: 1,
  167. Round: 1,
  168. Type: 1,
  169. BlockID: pbBi,
  170. Votes: *pbBits,
  171. },
  172. },
  173. }, false},
  174. {"failure", nil, &tmcons.Message{}, true},
  175. }
  176. for _, tt := range testsCases {
  177. tt := tt
  178. t.Run(tt.testName, func(t *testing.T) {
  179. pb, err := MsgToProto(tt.msg)
  180. if tt.wantErr == true {
  181. assert.Equal(t, err != nil, tt.wantErr)
  182. return
  183. }
  184. assert.EqualValues(t, tt.want, pb, tt.testName)
  185. msg, err := MsgFromProto(pb)
  186. if !tt.wantErr {
  187. require.NoError(t, err)
  188. bcm := assert.Equal(t, tt.msg, msg, tt.testName)
  189. assert.True(t, bcm, tt.testName)
  190. } else {
  191. require.Error(t, err, tt.testName)
  192. }
  193. })
  194. }
  195. }
  196. func TestWALMsgProto(t *testing.T) {
  197. parts := types.Part{
  198. Index: 1,
  199. Bytes: []byte("test"),
  200. Proof: merkle.Proof{
  201. Total: 1,
  202. Index: 1,
  203. LeafHash: tmrand.Bytes(32),
  204. Aunts: [][]byte{},
  205. },
  206. }
  207. pbParts, err := parts.ToProto()
  208. require.NoError(t, err)
  209. testsCases := []struct {
  210. testName string
  211. msg WALMessage
  212. want *tmcons.WALMessage
  213. wantErr bool
  214. }{
  215. {"successful EventDataRoundState", types.EventDataRoundState{
  216. Height: 2,
  217. Round: 1,
  218. Step: "ronies",
  219. }, &tmcons.WALMessage{
  220. Sum: &tmcons.WALMessage_EventDataRoundState{
  221. EventDataRoundState: &tmproto.EventDataRoundState{
  222. Height: 2,
  223. Round: 1,
  224. Step: "ronies",
  225. },
  226. },
  227. }, false},
  228. {"successful msgInfo", msgInfo{
  229. Msg: &BlockPartMessage{
  230. Height: 100,
  231. Round: 1,
  232. Part: &parts,
  233. },
  234. PeerID: p2p.ID("string"),
  235. }, &tmcons.WALMessage{
  236. Sum: &tmcons.WALMessage_MsgInfo{
  237. MsgInfo: &tmcons.MsgInfo{
  238. Msg: tmcons.Message{
  239. Sum: &tmcons.Message_BlockPart{
  240. BlockPart: &tmcons.BlockPart{
  241. Height: 100,
  242. Round: 1,
  243. Part: *pbParts,
  244. },
  245. },
  246. },
  247. PeerID: "string",
  248. },
  249. },
  250. }, false},
  251. {"successful timeoutInfo", timeoutInfo{
  252. Duration: time.Duration(100),
  253. Height: 1,
  254. Round: 1,
  255. Step: 1,
  256. }, &tmcons.WALMessage{
  257. Sum: &tmcons.WALMessage_TimeoutInfo{
  258. TimeoutInfo: &tmcons.TimeoutInfo{
  259. Duration: time.Duration(100),
  260. Height: 1,
  261. Round: 1,
  262. Step: 1,
  263. },
  264. },
  265. }, false},
  266. {"successful EndHeightMessage", EndHeightMessage{
  267. Height: 1,
  268. }, &tmcons.WALMessage{
  269. Sum: &tmcons.WALMessage_EndHeight{
  270. EndHeight: &tmcons.EndHeight{
  271. Height: 1,
  272. },
  273. },
  274. }, false},
  275. {"failure", nil, &tmcons.WALMessage{}, true},
  276. }
  277. for _, tt := range testsCases {
  278. tt := tt
  279. t.Run(tt.testName, func(t *testing.T) {
  280. pb, err := WALToProto(tt.msg)
  281. if tt.wantErr == true {
  282. assert.Equal(t, err != nil, tt.wantErr)
  283. return
  284. }
  285. assert.EqualValues(t, tt.want, pb, tt.testName)
  286. msg, err := WALFromProto(pb)
  287. if !tt.wantErr {
  288. require.NoError(t, err)
  289. assert.Equal(t, tt.msg, msg, tt.testName) // need the concrete type as WAL Message is a empty interface
  290. } else {
  291. require.Error(t, err, tt.testName)
  292. }
  293. })
  294. }
  295. }