github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/consensus/dbft/core/roundstate_test.go (about)

     1  package core
     2  
     3  import (
     4  	"math/big"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/quickchainproject/quickchain/common"
     9  	"github.com/quickchainproject/quickchain/consensus/dbft"
    10  )
    11  
    12  func newTestRoundState(view *bft.View, validatorSet bft.ValidatorSet) *roundState {
    13  	return &roundState{
    14  		round:      view.Round,
    15  		sequence:   view.Sequence,
    16  		Preprepare: newTestPreprepare(view),
    17  		Prepares:   newMessageSet(validatorSet),
    18  		Commits:    newMessageSet(validatorSet),
    19  		mu:         new(sync.RWMutex),
    20  		hasBadProposal: func(hash common.Hash) bool {
    21  			return false
    22  		},
    23  	}
    24  }
    25  
    26  func TestLockHash(t *testing.T) {
    27  	sys := NewTestSystemWithBackend(1, 0)
    28  	rs := newTestRoundState(
    29  		&bft.View{
    30  			Round:    big.NewInt(0),
    31  			Sequence: big.NewInt(0),
    32  		},
    33  		sys.backends[0].peers,
    34  	)
    35  	if !common.EmptyHash(rs.GetLockedHash()) {
    36  		t.Errorf("error mismatch: have %v, want empty", rs.GetLockedHash())
    37  	}
    38  	if rs.IsHashLocked() {
    39  		t.Error("IsHashLocked should return false")
    40  	}
    41  
    42  	// Lock
    43  	expected := rs.Proposal().Hash()
    44  	rs.LockHash()
    45  	if expected != rs.GetLockedHash() {
    46  		t.Errorf("error mismatch: have %v, want %v", rs.GetLockedHash(), expected)
    47  	}
    48  	if !rs.IsHashLocked() {
    49  		t.Error("IsHashLocked should return true")
    50  	}
    51  
    52  	// Unlock
    53  	rs.UnlockHash()
    54  	if !common.EmptyHash(rs.GetLockedHash()) {
    55  		t.Errorf("error mismatch: have %v, want empty", rs.GetLockedHash())
    56  	}
    57  	if rs.IsHashLocked() {
    58  		t.Error("IsHashLocked should return false")
    59  	}
    60  }