preprepare_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "math/big"
  19. "reflect"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/consensus/istanbul"
  22. istanbulcommon "github.com/ethereum/go-ethereum/consensus/istanbul/common"
  23. ibfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/ibft/types"
  24. )
  25. func newTestPreprepare(v *istanbul.View) *istanbul.Preprepare {
  26. return &istanbul.Preprepare{
  27. View: v,
  28. Proposal: newTestProposal(),
  29. }
  30. }
  31. func TestHandlePreprepare(t *testing.T) {
  32. N := uint64(4) // replica 0 is the proposer, it will send messages to others
  33. F := uint64(1) // F does not affect tests
  34. testCases := []struct {
  35. system *testSystem
  36. expectedRequest istanbul.Proposal
  37. expectedErr error
  38. existingBlock bool
  39. }{
  40. {
  41. // normal case
  42. func() *testSystem {
  43. sys := NewTestSystemWithBackend(N, F)
  44. for i, backend := range sys.backends {
  45. c := backend.engine
  46. c.valSet = backend.peers
  47. if i != 0 {
  48. c.state = ibfttypes.StateAcceptRequest
  49. }
  50. }
  51. return sys
  52. }(),
  53. newTestProposal(),
  54. nil,
  55. false,
  56. },
  57. {
  58. // future message
  59. func() *testSystem {
  60. sys := NewTestSystemWithBackend(N, F)
  61. for i, backend := range sys.backends {
  62. c := backend.engine
  63. c.valSet = backend.peers
  64. if i != 0 {
  65. c.state = ibfttypes.StateAcceptRequest
  66. // hack: force set subject that future message can be simulated
  67. c.current = newTestRoundState(
  68. &istanbul.View{
  69. Round: big.NewInt(0),
  70. Sequence: big.NewInt(0),
  71. },
  72. c.valSet,
  73. )
  74. } else {
  75. c.current.SetSequence(big.NewInt(10))
  76. }
  77. }
  78. return sys
  79. }(),
  80. makeBlock(1),
  81. istanbulcommon.ErrFutureMessage,
  82. false,
  83. },
  84. {
  85. // non-proposer
  86. func() *testSystem {
  87. sys := NewTestSystemWithBackend(N, F)
  88. // force remove replica 0, let replica 1 be the proposer
  89. sys.backends = sys.backends[1:]
  90. for i, backend := range sys.backends {
  91. c := backend.engine
  92. c.valSet = backend.peers
  93. if i != 0 {
  94. // replica 0 is the proposer
  95. c.state = ibfttypes.StatePreprepared
  96. }
  97. }
  98. return sys
  99. }(),
  100. makeBlock(1),
  101. istanbulcommon.ErrNotFromProposer,
  102. false,
  103. },
  104. {
  105. // errOldMessage
  106. func() *testSystem {
  107. sys := NewTestSystemWithBackend(N, F)
  108. for i, backend := range sys.backends {
  109. c := backend.engine
  110. c.valSet = backend.peers
  111. if i != 0 {
  112. c.state = ibfttypes.StatePreprepared
  113. c.current.SetSequence(big.NewInt(10))
  114. c.current.SetRound(big.NewInt(10))
  115. }
  116. }
  117. return sys
  118. }(),
  119. makeBlock(1),
  120. istanbulcommon.ErrOldMessage,
  121. false,
  122. },
  123. }
  124. OUTER:
  125. for _, test := range testCases {
  126. test.system.Run(false)
  127. v0 := test.system.backends[0]
  128. r0 := v0.engine
  129. curView := r0.currentView()
  130. preprepare := &istanbul.Preprepare{
  131. View: curView,
  132. Proposal: test.expectedRequest,
  133. }
  134. for i, v := range test.system.backends {
  135. // i == 0 is primary backend, it is responsible for send PRE-PREPARE messages to others.
  136. if i == 0 {
  137. continue
  138. }
  139. c := v.engine
  140. m, _ := ibfttypes.Encode(preprepare)
  141. _, val := r0.valSet.GetByAddress(v0.Address())
  142. // run each backends and verify handlePreprepare function.
  143. if err := c.handlePreprepare(&ibfttypes.Message{
  144. Code: ibfttypes.MsgPreprepare,
  145. Msg: m,
  146. Address: v0.Address(),
  147. }, val); err != nil {
  148. if err != test.expectedErr {
  149. t.Errorf("error mismatch: have %v, want %v", err, test.expectedErr)
  150. }
  151. continue OUTER
  152. }
  153. if c.state != ibfttypes.StatePreprepared {
  154. t.Errorf("state mismatch: have %v, want %v", c.state, ibfttypes.StatePreprepared)
  155. }
  156. if !test.existingBlock && !reflect.DeepEqual(c.current.Subject().View, curView) {
  157. t.Errorf("view mismatch: have %v, want %v", c.current.Subject().View, curView)
  158. }
  159. // verify prepare messages
  160. decodedMsg := new(ibfttypes.Message)
  161. err := decodedMsg.FromPayload(v.sentMsgs[0], nil)
  162. if err != nil {
  163. t.Errorf("error mismatch: have %v, want nil", err)
  164. }
  165. expectedCode := ibfttypes.MsgPrepare
  166. if test.existingBlock {
  167. expectedCode = ibfttypes.MsgCommit
  168. }
  169. if decodedMsg.Code != expectedCode {
  170. t.Errorf("message code mismatch: have %v, want %v", decodedMsg.Code, expectedCode)
  171. }
  172. var subject *istanbul.Subject
  173. err = decodedMsg.Decode(&subject)
  174. if err != nil {
  175. t.Errorf("error mismatch: have %v, want nil", err)
  176. }
  177. if !test.existingBlock && !reflect.DeepEqual(subject, c.current.Subject()) {
  178. t.Errorf("subject mismatch: have %v, want %v", subject, c.current.Subject())
  179. }
  180. }
  181. }
  182. }
  183. func TestHandlePreprepareWithLock(t *testing.T) {
  184. N := uint64(4) // replica 0 is the proposer, it will send messages to others
  185. F := uint64(1) // F does not affect tests
  186. proposal := newTestProposal()
  187. mismatchProposal := makeBlock(10)
  188. newSystem := func() *testSystem {
  189. sys := NewTestSystemWithBackend(N, F)
  190. for i, backend := range sys.backends {
  191. c := backend.engine
  192. c.valSet = backend.peers
  193. if i != 0 {
  194. c.state = ibfttypes.StateAcceptRequest
  195. }
  196. c.roundChangeSet = newRoundChangeSet(c.valSet)
  197. }
  198. return sys
  199. }
  200. testCases := []struct {
  201. system *testSystem
  202. proposal istanbul.Proposal
  203. lockProposal istanbul.Proposal
  204. }{
  205. {
  206. newSystem(),
  207. proposal,
  208. proposal,
  209. },
  210. {
  211. newSystem(),
  212. proposal,
  213. mismatchProposal,
  214. },
  215. }
  216. for _, test := range testCases {
  217. test.system.Run(false)
  218. v0 := test.system.backends[0]
  219. r0 := v0.engine
  220. curView := r0.currentView()
  221. preprepare := &istanbul.Preprepare{
  222. View: curView,
  223. Proposal: test.proposal,
  224. }
  225. lockPreprepare := &istanbul.Preprepare{
  226. View: curView,
  227. Proposal: test.lockProposal,
  228. }
  229. for i, v := range test.system.backends {
  230. // i == 0 is primary backend, it is responsible for send PRE-PREPARE messages to others.
  231. if i == 0 {
  232. continue
  233. }
  234. c := v.engine
  235. c.current.SetPreprepare(lockPreprepare)
  236. c.current.LockHash()
  237. m, _ := ibfttypes.Encode(preprepare)
  238. _, val := r0.valSet.GetByAddress(v0.Address())
  239. if err := c.handlePreprepare(&ibfttypes.Message{
  240. Code: ibfttypes.MsgPreprepare,
  241. Msg: m,
  242. Address: v0.Address(),
  243. }, val); err != nil {
  244. t.Errorf("error mismatch: have %v, want nil", err)
  245. }
  246. if test.proposal == test.lockProposal {
  247. if c.state != ibfttypes.StatePrepared {
  248. t.Errorf("state mismatch: have %v, want %v", c.state, ibfttypes.StatePreprepared)
  249. }
  250. if !reflect.DeepEqual(curView, c.currentView()) {
  251. t.Errorf("view mismatch: have %v, want %v", c.currentView(), curView)
  252. }
  253. } else {
  254. // Should stay at ibfttypes.StateAcceptRequest
  255. if c.state != ibfttypes.StateAcceptRequest {
  256. t.Errorf("state mismatch: have %v, want %v", c.state, ibfttypes.StateAcceptRequest)
  257. }
  258. // Should have triggered a round change
  259. expectedView := &istanbul.View{
  260. Sequence: curView.Sequence,
  261. Round: big.NewInt(1),
  262. }
  263. if !reflect.DeepEqual(expectedView, c.currentView()) {
  264. t.Errorf("view mismatch: have %v, want %v", c.currentView(), expectedView)
  265. }
  266. }
  267. }
  268. }
  269. }