github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/consensus/pbft/core/roundstate_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  	"sync"
    22  	"testing"
    23  
    24  	"github.com/bigzoro/my_simplechain/common"
    25  	"github.com/bigzoro/my_simplechain/consensus/pbft"
    26  )
    27  
    28  func newTestRoundState(view *pbft.View, validatorSet pbft.ValidatorSet) *roundState {
    29  	return &roundState{
    30  		round:      view.Round,
    31  		sequence:   view.Sequence,
    32  		Preprepare: newTestPreprepare(view),
    33  		Prepare:    newTestConclusion(),
    34  		Prepares:   newMessageSet(validatorSet),
    35  		Commits:    newMessageSet(validatorSet),
    36  		mu:         new(sync.RWMutex),
    37  		hasBadProposal: func(hash common.Hash) bool {
    38  			return false
    39  		},
    40  	}
    41  }
    42  
    43  func TestLockHash(t *testing.T) {
    44  	sys := NewTestSystemWithBackend(1, 0)
    45  	rs := newTestRoundState(
    46  		&pbft.View{
    47  			Round:    big.NewInt(0),
    48  			Sequence: big.NewInt(0),
    49  		},
    50  		sys.backends[0].peers,
    51  	)
    52  	if (common.Hash{}) != rs.GetLockedHash() {
    53  		t.Errorf("error mismatch: have %v, want empty", rs.GetLockedHash())
    54  	}
    55  	if rs.IsHashLocked() {
    56  		t.Error("IsHashLocked should return false")
    57  	}
    58  
    59  	// Lock
    60  	expected := rs.Proposal().PendingHash()
    61  	rs.LockHash()
    62  	if expected != rs.GetLockedHash() {
    63  		t.Errorf("error mismatch: have %v, want %v", rs.GetLockedHash(), expected)
    64  	}
    65  	if !rs.IsHashLocked() {
    66  		t.Error("IsHashLocked should return true")
    67  	}
    68  
    69  	// Unlock
    70  	rs.UnlockHash()
    71  	if (common.Hash{}) != rs.GetLockedHash() {
    72  		t.Errorf("error mismatch: have %v, want empty", rs.GetLockedHash())
    73  	}
    74  	if rs.IsHashLocked() {
    75  		t.Error("IsHashLocked should return false")
    76  	}
    77  }