Skip to content

Commit

Permalink
chore: add tests for State
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Jun 13, 2024
1 parent ff746a2 commit 35823be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/state/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (m *Manager) GetMissingBlocksSinceLatest(expected int64) []int64 {
}

func (m *Manager) GetSnapshot() (snapshotPkg.Snapshot, error) {
lastBlock := m.state.GetLastActiveSet()
lastBlock := m.state.GetLastBlock()

Check warning on line 138 in pkg/state/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/state/manager.go#L138

Added line #L138 was not covered by tests

validators := m.state.GetValidators()
entries := make(types.Entries, len(validators))

Check warning on line 141 in pkg/state/manager.go

View check run for this annotation

Codecov / codecov/patch

pkg/state/manager.go#L141

Added line #L141 was not covered by tests
Expand Down
8 changes: 4 additions & 4 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ func (s *State) SetBlocks(blocks map[int64]*types.Block) {

func (s *State) GetActiveValidators() types.Validators {
activeValidators := make(types.Validators, 0)
latestActiveSet := s.GetLastActiveSet()
if latestActiveSet == nil {
latestBlock := s.GetLastBlock()
if latestBlock == nil {
return activeValidators
}

for _, validator := range s.validators {
if _, ok := latestActiveSet.Validators[validator.ConsensusAddressHex]; ok {
if _, ok := latestBlock.Validators[validator.ConsensusAddressHex]; ok {
activeValidators = append(activeValidators, validator)
}
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func (s *State) GetLastBlockHeight() int64 {
return s.blocks.lastHeight
}

func (s *State) GetLastActiveSet() *types.Block {
func (s *State) GetLastBlock() *types.Block {
return s.blocks.blocks[s.blocks.lastHeight]
}

Expand Down
40 changes: 40 additions & 0 deletions pkg/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,43 @@ func TestValidatorsMissedBlocksSomeSkipped(t *testing.T) {
assert.Equal(t, int64(1), signature.NotActive, "Argument mismatch!")
assert.Equal(t, int64(0), signature.Proposed, "Argument mismatch!")
}

func TestGetLastBlock(t *testing.T) {
t.Parallel()

state := NewState()
block1 := state.GetLastBlock()
assert.Nil(t, block1)

state.AddBlock(&types.Block{Height: 1, Signatures: map[string]int32{}, Validators: map[string]bool{}})
state.AddBlock(&types.Block{Height: 2, Signatures: map[string]int32{}, Validators: map[string]bool{}})

block2 := state.GetLastBlock()
assert.Equal(t, int64(2), block2.Height)
}

func TestGetActiveValidators(t *testing.T) {
t.Parallel()

state := NewState()
validators1 := state.GetActiveValidators()
assert.Empty(t, validators1)

state.AddBlock(&types.Block{Height: 1, Signatures: map[string]int32{}, Validators: map[string]bool{
"validator1": true,
}})
state.AddBlock(&types.Block{Height: 2, Signatures: map[string]int32{}, Validators: map[string]bool{
"validator2": true,
}})

state.SetValidators(types.ValidatorsMap{
"validator1": &types.Validator{OperatorAddress: "validator1", ConsensusAddressHex: "validator1"},
"validator2": &types.Validator{OperatorAddress: "validator2", ConsensusAddressHex: "validator2"},
})

validators2 := state.GetActiveValidators()
assert.Len(t, validators2, 1)

validator := validators2[0]
assert.Equal(t, "validator2", validator.OperatorAddress)
}

0 comments on commit 35823be

Please sign in to comment.