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