Skip to content

Commit

Permalink
test: count difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Nov 16, 2023
1 parent a1d18ce commit a2c4b7b
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/relayer/protocol/difficulty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package protocol_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/pkg/relayer/protocol"
)

func TestCountDifficultyBits(t *testing.T) {
tests := []struct {
bz []byte
difficulty int
}{
{
bz: []byte{0b11111111, 255, 255, 255},
difficulty: 0,
},
{
bz: []byte{0b01111111, 255, 255, 255},
difficulty: 1,
},
{
bz: []byte{0, 255, 255, 255},
difficulty: 8,
},
{
bz: []byte{0, 0b01111111, 255, 255},
difficulty: 9,
},
{
bz: []byte{0, 0b00111111, 255, 255},
difficulty: 10,
},
{
bz: []byte{0, 0, 255, 255},
difficulty: 16,
},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("difficulty_%d", tt.difficulty), func(t *testing.T) {
actualDifficulty, err := protocol.CountDifficultyBits(tt.bz)
require.NoError(t, err)
require.Equal(t, tt.difficulty, actualDifficulty)
})
}
}

func TestCountDifficultyBits_Error(t *testing.T) {
_, err := protocol.CountDifficultyBits([]byte{0, 0, 0, 0})
require.ErrorIs(t, err, protocol.ErrDifficulty)
require.ErrorContains(t, err, "difficulty matches bytes length")
}

0 comments on commit a2c4b7b

Please sign in to comment.