github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/consensus/podc/core/message_set.go (about)

     1  // Copyright 2017 AMIS Technologies
     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/podc"
    27  )
    28  
    29  // Construct a new message set to accumulate messages for given sequence/view number.
    30  func newMessageSet(valSet podc.ValidatorSet) *messageSet {
    31  	return &messageSet{
    32  		view: &podc.View{
    33  			Round:    new(big.Int),
    34  			Sequence: new(big.Int),
    35  		},
    36  		messagesMu: new(sync.Mutex),
    37  		messages:   make(map[common.Hash]*message),
    38  		valSet:     valSet,
    39  	}
    40  }
    41  
    42  
    43  
    44  type messageSet struct {
    45  	view       *podc.View
    46  	valSet     podc.ValidatorSet
    47  	messagesMu *sync.Mutex
    48  	messages   map[common.Hash]*message
    49  }
    50  
    51  
    52  func (ms *messageSet) View() *podc.View {
    53  	return ms.view
    54  }
    55  
    56  func (ms *messageSet) Add(msg *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 []*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) verify(msg *message) error {
    85  	// verify if the message comes from one of the validators
    86  	if _, v := ms.valSet.GetByAddress(msg.Address); v == nil {
    87  		return podc.ErrUnauthorizedAddress
    88  	}
    89  
    90  	// TODO: check view number and sequence number
    91  
    92  	return nil
    93  }
    94  
    95  func (ms *messageSet) addVerifiedMessage(msg *message) error {
    96  	ms.messages[podc.RLPHash(msg)] = msg
    97  	return nil
    98  }
    99  
   100  func (ms *messageSet) String() string {
   101  	ms.messagesMu.Lock()
   102  	defer ms.messagesMu.Unlock()
   103  	addresses := make([]string, 0, len(ms.messages))
   104  	for _, v := range ms.messages {
   105  		addresses = append(addresses, v.Address.String())
   106  	}
   107  	return fmt.Sprintf("[%v]", strings.Join(addresses, ", "))
   108  }