github.com/Blockdaemon/celo-blockchain@v0.0.0-20200129231733-e667f6b08419/consensus/istanbul/core/testutils_test.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  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/consensus/istanbul"
    25  )
    26  
    27  func newView(seq, round uint64) *istanbul.View {
    28  	return &istanbul.View{Round: new(big.Int).SetUint64(round), Sequence: new(big.Int).SetUint64(seq)}
    29  }
    30  
    31  func newTestRoundState(view *istanbul.View, validatorSet istanbul.ValidatorSet) RoundState {
    32  	current := newRoundState(view, validatorSet, validatorSet.GetByIndex(0))
    33  	current.(*roundStateImpl).preprepare = newTestPreprepare(view)
    34  	return current
    35  }
    36  
    37  func checkError(t *testing.T, err error) {
    38  	if err != nil {
    39  		t.Errorf("Error %v", err)
    40  	}
    41  }
    42  func finishOnError(t *testing.T, err error) {
    43  	if err != nil {
    44  		t.Fatalf("Error %v", err)
    45  	}
    46  }
    47  
    48  func assertEqualView(t *testing.T, have, want *istanbul.View) {
    49  	if !reflect.DeepEqual(have, want) {
    50  		t.Errorf("View are not equal: have %v, want: %v", have, want)
    51  	}
    52  }
    53  func assertEqualRoundState(t *testing.T, have, want RoundState) {
    54  	testEqual := func(name string, have, want interface{}) {
    55  		if !reflect.DeepEqual(have, want) {
    56  			t.Errorf("RoundState.%s mismatch: have %v, want %v", name, have, want)
    57  		}
    58  	}
    59  
    60  	testEqual("State", have.State(), want.State())
    61  	testEqual("Round", have.Round(), want.Round())
    62  	testEqual("DesiredRound", have.DesiredRound(), want.DesiredRound())
    63  	testEqual("Sequence", have.Sequence(), want.Sequence())
    64  	testEqual("ValidatorSet", have.ValidatorSet(), want.ValidatorSet())
    65  	testEqual("Proposer", have.Proposer(), want.Proposer())
    66  	testEqual("ParentCommits", have.ParentCommits(), want.ParentCommits())
    67  	testEqual("Commits", have.Commits(), want.Commits())
    68  	testEqual("Prepares", have.Prepares(), want.Prepares())
    69  
    70  	if have.PendingRequest() == nil || want.PendingRequest() == nil {
    71  		testEqual("PendingRequest", have.PendingRequest(), want.PendingRequest())
    72  	} else {
    73  		haveBlock := have.PendingRequest().Proposal
    74  		wantBlock := want.PendingRequest().Proposal
    75  		testEqual("PendingRequest.Proposal.Hash", haveBlock.Hash(), wantBlock.Hash())
    76  	}
    77  
    78  	if have.Preprepare() == nil || want.Preprepare() == nil {
    79  		testEqual("Preprepare", have.Preprepare(), want.Preprepare())
    80  	} else {
    81  		testEqual("Preprepare.Proposal.Hash", have.Preprepare().Proposal.Hash(), want.Preprepare().Proposal.Hash())
    82  		testEqual("Preprepare.View", have.Preprepare().View, want.Preprepare().View)
    83  		testEqual("Preprepare.RoundChangeCertificate.IsEmpty", have.Preprepare().RoundChangeCertificate.IsEmpty(), want.Preprepare().RoundChangeCertificate.IsEmpty())
    84  
    85  		if !have.Preprepare().RoundChangeCertificate.IsEmpty() && !want.Preprepare().RoundChangeCertificate.IsEmpty() {
    86  			testEqual("Preprepare.RoundChangeCertificate.RoundChangeMessages", have.Preprepare().RoundChangeCertificate.RoundChangeMessages, want.Preprepare().RoundChangeCertificate.RoundChangeMessages)
    87  		}
    88  
    89  	}
    90  
    91  	havePPBlock := have.PreparedCertificate().Proposal
    92  	wantPPBlock := want.PreparedCertificate().Proposal
    93  	testEqual("PreparedCertificate().Proposal.Hash", havePPBlock.Hash(), wantPPBlock.Hash())
    94  	testEqual("PreparedCertificate().PrepareOrCommitMessages", have.PreparedCertificate().PrepareOrCommitMessages, want.PreparedCertificate().PrepareOrCommitMessages)
    95  }