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