roundchange_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ibfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/ibft/types"
  23. "github.com/ethereum/go-ethereum/consensus/istanbul/validator"
  24. )
  25. func TestRoundChangeSet(t *testing.T) {
  26. vset := validator.NewSet(generateValidators(4), istanbul.NewRoundRobinProposerPolicy())
  27. rc := newRoundChangeSet(vset)
  28. view := &istanbul.View{
  29. Sequence: big.NewInt(1),
  30. Round: big.NewInt(1),
  31. }
  32. r := &istanbul.Subject{
  33. View: view,
  34. Digest: common.Hash{},
  35. }
  36. m, _ := ibfttypes.Encode(r)
  37. // Test Add()
  38. // Add message from all validators
  39. for i, v := range vset.List() {
  40. msg := &ibfttypes.Message{
  41. Code: ibfttypes.MsgRoundChange,
  42. Msg: m,
  43. Address: v.Address(),
  44. }
  45. rc.Add(view.Round, msg)
  46. if rc.roundChanges[view.Round.Uint64()].Size() != i+1 {
  47. t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), i+1)
  48. }
  49. }
  50. // Add message again from all validators, but the size should be the same
  51. for _, v := range vset.List() {
  52. msg := &ibfttypes.Message{
  53. Code: ibfttypes.MsgRoundChange,
  54. Msg: m,
  55. Address: v.Address(),
  56. }
  57. rc.Add(view.Round, msg)
  58. if rc.roundChanges[view.Round.Uint64()].Size() != vset.Size() {
  59. t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), vset.Size())
  60. }
  61. }
  62. // Test MaxRound()
  63. for i := 0; i < 10; i++ {
  64. maxRound := rc.MaxRound(i)
  65. if i <= vset.Size() {
  66. if maxRound == nil || maxRound.Cmp(view.Round) != 0 {
  67. t.Errorf("max round mismatch: have %v, want %v", maxRound, view.Round)
  68. }
  69. } else if maxRound != nil {
  70. t.Errorf("max round mismatch: have %v, want nil", maxRound)
  71. }
  72. }
  73. // Test Clear()
  74. for i := int64(0); i < 2; i++ {
  75. rc.Clear(big.NewInt(i))
  76. if rc.roundChanges[view.Round.Uint64()].Size() != vset.Size() {
  77. t.Errorf("the size of round change messages mismatch: have %v, want %v", rc.roundChanges[view.Round.Uint64()].Size(), vset.Size())
  78. }
  79. }
  80. rc.Clear(big.NewInt(2))
  81. if rc.roundChanges[view.Round.Uint64()] != nil {
  82. t.Errorf("the change messages mismatch: have %v, want nil", rc.roundChanges[view.Round.Uint64()])
  83. }
  84. }