github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/pbft/core/roundchange_set.go (about)

     1  // Copyright 2020 The go-simplechain Authors
     2  // This file is part of the go-simplechain library.
     3  //
     4  // The go-simplechain 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-simplechain 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-simplechain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"math/big"
    21  	"sync"
    22  
    23  	"github.com/bigzoro/my_simplechain/consensus/pbft"
    24  )
    25  
    26  func newRoundChangeSet(valSet pbft.ValidatorSet) *roundChangeSet {
    27  	return &roundChangeSet{
    28  		validatorSet: valSet,
    29  		roundChanges: make(map[uint64]*messageSet),
    30  		mu:           new(sync.Mutex),
    31  	}
    32  }
    33  
    34  type roundChangeSet struct {
    35  	validatorSet pbft.ValidatorSet
    36  	roundChanges map[uint64]*messageSet
    37  	mu           *sync.Mutex
    38  }
    39  
    40  // Add adds the round and message into round change set
    41  func (rcs *roundChangeSet) Add(r *big.Int, msg *message) (int, error) {
    42  	rcs.mu.Lock()
    43  	defer rcs.mu.Unlock()
    44  
    45  	round := r.Uint64()
    46  	if rcs.roundChanges[round] == nil {
    47  		rcs.roundChanges[round] = newMessageSet(rcs.validatorSet)
    48  	}
    49  	err := rcs.roundChanges[round].Add(msg)
    50  	if err != nil {
    51  		return 0, err
    52  	}
    53  	return rcs.roundChanges[round].Size(), nil
    54  }
    55  
    56  // Clear deletes the messages with smaller round
    57  func (rcs *roundChangeSet) Clear(round *big.Int) {
    58  	rcs.mu.Lock()
    59  	defer rcs.mu.Unlock()
    60  
    61  	for k, rms := range rcs.roundChanges {
    62  		if len(rms.Values()) == 0 || k < round.Uint64() {
    63  			delete(rcs.roundChanges, k)
    64  		}
    65  	}
    66  }
    67  
    68  // MaxRound returns the max round which the number of messages is equal or larger than num
    69  func (rcs *roundChangeSet) MaxRound(num int) *big.Int {
    70  	rcs.mu.Lock()
    71  	defer rcs.mu.Unlock()
    72  
    73  	var maxRound *big.Int
    74  	for k, rms := range rcs.roundChanges {
    75  		if rms.Size() < num {
    76  			continue
    77  		}
    78  		r := big.NewInt(int64(k))
    79  		if maxRound == nil || maxRound.Cmp(r) < 0 {
    80  			maxRound = r
    81  		}
    82  	}
    83  	return maxRound
    84  }