github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/core/roundstate_save_decorator.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  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    24  )
    25  
    26  // createOrRestoreRoundState will obtain the last saved RoundState and use it if it's newer than the given Sequence,
    27  // if not it will create a new one with the information given.
    28  func withSavingDecorator(db RoundStateDB, rs RoundState) RoundState {
    29  	return &rsSaveDecorator{
    30  		db: db,
    31  		rs: rs,
    32  	}
    33  }
    34  
    35  type rsSaveDecorator struct {
    36  	rs RoundState
    37  	db RoundStateDB
    38  }
    39  
    40  func (rsp *rsSaveDecorator) persistOnNoError(err error) error {
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	return rsp.db.UpdateLastRoundState(rsp.rs)
    46  }
    47  
    48  // mutation functions
    49  func (rsp *rsSaveDecorator) StartNewRound(nextRound *big.Int, validatorSet istanbul.ValidatorSet, nextProposer istanbul.Validator) error {
    50  	return rsp.persistOnNoError(rsp.rs.StartNewRound(nextRound, validatorSet, nextProposer))
    51  }
    52  func (rsp *rsSaveDecorator) StartNewSequence(nextSequence *big.Int, validatorSet istanbul.ValidatorSet, nextProposer istanbul.Validator, parentCommits MessageSet) error {
    53  	return rsp.persistOnNoError(rsp.rs.StartNewSequence(nextSequence, validatorSet, nextProposer, parentCommits))
    54  }
    55  func (rsp *rsSaveDecorator) TransitionToPreprepared(preprepare *istanbul.Preprepare) error {
    56  	return rsp.persistOnNoError(rsp.rs.TransitionToPreprepared(preprepare))
    57  }
    58  func (rsp *rsSaveDecorator) TransitionToWaitingForNewRound(r *big.Int, nextProposer istanbul.Validator) error {
    59  	return rsp.persistOnNoError(rsp.rs.TransitionToWaitingForNewRound(r, nextProposer))
    60  }
    61  func (rsp *rsSaveDecorator) TransitionToCommitted() error {
    62  	return rsp.persistOnNoError(rsp.rs.TransitionToCommitted())
    63  }
    64  func (rsp *rsSaveDecorator) TransitionToPrepared(quorumSize int) error {
    65  	return rsp.persistOnNoError(rsp.rs.TransitionToPrepared(quorumSize))
    66  }
    67  func (rsp *rsSaveDecorator) AddCommit(msg *istanbul.Message) error {
    68  	return rsp.persistOnNoError(rsp.rs.AddCommit(msg))
    69  }
    70  func (rsp *rsSaveDecorator) AddPrepare(msg *istanbul.Message) error {
    71  	return rsp.persistOnNoError(rsp.rs.AddPrepare(msg))
    72  }
    73  func (rsp *rsSaveDecorator) AddParentCommit(msg *istanbul.Message) error {
    74  	return rsp.persistOnNoError(rsp.rs.AddParentCommit(msg))
    75  }
    76  func (rsp *rsSaveDecorator) SetPendingRequest(pendingRequest *istanbul.Request) error {
    77  	return rsp.persistOnNoError(rsp.rs.SetPendingRequest(pendingRequest))
    78  }
    79  func (rsp *rsSaveDecorator) SetProposalVerificationStatus(proposalHash common.Hash, verificationStatus error) {
    80  	// Don't persist on proposal verification status change, since it's just a cache
    81  	rsp.rs.SetProposalVerificationStatus(proposalHash, verificationStatus)
    82  }
    83  
    84  // DesiredRound implements RoundState.DesiredRound
    85  func (rsp *rsSaveDecorator) DesiredRound() *big.Int { return rsp.rs.DesiredRound() }
    86  
    87  // State implements RoundState.State
    88  func (rsp *rsSaveDecorator) State() State { return rsp.rs.State() }
    89  
    90  // Proposer implements RoundState.Proposer
    91  func (rsp *rsSaveDecorator) Proposer() istanbul.Validator { return rsp.rs.Proposer() }
    92  
    93  // Subject implements RoundState.Subject
    94  func (rsp *rsSaveDecorator) Subject() *istanbul.Subject { return rsp.rs.Subject() }
    95  
    96  // Proposal implements RoundState.Proposal
    97  func (rsp *rsSaveDecorator) Proposal() istanbul.Proposal { return rsp.rs.Proposal() }
    98  
    99  // Round implements RoundState.Round
   100  func (rsp *rsSaveDecorator) Round() *big.Int { return rsp.rs.Round() }
   101  
   102  // Commits implements RoundState.Commits
   103  func (rsp *rsSaveDecorator) Commits() MessageSet { return rsp.rs.Commits() }
   104  
   105  // Prepares implements RoundState.Prepares
   106  func (rsp *rsSaveDecorator) Prepares() MessageSet { return rsp.rs.Prepares() }
   107  
   108  // ParentCommits implements RoundState.ParentCommits
   109  func (rsp *rsSaveDecorator) ParentCommits() MessageSet { return rsp.rs.ParentCommits() }
   110  
   111  // Sequence implements RoundState.Sequence
   112  func (rsp *rsSaveDecorator) Sequence() *big.Int { return rsp.rs.Sequence() }
   113  
   114  // View implements RoundState.View
   115  func (rsp *rsSaveDecorator) View() *istanbul.View { return rsp.rs.View() }
   116  
   117  // Preprepare implements RoundState.Preprepare
   118  func (rsp *rsSaveDecorator) Preprepare() *istanbul.Preprepare { return rsp.rs.Preprepare() }
   119  
   120  // PendingRequest implements RoundState.PendingRequest
   121  func (rsp *rsSaveDecorator) PendingRequest() *istanbul.Request { return rsp.rs.PendingRequest() }
   122  
   123  // ValidatorSet implements RoundState.ValidatorSet
   124  func (rsp *rsSaveDecorator) ValidatorSet() istanbul.ValidatorSet { return rsp.rs.ValidatorSet() }
   125  
   126  // GetPrepareOrCommitSize implements RoundState.GetPrepareOrCommitSize
   127  func (rsp *rsSaveDecorator) GetPrepareOrCommitSize() int { return rsp.rs.GetPrepareOrCommitSize() }
   128  
   129  // GetValidatorByAddress implements RoundState.GetValidatorByAddress
   130  func (rsp *rsSaveDecorator) GetValidatorByAddress(address common.Address) istanbul.Validator {
   131  	return rsp.rs.GetValidatorByAddress(address)
   132  }
   133  
   134  // GetValidatorByAddress implements RoundState.GetProposalVerificationStatus
   135  func (rsp *rsSaveDecorator) GetProposalVerificationStatus(proposalHash common.Hash) (verificationStatus error, isChecked bool) {
   136  	return rsp.rs.GetProposalVerificationStatus(proposalHash)
   137  }
   138  
   139  // IsProposer implements RoundState.IsProposer
   140  func (rsp *rsSaveDecorator) IsProposer(address common.Address) bool { return rsp.rs.IsProposer(address) }
   141  
   142  // PreparedCertificate implements RoundState.PreparedCertificate
   143  func (rsp *rsSaveDecorator) PreparedCertificate() istanbul.PreparedCertificate {
   144  	return rsp.rs.PreparedCertificate()
   145  }
   146  
   147  // Summary implements RoundState.Summary
   148  func (rsp *rsSaveDecorator) Summary() *RoundStateSummary { return rsp.rs.Summary() }