handler_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  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. // notice: the normal case have been tested in integration tests.
  26. func TestHandleMsg(t *testing.T) {
  27. N := uint64(4)
  28. F := uint64(1)
  29. sys := NewTestSystemWithBackend(N, F)
  30. closer := sys.Run(true)
  31. defer closer()
  32. v0 := sys.backends[0]
  33. r0 := v0.engine
  34. m, _ := ibfttypes.Encode(&istanbul.Subject{
  35. View: &istanbul.View{
  36. Sequence: big.NewInt(0),
  37. Round: big.NewInt(0),
  38. },
  39. Digest: common.StringToHash("1234567890"),
  40. })
  41. // with a matched payload. msgPreprepare should match with *istanbul.Preprepare in normal case.
  42. msg := &ibfttypes.Message{
  43. Code: ibfttypes.MsgPreprepare,
  44. Msg: m,
  45. Address: v0.Address(),
  46. Signature: []byte{},
  47. CommittedSeal: []byte{},
  48. }
  49. _, val := v0.Validators(nil).GetByAddress(v0.Address())
  50. if err := r0.handleCheckedMsg(msg, val); err != istanbulcommon.ErrFailedDecodePreprepare {
  51. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFailedDecodePreprepare)
  52. }
  53. m, _ = ibfttypes.Encode(&istanbul.Preprepare{
  54. View: &istanbul.View{
  55. Sequence: big.NewInt(0),
  56. Round: big.NewInt(0),
  57. },
  58. Proposal: makeBlock(1),
  59. })
  60. // with a unmatched payload. msgPrepare should match with *istanbul.Subject in normal case.
  61. msg = &ibfttypes.Message{
  62. Code: ibfttypes.MsgPrepare,
  63. Msg: m,
  64. Address: v0.Address(),
  65. Signature: []byte{},
  66. CommittedSeal: []byte{},
  67. }
  68. _, val = v0.Validators(nil).GetByAddress(v0.Address())
  69. if err := r0.handleCheckedMsg(msg, val); err != istanbulcommon.ErrFailedDecodePrepare {
  70. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFailedDecodePreprepare)
  71. }
  72. m, _ = ibfttypes.Encode(&istanbul.Preprepare{
  73. View: &istanbul.View{
  74. Sequence: big.NewInt(0),
  75. Round: big.NewInt(0),
  76. },
  77. Proposal: makeBlock(2),
  78. })
  79. // with a unmatched payload. istanbul.MsgCommit should match with *istanbul.Subject in normal case.
  80. msg = &ibfttypes.Message{
  81. Code: ibfttypes.MsgCommit,
  82. Msg: m,
  83. Address: v0.Address(),
  84. Signature: []byte{},
  85. CommittedSeal: []byte{},
  86. }
  87. _, val = v0.Validators(nil).GetByAddress(v0.Address())
  88. if err := r0.handleCheckedMsg(msg, val); err != istanbulcommon.ErrFailedDecodeCommit {
  89. t.Errorf("error mismatch: have %v, want %v", err, istanbulcommon.ErrFailedDecodeCommit)
  90. }
  91. m, _ = ibfttypes.Encode(&istanbul.Preprepare{
  92. View: &istanbul.View{
  93. Sequence: big.NewInt(0),
  94. Round: big.NewInt(0),
  95. },
  96. Proposal: makeBlock(3),
  97. })
  98. // invalid message code. message code is not exists in list
  99. msg = &ibfttypes.Message{
  100. Code: uint64(99),
  101. Msg: m,
  102. Address: v0.Address(),
  103. Signature: []byte{},
  104. CommittedSeal: []byte{},
  105. }
  106. _, val = v0.Validators(nil).GetByAddress(v0.Address())
  107. if err := r0.handleCheckedMsg(msg, val); err == nil {
  108. t.Errorf("error mismatch: have %v, want nil", err)
  109. }
  110. // with malicious payload
  111. if err := r0.handleMsg([]byte{1}); err == nil {
  112. t.Errorf("error mismatch: have %v, want nil", err)
  113. }
  114. }