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