message_set.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "fmt"
  19. "math/big"
  20. "strings"
  21. "sync"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/consensus/istanbul"
  24. ibfttypes "github.com/ethereum/go-ethereum/consensus/istanbul/ibft/types"
  25. )
  26. // Construct a new message set to accumulate messages for given sequence/view number.
  27. func newMessageSet(valSet istanbul.ValidatorSet) *messageSet {
  28. return &messageSet{
  29. view: &istanbul.View{
  30. Round: new(big.Int),
  31. Sequence: new(big.Int),
  32. },
  33. messagesMu: new(sync.Mutex),
  34. messages: make(map[common.Address]*ibfttypes.Message),
  35. valSet: valSet,
  36. }
  37. }
  38. // ----------------------------------------------------------------------------
  39. type messageSet struct {
  40. view *istanbul.View
  41. valSet istanbul.ValidatorSet
  42. messagesMu *sync.Mutex
  43. messages map[common.Address]*ibfttypes.Message
  44. }
  45. func (ms *messageSet) View() *istanbul.View {
  46. return ms.view
  47. }
  48. func (ms *messageSet) Add(msg *ibfttypes.Message) error {
  49. ms.messagesMu.Lock()
  50. defer ms.messagesMu.Unlock()
  51. if err := ms.verify(msg); err != nil {
  52. return err
  53. }
  54. return ms.addVerifiedMessage(msg)
  55. }
  56. func (ms *messageSet) Values() (result []*ibfttypes.Message) {
  57. ms.messagesMu.Lock()
  58. defer ms.messagesMu.Unlock()
  59. for _, v := range ms.messages {
  60. result = append(result, v)
  61. }
  62. return result
  63. }
  64. func (ms *messageSet) Size() int {
  65. ms.messagesMu.Lock()
  66. defer ms.messagesMu.Unlock()
  67. return len(ms.messages)
  68. }
  69. func (ms *messageSet) Get(addr common.Address) *ibfttypes.Message {
  70. ms.messagesMu.Lock()
  71. defer ms.messagesMu.Unlock()
  72. return ms.messages[addr]
  73. }
  74. // ----------------------------------------------------------------------------
  75. func (ms *messageSet) verify(msg *ibfttypes.Message) error {
  76. // verify if the message comes from one of the validators
  77. if _, v := ms.valSet.GetByAddress(msg.Address); v == nil {
  78. return istanbul.ErrUnauthorizedAddress
  79. }
  80. // TODO: check view number and sequence number
  81. return nil
  82. }
  83. func (ms *messageSet) addVerifiedMessage(msg *ibfttypes.Message) error {
  84. ms.messages[msg.Address] = msg
  85. return nil
  86. }
  87. func (ms *messageSet) String() string {
  88. ms.messagesMu.Lock()
  89. defer ms.messagesMu.Unlock()
  90. addresses := make([]string, 0, len(ms.messages))
  91. for _, v := range ms.messages {
  92. addresses = append(addresses, v.Address.String())
  93. }
  94. return fmt.Sprintf("[%v]", strings.Join(addresses, ", "))
  95. }