diff --git a/pkg/relayer/protocol/difficulty_test.go b/pkg/relayer/protocol/difficulty_test.go new file mode 100644 index 000000000..22d6d4188 --- /dev/null +++ b/pkg/relayer/protocol/difficulty_test.go @@ -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") +}