default_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 validator
  17. import (
  18. fmt "fmt"
  19. "reflect"
  20. "strings"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/consensus/istanbul"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. )
  26. var (
  27. testAddress = "70524d664ffe731100208a0154e556f9bb679ae6"
  28. testAddress2 = "b37866a925bccd69cfa98d43b510f1d23d78a851"
  29. )
  30. func TestValidatorSet(t *testing.T) {
  31. testNewValidatorSet(t)
  32. testNormalValSet(t)
  33. testEmptyValSet(t)
  34. testStickyProposer(t)
  35. testAddAndRemoveValidator(t)
  36. }
  37. func testNewValidatorSet(t *testing.T) {
  38. var validators []istanbul.Validator
  39. const ValCnt = 100
  40. // Create 100 validators with random addresses
  41. b := []byte{}
  42. for i := 0; i < ValCnt; i++ {
  43. key, _ := crypto.GenerateKey()
  44. addr := crypto.PubkeyToAddress(key.PublicKey)
  45. val := New(addr)
  46. validators = append(validators, val)
  47. b = append(b, val.Address().Bytes()...)
  48. }
  49. // Create ValidatorSet
  50. valSet := NewSet(ExtractValidators(b), istanbul.NewRoundRobinProposerPolicy())
  51. if valSet == nil {
  52. t.Errorf("the validator byte array cannot be parsed")
  53. t.FailNow()
  54. }
  55. // Check validators sorting: should be in ascending order
  56. for i := 0; i < ValCnt-1; i++ {
  57. val := valSet.GetByIndex(uint64(i))
  58. nextVal := valSet.GetByIndex(uint64(i + 1))
  59. if strings.Compare(val.String(), nextVal.String()) >= 0 {
  60. t.Errorf("validator set is not sorted in ascending order")
  61. }
  62. }
  63. }
  64. func testNormalValSet(t *testing.T) {
  65. b1 := common.Hex2Bytes(testAddress)
  66. b2 := common.Hex2Bytes(testAddress2)
  67. addr1 := common.BytesToAddress(b1)
  68. addr2 := common.BytesToAddress(b2)
  69. val1 := New(addr1)
  70. val2 := New(addr2)
  71. valSet := newDefaultSet([]common.Address{addr1, addr2}, istanbul.NewRoundRobinProposerPolicy())
  72. if valSet == nil {
  73. t.Errorf("the format of validator set is invalid")
  74. t.FailNow()
  75. }
  76. // check size
  77. if size := valSet.Size(); size != 2 {
  78. t.Errorf("the size of validator set is wrong: have %v, want 2", size)
  79. }
  80. // test get by index
  81. if val := valSet.GetByIndex(uint64(0)); !reflect.DeepEqual(val, val1) {
  82. t.Errorf("validator mismatch: have %v, want %v", val, val1)
  83. }
  84. // test get by invalid index
  85. if val := valSet.GetByIndex(uint64(2)); val != nil {
  86. t.Errorf("validator mismatch: have %v, want nil", val)
  87. }
  88. // test get by address
  89. if _, val := valSet.GetByAddress(addr2); !reflect.DeepEqual(val, val2) {
  90. t.Errorf("validator mismatch: have %v, want %v", val, val2)
  91. }
  92. // test get by invalid address
  93. invalidAddr := common.HexToAddress("0x9535b2e7faaba5288511d89341d94a38063a349b")
  94. if _, val := valSet.GetByAddress(invalidAddr); val != nil {
  95. t.Errorf("validator mismatch: have %v, want nil", val)
  96. }
  97. // test get proposer
  98. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val1) {
  99. t.Errorf("proposer mismatch: have %v, want %v", val, val1)
  100. }
  101. // test calculate proposer
  102. lastProposer := addr1
  103. valSet.CalcProposer(lastProposer, uint64(0))
  104. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val2) {
  105. t.Errorf("proposer mismatch: have %v, want %v", val, val2)
  106. }
  107. valSet.CalcProposer(lastProposer, uint64(3))
  108. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val1) {
  109. t.Errorf("proposer mismatch: have %v, want %v", val, val1)
  110. }
  111. // test empty last proposer
  112. lastProposer = common.Address{}
  113. valSet.CalcProposer(lastProposer, uint64(3))
  114. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val2) {
  115. t.Errorf("proposer mismatch: have %v, want %v", val, val2)
  116. }
  117. }
  118. func testEmptyValSet(t *testing.T) {
  119. valSet := NewSet(ExtractValidators([]byte{}), istanbul.NewRoundRobinProposerPolicy())
  120. if valSet == nil {
  121. t.Errorf("validator set should not be nil")
  122. }
  123. }
  124. func testAddAndRemoveValidator(t *testing.T) {
  125. valSet := NewSet(ExtractValidators([]byte{}), istanbul.NewRoundRobinProposerPolicy())
  126. if !valSet.AddValidator(common.StringToAddress("2")) {
  127. t.Error("the validator should be added")
  128. }
  129. if valSet.AddValidator(common.StringToAddress("2")) {
  130. t.Error("the existing validator should not be added")
  131. }
  132. valSet.AddValidator(common.StringToAddress("1"))
  133. valSet.AddValidator(common.StringToAddress("0"))
  134. if len(valSet.List()) != 3 {
  135. t.Error("the size of validator set should be 3")
  136. }
  137. for i, v := range valSet.List() {
  138. expected := common.StringToAddress((fmt.Sprint(i)))
  139. if v.Address() != expected {
  140. t.Errorf("the order of validators is wrong: have %v, want %v", v.Address().Hex(), expected.Hex())
  141. }
  142. }
  143. if !valSet.RemoveValidator(common.StringToAddress("2")) {
  144. t.Error("the validator should be removed")
  145. }
  146. if valSet.RemoveValidator(common.StringToAddress("2")) {
  147. t.Error("the non-existing validator should not be removed")
  148. }
  149. if len(valSet.List()) != 2 {
  150. t.Error("the size of validator set should be 2")
  151. }
  152. valSet.RemoveValidator(common.StringToAddress("1"))
  153. if len(valSet.List()) != 1 {
  154. t.Error("the size of validator set should be 1")
  155. }
  156. valSet.RemoveValidator(common.StringToAddress("0"))
  157. if len(valSet.List()) != 0 {
  158. t.Error("the size of validator set should be 0")
  159. }
  160. }
  161. func testStickyProposer(t *testing.T) {
  162. b1 := common.Hex2Bytes(testAddress)
  163. b2 := common.Hex2Bytes(testAddress2)
  164. addr1 := common.BytesToAddress(b1)
  165. addr2 := common.BytesToAddress(b2)
  166. val1 := New(addr1)
  167. val2 := New(addr2)
  168. valSet := newDefaultSet([]common.Address{addr1, addr2}, istanbul.NewStickyProposerPolicy())
  169. // test get proposer
  170. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val1) {
  171. t.Errorf("proposer mismatch: have %v, want %v", val, val1)
  172. }
  173. // test calculate proposer
  174. lastProposer := addr1
  175. valSet.CalcProposer(lastProposer, uint64(0))
  176. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val1) {
  177. t.Errorf("proposer mismatch: have %v, want %v", val, val1)
  178. }
  179. valSet.CalcProposer(lastProposer, uint64(1))
  180. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val2) {
  181. t.Errorf("proposer mismatch: have %v, want %v", val, val2)
  182. }
  183. // test empty last proposer
  184. lastProposer = common.Address{}
  185. valSet.CalcProposer(lastProposer, uint64(3))
  186. if val := valSet.GetProposer(); !reflect.DeepEqual(val, val2) {
  187. t.Errorf("proposer mismatch: have %v, want %v", val, val2)
  188. }
  189. }