request_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 TestCheckRequestMsg(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 request
  40. err := c.checkRequestMsg(nil)
  41. if err != istanbulcommon.ErrInvalidMessage {
  42. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrInvalidMessage)
  43. }
  44. r := &istanbul.Request{
  45. Proposal: nil,
  46. }
  47. err = c.checkRequestMsg(r)
  48. if err != istanbulcommon.ErrInvalidMessage {
  49. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrInvalidMessage)
  50. }
  51. // old request
  52. r = &istanbul.Request{
  53. Proposal: makeBlock(0),
  54. }
  55. err = c.checkRequestMsg(r)
  56. if err != istanbulcommon.ErrOldMessage {
  57. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrOldMessage)
  58. }
  59. // future request
  60. r = &istanbul.Request{
  61. Proposal: makeBlock(2),
  62. }
  63. err = c.checkRequestMsg(r)
  64. if err != istanbulcommon.ErrFutureMessage {
  65. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFutureMessage)
  66. }
  67. // current request
  68. r = &istanbul.Request{
  69. Proposal: makeBlock(1),
  70. }
  71. err = c.checkRequestMsg(r)
  72. if err != nil {
  73. t.Errorf("error mismatch: have %v, want nil", err)
  74. }
  75. }
  76. func TestStoreRequestMsg(t *testing.T) {
  77. backend := &testSystemBackend{
  78. events: new(event.TypeMux),
  79. }
  80. c := &core{
  81. logger: log.New("backend", "test", "id", 0),
  82. backend: backend,
  83. state: ibfttypes.StateAcceptRequest,
  84. current: newRoundState(&istanbul.View{
  85. Sequence: big.NewInt(0),
  86. Round: big.NewInt(0),
  87. }, newTestValidatorSet(4), common.Hash{}, nil, nil, nil),
  88. pendingRequests: prque.New(),
  89. pendingRequestsMu: new(sync.Mutex),
  90. }
  91. requests := []istanbul.Request{
  92. {
  93. Proposal: makeBlock(1),
  94. },
  95. {
  96. Proposal: makeBlock(2),
  97. },
  98. {
  99. Proposal: makeBlock(3),
  100. },
  101. }
  102. c.storeRequestMsg(&requests[1])
  103. c.storeRequestMsg(&requests[0])
  104. c.storeRequestMsg(&requests[2])
  105. if c.pendingRequests.Size() != len(requests) {
  106. t.Errorf("the size of pending requests mismatch: have %v, want %v", c.pendingRequests.Size(), len(requests))
  107. }
  108. c.current.sequence = big.NewInt(3)
  109. c.subscribeEvents()
  110. defer c.unsubscribeEvents()
  111. c.processPendingRequests()
  112. const timeoutDura = 2 * time.Second
  113. timeout := time.NewTimer(timeoutDura)
  114. select {
  115. case ev := <-c.events.Chan():
  116. e, ok := ev.Data.(istanbul.RequestEvent)
  117. if !ok {
  118. t.Errorf("unexpected event comes: %v", reflect.TypeOf(ev.Data))
  119. }
  120. if e.Proposal.Number().Cmp(requests[2].Proposal.Number()) != 0 {
  121. t.Errorf("the number of proposal mismatch: have %v, want %v", e.Proposal.Number(), requests[2].Proposal.Number())
  122. }
  123. case <-timeout.C:
  124. t.Error("unexpected timeout occurs")
  125. }
  126. }