roundstate_test.go 2.2 KB

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