github.com/electroneum/electroneum-sc@v0.0.0-20230105223411-3bc1d078281e/consensus/istanbul/core/roundstate.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  	"math/big"
    21  	"sync"
    22  
    23  	"github.com/electroneum/electroneum-sc/common"
    24  	"github.com/electroneum/electroneum-sc/consensus/istanbul"
    25  	qbfttypes "github.com/electroneum/electroneum-sc/consensus/istanbul/types"
    26  )
    27  
    28  // newRoundState creates a new roundState instance with the given view and validatorSet
    29  func newRoundState(view *istanbul.View, validatorSet istanbul.ValidatorSet, preprepare *qbfttypes.Preprepare, preparedRound *big.Int, preparedBlock istanbul.Proposal, pendingRequest *Request, hasBadProposal func(hash common.Hash) bool) *roundState {
    30  	return &roundState{
    31  		round:      view.Round,
    32  		sequence:   view.Sequence,
    33  		Preprepare: preprepare,
    34  		//Prepares:       newMessageSet(validatorSet),
    35  		//Commits:        newMessageSet(validatorSet),
    36  		QBFTPrepares:   newQBFTMsgSet(validatorSet),
    37  		QBFTCommits:    newQBFTMsgSet(validatorSet),
    38  		preparedRound:  preparedRound,
    39  		preparedBlock:  preparedBlock,
    40  		mu:             new(sync.RWMutex),
    41  		pendingRequest: pendingRequest,
    42  		hasBadProposal: hasBadProposal,
    43  		preprepareSent: big.NewInt(0),
    44  	}
    45  }
    46  
    47  // roundState stores the consensus state
    48  type roundState struct {
    49  	round      *big.Int
    50  	sequence   *big.Int
    51  	Preprepare *qbfttypes.Preprepare
    52  
    53  	QBFTPrepares *qbftMsgSet
    54  	QBFTCommits  *qbftMsgSet
    55  
    56  	pendingRequest *Request
    57  	preparedRound  *big.Int
    58  	preparedBlock  istanbul.Proposal
    59  
    60  	mu             *sync.RWMutex
    61  	hasBadProposal func(hash common.Hash) bool
    62  
    63  	// Keep track of preprepare sent messages
    64  	preprepareSent *big.Int
    65  }
    66  
    67  func (s *roundState) Subject() *Subject {
    68  	s.mu.RLock()
    69  	defer s.mu.RUnlock()
    70  
    71  	if s.Preprepare == nil {
    72  		return nil
    73  	}
    74  
    75  	return &Subject{
    76  		View: &istanbul.View{
    77  			Round:    new(big.Int).Set(s.round),
    78  			Sequence: new(big.Int).Set(s.sequence),
    79  		},
    80  		Digest: s.Preprepare.Proposal.Hash(),
    81  	}
    82  }
    83  
    84  func (s *roundState) SetPreprepare(preprepare *qbfttypes.Preprepare) {
    85  	s.mu.Lock()
    86  	defer s.mu.Unlock()
    87  
    88  	s.Preprepare = preprepare
    89  }
    90  
    91  func (s *roundState) Proposal() istanbul.Proposal {
    92  	s.mu.RLock()
    93  	defer s.mu.RUnlock()
    94  
    95  	if s.Preprepare != nil {
    96  		return s.Preprepare.Proposal
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  func (s *roundState) SetRound(r *big.Int) {
   103  	s.mu.Lock()
   104  	defer s.mu.Unlock()
   105  
   106  	s.round = new(big.Int).Set(r)
   107  }
   108  
   109  func (s *roundState) Round() *big.Int {
   110  	s.mu.RLock()
   111  	defer s.mu.RUnlock()
   112  
   113  	return s.round
   114  }
   115  
   116  func (s *roundState) SetSequence(seq *big.Int) {
   117  	s.mu.Lock()
   118  	defer s.mu.Unlock()
   119  
   120  	s.sequence = seq
   121  }
   122  
   123  func (s *roundState) Sequence() *big.Int {
   124  	s.mu.RLock()
   125  	defer s.mu.RUnlock()
   126  
   127  	return s.sequence
   128  }