You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package state
  2. import (
  3. "bytes"
  4. "fmt"
  5. "sort"
  6. "strings"
  7. . "github.com/tendermint/tendermint/common"
  8. "github.com/tendermint/tendermint/merkle"
  9. )
  10. // ValidatorSet represent a set of *Validator at a given height.
  11. // The validators can be fetched by address or index.
  12. // The index is in order of .Address, so the indices are fixed
  13. // for all rounds of a given blockchain height.
  14. // On the other hand, the .AccumPower of each validator and
  15. // the designated .Proposer() of a set changes every round,
  16. // upon calling .IncrementAccum().
  17. // NOTE: Not goroutine-safe.
  18. // NOTE: All get/set to validators should copy the value for safety.
  19. // TODO: consider validator Accum overflow
  20. // TODO: replace validators []*Validator with github.com/jaekwon/go-ibbs?
  21. type ValidatorSet struct {
  22. Validators []*Validator // NOTE: persisted via reflect, must be exported.
  23. // cached (unexported)
  24. proposer *Validator
  25. totalVotingPower uint64
  26. }
  27. func NewValidatorSet(vals []*Validator) *ValidatorSet {
  28. validators := make([]*Validator, len(vals))
  29. for i, val := range vals {
  30. validators[i] = val.Copy()
  31. }
  32. sort.Sort(ValidatorsByAddress(validators))
  33. return &ValidatorSet{
  34. Validators: validators,
  35. }
  36. }
  37. // TODO: mind the overflow when times and votingPower shares too large.
  38. func (valSet *ValidatorSet) IncrementAccum(times uint) {
  39. // Add VotingPower * times to each validator and order into heap.
  40. validatorsHeap := NewHeap()
  41. for _, val := range valSet.Validators {
  42. val.Accum += int64(val.VotingPower) * int64(times) // TODO: mind overflow
  43. validatorsHeap.Push(val, accumComparable(val.Accum))
  44. }
  45. // Decrement the validator with most accum, times times.
  46. for i := uint(0); i < times; i++ {
  47. mostest := validatorsHeap.Peek().(*Validator)
  48. mostest.Accum -= int64(valSet.TotalVotingPower())
  49. validatorsHeap.Update(mostest, accumComparable(mostest.Accum))
  50. }
  51. // The next proposer is the next most accums remaining
  52. valSet.proposer = validatorsHeap.Peek().(*Validator)
  53. }
  54. func (valSet *ValidatorSet) Copy() *ValidatorSet {
  55. validators := make([]*Validator, len(valSet.Validators))
  56. for i, val := range valSet.Validators {
  57. // NOTE: must copy, since IncrementAccum updates in place.
  58. validators[i] = val.Copy()
  59. }
  60. return &ValidatorSet{
  61. Validators: validators,
  62. proposer: valSet.proposer,
  63. totalVotingPower: valSet.totalVotingPower,
  64. }
  65. }
  66. func (valSet *ValidatorSet) HasAddress(address []byte) bool {
  67. idx := sort.Search(len(valSet.Validators), func(i int) bool {
  68. return bytes.Compare(address, valSet.Validators[i].Address) <= 0
  69. })
  70. return idx != len(valSet.Validators) && bytes.Compare(valSet.Validators[idx].Address, address) == 0
  71. }
  72. func (valSet *ValidatorSet) GetByAddress(address []byte) (index uint, val *Validator) {
  73. idx := sort.Search(len(valSet.Validators), func(i int) bool {
  74. return bytes.Compare(address, valSet.Validators[i].Address) <= 0
  75. })
  76. if idx != len(valSet.Validators) && bytes.Compare(valSet.Validators[idx].Address, address) == 0 {
  77. return uint(idx), valSet.Validators[idx].Copy()
  78. } else {
  79. return 0, nil
  80. }
  81. }
  82. func (valSet *ValidatorSet) GetByIndex(index uint) (address []byte, val *Validator) {
  83. val = valSet.Validators[index]
  84. return val.Address, val.Copy()
  85. }
  86. func (valSet *ValidatorSet) Size() uint {
  87. return uint(len(valSet.Validators))
  88. }
  89. func (valSet *ValidatorSet) TotalVotingPower() uint64 {
  90. if valSet.totalVotingPower == 0 {
  91. for _, val := range valSet.Validators {
  92. valSet.totalVotingPower += val.VotingPower
  93. }
  94. }
  95. return valSet.totalVotingPower
  96. }
  97. func (valSet *ValidatorSet) Proposer() (proposer *Validator) {
  98. if valSet.proposer == nil {
  99. for _, val := range valSet.Validators {
  100. valSet.proposer = valSet.proposer.CompareAccum(val)
  101. }
  102. }
  103. return valSet.proposer.Copy()
  104. }
  105. func (valSet *ValidatorSet) Hash() []byte {
  106. if len(valSet.Validators) == 0 {
  107. return nil
  108. }
  109. hashables := make([]merkle.Hashable, len(valSet.Validators))
  110. for i, val := range valSet.Validators {
  111. hashables[i] = val
  112. }
  113. return merkle.HashFromHashables(hashables)
  114. }
  115. func (valSet *ValidatorSet) Add(val *Validator) (added bool) {
  116. val = val.Copy()
  117. idx := sort.Search(len(valSet.Validators), func(i int) bool {
  118. return bytes.Compare(val.Address, valSet.Validators[i].Address) <= 0
  119. })
  120. if idx == len(valSet.Validators) {
  121. valSet.Validators = append(valSet.Validators, val)
  122. // Invalidate cache
  123. valSet.proposer = nil
  124. valSet.totalVotingPower = 0
  125. return true
  126. } else if bytes.Compare(valSet.Validators[idx].Address, val.Address) == 0 {
  127. return false
  128. } else {
  129. newValidators := append(valSet.Validators[:idx], val)
  130. newValidators = append(newValidators, valSet.Validators[idx:]...)
  131. valSet.Validators = newValidators
  132. // Invalidate cache
  133. valSet.proposer = nil
  134. valSet.totalVotingPower = 0
  135. return true
  136. }
  137. }
  138. func (valSet *ValidatorSet) Update(val *Validator) (updated bool) {
  139. index, sameVal := valSet.GetByAddress(val.Address)
  140. if sameVal == nil {
  141. return false
  142. } else {
  143. valSet.Validators[index] = val.Copy()
  144. // Invalidate cache
  145. valSet.proposer = nil
  146. valSet.totalVotingPower = 0
  147. return true
  148. }
  149. }
  150. func (valSet *ValidatorSet) Remove(address []byte) (val *Validator, removed bool) {
  151. idx := sort.Search(len(valSet.Validators), func(i int) bool {
  152. return bytes.Compare(address, valSet.Validators[i].Address) <= 0
  153. })
  154. if idx == len(valSet.Validators) || bytes.Compare(valSet.Validators[idx].Address, address) != 0 {
  155. return nil, false
  156. } else {
  157. removedVal := valSet.Validators[idx]
  158. newValidators := valSet.Validators[:idx]
  159. if idx+1 < len(valSet.Validators) {
  160. newValidators = append(newValidators, valSet.Validators[idx+1:]...)
  161. }
  162. valSet.Validators = newValidators
  163. // Invalidate cache
  164. valSet.proposer = nil
  165. valSet.totalVotingPower = 0
  166. return removedVal, true
  167. }
  168. }
  169. func (valSet *ValidatorSet) Iterate(fn func(index uint, val *Validator) bool) {
  170. for i, val := range valSet.Validators {
  171. stop := fn(uint(i), val.Copy())
  172. if stop {
  173. break
  174. }
  175. }
  176. }
  177. func (valSet *ValidatorSet) String() string {
  178. return valSet.StringIndented("")
  179. }
  180. func (valSet *ValidatorSet) StringIndented(indent string) string {
  181. valStrings := []string{}
  182. valSet.Iterate(func(index uint, val *Validator) bool {
  183. valStrings = append(valStrings, val.String())
  184. return false
  185. })
  186. return fmt.Sprintf(`ValidatorSet{
  187. %s Proposer: %v
  188. %s Validators:
  189. %s %v
  190. %s}`,
  191. indent, valSet.Proposer().String(),
  192. indent,
  193. indent, strings.Join(valStrings, "\n"+indent+" "),
  194. indent)
  195. }
  196. //-------------------------------------
  197. // Implements sort for sorting validators by address.
  198. type ValidatorsByAddress []*Validator
  199. func (vs ValidatorsByAddress) Len() int {
  200. return len(vs)
  201. }
  202. func (vs ValidatorsByAddress) Less(i, j int) bool {
  203. return bytes.Compare(vs[i].Address, vs[j].Address) == -1
  204. }
  205. func (vs ValidatorsByAddress) Swap(i, j int) {
  206. it := vs[i]
  207. vs[i] = vs[j]
  208. vs[j] = it
  209. }
  210. //-------------------------------------
  211. // Use with Heap for sorting validators by accum
  212. type accumComparable uint64
  213. // We want to find the validator with the greatest accum.
  214. func (ac accumComparable) Less(o interface{}) bool {
  215. return uint64(ac) > uint64(o.(accumComparable))
  216. }