config_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 istanbul
  17. import (
  18. "math/big"
  19. "reflect"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/params"
  23. "github.com/naoina/toml"
  24. "github.com/stretchr/testify/assert"
  25. )
  26. func TestQbftTransitionAtBlockZero(t *testing.T) {
  27. config := *DefaultConfig
  28. config.TestQBFTBlock = nil
  29. config.Transitions = []params.Transition{{
  30. Block: big.NewInt(0),
  31. Algorithm: params.QBFT,
  32. }}
  33. assert.True(t, config.IsQBFTConsensusAt(big.NewInt(0)))
  34. assert.True(t, config.IsQBFTConsensusAt(nil))
  35. }
  36. func TestProposerPolicy_UnmarshalTOML(t *testing.T) {
  37. input := `id = 2
  38. `
  39. expectedId := ProposerPolicyId(2)
  40. var p proposerPolicyToml
  41. assert.NoError(t, toml.Unmarshal([]byte(input), &p))
  42. assert.Equal(t, expectedId, p.Id, "ProposerPolicyId mismatch")
  43. }
  44. func TestProposerPolicy_MarshalTOML(t *testing.T) {
  45. output := `id = 1
  46. `
  47. p := &ProposerPolicy{Id: 1}
  48. b, err := p.MarshalTOML()
  49. if err != nil {
  50. t.Errorf("error marshalling ProposerPolicy: %v", err)
  51. }
  52. assert.Equal(t, output, b, "ProposerPolicy MarshalTOML mismatch")
  53. }
  54. func TestGetConfig(t *testing.T) {
  55. if !reflect.DeepEqual(DefaultConfig.GetConfig(nil), *DefaultConfig) {
  56. t.Errorf("error default config:\nexpected: %v\n", DefaultConfig)
  57. }
  58. config := *DefaultConfig
  59. config.Transitions = []params.Transition{{
  60. Block: big.NewInt(1),
  61. EpochLength: 40000,
  62. }, {
  63. Block: big.NewInt(3),
  64. BlockPeriodSeconds: 5,
  65. }, {
  66. Block: big.NewInt(5),
  67. RequestTimeoutSeconds: 15000,
  68. EmptyBlockPeriodSeconds: nil,
  69. }}
  70. config1 := *DefaultConfig
  71. config1.Epoch = 40000
  72. config1.BlockPeriod = 1 // default value
  73. config1.EmptyBlockPeriod = 10 // default value
  74. config3 := config1
  75. config3.BlockPeriod = 5
  76. config3.EmptyBlockPeriod = 10
  77. config5 := config3
  78. config5.EmptyBlockPeriod = 5 // equals to block period because it can be lower than this active value (5) from block 3
  79. config5.RequestTimeout = 15000
  80. type test struct {
  81. blockNumber int64
  82. expectedConfig Config
  83. }
  84. tests := []test{
  85. {1, config1},
  86. {2, config1},
  87. {3, config3},
  88. {4, config3},
  89. {5, config5},
  90. {10, config5},
  91. {100, config5},
  92. }
  93. for _, test := range tests {
  94. c := test.expectedConfig.GetConfig(big.NewInt(test.blockNumber))
  95. if !reflect.DeepEqual(c, test.expectedConfig) {
  96. t.Errorf("error mismatch:\nexpected: %v\ngot: %v\n", test.expectedConfig, c)
  97. }
  98. }
  99. }
  100. func TestIsQBFTConsensusAt(t *testing.T) {
  101. config1 := *DefaultConfig
  102. config1.TestQBFTBlock = nil
  103. config2 := *DefaultConfig
  104. config2.TestQBFTBlock = big.NewInt(5)
  105. config3 := *DefaultConfig
  106. config3.TestQBFTBlock = nil
  107. config3.Transitions = []params.Transition{
  108. {Block: big.NewInt(10), Algorithm: params.QBFT},
  109. }
  110. type test struct {
  111. config Config
  112. blockNumber int64
  113. isQBFT bool
  114. }
  115. tests := []test{
  116. {*DefaultConfig, 0, true},
  117. {*DefaultConfig, 10, true},
  118. {config1, 0, false},
  119. {config1, 10, false},
  120. {config2, 4, false},
  121. {config2, 5, true},
  122. {config2, 7, true},
  123. {config3, 0, false},
  124. {config3, 7, false},
  125. {config3, 10, true},
  126. {config3, 11, true},
  127. }
  128. for _, test := range tests {
  129. isQbft := test.config.IsQBFTConsensusAt(big.NewInt(test.blockNumber))
  130. if !reflect.DeepEqual(isQbft, test.isQBFT) {
  131. t.Errorf("error mismatch:\nexpected: %v\ngot: %v\n", test.isQBFT, isQbft)
  132. }
  133. }
  134. }
  135. func TestGetValidatorContractAddress(t *testing.T) {
  136. address, address1, address2, address3 := common.Address{}, common.Address{0x2}, common.Address{0x4}, common.Address{0x6}
  137. config := *DefaultConfig
  138. config.Transitions = []params.Transition{{
  139. Block: big.NewInt(2),
  140. ValidatorContractAddress: address1,
  141. }, {
  142. Block: big.NewInt(4),
  143. ValidatorContractAddress: address2,
  144. }, {
  145. Block: big.NewInt(6),
  146. ValidatorContractAddress: address3,
  147. }}
  148. type test struct {
  149. blockNumber int64
  150. expectedAddress common.Address
  151. }
  152. tests := []test{
  153. {0, address},
  154. {1, address},
  155. {2, address1},
  156. {3, address1},
  157. {4, address2},
  158. {5, address2},
  159. {10, address3},
  160. {100, address3},
  161. }
  162. for _, test := range tests {
  163. c := config.GetValidatorContractAddress(big.NewInt(test.blockNumber))
  164. if !reflect.DeepEqual(c, test.expectedAddress) {
  165. t.Errorf("error mismatch:\nexpected: %v\ngot: %v\n", test.expectedAddress, c)
  166. }
  167. }
  168. }