Skip to content

Commit

Permalink
DifficultyFromBits now takes []byte instead of string (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielKrawisz authored Sep 7, 2021
1 parent 52f4ad4 commit c46106d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 2 additions & 3 deletions difficulty.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ func DifficultyToHashrate(coin string, diff uint64, targetSeconds float64) float
}

// DifficultyFromBits returns the mining difficulty from the nBits field in the block header.
func DifficultyFromBits(bits string) (float64, error) {
b, _ := hex.DecodeString(bits)
ib := binary.BigEndian.Uint32(b)
func DifficultyFromBits(bits []byte) (float64, error) {
ib := binary.BigEndian.Uint32(bits)
return targetToDifficulty(toCompactSize(ib))
}

Expand Down
4 changes: 3 additions & 1 deletion difficulty_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bc_test

import (
"encoding/hex"
"testing"

"github.com/libsv/go-bc"
Expand Down Expand Up @@ -64,7 +65,8 @@ func TestDifficultyFromBits(t *testing.T) {
}

func testDifficulty(bits string, expected float64, t *testing.T) {
d, _ := bc.DifficultyFromBits(bits)
b, _ := hex.DecodeString(bits)
d, _ := bc.DifficultyFromBits(b)

if d != expected {
t.Errorf("Expected difficulty of '%s' to be '%v', got %v", bits, expected, d)
Expand Down

0 comments on commit c46106d

Please sign in to comment.