backlog_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. "sync"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/consensus/istanbul"
  25. istanbulcommon "github.com/ethereum/go-ethereum/consensus/istanbul/common"
  26. ibfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/ibft/types"
  27. "github.com/ethereum/go-ethereum/event"
  28. "github.com/ethereum/go-ethereum/log"
  29. "gopkg.in/karalabe/cookiejar.v2/collections/prque"
  30. )
  31. func TestCheckMessage(t *testing.T) {
  32. c := &core{
  33. state: ibfttypes.StateAcceptRequest,
  34. current: newRoundState(&istanbul.View{
  35. Sequence: big.NewInt(1),
  36. Round: big.NewInt(0),
  37. }, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
  38. }
  39. // invalid view format
  40. err := c.checkMessage(ibfttypes.MsgPreprepare, nil)
  41. if err != istanbulcommon.ErrInvalidMessage {
  42. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrInvalidMessage)
  43. }
  44. testStates := []ibfttypes.State{ibfttypes.StateAcceptRequest, ibfttypes.StatePreprepared, ibfttypes.StatePrepared, ibfttypes.StateCommitted}
  45. testCode := []uint64{ibfttypes.MsgPreprepare, ibfttypes.MsgPrepare, ibfttypes.MsgCommit, ibfttypes.MsgRoundChange}
  46. // future sequence
  47. v := &istanbul.View{
  48. Sequence: big.NewInt(2),
  49. Round: big.NewInt(0),
  50. }
  51. for i := 0; i < len(testStates); i++ {
  52. c.state = testStates[i]
  53. for j := 0; j < len(testCode); j++ {
  54. err := c.checkMessage(testCode[j], v)
  55. if err != istanbulcommon.ErrFutureMessage {
  56. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFutureMessage)
  57. }
  58. }
  59. }
  60. // future round
  61. v = &istanbul.View{
  62. Sequence: big.NewInt(1),
  63. Round: big.NewInt(1),
  64. }
  65. for i := 0; i < len(testStates); i++ {
  66. c.state = testStates[i]
  67. for j := 0; j < len(testCode); j++ {
  68. err := c.checkMessage(testCode[j], v)
  69. if testCode[j] == ibfttypes.MsgRoundChange {
  70. if err != nil {
  71. t.Errorf("error mismatch: have %v, want nil", err)
  72. }
  73. } else if err != istanbulcommon.ErrFutureMessage {
  74. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFutureMessage)
  75. }
  76. }
  77. }
  78. // current view but waiting for round change
  79. v = &istanbul.View{
  80. Sequence: big.NewInt(1),
  81. Round: big.NewInt(0),
  82. }
  83. c.waitingForRoundChange = true
  84. for i := 0; i < len(testStates); i++ {
  85. c.state = testStates[i]
  86. for j := 0; j < len(testCode); j++ {
  87. err := c.checkMessage(testCode[j], v)
  88. if testCode[j] == ibfttypes.MsgRoundChange {
  89. if err != nil {
  90. t.Errorf("error mismatch: have %v, want nil", err)
  91. }
  92. } else if err != istanbulcommon.ErrFutureMessage {
  93. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFutureMessage)
  94. }
  95. }
  96. }
  97. c.waitingForRoundChange = false
  98. v = c.currentView()
  99. // current view, state = ibfttypes.StateAcceptRequest
  100. c.state = ibfttypes.StateAcceptRequest
  101. for i := 0; i < len(testCode); i++ {
  102. err = c.checkMessage(testCode[i], v)
  103. if testCode[i] == ibfttypes.MsgRoundChange {
  104. if err != nil {
  105. t.Errorf("error mismatch: have %v, want nil", err)
  106. }
  107. } else if testCode[i] == ibfttypes.MsgPreprepare {
  108. if err != nil {
  109. t.Errorf("error mismatch: have %v, want nil", err)
  110. }
  111. } else {
  112. if err != istanbulcommon.ErrFutureMessage {
  113. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFutureMessage)
  114. }
  115. }
  116. }
  117. // current view, state = StatePreprepared
  118. c.state = ibfttypes.StatePreprepared
  119. for i := 0; i < len(testCode); i++ {
  120. err = c.checkMessage(testCode[i], v)
  121. if testCode[i] == ibfttypes.MsgRoundChange {
  122. if err != nil {
  123. t.Errorf("error mismatch: have %v, want nil", err)
  124. }
  125. } else if err != nil {
  126. t.Errorf("error mismatch: have %v, want nil", err)
  127. }
  128. }
  129. // current view, state = ibfttypes.StatePrepared
  130. c.state = ibfttypes.StatePrepared
  131. for i := 0; i < len(testCode); i++ {
  132. err = c.checkMessage(testCode[i], v)
  133. if testCode[i] == ibfttypes.MsgRoundChange {
  134. if err != nil {
  135. t.Errorf("error mismatch: have %v, want nil", err)
  136. }
  137. } else if err != nil {
  138. t.Errorf("error mismatch: have %v, want nil", err)
  139. }
  140. }
  141. // current view, state = ibfttypes.StateCommitted
  142. c.state = ibfttypes.StateCommitted
  143. for i := 0; i < len(testCode); i++ {
  144. err = c.checkMessage(testCode[i], v)
  145. if testCode[i] == ibfttypes.MsgRoundChange {
  146. if err != nil {
  147. t.Errorf("error mismatch: have %v, want nil", err)
  148. }
  149. } else if err != nil {
  150. t.Errorf("error mismatch: have %v, want nil", err)
  151. }
  152. }
  153. }
  154. func TestStoreBacklog(t *testing.T) {
  155. c := &core{
  156. logger: log.New("backend", "test", "id", 0),
  157. valSet: newTestValidatorSet(1),
  158. backlogs: make(map[common.Address]*prque.Prque),
  159. backlogsMu: new(sync.Mutex),
  160. }
  161. v := &istanbul.View{
  162. Round: big.NewInt(10),
  163. Sequence: big.NewInt(10),
  164. }
  165. p := c.valSet.GetByIndex(0)
  166. // push preprepare msg
  167. preprepare := &istanbul.Preprepare{
  168. View: v,
  169. Proposal: makeBlock(1),
  170. }
  171. prepreparePayload, _ := ibfttypes.Encode(preprepare)
  172. m := &ibfttypes.Message{
  173. Code: ibfttypes.MsgPreprepare,
  174. Msg: prepreparePayload,
  175. }
  176. c.storeBacklog(m, p)
  177. msg := c.backlogs[p.Address()].PopItem()
  178. if !reflect.DeepEqual(msg, m) {
  179. t.Errorf("message mismatch: have %v, want %v", msg, m)
  180. }
  181. // push prepare msg
  182. subject := &istanbul.Subject{
  183. View: v,
  184. Digest: common.StringToHash("1234567890"),
  185. }
  186. subjectPayload, _ := ibfttypes.Encode(subject)
  187. m = &ibfttypes.Message{
  188. Code: ibfttypes.MsgPrepare,
  189. Msg: subjectPayload,
  190. }
  191. c.storeBacklog(m, p)
  192. msg = c.backlogs[p.Address()].PopItem()
  193. if !reflect.DeepEqual(msg, m) {
  194. t.Errorf("message mismatch: have %v, want %v", msg, m)
  195. }
  196. // push commit msg
  197. m = &ibfttypes.Message{
  198. Code: ibfttypes.MsgCommit,
  199. Msg: subjectPayload,
  200. }
  201. c.storeBacklog(m, p)
  202. msg = c.backlogs[p.Address()].PopItem()
  203. if !reflect.DeepEqual(msg, m) {
  204. t.Errorf("message mismatch: have %v, want %v", msg, m)
  205. }
  206. // push roundChange msg
  207. m = &ibfttypes.Message{
  208. Code: ibfttypes.MsgRoundChange,
  209. Msg: subjectPayload,
  210. }
  211. c.storeBacklog(m, p)
  212. msg = c.backlogs[p.Address()].PopItem()
  213. if !reflect.DeepEqual(msg, m) {
  214. t.Errorf("message mismatch: have %v, want %v", msg, m)
  215. }
  216. }
  217. func TestProcessFutureBacklog(t *testing.T) {
  218. backend := &testSystemBackend{
  219. events: new(event.TypeMux),
  220. }
  221. c := &core{
  222. logger: log.New("backend", "test", "id", 0),
  223. valSet: newTestValidatorSet(1),
  224. backlogs: make(map[common.Address]*prque.Prque),
  225. backlogsMu: new(sync.Mutex),
  226. backend: backend,
  227. current: newRoundState(&istanbul.View{
  228. Sequence: big.NewInt(1),
  229. Round: big.NewInt(0),
  230. }, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
  231. state: ibfttypes.StateAcceptRequest,
  232. }
  233. c.subscribeEvents()
  234. defer c.unsubscribeEvents()
  235. v := &istanbul.View{
  236. Round: big.NewInt(10),
  237. Sequence: big.NewInt(10),
  238. }
  239. p := c.valSet.GetByIndex(0)
  240. // push a future msg
  241. subject := &istanbul.Subject{
  242. View: v,
  243. Digest: common.StringToHash("1234567890"),
  244. }
  245. subjectPayload, _ := ibfttypes.Encode(subject)
  246. m := &ibfttypes.Message{
  247. Code: ibfttypes.MsgCommit,
  248. Msg: subjectPayload,
  249. }
  250. c.storeBacklog(m, p)
  251. c.processBacklog()
  252. const timeoutDura = 2 * time.Second
  253. timeout := time.NewTimer(timeoutDura)
  254. select {
  255. case e, ok := <-c.events.Chan():
  256. if !ok {
  257. return
  258. }
  259. t.Errorf("unexpected events comes: %v", e)
  260. case <-timeout.C:
  261. // success
  262. }
  263. }
  264. func TestProcessBacklog(t *testing.T) {
  265. v := &istanbul.View{
  266. Round: big.NewInt(0),
  267. Sequence: big.NewInt(1),
  268. }
  269. preprepare := &istanbul.Preprepare{
  270. View: v,
  271. Proposal: makeBlock(1),
  272. }
  273. prepreparePayload, _ := ibfttypes.Encode(preprepare)
  274. subject := &istanbul.Subject{
  275. View: v,
  276. Digest: common.StringToHash("1234567890"),
  277. }
  278. subjectPayload, _ := ibfttypes.Encode(subject)
  279. msgs := []*ibfttypes.Message{
  280. {
  281. Code: ibfttypes.MsgPreprepare,
  282. Msg: prepreparePayload,
  283. },
  284. {
  285. Code: ibfttypes.MsgPrepare,
  286. Msg: subjectPayload,
  287. },
  288. {
  289. Code: ibfttypes.MsgCommit,
  290. Msg: subjectPayload,
  291. },
  292. {
  293. Code: ibfttypes.MsgRoundChange,
  294. Msg: subjectPayload,
  295. },
  296. }
  297. for i := 0; i < len(msgs); i++ {
  298. testProcessBacklog(t, msgs[i])
  299. }
  300. }
  301. func testProcessBacklog(t *testing.T, msg *ibfttypes.Message) {
  302. vset := newTestValidatorSet(1)
  303. backend := &testSystemBackend{
  304. events: new(event.TypeMux),
  305. peers: vset,
  306. }
  307. c := &core{
  308. logger: log.New("backend", "test", "id", 0),
  309. backlogs: make(map[common.Address]*prque.Prque),
  310. backlogsMu: new(sync.Mutex),
  311. valSet: vset,
  312. backend: backend,
  313. state: ibfttypes.State(msg.Code),
  314. current: newRoundState(&istanbul.View{
  315. Sequence: big.NewInt(1),
  316. Round: big.NewInt(0),
  317. }, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
  318. }
  319. c.subscribeEvents()
  320. defer c.unsubscribeEvents()
  321. c.storeBacklog(msg, vset.GetByIndex(0))
  322. c.processBacklog()
  323. const timeoutDura = 2 * time.Second
  324. timeout := time.NewTimer(timeoutDura)
  325. select {
  326. case ev := <-c.events.Chan():
  327. e, ok := ev.Data.(backlogEvent)
  328. if !ok {
  329. t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
  330. }
  331. if e.msg.Code != msg.Code {
  332. t.Errorf("message code mismatch: have %v, want %v", e.msg.Code, msg.Code)
  333. }
  334. // success
  335. case <-timeout.C:
  336. t.Error("unexpected timeout occurs")
  337. }
  338. }